-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Changes from 1 commit
0ec07e4
292b9d0
5c54894
029aaa4
b56e6da
91269fc
c48e825
449c0e2
c5ba601
4b3a3e8
9ef9d2c
9c408c1
548d656
3e3fefd
391fb51
df8c7d3
bc14fa6
54c6f1b
ce6bfb2
4c1956b
301158b
1aa1891
bf2a9af
169f521
90f9072
f143443
a0d661e
145a2e4
638a98f
7f5acc0
b06bb6f
a19b0a7
87f49b2
f764aa8
9843ac0
d6cb917
469d26f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I'll create a new PR to restructure this a bit to make it work with the new bit flag. |
||||||||
} | ||||||||
|
||||||||
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); | ||||||||
} | ||||||||
|
||||||||
|
@@ -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; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not have There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, fine. Next question: maybe we also need a |
||||||||
} | ||||||||
|
||||||||
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; | ||||||||
} | ||||||||
|
||||||||
|
@@ -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); | ||||||||
} | ||||||||
|
||||||||
|
@@ -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) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name confused me -- why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it shouldn't be called with It probably should be called on compact ints either. I'll check if it is, and rename it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've renamed it to |
||||||||
{ | ||||||||
assert(PyLong_Check(op)); | ||||||||
assert(unused_bits_are_zero(op)); | ||||||||
assert(!inconsistent_zero(op)); | ||||||||
return 1 - (op->long_value.lv_tag & SIGN_MASK); | ||||||||
} | ||||||||
|
||||||||
|
@@ -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); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
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); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
op->long_value.lv_tag &= ~7; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||
op->long_value.lv_tag |= flipped_sign; | ||||||||
assert(unused_bits_are_zero(op)); | ||||||||
assert(!inconsistent_zero(op)); | ||||||||
} | ||||||||
|
||||||||
#define _PyLong_DIGIT_INIT(val) \ | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.