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 4 commits
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
8 changes: 8 additions & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ _PyLong_UnsignedDigitCount(const PyLongObject *op)
return op->long_value.lv_tag >> NON_SIZE_BITS;
}

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

Comment on lines +196 to +203
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be the new implementation of _PyLong_Sign(), if _PyLong_NonCompactSign() is removed? This gets rid of a branch in the proposed version of _PyLong_Sign().

Copy link
Member

Choose a reason for hiding this comment

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

They sure look identical to me. Maybe Mark has plans and maybe the compiler would optimize this anyway?

if (P(x))
  return F(x);
else
  return F(x);

could just become return F(x);.

Copy link
Member Author

Choose a reason for hiding this comment

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

We want the freedom to implement the "compact" and non-compact forms differently.
They have the same implementation at the moment, but that will change.

_PyLong_Sign() is part of the ABI, so we need to retain it. But almost all code using _PyLong_Sign() actually wants to know if an int is negative and should be using _PyLong_IsNegative().

static inline int
_PyLong_NonCompactSign(const PyLongObject *op)
{
Expand Down
4 changes: 2 additions & 2 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,13 +1726,13 @@ math_isqrt(PyObject *module, PyObject *n)
return NULL;
}

if (_PyLong_Sign(n) < 0) {
if (_PyLong_IsNegative((PyLongObject *)n)) {
PyErr_SetString(
PyExc_ValueError,
"isqrt() argument must be nonnegative");
goto error;
}
if (_PyLong_Sign(n) == 0) {
if (_PyLong_IsZero((PyLongObject *)n)) {
Py_DECREF(n);
return PyLong_FromLong(0);
}
Expand Down
3 changes: 2 additions & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
#include "pycore_object.h" // _Py_CheckSlotResult()
#include "pycore_long.h" // _Py_IsNegative
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_unionobject.h" // _PyUnion_Check()
Expand Down Expand Up @@ -1483,7 +1484,7 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
/* Whether or not it is less than or equal to
zero is determined by the sign of ob_size
*/
if (_PyLong_Sign(value) < 0)
if (_PyLong_IsNegative((PyLongObject *)value))
result = PY_SSIZE_T_MIN;
else
result = PY_SSIZE_T_MAX;
Expand Down
27 changes: 17 additions & 10 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,9 @@ _PyLong_Sign(PyObject *vv)

assert(v != NULL);
assert(PyLong_Check(v));
if (_PyLong_IsCompact(v)) {
return _PyLong_CompactSign(v);
}
return _PyLong_NonCompactSign(v);
}

Expand Down Expand Up @@ -1079,21 +1082,25 @@ PyLong_AsVoidPtr(PyObject *vv)
#if SIZEOF_VOID_P <= SIZEOF_LONG
long x;

if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
if (PyLong_Check(vv) && _PyLong_IsNegative((PyLongObject *)vv)) {
x = PyLong_AsLong(vv);
else
}
else {
x = PyLong_AsUnsignedLong(vv);
}
#else

#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
# error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)"
#endif
long long x;

if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
if (PyLong_Check(vv) && _PyLong_IsNegative((PyLongObject *)vv)) {
x = PyLong_AsLongLong(vv);
else
}
else {
x = PyLong_AsUnsignedLongLong(vv);
}

#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */

Expand Down Expand Up @@ -1403,7 +1410,7 @@ _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr)
{
unsigned long uval;

if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
PyErr_SetString(PyExc_ValueError, "value must be positive");
return 0;
}
Expand All @@ -1425,7 +1432,7 @@ _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr)
{
unsigned long uval;

if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
PyErr_SetString(PyExc_ValueError, "value must be positive");
return 0;
}
Expand All @@ -1447,7 +1454,7 @@ _PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr)
{
unsigned long uval;

if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
PyErr_SetString(PyExc_ValueError, "value must be positive");
return 0;
}
Expand All @@ -1464,7 +1471,7 @@ _PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr)
{
unsigned long long uval;

if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
PyErr_SetString(PyExc_ValueError, "value must be positive");
return 0;
}
Expand All @@ -1481,7 +1488,7 @@ _PyLong_Size_t_Converter(PyObject *obj, void *ptr)
{
size_t uval;

if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
PyErr_SetString(PyExc_ValueError, "value must be positive");
return 0;
}
Expand Down Expand Up @@ -3938,7 +3945,7 @@ fast_mod(PyLongObject *a, PyLongObject *b)

assert(_PyLong_DigitCount(a) == 1);
assert(_PyLong_DigitCount(b) == 1);
sdigit sign = _PyLong_NonCompactSign(b);
sdigit sign = _PyLong_CompactSign(b);
if (_PyLong_SameSign(a, b)) {
mod = left % right;
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ validate_step(PyObject *step)
return PyLong_FromLong(1);

step = PyNumber_Index(step);
if (step && _PyLong_Sign(step) == 0) {
if (step && _PyLong_IsZero((PyLongObject *)step)) {
PyErr_SetString(PyExc_ValueError,
"range() arg 3 must not be zero");
Py_CLEAR(step);
Expand Down
6 changes: 3 additions & 3 deletions Objects/sliceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (start == NULL)
goto error;

if (_PyLong_Sign(start) < 0) {
if (_PyLong_IsNegative((PyLongObject *)start)) {
/* start += length */
PyObject *tmp = PyNumber_Add(start, length);
Py_SETREF(start, tmp);
Expand Down Expand Up @@ -478,7 +478,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (stop == NULL)
goto error;

if (_PyLong_Sign(stop) < 0) {
if (_PyLong_IsNegative((PyLongObject *)stop)) {
/* stop += length */
PyObject *tmp = PyNumber_Add(stop, length);
Py_SETREF(stop, tmp);
Expand Down Expand Up @@ -533,7 +533,7 @@ slice_indices(PySliceObject* self, PyObject* len)
if (length == NULL)
return NULL;

if (_PyLong_Sign(length) < 0) {
if (_PyLong_IsNegative((PyLongObject *)length)) {
PyErr_SetString(PyExc_ValueError,
"length should not be negative");
Py_DECREF(length);
Expand Down
47 changes: 17 additions & 30 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.