Skip to content
Merged
Changes from 3 commits
Commits
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
83 changes: 58 additions & 25 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,9 @@ PyLong_FromDouble(double dval)
long
PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
{
/* This version by Tim Peters */
/* This version originally by Tim Peters */
PyLongObject *v;
unsigned long x, prev;
unsigned long x;
long res;
Py_ssize_t i;
int sign;
Expand Down Expand Up @@ -556,14 +556,28 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
res = -1;
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);
x = 0;

digit *digits = v->long_value.ob_digit;
assert(i >= 2);
/* unroll 1 digit */
--i;
assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1));
x = digits[i];

#if ((ULONG_MAX >> PyLong_SHIFT)) >= ((1UL << PyLong_SHIFT) - 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can also unroll the second digit for the other methods. It gains a bit of performance at the cost of a bit of extra code. The unrolling can be factored out to a helper function (the loop itself is not so easy because there are small differences in how to handle the overflow)

/* unroll another digit */
x <<= PyLong_SHIFT;
--i;
x |= digits[i];
#endif


while (--i >= 0) {
prev = x;
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev) {
if (x > SIZE_MAX >> PyLong_SHIFT) {
*overflow = sign;
goto exit;
}
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
}
/* Haven't lost any bits, but casting to long requires extra
* care (see comment above).
Expand Down Expand Up @@ -627,7 +641,7 @@ PyLong_AsInt(PyObject *obj)
Py_ssize_t
PyLong_AsSsize_t(PyObject *vv) {
PyLongObject *v;
size_t x, prev;
size_t x;
Py_ssize_t i;
int sign;

Expand All @@ -646,12 +660,19 @@ PyLong_AsSsize_t(PyObject *vv) {
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);
x = 0;

digit *digits = v->long_value.ob_digit;
assert(i >= 2);
/* unroll 1 digit */
--i;
assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1));
x = digits[i];

while (--i >= 0) {
prev = x;
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev)
if (x > SIZE_MAX >> PyLong_SHIFT) {
goto overflow;
}
x = (x << PyLong_SHIFT) | digits[i];
}
/* Haven't lost any bits, but casting to a signed type requires
* extra care (see comment above).
Expand All @@ -677,7 +698,7 @@ unsigned long
PyLong_AsUnsignedLong(PyObject *vv)
{
PyLongObject *v;
unsigned long x, prev;
unsigned long x;
Py_ssize_t i;

if (vv == NULL) {
Expand Down Expand Up @@ -708,13 +729,19 @@ PyLong_AsUnsignedLong(PyObject *vv)
return (unsigned long) -1;
}
i = _PyLong_DigitCount(v);
x = 0;

digit *digits = v->long_value.ob_digit;
assert(i >= 2);
/* unroll 1 digit */
--i;
assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1));
x = digits[i];

while (--i >= 0) {
prev = x;
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev) {
if (x > SIZE_MAX >> PyLong_SHIFT) {
goto overflow;
}
x = (x << PyLong_SHIFT) | digits[i];
}
return x;
overflow:
Expand All @@ -731,7 +758,7 @@ size_t
PyLong_AsSize_t(PyObject *vv)
{
PyLongObject *v;
size_t x, prev;
size_t x;
Py_ssize_t i;

if (vv == NULL) {
Expand All @@ -753,16 +780,22 @@ PyLong_AsSize_t(PyObject *vv)
return (size_t) -1;
}
i = _PyLong_DigitCount(v);
x = 0;

digit *digits = v->long_value.ob_digit;
assert(i >= 2);
/* unroll 1 digit */
--i;
assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1));
x = digits[i];

while (--i >= 0) {
prev = x;
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C size_t");
return (size_t) -1;
if (x > SIZE_MAX >> PyLong_SHIFT) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C size_t");
return (size_t) -1;
}
x = (x << PyLong_SHIFT) | digits[i];
}
}
return x;
}

Expand Down
Loading