-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-129149: Add Missing fast path in PYLONG_FROM_UINT macro for compact integers #129168
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 13 commits
ed9be47
ba70518
34315cb
0ef383c
c00ba8c
2b5163f
955bb37
237b8b9
11ad06f
c25b910
37a5c9a
2cb1aec
99c5420
2724758
2a8692d
70d85f1
af410e0
5a9e201
5760c95
1f81b87
f4d74d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add missing fast path in :c:func:`PyLong_FromUnsignedLong`, :c:func:`PyLong_FromUnsignedLongLong` and :c:func:`PyLong_FromSize_t` functions | ||
for compact integers. And refactor :c:func:`PyLong_FromLong` and :c:func:`PyLong_FromLongLong` to use the macro ``PYLONG_FROM_SIGNED``. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,38 @@ maybe_small_long(PyLongObject *v) | |
if (PyErr_CheckSignals()) PyTryBlock \ | ||
} while(0) | ||
|
||
#define PYLONG_FROM_SIGNED(INT_TYPE, ival) \ | ||
skirpichev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
do { \ | ||
if (IS_SMALL_INT(ival)) { \ | ||
return get_small_int((sdigit)(ival)); \ | ||
} \ | ||
if (-(INT_TYPE)PyLong_MASK <= (ival) && (ival) <= (INT_TYPE)PyLong_MASK) { \ | ||
return _PyLong_FromMedium((sdigit)(ival)); \ | ||
} \ | ||
/* Count digits (at least two - smaller cases were handled above). */ \ | ||
unsigned INT_TYPE abs_ival, t; \ | ||
abs_ival = (ival) < 0 ? 0U-(unsigned INT_TYPE)(ival) : (unsigned INT_TYPE)(ival); \ | ||
/* Do shift in two steps to avoid possible undefined behavior. */ \ | ||
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; \ | ||
Py_ssize_t ndigits = 2; \ | ||
while (t) { \ | ||
++ndigits; \ | ||
t >>= PyLong_SHIFT; \ | ||
} \ | ||
PyLongObject *v = _PyLong_New(ndigits); \ | ||
if (v == NULL) { \ | ||
return NULL; \ | ||
} \ | ||
digit *p = v->long_value.ob_digit; \ | ||
_PyLong_SetSignAndDigitCount(v, (ival) < 0 ? -1 : 1, ndigits); \ | ||
t = abs_ival; \ | ||
while (t) { \ | ||
*p++ = (digit)(t & PyLong_MASK); \ | ||
t >>= PyLong_SHIFT; \ | ||
} \ | ||
return (PyObject *)v; \ | ||
} while(0) | ||
|
||
/* Normalize (remove leading zeros from) an int object. | ||
Doesn't attempt to free the storage--in most cases, due to the nature | ||
of the algorithms used, this could save at most be one word anyway. */ | ||
|
@@ -318,50 +350,21 @@ _PyLong_Negate(PyLongObject **x_p) | |
PyObject * | ||
PyLong_FromLong(long ival) | ||
{ | ||
PyLongObject *v; | ||
unsigned long abs_ival, t; | ||
int ndigits; | ||
|
||
/* Handle small and medium cases. */ | ||
if (IS_SMALL_INT(ival)) { | ||
return get_small_int((sdigit)ival); | ||
} | ||
if (-(long)PyLong_MASK <= ival && ival <= (long)PyLong_MASK) { | ||
return _PyLong_FromMedium((sdigit)ival); | ||
} | ||
|
||
/* Count digits (at least two - smaller cases were handled above). */ | ||
abs_ival = ival < 0 ? 0U-(unsigned long)ival : (unsigned long)ival; | ||
/* Do shift in two steps to avoid possible undefined behavior. */ | ||
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; | ||
ndigits = 2; | ||
while (t) { | ||
++ndigits; | ||
t >>= PyLong_SHIFT; | ||
} | ||
|
||
/* Construct output value. */ | ||
v = _PyLong_New(ndigits); | ||
if (v != NULL) { | ||
digit *p = v->long_value.ob_digit; | ||
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits); | ||
t = abs_ival; | ||
while (t) { | ||
*p++ = (digit)(t & PyLong_MASK); | ||
t >>= PyLong_SHIFT; | ||
} | ||
} | ||
return (PyObject *)v; | ||
PYLONG_FROM_SIGNED(long, ival); | ||
} | ||
|
||
#define PYLONG_FROM_UINT(INT_TYPE, ival) \ | ||
do { \ | ||
if (IS_SMALL_UINT(ival)) { \ | ||
return get_small_int((sdigit)(ival)); \ | ||
} \ | ||
if ((ival) <= PyLong_MASK) { \ | ||
return _PyLong_FromMedium((sdigit)(ival)); \ | ||
} \ | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* Do shift in two steps to avoid possible undefined behavior. */ \ | ||
INT_TYPE t = (ival) >> PyLong_SHIFT >> PyLong_SHIFT; \ | ||
/* Count the number of Python digits. */ \ | ||
Py_ssize_t ndigits = 0; \ | ||
INT_TYPE t = (ival); \ | ||
Py_ssize_t ndigits = 2; \ | ||
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. Depending on the main...eendebakpt:cpython:fast_digit_count @srinivasreddy Would you be interested in benchmarking this approach and making a PR? 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. Yes. Absolutely. 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 do not think this works on a platform where e.g. 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. Correct. We would need some guards for that. Whether that bit of complexity is worth is, depends on the benchmark results. |
||
while (t) { \ | ||
++ndigits; \ | ||
t >>= PyLong_SHIFT; \ | ||
|
@@ -1453,40 +1456,7 @@ PyLong_AsVoidPtr(PyObject *vv) | |
PyObject * | ||
PyLong_FromLongLong(long long ival) | ||
{ | ||
PyLongObject *v; | ||
unsigned long long abs_ival, t; | ||
int ndigits; | ||
|
||
/* Handle small and medium cases. */ | ||
if (IS_SMALL_INT(ival)) { | ||
return get_small_int((sdigit)ival); | ||
} | ||
if (-(long long)PyLong_MASK <= ival && ival <= (long long)PyLong_MASK) { | ||
return _PyLong_FromMedium((sdigit)ival); | ||
} | ||
|
||
/* Count digits (at least two - smaller cases were handled above). */ | ||
abs_ival = ival < 0 ? 0U-(unsigned long long)ival : (unsigned long long)ival; | ||
/* Do shift in two steps to avoid possible undefined behavior. */ | ||
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; | ||
ndigits = 2; | ||
while (t) { | ||
++ndigits; | ||
t >>= PyLong_SHIFT; | ||
} | ||
|
||
/* Construct output value. */ | ||
v = _PyLong_New(ndigits); | ||
if (v != NULL) { | ||
digit *p = v->long_value.ob_digit; | ||
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits); | ||
t = abs_ival; | ||
while (t) { | ||
*p++ = (digit)(t & PyLong_MASK); | ||
t >>= PyLong_SHIFT; | ||
} | ||
} | ||
return (PyObject *)v; | ||
PYLONG_FROM_SIGNED(long long, ival); | ||
} | ||
|
||
/* Create a new int object from a C Py_ssize_t. */ | ||
|
Uh oh!
There was an error while loading. Please reload this page.