Skip to content

Commit 2724758

Browse files
committed
Revert the refactoring
1 parent 99c5420 commit 2724758

File tree

1 file changed

+68
-33
lines changed

1 file changed

+68
-33
lines changed

Objects/longobject.c

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,6 @@ maybe_small_long(PyLongObject *v)
115115
if (PyErr_CheckSignals()) PyTryBlock \
116116
} while(0)
117117

118-
#define PYLONG_FROM_SIGNED(INT_TYPE, ival) \
119-
do { \
120-
if (IS_SMALL_INT(ival)) { \
121-
return get_small_int((sdigit)(ival)); \
122-
} \
123-
if (-(INT_TYPE)PyLong_MASK <= (ival) && (ival) <= (INT_TYPE)PyLong_MASK) { \
124-
return _PyLong_FromMedium((sdigit)(ival)); \
125-
} \
126-
/* Count digits (at least two - smaller cases were handled above). */ \
127-
unsigned INT_TYPE abs_ival, t; \
128-
abs_ival = (ival) < 0 ? 0U-(unsigned INT_TYPE)(ival) : (unsigned INT_TYPE)(ival); \
129-
/* Do shift in two steps to avoid possible undefined behavior. */ \
130-
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT; \
131-
Py_ssize_t ndigits = 2; \
132-
while (t) { \
133-
++ndigits; \
134-
t >>= PyLong_SHIFT; \
135-
} \
136-
PyLongObject *v = _PyLong_New(ndigits); \
137-
if (v == NULL) { \
138-
return NULL; \
139-
} \
140-
digit *p = v->long_value.ob_digit; \
141-
_PyLong_SetSignAndDigitCount(v, (ival) < 0 ? -1 : 1, ndigits); \
142-
t = abs_ival; \
143-
while (t) { \
144-
*p++ = (digit)(t & PyLong_MASK); \
145-
t >>= PyLong_SHIFT; \
146-
} \
147-
return (PyObject *)v; \
148-
} while(0)
149118

150119
/* Normalize (remove leading zeros from) an int object.
151120
Doesn't attempt to free the storage--in most cases, due to the nature
@@ -350,7 +319,40 @@ _PyLong_Negate(PyLongObject **x_p)
350319
PyObject *
351320
PyLong_FromLong(long ival)
352321
{
353-
PYLONG_FROM_SIGNED(long, ival);
322+
PyLongObject *v;
323+
unsigned long abs_ival, t;
324+
int ndigits;
325+
326+
/* Handle small and medium cases. */
327+
if (IS_SMALL_INT(ival)) {
328+
return get_small_int((sdigit)ival);
329+
}
330+
if (-(long)PyLong_MASK <= ival && ival <= (long)PyLong_MASK) {
331+
return _PyLong_FromMedium((sdigit)ival);
332+
}
333+
334+
/* Count digits (at least two - smaller cases were handled above). */
335+
abs_ival = ival < 0 ? 0U-(unsigned long)ival : (unsigned long)ival;
336+
/* Do shift in two steps to avoid possible undefined behavior. */
337+
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;
338+
ndigits = 2;
339+
while (t) {
340+
++ndigits;
341+
t >>= PyLong_SHIFT;
342+
}
343+
344+
/* Construct output value. */
345+
v = _PyLong_New(ndigits);
346+
if (v != NULL) {
347+
digit *p = v->long_value.ob_digit;
348+
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
349+
t = abs_ival;
350+
while (t) {
351+
*p++ = (digit)(t & PyLong_MASK);
352+
t >>= PyLong_SHIFT;
353+
}
354+
}
355+
return (PyObject *)v;
354356
}
355357

356358
#define PYLONG_FROM_UINT(INT_TYPE, ival) \
@@ -1456,7 +1458,40 @@ PyLong_AsVoidPtr(PyObject *vv)
14561458
PyObject *
14571459
PyLong_FromLongLong(long long ival)
14581460
{
1459-
PYLONG_FROM_SIGNED(long long, ival);
1461+
PyLongObject *v;
1462+
unsigned long long abs_ival, t;
1463+
int ndigits;
1464+
1465+
/* Handle small and medium cases. */
1466+
if (IS_SMALL_INT(ival)) {
1467+
return get_small_int((sdigit)ival);
1468+
}
1469+
if (-(long long)PyLong_MASK <= ival && ival <= (long long)PyLong_MASK) {
1470+
return _PyLong_FromMedium((sdigit)ival);
1471+
}
1472+
1473+
/* Count digits (at least two - smaller cases were handled above). */
1474+
abs_ival = ival < 0 ? 0U-(unsigned long long)ival : (unsigned long long)ival;
1475+
/* Do shift in two steps to avoid possible undefined behavior. */
1476+
t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;
1477+
ndigits = 2;
1478+
while (t) {
1479+
++ndigits;
1480+
t >>= PyLong_SHIFT;
1481+
}
1482+
1483+
/* Construct output value. */
1484+
v = _PyLong_New(ndigits);
1485+
if (v != NULL) {
1486+
digit *p = v->long_value.ob_digit;
1487+
_PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
1488+
t = abs_ival;
1489+
while (t) {
1490+
*p++ = (digit)(t & PyLong_MASK);
1491+
t >>= PyLong_SHIFT;
1492+
}
1493+
}
1494+
return (PyObject *)v;
14601495
}
14611496

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

0 commit comments

Comments
 (0)