Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-101291: Rearrange the size bits in PyLongObject #102464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0ec07e4
Add functions to hide some internals of long object.
markshannon Jan 25, 2023
292b9d0
Add internal functions to longobject.c for setting sign and digit count.
markshannon Jan 25, 2023
5c54894
Replace Py_SIZE(x) < 0 with _PyLong_IsNegative(x) in longobject.c
markshannon Feb 28, 2023
029aaa4
Replace Py_ABS(Py_SIZE(a)) with _PyLong_DigitCount(a) in longobject.c
markshannon Feb 28, 2023
b56e6da
Remove many uses of Py_SIZE in longobject.c
markshannon Feb 28, 2023
91269fc
Remove _PyLong_AssignValue, as it is no longer used.
markshannon Feb 28, 2023
c48e825
Remove some more uses of Py_SIZE in longobject.c.
markshannon Feb 28, 2023
449c0e2
Remove a few more uses of Py_SIZE in longobject.c.
markshannon Mar 1, 2023
c5ba601
Remove some more uses of Py_SIZE, replacing with _PyLong_UnsignedDigi…
markshannon Mar 1, 2023
4b3a3e8
Replace a few Py_SIZE() with _PyLong_SameSign().
markshannon Mar 1, 2023
9ef9d2c
Remove a few more Py_SIZE() from longobject.c
markshannon Mar 1, 2023
9c408c1
Replace uses of IS_MEDIUM_VALUE macro with _PyLong_IsSingleDigit.
markshannon Mar 1, 2023
548d656
Remove most of the remaining uses of Py_SIZE in longobject.c
markshannon Mar 1, 2023
3e3fefd
Replace last remaining uses of Py_SIZE applied to longobject with _Py…
markshannon Mar 1, 2023
391fb51
Don't use _PyObject_InitVar and move a couple of inline functions to …
markshannon Mar 1, 2023
df8c7d3
Correct name of inline function.
markshannon Mar 1, 2023
bc14fa6
Eliminate all remaining uses of Py_SIZE and Py_SET_SIZE on PyLongObject.
markshannon Mar 1, 2023
54c6f1b
Change layout of size/sign bits in longobject to support future addit…
markshannon Mar 2, 2023
ce6bfb2
Test pairs of longs together on fast path of add/mul/sub.
markshannon Mar 2, 2023
4c1956b
Tidy up comment and delete commented out code.
markshannon Mar 6, 2023
301158b
Add news.
markshannon Mar 6, 2023
1aa1891
Remove debugging asserts.
markshannon Mar 6, 2023
bf2a9af
Fix storage classes.
markshannon Mar 6, 2023
169f521
Remove development debug functions.
markshannon Mar 6, 2023
90f9072
Avoid casting to smaller int.
markshannon Mar 8, 2023
f143443
Apply suggestions from code review.
markshannon Mar 8, 2023
a0d661e
Widen types to avoid data loss.
markshannon Mar 8, 2023
145a2e4
Fix syntax error.
markshannon Mar 8, 2023
638a98f
Replace 'SingleDigit' with 'Compact' as the term 'single digit' seems…
markshannon Mar 9, 2023
7f5acc0
Address review comments.
markshannon Mar 16, 2023
b06bb6f
Merge branch 'main' into long-rearrange-size-bits
markshannon Mar 16, 2023
a19b0a7
Merge branch 'main' into long-rearrange-size-bits
markshannon Mar 16, 2023
87f49b2
Fix _PyLong_Sign
markshannon Mar 16, 2023
f764aa8
Replace _PyLong_Sign(x) < 0 with _PyLong_IsNegative(x).
markshannon Mar 16, 2023
9843ac0
fix sign check
markshannon Mar 16, 2023
d6cb917
Address some review comments.
markshannon Mar 22, 2023
469d26f
Change asserts on digit counts to asserts on sign where applicable.
markshannon Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove some more uses of Py_SIZE, replacing with _PyLong_UnsignedDigi…
…tCount().
  • Loading branch information
markshannon committed Mar 1, 2023
commit c5ba601720d99ad723f2460285a523d69546edae
28 changes: 18 additions & 10 deletions 28 Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ _PyLong_SingleDigitValue(const PyLongObject *op)
return Py_SIZE(op) * op->long_value.ob_digit[0];
}

static inline Py_ssize_t
_PyLong_DigitCount(const PyLongObject *op)
static inline bool
_PyLong_IsPositive(const PyLongObject *op)
{
assert(PyLong_Check(op));
return Py_ABS(Py_SIZE(op));
return Py_SIZE(op) > 0;
}

static inline bool
Expand All @@ -156,6 +155,21 @@ _PyLong_IsNegative(const PyLongObject *op)
return Py_SIZE(op) < 0;
}

static inline Py_ssize_t
_PyLong_DigitCount(const PyLongObject *op)
{
assert(PyLong_Check(op));
return Py_ABS(Py_SIZE(op));
}

/* Like _PyLong_DigitCount but asserts that op is non-negative */
static inline Py_ssize_t
_PyLong_UnsignedDigitCount(const PyLongObject *op)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not excited about this name; I keep having to look up how it differs from _PyLong_DigitCount, and it's not really related to _PyLong_SignedDigitCount. :-( Maybe _PyLong_NonNegativeDigitCount? Or perhaps better _PyLong_DigitCountOfNonNegative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed this for the extra check during implementation. _PyLong_UnsignedDigitCount is now the same as _PyLong_DigitCount, and should remain so.

I'll remove it.

{
assert(PyLong_Check(op));
assert(!_PyLong_IsNegative(op));
return Py_ABS(Py_SIZE(op));
}

static inline bool
_PyLong_IsZero(const PyLongObject *op)
Expand All @@ -171,12 +185,6 @@ _PyLong_NonZeroSign(const PyLongObject *op)
return ((Py_SIZE(op) > 0) << 1) - 1;
}

static inline bool
_PyLong_IsPositive(const PyLongObject *op)
{
return Py_SIZE(op) > 0;
}


#ifdef __cplusplus
}
Expand Down
42 changes: 21 additions & 21 deletions 42 Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3488,7 +3488,7 @@ x_mul(PyLongObject *a, PyLongObject *b)
if (z == NULL)
return NULL;

memset(z->long_value.ob_digit, 0, Py_SIZE(z) * sizeof(digit));
memset(z->long_value.ob_digit, 0, _PyLong_UnsignedDigitCount(z) * sizeof(digit));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since z was just created with a nonnegative size:

Suggested change
memset(z->long_value.ob_digit, 0, _PyLong_UnsignedDigitCount(z) * sizeof(digit));
memset(z->long_value.ob_digit, 0, _PyLong_DigitCount(z) * sizeof(digit));

if (a == b) {
/* Efficient squaring per HAC, Algorithm 14.16:
* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
Expand Down Expand Up @@ -3671,7 +3671,7 @@ k_mul(PyLongObject *a, PyLongObject *b)
/* Split a & b into hi & lo pieces. */
shift = bsize >> 1;
if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
assert(_PyLong_UnsignedDigitCount(ah) > 0); /* the split isn't degenerate */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just check the sign, right?

Suggested change
assert(_PyLong_UnsignedDigitCount(ah) > 0); /* the split isn't degenerate */
assert(_PyLong_IsPositive(ah)); /* the split isn't degenerate */

Same below several occurrences.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


if (a == b) {
bh = (PyLongObject*)Py_NewRef(ah);
Expand Down Expand Up @@ -3700,44 +3700,44 @@ k_mul(PyLongObject *a, PyLongObject *b)
if (ret == NULL) goto fail;
#ifdef Py_DEBUG
/* Fill with trash, to catch reference to uninitialized digits. */
memset(ret->long_value.ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
memset(ret->long_value.ob_digit, 0xDF, _PyLong_UnsignedDigitCount(ret) * sizeof(digit));
#endif

/* 2. t1 <- ah*bh, and copy into high digits of result. */
if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
assert(Py_SIZE(t1) >= 0);
assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
assert(_PyLong_UnsignedDigitCount(t1) >= 0);
assert(2*shift + _PyLong_UnsignedDigitCount(t1) <= _PyLong_UnsignedDigitCount(ret));
memcpy(ret->long_value.ob_digit + 2*shift, t1->long_value.ob_digit,
Py_SIZE(t1) * sizeof(digit));
_PyLong_UnsignedDigitCount(t1) * sizeof(digit));

/* Zero-out the digits higher than the ah*bh copy. */
i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
i = _PyLong_UnsignedDigitCount(ret) - 2*shift - _PyLong_UnsignedDigitCount(t1);
if (i)
memset(ret->long_value.ob_digit + 2*shift + Py_SIZE(t1), 0,
memset(ret->long_value.ob_digit + 2*shift + _PyLong_UnsignedDigitCount(t1), 0,
i * sizeof(digit));

/* 3. t2 <- al*bl, and copy into the low digits. */
if ((t2 = k_mul(al, bl)) == NULL) {
Py_DECREF(t1);
goto fail;
}
assert(Py_SIZE(t2) >= 0);
assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
memcpy(ret->long_value.ob_digit, t2->long_value.ob_digit, Py_SIZE(t2) * sizeof(digit));
assert(_PyLong_UnsignedDigitCount(t2) >= 0);
assert(_PyLong_UnsignedDigitCount(t2) <= 2*shift); /* no overlap with high digits */
memcpy(ret->long_value.ob_digit, t2->long_value.ob_digit, _PyLong_UnsignedDigitCount(t2) * sizeof(digit));

/* Zero out remaining digits. */
i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
i = 2*shift - _PyLong_UnsignedDigitCount(t2); /* number of uninitialized digits */
if (i)
memset(ret->long_value.ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
memset(ret->long_value.ob_digit + _PyLong_UnsignedDigitCount(t2), 0, i * sizeof(digit));

/* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
* because it's fresher in cache.
*/
i = Py_SIZE(ret) - shift; /* # digits after shift */
(void)v_isub(ret->long_value.ob_digit + shift, i, t2->long_value.ob_digit, Py_SIZE(t2));
i = _PyLong_UnsignedDigitCount(ret) - shift; /* # digits after shift */
(void)v_isub(ret->long_value.ob_digit + shift, i, t2->long_value.ob_digit, _PyLong_UnsignedDigitCount(t2));
_Py_DECREF_INT(t2);

(void)v_isub(ret->long_value.ob_digit + shift, i, t1->long_value.ob_digit, Py_SIZE(t1));
(void)v_isub(ret->long_value.ob_digit + shift, i, t1->long_value.ob_digit, _PyLong_UnsignedDigitCount(t1));
_Py_DECREF_INT(t1);

/* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Expand All @@ -3761,12 +3761,12 @@ k_mul(PyLongObject *a, PyLongObject *b)
_Py_DECREF_INT(t1);
_Py_DECREF_INT(t2);
if (t3 == NULL) goto fail;
assert(Py_SIZE(t3) >= 0);
assert(_PyLong_UnsignedDigitCount(t3) >= 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really check the sign:

Suggested change
assert(_PyLong_UnsignedDigitCount(t3) >= 0);
assert(!_PyLong_IsNegative(t3));

Same several times above.


/* Add t3. It's not obvious why we can't run out of room here.
* See the (*) comment after this function.
*/
(void)v_iadd(ret->long_value.ob_digit + shift, i, t3->long_value.ob_digit, Py_SIZE(t3));
(void)v_iadd(ret->long_value.ob_digit + shift, i, t3->long_value.ob_digit, _PyLong_UnsignedDigitCount(t3));
markshannon marked this conversation as resolved.
Show resolved Hide resolved
_Py_DECREF_INT(t3);

return long_normalize(ret);
Expand Down Expand Up @@ -3849,7 +3849,7 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b)
ret = _PyLong_New(asize + bsize);
if (ret == NULL)
return NULL;
memset(ret->long_value.ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
memset(ret->long_value.ob_digit, 0, _PyLong_UnsignedDigitCount(ret) * sizeof(digit));

/* Successive slices of b are copied into bslice. */
bslice = _PyLong_New(asize);
Expand All @@ -3871,8 +3871,8 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b)
goto fail;

/* Add into result. */
(void)v_iadd(ret->long_value.ob_digit + nbdone, Py_SIZE(ret) - nbdone,
product->long_value.ob_digit, Py_SIZE(product));
(void)v_iadd(ret->long_value.ob_digit + nbdone, _PyLong_UnsignedDigitCount(ret) - nbdone,
product->long_value.ob_digit, _PyLong_UnsignedDigitCount(product));
_Py_DECREF_INT(product);

bsize -= nbtouse;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.