8000 gh-129149: Add fast path for medium-size integers in `PyLong_FromSsize_t()` by chris-eibl · Pull Request #129301 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-129149: Add fast path for medium-size integers in PyLong_FromSsize_t() #129301

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 13 commits into from
Mar 13, 2025
Merged
Prev Previous commit
Next Next commit
Add UINT_TYPE to the macro PYLONG_FROM_INT.
Use it in now in PyLong_FromSsize_t(), too.
  • Loading branch information
chris-eibl committed Jan 25, 2025
commit b4ee2afeb9d89108cd01a000384ee374665100b1
47 changes: 6 additions & 41 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ _PyLong_Negate(PyLongObject **x_p)
Py_DECREF(x);
}

#define PYLONG_FROM_INT(INT_TYPE, ival) \
#define PYLONG_FROM_INT(UINT_TYPE, INT_TYPE, ival) \
do { \
PyLongObject *v; \
unsigned INT_TYPE abs_ival, t; \
UINT_TYPE abs_ival, t; \
int ndigits; \
/* Handle small and medium cases. */ \
if (IS_SMALL_INT(ival)) { \
Expand All @@ -345,7 +345,7 @@ _PyLong_Negate(PyLongObject **x_p)
return _PyLong_FromMedium((sdigit)ival); \
} \
/* Count digits (at least two - smaller cases were handled above). */ \
abs_ival = ival < 0 ? 0U-(unsigned INT_TYPE)ival : (unsigned INT_TYPE)ival; \
abs_ival = ival < 0 ? 0U-(UINT_TYPE)ival : (UINT_TYPE)ival; \
/* Do shift in two steps to avoid possible undefined behavior. */ \
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; \
ndigits = 2; \
Expand Down Expand Up @@ -373,7 +373,7 @@ _PyLong_Negate(PyLongObject **x_p)
PyObject *
PyLong_FromLong(long ival)
{
PYLONG_FROM_INT(long, ival);
PYLONG_FROM_INT(unsigned long, long, ival);
}

#define PYLONG_FROM_UINT(INT_TYPE, ival) \
Expand Down Expand Up @@ -1485,50 +1485,15 @@ PyLong_AsVoidPtr(PyObject *vv)
PyObject *
PyLong_FromLongLong(long long ival)
{
PYLONG_FROM_INT(long long, ival);
PYLONG_FROM_INT(unsigned long long, long long, ival);
}

/* Create a new int object from a C Py_ssize_t. */

PyObject *
PyLong_FromSsize_t(Py_ssize_t ival)
{
PyLongObject *v;
size_t abs_ival;
size_t t; /* unsigned so >> doesn't propagate sign bit */
int ndigits = 0;
int negative = 0;

if (IS_SMALL_INT(ival)) {
return get_small_int((sdigit)ival);
}

if (ival < 0) {
/* avoid signed overflow when ival = SIZE_T_MIN */
abs_ival = (size_t)(-1-ival)+1;
negative = 1;
}
else {
abs_ival = (size_t)ival;
}

/* Count the number of Python digits. */
t = abs_ival;
while (t) {
++ndigits;
t >>= PyLong_SHIFT;
}
v = long_alloc(ndigits);
if (v != NULL) {
digit *p = v->long_value.ob_digit;
_PyLong_SetS 597D ignAndDigitCount(v, negative ? -1 : 1, ndigits);
t = abs_ival;
while (t) {
*p++ = (digit)(t & PyLong_MASK);
t >>= PyLong_SHIFT;
}
}
return (PyObject *)v;
PYLONG_FROM_INT(size_t, Py_ssize_t, ival);
}

/* Get a C long long int from an int object or any object that has an
Expand Down
0