8000 GH-101291: Rearrange the size bits in PyLongObject by markshannon · Pull Request #102464 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
Address review comments.
  • Loading branch information
markshannon committed Mar 16, 2023
commit 7f5acc0eba93a4c5b38642792464eae927774e62
23 changes: 15 additions & 8 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
#define SIGN_NEGATIVE 2
#define NON_SIZE_BITS 3

/* All "single digit" values are guaranteed to fit into
/* All *compact" values are guaranteed to fit into
* a Py_ssize_t with at least one bit to spare.
* In other words, for 64 bit machines, compact
* will be signed 63 (or fewer) bit values
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also add that compact values have at most one digit? I've seen some code depending on that (e.g. _PyLong_Multiply).

Copy link
Member Author

Choose a reason for hiding this comment

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

Not with tagged ints. In theory a compact int could have 5 digits. (63 bit compact ints, and 15 bit digits).

For a sensible implementation, a compact int will be one or two digits.

*/

/* Return 1 if the argument is positive single digit int */
/* Return 1 if the argument is compact int */
static inline int
_PyLong_IsNonNegativeCompact(const PyLongObject* op) {
assert(PyLong_Check(op));
Expand All @@ -142,7 +144,9 @@ _PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
return (a->long_value.lv_tag | b->long_value.lv_tag) < (2 << NON_SIZE_BITS);
}

/* The value returned by this function will have at least one bit to spare,
/* Returns a *compact* value, iff `_PyLong_IsCompact` is true for `op`.
*
* "Compact" values have at least one bit to spare,
* so that addition and subtraction can be performed on the values
* without risk of overflow.
*/
Expand Down Expand Up @@ -180,7 +184,7 @@ _PyLong_DigitCount(const PyLongObject *op)
return op->long_value.lv_tag >> NON_SIZE_BITS;
}

/* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonZeroSign(op) */
/* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonCompactSign(op) */
static inline Py_ssize_t
_PyLong_SignedDigitCount(const PyLongObject *op)
{
Expand All @@ -199,9 +203,10 @@ _PyLong_UnsignedDigitCount(const PyLongObject *op)
}

static inline int
_PyLong_NonZeroSign(const PyLongObject *op)
_PyLong_NonCompactSign(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(!_PyLong_IsCompact(op));
return 1 - (op->long_value.lv_tag & SIGN_MASK);
}

Expand All @@ -215,7 +220,7 @@ _PyLong_SameSign(const PyLongObject *a, const PyLongObject *b)
#define TAG_FROM_SIGN_AND_SIZE(sign, size) ((1 - (sign)) | ((size) << NON_SIZE_BITS))
Copy link
Contributor

Choose a reason for hiding this comment

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

size should be cast to size_t before shifting, and the result cast to Py_ssize_t to avoid UB.

I also haven't checked the assembly here, but I don't really know what happens when OR-ing a signed 64-bit int with a signed 32-bit int, and if this is doing work that's not strictly necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is only in _PyLong_SetSignAndSize that size is a variable. I'll do the conversion there.

Copy link
Member

Choose a reason for hiding this comment

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

So maybe add a comment that this macro should only be used with literal or size_t arguments?


static inline void
_PyLong_SetSignAndSize(PyLongObject *op, int sign, Py_ssize_t size)
_PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size)
{
assert(size >= 0);
assert(-1 <= sign && sign <= 1);
Expand All @@ -224,16 +229,18 @@ _PyLong_SetSignAndSize(PyLongObject *op, int sign, Py_ssize_t size)
}

static inline void
_PyLong_SetSize(PyLongObject *op, Py_ssize_t size)
_PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
{
assert(size >= 0);
op->long_value.lv_tag = (((size_t)size) << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
}

#define NON_SIZE_MASK ~((1 << NON_SIZE_BITS) - 1)

static inline void
_PyLong_FlipSign(PyLongObject *op) {
unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
op->long_value.lv_tag &= ~7;
op->long_value.lv_tag &= NON_SIZE_MASK;
op->long_value.lv_tag |= flipped_sign;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rearrage bits in first field (after header) of PyLongObject. * Bits 0 and 1:
1- sign. I.e. 0 for positive numbers, 1 for zero and 2 for negative numbers.
1 - sign. I.e. 0 for positive numbers, 1 for zero and 2 for negative numbers.
* Bit 2 reserved (probably for the immortal bit) * Bits 3+ the unsigned
size.

Expand Down
56 changes: 28 additions & 28 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ long_normalize(PyLongObject *v)
--i;
if (i != j) {
if (i == 0) {
_PyLong_SetSignAndSize(v, 0, 0);
_PyLong_SetSignAndDigitCount(v, 0, 0);
}
else {
_PyLong_SetSize(v, i);
_PyLong_SetDigitCount(v, i);
}
}
return v;
Expand Down Expand Up @@ -163,7 +163,7 @@ _PyLong_New(Py_ssize_t size)
PyErr_NoMemory();
return NULL;
}
_PyLong_SetSignAndSize(result, size != 0, size);
_PyLong_SetSignAndDigitCount(result, size != 0, size);
_PyObject_Init((PyObject*)result, &PyLong_Type);
return result;
}
Expand All @@ -180,7 +180,7 @@ _PyLong_FromDigits(int negative, Py_ssize_t digit_count, digit *digits)
PyErr_NoMemory();
return NULL;
}
_PyLong_SetSignAndSize(result, negative?-1:1, digit_count);
_PyLong_SetSignAndDigitCount(result, negative?-1:1, digit_count);
memcpy(result->long_value.ob_digit, digits, digit_count * sizeof(digit));
return result;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ _PyLong_FromMedium(sdigit x)
return NULL;
}
digit abs_x = x < 0 ? -x : x;
_PyLong_SetSignAndSize(v, x<0?-1:1, 1);
_PyLong_SetSignAndDigitCount(v, x<0?-1:1, 1);
_PyObject_Init((PyObject*)v, &PyLong_Type);
v->long_value.ob_digit[0] = abs_x;
return (PyObject*)v;
Expand Down Expand Up @@ -246,7 +246,7 @@ _PyLong_FromLarge(stwodigits ival)
PyLongObject *v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->long_value.ob_digit;
_PyLong_SetSignAndSize(v, sign, ndigits);
_PyLong_SetSignAndDigitCount(v, sign, ndigits);
t = abs_ival;
while (t) {
*p++ = Py_SAFE_DOWNCAST(
Expand Down Expand Up @@ -319,7 +319,7 @@ PyLong_FromLong(long ival)
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->long_value.ob_digit;
_PyLong_SetSignAndSize(v, ival < 0 ? -1 : 1, ndigits);
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
t = abs_ival;
while (t) {
*p++ = (digit)(t & PyLong_MASK);
Expand Down Expand Up @@ -496,7 +496,7 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
else {
res = -1;
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
prev = x;
Expand Down Expand Up @@ -586,7 +586,7 @@ PyLong_AsSsize_t(PyObject *vv) {
return _PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
prev = x;
Expand Down Expand Up @@ -721,11 +721,11 @@ _PyLong_AsUnsignedLongMask(PyObject *vv)
return (unsigned long) -1;
}
v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeCompact(v)) {
if (_PyLong_IsCompact(v)) {
return (unsigned long)_PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
int sign = _PyLong_NonZeroSign(v);
int sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
Expand Down Expand Up @@ -764,7 +764,7 @@ _PyLong_Sign(PyObject *vv)

assert(v != NULL);
assert(PyLong_Check(v));
return _PyLong_NonZeroSign(v);
return _PyLong_NonCompactSign(v);
}

static int
Expand Down Expand Up @@ -918,7 +918,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
if (idigit == 0) {
sign = 0;
}
_PyLong_SetSignAndSize(v, sign, idigit);
_PyLong_SetSignAndDigitCount(v, sign, idigit);
return (PyObject *)maybe_small_long(long_normalize(v));
}

Expand Down Expand Up @@ -1139,7 +1139,7 @@ PyLong_FromLongLong(long long ival)
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->long_value.ob_digit;
_PyLong_SetSignAndSize(v, ival < 0 ? -1 : 1, ndigits);
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
t = abs_ival;
while (t) {
*p++ = (digit)(t & PyLong_MASK);
Expand Down Expand Up @@ -1182,7 10000 +1182,7 @@ PyLong_FromSsize_t(Py_ssize_t ival)
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->long_value.ob_digit;
_PyLong_SetSignAndSize(v, negative ? -1 : 1, ndigits);
_PyLong_SetSignAndDigitCount(v, negative ? -1 : 1, ndigits);
t = abs_ival;
while (t) {
*p++ = (digit)(t & PyLong_MASK);
Expand Down Expand Up @@ -1289,11 +1289,11 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv)
return (unsigned long long) -1;
}
v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeCompact(v)) {
return _PyLong_CompactValue(v);
if (_PyLong_IsCompact(v)) {
return (unsigned long long)(signed long long)_PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
Expand Down Expand Up @@ -1366,7 +1366,7 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
}
else {
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
prev = x;
Expand Down Expand Up @@ -2473,7 +2473,7 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits,
*res = NULL;
return 0;
}
_PyLong_SetSignAndSize(z, 0, 0);
_PyLong_SetSignAndDigitCount(z, 0, 0);

/* `convwidth` consecutive input digits are treated as a single
* digit in base `convmultmax`.
Expand Down Expand Up @@ -2525,7 +2525,7 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits,
if (_PyLong_DigitCount(z) < size_z) {
*pz = (digit)c;
assert(!_PyLong_IsNegative(z));
_PyLong_SetSignAndSize(z, 1, _PyLong_DigitCount(z) + 1);
_PyLong_SetSignAndDigitCount(z, 1, _PyLong_DigitCount(z) + 1);
}
else {
PyLongObject *tmp;
Expand Down Expand Up @@ -3279,7 +3279,7 @@ long_hash(PyLongObject *v)
return x;
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
while (--i >= 0) {
/* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
Expand Down Expand Up @@ -3877,7 +3877,7 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b)
memcpy(bslice->long_value.ob_digit, b->long_value.ob_digit + nbdone,
nbtouse * sizeof(digit));
assert(nbtouse >= 0);
_PyLong_SetSignAndSize(bslice, 1, nbtouse);
_PyLong_SetSignAndDigitCount(bslice, 1, nbtouse);
product = k_mul(a, bslice);
if (product == NULL)
goto fail;
Expand Down Expand Up @@ -3938,7 +3938,7 @@ fast_mod(PyLongObject *a, PyLongObject *b)

assert(_PyLong_DigitCount(a) == 1);
assert(_PyLong_DigitCount(b) == 1);
sdigit sign = _PyLong_NonZeroSign(b);
sdigit sign = _PyLong_NonCompactSign(b);
if (_PyLong_SameSign(a, b)) {
mod = left % right;
}
Expand Down Expand Up @@ -4966,7 +4966,7 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
significant `wordshift` digits of `a` is nonzero. Digit `wordshift`
of `2**shift - 1` has value `PyLong_MASK >> hishift`.
*/
_PyLong_SetSignAndSize(z, -1, newsize);
_PyLong_SetSignAndDigitCount(z, -1, newsize);

digit sticky = 0;
for (Py_ssize_t j = 0; j < wordshift; j++) {
Expand Down Expand Up @@ -5387,7 +5387,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
}
if (c != NULL) {
assert(size_a >= 0);
_PyLong_SetSignAndSize(c, 1, size_a);
_PyLong_SetSignAndDigitCount(c, 1, size_a);
}
else if (Py_REFCNT(a) == 1) {
c = (PyLongObject*)Py_NewRef(a);
Expand All @@ -5401,12 +5401,12 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)

if (d != NULL) {
assert(size_a >= 0);
_PyLong_SetSignAndSize(d, 1, size_a);
_PyLong_SetSignAndDigitCount(d, 1, size_a);
}
else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
d = (PyLongObject*)Py_NewRef(b);
assert(size_a >= 0);
_PyLong_SetSignAndSize(d, 1, size_a);
_PyLong_SetSignAndDigitCount(d, 1, size_a);
}
else {
alloc_b = size_a;
Expand Down
2 changes: 1 addition & 1 deletion Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ r_PyLong(RFILE *p)
if (ob == NULL)
return NULL;

_PyLong_SetSignAndSize(ob, n < 0 ? -1 : 1, size);
_PyLong_SetSignAndDigitCount(ob, n < 0 ? -1 : 1, size);

for (i = 0; i < size-1; i++) {
d = 0;
Expand Down
2 changes: 0 additions & 2 deletions Tools/build/umarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def __init__(self, data: bytes):
self.level: int = 0

def r_string(self, n: int) -> bytes:
if not (0 <= n <= self.end - self.pos):
print(n, self.end, self.pos)
assert 0 <= n <= self.end - self.pos
buf = self.data[self.pos : self.pos + n]
self.pos += n
Expand Down
0