Skip to content

Commit 0ef383c

Browse files
committed
Convert PyLong_FromLongLong() as well to leverage macro PYLONG_FROM_SIGNED. And also added a blurb about the changes
1 parent 34315cb commit 0ef383c

File tree

2 files changed

+5
-36
lines changed

2 files changed

+5
-36
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add Missing fast path in PyLong_FromLong(), PyLong_FromLongLong() functions
2+
for compact integers

Objects/longobject.c

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ maybe_small_long(PyLongObject *v)
117117

118118
#define PYLONG_FROM_SIGNED(INT_TYPE, ival) \
119119
do { \
120-
unsigned long abs_ival, t; \
120+
unsigned INT_TYPE abs_ival, t; \
121121
if (IS_SMALL_INT(ival)) { \
122122
return get_small_int((sdigit)(ival)); \
123123
} \
124124
if (-(INT_TYPE)PyLong_MASK <= (ival) && (ival) <= (INT_TYPE)PyLong_MASK) { \
125125
return _PyLong_FromMedium((sdigit)(ival)); \
126126
} \
127127
/* Count digits (at least two - smaller cases were handled above). */ \
128-
abs_ival = (ival) < 0 ? 0U-(unsigned long)(ival) : (unsigned long)(ival); \
128+
abs_ival = (ival) < 0 ? 0U-(unsigned INT_TYPE)(ival) : (unsigned INT_TYPE)(ival); \
129129
/* Do shift in two steps to avoid possible undefined behavior. */ \
130130
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; \
131131
Py_ssize_t ndigits = 2; \
@@ -1456,40 +1456,7 @@ PyLong_AsVoidPtr(PyObject *vv)
14561456
PyObject *
14571457
PyLong_FromLongLong(long long ival)
14581458
{
1459-
PyLongObject *v;
1460-
unsigned long long abs_ival, t;
1461-
int ndigits;
1462-
1463-
/* Handle small and medium cases. */
1464-
if (IS_SMALL_INT(ival)) {
1465-
return get_small_int((sdigit)ival);
1466-
}
1467-
if (-(long long)PyLong_MASK <= ival && ival <= (long long)PyLong_MASK) {
1468-
return _PyLong_FromMedium((sdigit)ival);
1469-
}
1470-
1471-
/* Count digits (at least two - smaller cases were handled above). */
1472-
abs_ival = ival < 0 ? 0U-(unsigned long long)ival : (unsigned long long)ival;
1473-
/* Do shift in two steps to avoid possible undefined behavior. */
1474-
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;
1475-
ndigits = 2;
1476-
while (t) {
1477-
++ndigits;
1478-
t >>= PyLong_SHIFT;
1479-
}
1480-
1481-
/* Construct output value. */
1482-
v = _PyLong_New(ndigits);
1483-
if (v != NULL) {
1484-
digit *p = v->long_value.ob_digit;
1485-
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
1486-
t = abs_ival;
1487-
while (t) {
1488-
*p++ = (digit)(t & PyLong_MASK);
1489-
t >>= PyLong_SHIFT;
1490-
}
1491-
}
1492-
return (PyObject *)v;
1459+
PYLONG_FROM_SIGNED(long long, ival);
14931460
}
14941461

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

0 commit comments

Comments
 (0)