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
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 development debug functions.
  • Loading branch information
markshannon committed Mar 6, 2023
commit 169f52172ddac02ec0614307df6c48ad65203440
40 changes: 0 additions & 40 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,16 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
#define SIGN_NEGATIVE 2
#define NON_SIZE_BITS 3

static int
unused_bits_are_zero(const PyLongObject* op) {
return (op->long_value.lv_tag & 4) == 0;
}

static int
inconsistent_zero(const PyLongObject* op) {
return
((op->long_value.lv_tag & SIGN_MASK) == SIGN_ZERO
&& (op->long_value.lv_tag >> NON_SIZE_BITS) != 0)
||
((op->long_value.lv_tag & SIGN_MASK) != SIGN_ZERO
&& (op->long_value.lv_tag >> NON_SIZE_BITS) == 0);
}

/* Return 1 if the argument is positive single digit int */
static inline int
_PyLong_IsNonNegativeSingleDigit(const PyLongObject* op) {
assert(PyLong_Check(op));
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return op->long_value.lv_tag <= (1 << NON_SIZE_BITS);
Copy link
Contributor
@eduardo-elizondo eduardo-elizondo Apr 7, 2023

Choose a reason for hiding this comment

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

This doesn't work if we set the second (immortal/static) bit, i.e: the immortal small int 1 since it will have an lv_tag of 1100 and return an incorrect value here.

I'll create a new PR to restructure this a bit to make it work with the new bit flag.

cc @ericsnowcurrently

}

static inline int
_PyLong_IsSingleDigit(const PyLongObject* op) {
assert(PyLong_Check(op));
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return op->long_value.lv_tag < (2 << NON_SIZE_BITS);
}

Expand All @@ -162,40 +143,32 @@ _PyLong_SingleDigitValue(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(_PyLong_IsSingleDigit(op));
assert(unused_bits_are_zero(op));
Py_ssize_t sign = 1 - (op->long_value.lv_tag & SIGN_MASK);
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
}

static inline bool
_PyLong_IsZero(const PyLongObject *op)
{
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return (op->long_value.lv_tag & SIGN_MASK) == SIGN_ZERO;
}

static inline bool
_PyLong_IsNegative(const PyLongObject *op)
{
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return (op->long_value.lv_tag & SIGN_MASK) == SIGN_NEGATIVE;
}

static inline bool
_PyLong_IsPositive(const PyLongObject *op)
{
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return (op->long_value.lv_tag & SIGN_MASK) == 0;
Copy link
Member

Choose a reason for hiding this comment

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

Why not have #define SIGN_POSITIVE 0?

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 want these functions to be the only way to determine the sign.
Defining SIGN_POSITIVE will just encourage people to do the test elsewhere.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, fine. Next question: maybe we also need a _PyLong_IsNonZero? I see !_PyLong_IsZero a lot, and the ! is easily missed. (Or maybe that's just my old eyes.) Possibly also IsNonNegative and IsNonPositive.

}

static inline Py_ssize_t
_PyLong_DigitCount(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(unused_bits_are_zero(op));
return op->long_value.lv_tag >> NON_SIZE_BITS;
}

Expand All @@ -204,9 +177,7 @@ static inline Py_ssize_t
_PyLong_SignedDigitCount(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(unused_bits_are_zero(op));
Py_ssize_t sign = 1 - (op->long_value.lv_tag & SIGN_MASK);
assert(!inconsistent_zero(op));
return sign * (Py_ssize_t)(op->long_value.lv_tag >> NON_SIZE_BITS);
}

Expand All @@ -216,17 +187,13 @@ _PyLong_UnsignedDigitCount(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(!_PyLong_IsNegative(op));
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return op->long_value.lv_tag >> NON_SIZE_BITS;
}

static inline int
_PyLong_NonZeroSign(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.

The name confused me -- why not just _PyLong_Sign?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because it shouldn't be called with 0.

It probably should be called on compact ints either. I'll check if it is, and rename it.

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've renamed it to _PyLong_NonCompactSign which will make implementing tagged ints easier.

{
assert(PyLong_Check(op));
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
return 1 - (op->long_value.lv_tag & SIGN_MASK);
}

Expand All @@ -246,27 +213,20 @@ _PyLong_SetSignAndSize(PyLongObject *op, int sign, Py_ssize_t size)
assert(-1 <= sign && sign <= 1);
assert(sign != 0 || size == 0);
op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, size);
assert(!inconsistent_zero(op));
}

static inline void
_PyLong_SetSize(PyLongObject *op, Py_ssize_t size)
{
assert(size >= 0);
assert(!inconsistent_zero(op));
op->long_value.lv_tag = (size << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
op->long_value.lv_tag = (size << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
Py_ssize_t size_shifted = (Py_ssize_t)((size_t)size << NON_SIZE_BITS);
op->long_value.lv_tag = size_shifted | (op->long_value.lv_tag & SIGN_MASK);

Copy link
Member Author

Choose a reason for hiding this comment

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

lv_tag is unsigned, so doesn't need the double conversion.

assert(!inconsistent_zero(op));
}

static inline void
_PyLong_FlipSign(PyLongObject *op) {
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);

op->long_value.lv_tag &= ~7;
Copy link
Member

Choose a reason for hiding this comment

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

I think you want to use some defined name instead of hardcoding 7? Perhaps ~((1 << NON_SIZE_BITS) - 1).

op->long_value.lv_tag |= flipped_sign;
assert(unused_bits_are_zero(op));
assert(!inconsistent_zero(op));
}

#define _PyLong_DIGIT_INIT(val) \
Expand Down
0