• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Long (arbitrary precision) integer object implementation */
2 
3 /* XXX The functional organization of this file is terrible */
4 
5 #include "Python.h"
6 #include "pycore_bitutils.h"      // _Py_popcount32()
7 #include "pycore_initconfig.h"    // _PyStatus_OK()
8 #include "pycore_call.h"          // _PyObject_MakeTpCall
9 #include "pycore_long.h"          // _Py_SmallInts
10 #include "pycore_object.h"        // _PyObject_Init()
11 #include "pycore_runtime.h"       // _PY_NSMALLPOSINTS
12 #include "pycore_structseq.h"     // _PyStructSequence_FiniBuiltin()
13 
14 #include <float.h>                // DBL_MANT_DIG
15 #include <stddef.h>               // offsetof
16 
17 #include "clinic/longobject.c.h"
18 /*[clinic input]
19 class int "PyObject *" "&PyLong_Type"
20 [clinic start generated code]*/
21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/
22 
23 #define medium_value(x) ((stwodigits)_PyLong_CompactValue(x))
24 
25 #define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
26 #define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)
27 
28 #define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d digits) for integer string conversion: value has %zd digits; use sys.set_int_max_str_digits() to increase the limit"
29 #define _MAX_STR_DIGITS_ERROR_FMT_TO_STR "Exceeds the limit (%d digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit"
30 
31 /* If defined, use algorithms from the _pylong.py module */
32 #define WITH_PYLONG_MODULE 1
33 
34 static inline void
_Py_DECREF_INT(PyLongObject * op)35 _Py_DECREF_INT(PyLongObject *op)
36 {
37     assert(PyLong_CheckExact(op));
38     _Py_DECREF_SPECIALIZED((PyObject *)op, (destructor)PyObject_Free);
39 }
40 
41 static inline int
is_medium_int(stwodigits x)42 is_medium_int(stwodigits x)
43 {
44     /* Take care that we are comparing unsigned values. */
45     twodigits x_plus_mask = ((twodigits)x) + PyLong_MASK;
46     return x_plus_mask < ((twodigits)PyLong_MASK) + PyLong_BASE;
47 }
48 
49 static PyObject *
get_small_int(sdigit ival)50 get_small_int(sdigit ival)
51 {
52     assert(IS_SMALL_INT(ival));
53     return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + ival];
54 }
55 
56 static PyLongObject *
maybe_small_long(PyLongObject * v)57 maybe_small_long(PyLongObject *v)
58 {
59     if (v && _PyLong_IsCompact(v)) {
60         stwodigits ival = medium_value(v);
61         if (IS_SMALL_INT(ival)) {
62             _Py_DECREF_INT(v);
63             return (PyLongObject *)get_small_int((sdigit)ival);
64         }
65     }
66     return v;
67 }
68 
69 /* For int multiplication, use the O(N**2) school algorithm unless
70  * both operands contain more than KARATSUBA_CUTOFF digits (this
71  * being an internal Python int digit, in base BASE).
72  */
73 #define KARATSUBA_CUTOFF 70
74 #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
75 
76 /* For exponentiation, use the binary left-to-right algorithm unless the
77  ^ exponent contains more than HUGE_EXP_CUTOFF bits.  In that case, do
78  * (no more than) EXP_WINDOW_SIZE bits at a time.  The potential drawback is
79  * that a table of 2**(EXP_WINDOW_SIZE - 1) intermediate results is
80  * precomputed.
81  */
82 #define EXP_WINDOW_SIZE 5
83 #define EXP_TABLE_LEN (1 << (EXP_WINDOW_SIZE - 1))
84 /* Suppose the exponent has bit length e. All ways of doing this
85  * need e squarings. The binary method also needs a multiply for
86  * each bit set. In a k-ary method with window width w, a multiply
87  * for each non-zero window, so at worst (and likely!)
88  * ceiling(e/w). The k-ary sliding window method has the same
89  * worst case, but the window slides so it can sometimes skip
90  * over an all-zero window that the fixed-window method can't
91  * exploit. In addition, the windowing methods need multiplies
92  * to precompute a table of small powers.
93  *
94  * For the sliding window method with width 5, 16 precomputation
95  * multiplies are needed. Assuming about half the exponent bits
96  * are set, then, the binary method needs about e/2 extra mults
97  * and the window method about 16 + e/5.
98  *
99  * The latter is smaller for e > 53 1/3. We don't have direct
100  * access to the bit length, though, so call it 60, which is a
101  * multiple of a long digit's max bit length (15 or 30 so far).
102  */
103 #define HUGE_EXP_CUTOFF 60
104 
105 #define SIGCHECK(PyTryBlock)                    \
106     do {                                        \
107         if (PyErr_CheckSignals()) PyTryBlock    \
108     } while(0)
109 
110 /* Normalize (remove leading zeros from) an int object.
111    Doesn't attempt to free the storage--in most cases, due to the nature
112    of the algorithms used, this could save at most be one word anyway. */
113 
114 static PyLongObject *
long_normalize(PyLongObject * v)115 long_normalize(PyLongObject *v)
116 {
117     Py_ssize_t j = _PyLong_DigitCount(v);
118     Py_ssize_t i = j;
119 
120     while (i > 0 && v->long_value.ob_digit[i-1] == 0)
121         --i;
122     if (i != j) {
123         if (i == 0) {
124             _PyLong_SetSignAndDigitCount(v, 0, 0);
125         }
126         else {
127             _PyLong_SetDigitCount(v, i);
128         }
129     }
130     return v;
131 }
132 
133 /* Allocate a new int object with size digits.
134    Return NULL and set exception if we run out of memory. */
135 
136 #define MAX_LONG_DIGITS \
137     ((PY_SSIZE_T_MAX - offsetof(PyLongObject, long_value.ob_digit))/sizeof(digit))
138 
139 PyLongObject *
_PyLong_New(Py_ssize_t size)140 _PyLong_New(Py_ssize_t size)
141 {
142     assert(size >= 0);
143     PyLongObject *result;
144     if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
145         PyErr_SetString(PyExc_OverflowError,
146                         "too many digits in integer");
147         return NULL;
148     }
149     /* Fast operations for single digit integers (including zero)
150      * assume that there is always at least one digit present. */
151     Py_ssize_t ndigits = size ? size : 1;
152     /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
153        sizeof(digit)*size.  Previous incarnations of this code used
154        sizeof() instead of the offsetof, but this risks being
155        incorrect in the presence of padding between the header
156        and the digits. */
157     result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) +
158                              ndigits*sizeof(digit));
159     if (!result) {
160         PyErr_NoMemory();
161         return NULL;
162     }
163     _PyLong_SetSignAndDigitCount(result, size != 0, size);
164     _PyObject_Init((PyObject*)result, &PyLong_Type);
165     /* The digit has to be initialized explicitly to avoid
166      * use-of-uninitialized-value. */
167     result->long_value.ob_digit[0] = 0;
168     return result;
169 }
170 
171 PyLongObject *
_PyLong_FromDigits(int negative,Py_ssize_t digit_count,digit * digits)172 _PyLong_FromDigits(int negative, Py_ssize_t digit_count, digit *digits)
173 {
174     assert(digit_count >= 0);
175     if (digit_count == 0) {
176         return (PyLongObject *)_PyLong_GetZero();
177     }
178     PyLongObject *result = _PyLong_New(digit_count);
179     if (result == NULL) {
180         PyErr_NoMemory();
181         return NULL;
182     }
183     _PyLong_SetSignAndDigitCount(result, negative?-1:1, digit_count);
184     memcpy(result->long_value.ob_digit, digits, digit_count * sizeof(digit));
185     return result;
186 }
187 
188 PyObject *
_PyLong_Copy(PyLongObject * src)189 _PyLong_Copy(PyLongObject *src)
190 {
191     assert(src != NULL);
192 
193     if (_PyLong_IsCompact(src)) {
194         stwodigits ival = medium_value(src);
195         if (IS_SMALL_INT(ival)) {
196             return get_small_int((sdigit)ival);
197         }
198     }
199     Py_ssize_t size = _PyLong_DigitCount(src);
200     return (PyObject *)_PyLong_FromDigits(_PyLong_IsNegative(src), size, src->long_value.ob_digit);
201 }
202 
203 static PyObject *
_PyLong_FromMedium(sdigit x)204 _PyLong_FromMedium(sdigit x)
205 {
206     assert(!IS_SMALL_INT(x));
207     assert(is_medium_int(x));
208     /* We could use a freelist here */
209     PyLongObject *v = PyObject_Malloc(sizeof(PyLongObject));
210     if (v == NULL) {
211         PyErr_NoMemory();
212         return NULL;
213     }
214     digit abs_x = x < 0 ? -x : x;
215     _PyLong_SetSignAndDigitCount(v, x<0?-1:1, 1);
216     _PyObject_Init((PyObject*)v, &PyLong_Type);
217     v->long_value.ob_digit[0] = abs_x;
218     return (PyObject*)v;
219 }
220 
221 static PyObject *
_PyLong_FromLarge(stwodigits ival)222 _PyLong_FromLarge(stwodigits ival)
223 {
224     twodigits abs_ival;
225     int sign;
226     assert(!is_medium_int(ival));
227 
228     if (ival < 0) {
229         /* negate: can't write this as abs_ival = -ival since that
230            invokes undefined behaviour when ival is LONG_MIN */
231         abs_ival = 0U-(twodigits)ival;
232         sign = -1;
233     }
234     else {
235         abs_ival = (twodigits)ival;
236         sign = 1;
237     }
238     /* Must be at least two digits */
239     assert(abs_ival >> PyLong_SHIFT != 0);
240     twodigits t = abs_ival >> (PyLong_SHIFT * 2);
241     Py_ssize_t ndigits = 2;
242     while (t) {
243         ++ndigits;
244         t >>= PyLong_SHIFT;
245     }
246     PyLongObject *v = _PyLong_New(ndigits);
247     if (v != NULL) {
248         digit *p = v->long_value.ob_digit;
249         _PyLong_SetSignAndDigitCount(v, sign, ndigits);
250         t = abs_ival;
251         while (t) {
252             *p++ = Py_SAFE_DOWNCAST(
253                 t & PyLong_MASK, twodigits, digit);
254             t >>= PyLong_SHIFT;
255         }
256     }
257     return (PyObject *)v;
258 }
259 
260 /* Create a new int object from a C word-sized int */
261 static inline PyObject *
_PyLong_FromSTwoDigits(stwodigits x)262 _PyLong_FromSTwoDigits(stwodigits x)
263 {
264     if (IS_SMALL_INT(x)) {
265         return get_small_int((sdigit)x);
266     }
267     assert(x != 0);
268     if (is_medium_int(x)) {
269         return _PyLong_FromMedium((sdigit)x);
270     }
271     return _PyLong_FromLarge(x);
272 }
273 
274 /* If a freshly-allocated int is already shared, it must
275    be a small integer, so negating it must go to PyLong_FromLong */
276 Py_LOCAL_INLINE(void)
_PyLong_Negate(PyLongObject ** x_p)277 _PyLong_Negate(PyLongObject **x_p)
278 {
279     PyLongObject *x;
280 
281     x = (PyLongObject *)*x_p;
282     if (Py_REFCNT(x) == 1) {
283          _PyLong_FlipSign(x);
284         return;
285     }
286 
287     *x_p = (PyLongObject *)_PyLong_FromSTwoDigits(-medium_value(x));
288     Py_DECREF(x);
289 }
290 
291 /* Create a new int object from a C long int */
292 
293 PyObject *
PyLong_FromLong(long ival)294 PyLong_FromLong(long ival)
295 {
296     PyLongObject *v;
297     unsigned long abs_ival, t;
298     int ndigits;
299 
300     /* Handle small and medium cases. */
301     if (IS_SMALL_INT(ival)) {
302         return get_small_int((sdigit)ival);
303     }
304     if (-(long)PyLong_MASK <= ival && ival <= (long)PyLong_MASK) {
305         return _PyLong_FromMedium((sdigit)ival);
306     }
307 
308     /* Count digits (at least two - smaller cases were handled above). */
309     abs_ival = ival < 0 ? 0U-(unsigned long)ival : (unsigned long)ival;
310     /* Do shift in two steps to avoid possible undefined behavior. */
311     t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;
312     ndigits = 2;
313     while (t) {
314         ++ndigits;
315         t >>= PyLong_SHIFT;
316     }
317 
318     /* Construct output value. */
319     v = _PyLong_New(ndigits);
320     if (v != NULL) {
321         digit *p = v->long_value.ob_digit;
322         _PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
323         t = abs_ival;
324         while (t) {
325             *p++ = (digit)(t & PyLong_MASK);
326             t >>= PyLong_SHIFT;
327         }
328     }
329     return (PyObject *)v;
330 }
331 
332 #define PYLONG_FROM_UINT(INT_TYPE, ival) \
333     do { \
334         if (IS_SMALL_UINT(ival)) { \
335             return get_small_int((sdigit)(ival)); \
336         } \
337         /* Count the number of Python digits. */ \
338         Py_ssize_t ndigits = 0; \
339         INT_TYPE t = (ival); \
340         while (t) { \
341             ++ndigits; \
342             t >>= PyLong_SHIFT; \
343         } \
344         PyLongObject *v = _PyLong_New(ndigits); \
345         if (v == NULL) { \
346             return NULL; \
347         } \
348         digit *p = v->long_value.ob_digit; \
349         while ((ival)) { \
350             *p++ = (digit)((ival) & PyLong_MASK); \
351             (ival) >>= PyLong_SHIFT; \
352         } \
353         return (PyObject *)v; \
354     } while(0)
355 
356 /* Create a new int object from a C unsigned long int */
357 
358 PyObject *
PyLong_FromUnsignedLong(unsigned long ival)359 PyLong_FromUnsignedLong(unsigned long ival)
360 {
361     PYLONG_FROM_UINT(unsigned long, ival);
362 }
363 
364 /* Create a new int object from a C unsigned long long int. */
365 
366 PyObject *
PyLong_FromUnsignedLongLong(unsigned long long ival)367 PyLong_FromUnsignedLongLong(unsigned long long ival)
368 {
369     PYLONG_FROM_UINT(unsigned long long, ival);
370 }
371 
372 /* Create a new int object from a C size_t. */
373 
374 PyObject *
PyLong_FromSize_t(size_t ival)375 PyLong_FromSize_t(size_t ival)
376 {
377     PYLONG_FROM_UINT(size_t, ival);
378 }
379 
380 /* Create a new int object from a C double */
381 
382 PyObject *
PyLong_FromDouble(double dval)383 PyLong_FromDouble(double dval)
384 {
385     /* Try to get out cheap if this fits in a long. When a finite value of real
386      * floating type is converted to an integer type, the value is truncated
387      * toward zero. If the value of the integral part cannot be represented by
388      * the integer type, the behavior is undefined. Thus, we must check that
389      * value is in range (LONG_MIN - 1, LONG_MAX + 1). If a long has more bits
390      * of precision than a double, casting LONG_MIN - 1 to double may yield an
391      * approximation, but LONG_MAX + 1 is a power of two and can be represented
392      * as double exactly (assuming FLT_RADIX is 2 or 16), so for simplicity
393      * check against [-(LONG_MAX + 1), LONG_MAX + 1).
394      */
395     const double int_max = (unsigned long)LONG_MAX + 1;
396     if (-int_max < dval && dval < int_max) {
397         return PyLong_FromLong((long)dval);
398     }
399 
400     PyLongObject *v;
401     double frac;
402     int i, ndig, expo, neg;
403     neg = 0;
404     if (Py_IS_INFINITY(dval)) {
405         PyErr_SetString(PyExc_OverflowError,
406                         "cannot convert float infinity to integer");
407         return NULL;
408     }
409     if (Py_IS_NAN(dval)) {
410         PyErr_SetString(PyExc_ValueError,
411                         "cannot convert float NaN to integer");
412         return NULL;
413     }
414     if (dval < 0.0) {
415         neg = 1;
416         dval = -dval;
417     }
418     frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
419     assert(expo > 0);
420     ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
421     v = _PyLong_New(ndig);
422     if (v == NULL)
423         return NULL;
424     frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
425     for (i = ndig; --i >= 0; ) {
426         digit bits = (digit)frac;
427         v->long_value.ob_digit[i] = bits;
428         frac = frac - (double)bits;
429         frac = ldexp(frac, PyLong_SHIFT);
430     }
431     if (neg) {
432         _PyLong_FlipSign(v);
433     }
434     return (PyObject *)v;
435 }
436 
437 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
438  * anything about what happens when a signed integer operation overflows,
439  * and some compilers think they're doing you a favor by being "clever"
440  * then.  The bit pattern for the largest positive signed long is
441  * (unsigned long)LONG_MAX, and for the smallest negative signed long
442  * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
443  * However, some other compilers warn about applying unary minus to an
444  * unsigned operand.  Hence the weird "0-".
445  */
446 #define PY_ABS_LONG_MIN         (0-(unsigned long)LONG_MIN)
447 #define PY_ABS_SSIZE_T_MIN      (0-(size_t)PY_SSIZE_T_MIN)
448 
449 /* Get a C long int from an int object or any object that has an __index__
450    method.
451 
452    On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of
453    the result.  Otherwise *overflow is 0.
454 
455    For other errors (e.g., TypeError), return -1 and set an error condition.
456    In this case *overflow will be 0.
457 */
458 
459 long
PyLong_AsLongAndOverflow(PyObject * vv,int * overflow)460 PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
461 {
462     /* This version by Tim Peters */
463     PyLongObject *v;
464     unsigned long x, prev;
465     long res;
466     Py_ssize_t i;
467     int sign;
468     int do_decref = 0; /* if PyNumber_Index was called */
469 
470     *overflow = 0;
471     if (vv == NULL) {
472         PyErr_BadInternalCall();
473         return -1;
474     }
475 
476     if (PyLong_Check(vv)) {
477         v = (PyLongObject *)vv;
478     }
479     else {
480         v = (PyLongObject *)_PyNumber_Index(vv);
481         if (v == NULL)
482             return -1;
483         do_decref = 1;
484     }
485     if (_PyLong_IsCompact(v)) {
486 #if SIZEOF_LONG < SIZEOF_SIZE_T
487         Py_ssize_t tmp = _PyLong_CompactValue(v);
488         if (tmp < LONG_MIN) {
489             *overflow = -1;
490             res = -1;
491         }
492         else if (tmp > LONG_MAX) {
493             *overflow = 1;
494             res = -1;
495         }
496         else {
497             res = (long)tmp;
498         }
499 #else
500         res = _PyLong_CompactValue(v);
501 #endif
502     }
503     else {
504         res = -1;
505         i = _PyLong_DigitCount(v);
506         sign = _PyLong_NonCompactSign(v);
507         x = 0;
508         while (--i >= 0) {
509             prev = x;
510             x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
511             if ((x >> PyLong_SHIFT) != prev) {
512                 *overflow = sign;
513                 goto exit;
514             }
515         }
516         /* Haven't lost any bits, but casting to long requires extra
517         * care (see comment above).
518         */
519         if (x <= (unsigned long)LONG_MAX) {
520             res = (long)x * sign;
521         }
522         else if (sign < 0 && x == PY_ABS_LONG_MIN) {
523             res = LONG_MIN;
524         }
525         else {
526             *overflow = sign;
527             /* res is already set to -1 */
528         }
529     }
530   exit:
531     if (do_decref) {
532         Py_DECREF(v);
533     }
534     return res;
535 }
536 
537 /* Get a C long int from an int object or any object that has an __index__
538    method.  Return -1 and set an error if overflow occurs. */
539 
540 long
PyLong_AsLong(PyObject * obj)541 PyLong_AsLong(PyObject *obj)
542 {
543     int overflow;
544     long result = PyLong_AsLongAndOverflow(obj, &overflow);
545     if (overflow) {
546         /* XXX: could be cute and give a different
547            message for overflow == -1 */
548         PyErr_SetString(PyExc_OverflowError,
549                         "Python int too large to convert to C long");
550     }
551     return result;
552 }
553 
554 /* Get a C int from an int object or any object that has an __index__
555    method.  Return -1 and set an error if overflow occurs. */
556 
557 int
PyLong_AsInt(PyObject * obj)558 PyLong_AsInt(PyObject *obj)
559 {
560     int overflow;
561     long result = PyLong_AsLongAndOverflow(obj, &overflow);
562     if (overflow || result > INT_MAX || result < INT_MIN) {
563         /* XXX: could be cute and give a different
564            message for overflow == -1 */
565         PyErr_SetString(PyExc_OverflowError,
566                         "Python int too large to convert to C int");
567         return -1;
568     }
569     return (int)result;
570 }
571 
572 /* Get a Py_ssize_t from an int object.
573    Returns -1 and sets an error condition if overflow occurs. */
574 
575 Py_ssize_t
PyLong_AsSsize_t(PyObject * vv)576 PyLong_AsSsize_t(PyObject *vv) {
577     PyLongObject *v;
578     size_t x, prev;
579     Py_ssize_t i;
580     int sign;
581 
582     if (vv == NULL) {
583         PyErr_BadInternalCall();
584         return -1;
585     }
586     if (!PyLong_Check(vv)) {
587         PyErr_SetString(PyExc_TypeError, "an integer is required");
588         return -1;
589     }
590 
591     v = (PyLongObject *)vv;
592     if (_PyLong_IsCompact(v)) {
593         return _PyLong_CompactValue(v);
594     }
595     i = _PyLong_DigitCount(v);
596     sign = _PyLong_NonCompactSign(v);
597     x = 0;
598     while (--i >= 0) {
599         prev = x;
600         x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
601         if ((x >> PyLong_SHIFT) != prev)
602             goto overflow;
603     }
604     /* Haven't lost any bits, but casting to a signed type requires
605      * extra care (see comment above).
606      */
607     if (x <= (size_t)PY_SSIZE_T_MAX) {
608         return (Py_ssize_t)x * sign;
609     }
610     else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
611         return PY_SSIZE_T_MIN;
612     }
613     /* else overflow */
614 
615   overflow:
616     PyErr_SetString(PyExc_OverflowError,
617                     "Python int too large to convert to C ssize_t");
618     return -1;
619 }
620 
621 /* Get a C unsigned long int from an int object.
622    Returns -1 and sets an error condition if overflow occurs. */
623 
624 unsigned long
PyLong_AsUnsignedLong(PyObject * vv)625 PyLong_AsUnsignedLong(PyObject *vv)
626 {
627     PyLongObject *v;
628     unsigned long x, prev;
629     Py_ssize_t i;
630 
631     if (vv == NULL) {
632         PyErr_BadInternalCall();
633         return (unsigned long)-1;
634     }
635     if (!PyLong_Check(vv)) {
636         PyErr_SetString(PyExc_TypeError, "an integer is required");
637         return (unsigned long)-1;
638     }
639 
640     v = (PyLongObject *)vv;
641     if (_PyLong_IsNonNegativeCompact(v)) {
642 #if SIZEOF_LONG < SIZEOF_SIZE_T
643         size_t tmp = (size_t)_PyLong_CompactValue(v);
644         unsigned long res = (unsigned long)tmp;
645         if (res != tmp) {
646             goto overflow;
647         }
648         return res;
649 #else
650         return (unsigned long)(size_t)_PyLong_CompactValue(v);
651 #endif
652     }
653     if (_PyLong_IsNegative(v)) {
654         PyErr_SetString(PyExc_OverflowError,
655                         "can't convert negative value to unsigned int");
656         return (unsigned long) -1;
657     }
658     i = _PyLong_DigitCount(v);
659     x = 0;
660     while (--i >= 0) {
661         prev = x;
662         x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
663         if ((x >> PyLong_SHIFT) != prev) {
664             goto overflow;
665         }
666     }
667     return x;
668 overflow:
669     PyErr_SetString(PyExc_OverflowError,
670                     "Python int too large to convert "
671                     "to C unsigned long");
672     return (unsigned long) -1;
673 }
674 
675 /* Get a C size_t from an int object. Returns (size_t)-1 and sets
676    an error condition if overflow occurs. */
677 
678 size_t
PyLong_AsSize_t(PyObject * vv)679 PyLong_AsSize_t(PyObject *vv)
680 {
681     PyLongObject *v;
682     size_t x, prev;
683     Py_ssize_t i;
684 
685     if (vv == NULL) {
686         PyErr_BadInternalCall();
687         return (size_t) -1;
688     }
689     if (!PyLong_Check(vv)) {
690         PyErr_SetString(PyExc_TypeError, "an integer is required");
691         return (size_t)-1;
692     }
693 
694     v = (PyLongObject *)vv;
695     if (_PyLong_IsNonNegativeCompact(v)) {
696         return (size_t)_PyLong_CompactValue(v);
697     }
698     if (_PyLong_IsNegative(v)) {
699         PyErr_SetString(PyExc_OverflowError,
700                    "can't convert negative value to size_t");
701         return (size_t) -1;
702     }
703     i = _PyLong_DigitCount(v);
704     x = 0;
705     while (--i >= 0) {
706         prev = x;
707         x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
708         if ((x >> PyLong_SHIFT) != prev) {
709             PyErr_SetString(PyExc_OverflowError,
710                 "Python int too large to convert to C size_t");
711             return (size_t) -1;
712         }
713     }
714     return x;
715 }
716 
717 /* Get a C unsigned long int from an int object, ignoring the high bits.
718    Returns -1 and sets an error condition if an error occurs. */
719 
720 static unsigned long
_PyLong_AsUnsignedLongMask(PyObject * vv)721 _PyLong_AsUnsignedLongMask(PyObject *vv)
722 {
723     PyLongObject *v;
724     unsigned long x;
725     Py_ssize_t i;
726 
727     if (vv == NULL || !PyLong_Check(vv)) {
728         PyErr_BadInternalCall();
729         return (unsigned long) -1;
730     }
731     v = (PyLongObject *)vv;
732     if (_PyLong_IsCompact(v)) {
733 #if SIZEOF_LONG < SIZEOF_SIZE_T
734         return (unsigned long)(size_t)_PyLong_CompactValue(v);
735 #else
736         return (unsigned long)(long)_PyLong_CompactValue(v);
737 #endif
738     }
739     i = _PyLong_DigitCount(v);
740     int sign = _PyLong_NonCompactSign(v);
741     x = 0;
742     while (--i >= 0) {
743         x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
744     }
745     return x * sign;
746 }
747 
748 unsigned long
PyLong_AsUnsignedLongMask(PyObject * op)749 PyLong_AsUnsignedLongMask(PyObject *op)
750 {
751     PyLongObject *lo;
752     unsigned long val;
753 
754     if (op == NULL) {
755         PyErr_BadInternalCall();
756         return (unsigned long)-1;
757     }
758 
759     if (PyLong_Check(op)) {
760         return _PyLong_AsUnsignedLongMask(op);
761     }
762 
763     lo = (PyLongObject *)_PyNumber_Index(op);
764     if (lo == NULL)
765         return (unsigned long)-1;
766 
767     val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
768     Py_DECREF(lo);
769     return val;
770 }
771 
772 int
_PyLong_Sign(PyObject * vv)773 _PyLong_Sign(PyObject *vv)
774 {
775     PyLongObject *v = (PyLongObject *)vv;
776 
777     assert(v != NULL);
778     assert(PyLong_Check(v));
779     if (_PyLong_IsCompact(v)) {
780         return _PyLong_CompactSign(v);
781     }
782     return _PyLong_NonCompactSign(v);
783 }
784 
785 static int
bit_length_digit(digit x)786 bit_length_digit(digit x)
787 {
788     // digit can be larger than unsigned long, but only PyLong_SHIFT bits
789     // of it will be ever used.
790     static_assert(PyLong_SHIFT <= sizeof(unsigned long) * 8,
791                   "digit is larger than unsigned long");
792     return _Py_bit_length((unsigned long)x);
793 }
794 
795 size_t
_PyLong_NumBits(PyObject * vv)796 _PyLong_NumBits(PyObject *vv)
797 {
798     PyLongObject *v = (PyLongObject *)vv;
799     size_t result = 0;
800     Py_ssize_t ndigits;
801     int msd_bits;
802 
803     assert(v != NULL);
804     assert(PyLong_Check(v));
805     ndigits = _PyLong_DigitCount(v);
806     assert(ndigits == 0 || v->long_value.ob_digit[ndigits - 1] != 0);
807     if (ndigits > 0) {
808         digit msd = v->long_value.ob_digit[ndigits - 1];
809         if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT)
810             goto Overflow;
811         result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT;
812         msd_bits = bit_length_digit(msd);
813         if (SIZE_MAX - msd_bits < result)
814             goto Overflow;
815         result += msd_bits;
816     }
817     return result;
818 
819   Overflow:
820     PyErr_SetString(PyExc_OverflowError, "int has too many bits "
821                     "to express in a platform size_t");
822     return (size_t)-1;
823 }
824 
825 PyObject *
_PyLong_FromByteArray(const unsigned char * bytes,size_t n,int little_endian,int is_signed)826 _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
827                       int little_endian, int is_signed)
828 {
829     const unsigned char* pstartbyte;    /* LSB of bytes */
830     int incr;                           /* direction to move pstartbyte */
831     const unsigned char* pendbyte;      /* MSB of bytes */
832     size_t numsignificantbytes;         /* number of bytes that matter */
833     Py_ssize_t ndigits;                 /* number of Python int digits */
834     PyLongObject* v;                    /* result */
835     Py_ssize_t idigit = 0;              /* next free index in v->long_value.ob_digit */
836 
837     if (n == 0)
838         return PyLong_FromLong(0L);
839 
840     if (little_endian) {
841         pstartbyte = bytes;
842         pendbyte = bytes + n - 1;
843         incr = 1;
844     }
845     else {
846         pstartbyte = bytes + n - 1;
847         pendbyte = bytes;
848         incr = -1;
849     }
850 
851     if (is_signed)
852         is_signed = *pendbyte >= 0x80;
853 
854     /* Compute numsignificantbytes.  This consists of finding the most
855        significant byte.  Leading 0 bytes are insignificant if the number
856        is positive, and leading 0xff bytes if negative. */
857     {
858         size_t i;
859         const unsigned char* p = pendbyte;
860         const int pincr = -incr;  /* search MSB to LSB */
861         const unsigned char insignificant = is_signed ? 0xff : 0x00;
862 
863         for (i = 0; i < n; ++i, p += pincr) {
864             if (*p != insignificant)
865                 break;
866         }
867         numsignificantbytes = n - i;
868         /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
869            actually has 2 significant bytes.  OTOH, 0xff0001 ==
870            -0x00ffff, so we wouldn't *need* to bump it there; but we
871            do for 0xffff = -0x0001.  To be safe without bothering to
872            check every case, bump it regardless. */
873         if (is_signed && numsignificantbytes < n)
874             ++numsignificantbytes;
875     }
876 
877     /* How many Python int digits do we need?  We have
878        8*numsignificantbytes bits, and each Python int digit has
879        PyLong_SHIFT bits, so it's the ceiling of the quotient. */
880     /* catch overflow before it happens */
881     if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
882         PyErr_SetString(PyExc_OverflowError,
883                         "byte array too long to convert to int");
884         return NULL;
885     }
886     ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
887     v = _PyLong_New(ndigits);
888     if (v == NULL)
889         return NULL;
890 
891     /* Copy the bits over.  The tricky parts are computing 2's-comp on
892        the fly for signed numbers, and dealing with the mismatch between
893        8-bit bytes and (probably) 15-bit Python digits.*/
894     {
895         size_t i;
896         twodigits carry = 1;                    /* for 2's-comp calculation */
897         twodigits accum = 0;                    /* sliding register */
898         unsigned int accumbits = 0;             /* number of bits in accum */
899         const unsigned char* p = pstartbyte;
900 
901         for (i = 0; i < numsignificantbytes; ++i, p += incr) {
902             twodigits thisbyte = *p;
903             /* Compute correction for 2's comp, if needed. */
904             if (is_signed) {
905                 thisbyte = (0xff ^ thisbyte) + carry;
906                 carry = thisbyte >> 8;
907                 thisbyte &= 0xff;
908             }
909             /* Because we're going LSB to MSB, thisbyte is
910                more significant than what's already in accum,
911                so needs to be prepended to accum. */
912             accum |= thisbyte << accumbits;
913             accumbits += 8;
914             if (accumbits >= PyLong_SHIFT) {
915                 /* There's enough to fill a Python digit. */
916                 assert(idigit < ndigits);
917                 v->long_value.ob_digit[idigit] = (digit)(accum & PyLong_MASK);
918                 ++idigit;
919                 accum >>= PyLong_SHIFT;
920                 accumbits -= PyLong_SHIFT;
921                 assert(accumbits < PyLong_SHIFT);
922             }
923         }
924         assert(accumbits < PyLong_SHIFT);
925         if (accumbits) {
926             assert(idigit < ndigits);
927             v->long_value.ob_digit[idigit] = (digit)accum;
928             ++idigit;
929         }
930     }
931 
932     int sign = is_signed ? -1: 1;
933     if (idigit == 0) {
934         sign = 0;
935     }
936     _PyLong_SetSignAndDigitCount(v, sign, idigit);
937     return (PyObject *)maybe_small_long(long_normalize(v));
938 }
939 
940 int
_PyLong_AsByteArray(PyLongObject * v,unsigned char * bytes,size_t n,int little_endian,int is_signed,int with_exceptions)941 _PyLong_AsByteArray(PyLongObject* v,
942                     unsigned char* bytes, size_t n,
943                     int little_endian, int is_signed,
944                     int with_exceptions)
945 {
946     Py_ssize_t i;               /* index into v->long_value.ob_digit */
947     Py_ssize_t ndigits;         /* number of digits */
948     twodigits accum;            /* sliding register */
949     unsigned int accumbits;     /* # bits in accum */
950     int do_twos_comp;           /* store 2's-comp?  is_signed and v < 0 */
951     digit carry;                /* for computing 2's-comp */
952     size_t j;                   /* # bytes filled */
953     unsigned char* p;           /* pointer to next byte in bytes */
954     int pincr;                  /* direction to move p */
955 
956     assert(v != NULL && PyLong_Check(v));
957 
958     ndigits = _PyLong_DigitCount(v);
959     if (_PyLong_IsNegative(v)) {
960         if (!is_signed) {
961             if (with_exceptions) {
962                 PyErr_SetString(PyExc_OverflowError,
963                                 "can't convert negative int to unsigned");
964             }
965             return -1;
966         }
967         do_twos_comp = 1;
968     }
969     else {
970         do_twos_comp = 0;
971     }
972 
973     if (little_endian) {
974         p = bytes;
975         pincr = 1;
976     }
977     else {
978         p = bytes + n - 1;
979         pincr = -1;
980     }
981 
982     /* Copy over all the Python digits.
983        It's crucial that every Python digit except for the MSD contribute
984        exactly PyLong_SHIFT bits to the total, so first assert that the int is
985        normalized.
986        NOTE: PyLong_AsNativeBytes() assumes that this function will fill in 'n'
987        bytes even if it eventually fails to convert the whole number. Make sure
988        you account for that if you are changing this algorithm to return without
989        doing that.
990        */
991     assert(ndigits == 0 || v->long_value.ob_digit[ndigits - 1] != 0);
992     j = 0;
993     accum = 0;
994     accumbits = 0;
995     carry = do_twos_comp ? 1 : 0;
996     for (i = 0; i < ndigits; ++i) {
997         digit thisdigit = v->long_value.ob_digit[i];
998         if (do_twos_comp) {
999             thisdigit = (thisdigit ^ PyLong_MASK) + carry;
1000             carry = thisdigit >> PyLong_SHIFT;
1001             thisdigit &= PyLong_MASK;
1002         }
1003         /* Because we're going LSB to MSB, thisdigit is more
1004            significant than what's already in accum, so needs to be
1005            prepended to accum. */
1006         accum |= (twodigits)thisdigit << accumbits;
1007 
1008         /* The most-significant digit may be (probably is) at least
1009            partly empty. */
1010         if (i == ndigits - 1) {
1011             /* Count # of sign bits -- they needn't be stored,
1012              * although for signed conversion we need later to
1013              * make sure at least one sign bit gets stored. */
1014             digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit;
1015             while (s != 0) {
1016                 s >>= 1;
1017                 accumbits++;
1018             }
1019         }
1020         else
1021             accumbits += PyLong_SHIFT;
1022 
1023         /* Store as many bytes as possible. */
1024         while (accumbits >= 8) {
1025             if (j >= n)
1026                 goto Overflow;
1027             ++j;
1028             *p = (unsigned char)(accum & 0xff);
1029             p += pincr;
1030             accumbits -= 8;
1031             accum >>= 8;
1032         }
1033     }
1034 
1035     /* Store the straggler (if any). */
1036     assert(accumbits < 8);
1037     assert(carry == 0);  /* else do_twos_comp and *every* digit was 0 */
1038     if (accumbits > 0) {
1039         if (j >= n)
1040             goto Overflow;
1041         ++j;
1042         if (do_twos_comp) {
1043             /* Fill leading bits of the byte with sign bits
1044                (appropriately pretending that the int had an
1045                infinite supply of sign bits). */
1046             accum |= (~(twodigits)0) << accumbits;
1047         }
1048         *p = (unsigned char)(accum & 0xff);
1049         p += pincr;
1050     }
1051     else if (j == n && n > 0 && is_signed) {
1052         /* The main loop filled the byte array exactly, so the code
1053            just above didn't get to ensure there's a sign bit, and the
1054            loop below wouldn't add one either.  Make sure a sign bit
1055            exists. */
1056         unsigned char msb = *(p - pincr);
1057         int sign_bit_set = msb >= 0x80;
1058         assert(accumbits == 0);
1059         if (sign_bit_set == do_twos_comp)
1060             return 0;
1061         else
1062             goto Overflow;
1063     }
1064 
1065     /* Fill remaining bytes with copies of the sign bit. */
1066     {
1067         unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
1068         for ( ; j < n; ++j, p += pincr)
1069             *p = signbyte;
1070     }
1071 
1072     return 0;
1073 
1074   Overflow:
1075     if (with_exceptions) {
1076         PyErr_SetString(PyExc_OverflowError, "int too big to convert");
1077     }
1078     return -1;
1079 
1080 }
1081 
1082 // Refactored out for readability, not reuse
1083 static inline int
_fits_in_n_bits(Py_ssize_t v,Py_ssize_t n)1084 _fits_in_n_bits(Py_ssize_t v, Py_ssize_t n)
1085 {
1086     if (n >= (Py_ssize_t)sizeof(Py_ssize_t) * 8) {
1087         return 1;
1088     }
1089     // If all bits above n are the same, we fit.
1090     // (Use n-1 if we require the sign bit to be consistent.)
1091     Py_ssize_t v_extended = v >> ((int)n - 1);
1092     return v_extended == 0 || v_extended == -1;
1093 }
1094 
1095 static inline int
_resolve_endianness(int * endianness)1096 _resolve_endianness(int *endianness)
1097 {
1098     if (*endianness == -1 || (*endianness & 2)) {
1099         *endianness = PY_LITTLE_ENDIAN;
1100     } else {
1101         *endianness &= 1;
1102     }
1103     assert(*endianness == 0 || *endianness == 1);
1104     return 0;
1105 }
1106 
1107 Py_ssize_t
PyLong_AsNativeBytes(PyObject * vv,void * buffer,Py_ssize_t n,int flags)1108 PyLong_AsNativeBytes(PyObject* vv, void* buffer, Py_ssize_t n, int flags)
1109 {
1110     PyLongObject *v;
1111     union {
1112         Py_ssize_t v;
1113         unsigned char b[sizeof(Py_ssize_t)];
1114     } cv;
1115     int do_decref = 0;
1116     Py_ssize_t res = 0;
1117 
1118     if (vv == NULL || n < 0) {
1119         PyErr_BadInternalCall();
1120         return -1;
1121     }
1122 
1123     int little_endian = flags;
1124     if (_resolve_endianness(&little_endian) < 0) {
1125         return -1;
1126     }
1127 
1128     if (PyLong_Check(vv)) {
1129         v = (PyLongObject *)vv;
1130     }
1131     else if (flags != -1 && (flags & Py_ASNATIVEBYTES_ALLOW_INDEX)) {
1132         v = (PyLongObject *)_PyNumber_Index(vv);
1133         if (v == NULL) {
1134             return -1;
1135         }
1136         do_decref = 1;
1137     }
1138     else {
1139         PyErr_Format(PyExc_TypeError, "expect int, got %T", vv);
1140         return -1;
1141     }
1142 
1143     if ((flags != -1 && (flags & Py_ASNATIVEBYTES_REJECT_NEGATIVE))
1144         && _PyLong_IsNegative(v)) {
1145         PyErr_SetString(PyExc_ValueError, "Cannot convert negative int");
1146         if (do_decref) {
1147             Py_DECREF(v);
1148         }
1149         return -1;
1150     }
1151 
1152     if (_PyLong_IsCompact(v)) {
1153         res = 0;
1154         cv.v = _PyLong_CompactValue(v);
1155         /* Most paths result in res = sizeof(compact value). Only the case
1156          * where 0 < n < sizeof(compact value) do we need to check and adjust
1157          * our return value. */
1158         res = sizeof(cv.b);
1159         if (n <= 0) {
1160             // nothing to do!
1161         }
1162         else if (n <= (Py_ssize_t)sizeof(cv.b)) {
1163 #if PY_LITTLE_ENDIAN
1164             if (little_endian) {
1165                 memcpy(buffer, cv.b, n);
1166             }
1167             else {
1168                 for (Py_ssize_t i = 0; i < n; ++i) {
1169                     ((unsigned char*)buffer)[n - i - 1] = cv.b[i];
1170                 }
1171             }
1172 #else
1173             if (little_endian) {
1174                 for (Py_ssize_t i = 0; i < n; ++i) {
1175                     ((unsigned char*)buffer)[i] = cv.b[sizeof(cv.b) - i - 1];
1176                 }
1177             }
1178             else {
1179                 memcpy(buffer, &cv.b[sizeof(cv.b) - n], n);
1180             }
1181 #endif
1182 
1183             /* If we fit, return the requested number of bytes */
1184             if (_fits_in_n_bits(cv.v, n * 8)) {
1185                 res = n;
1186             } else if (cv.v > 0 && _fits_in_n_bits(cv.v, n * 8 + 1)) {
1187                 /* Positive values with the MSB set do not require an
1188                  * additional bit when the caller's intent is to treat them
1189                  * as unsigned. */
1190                 if (flags == -1 || (flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER)) {
1191                     res = n;
1192                 } else {
1193                     res = n + 1;
1194                 }
1195             }
1196         }
1197         else {
1198             unsigned char fill = cv.v < 0 ? 0xFF : 0x00;
1199 #if PY_LITTLE_ENDIAN
1200             if (little_endian) {
1201                 memcpy(buffer, cv.b, sizeof(cv.b));
1202                 memset((char *)buffer + sizeof(cv.b), fill, n - sizeof(cv.b));
1203             }
1204             else {
1205                 unsigned char *b = (unsigned char *)buffer;
1206                 for (Py_ssize_t i = 0; i < n - (int)sizeof(cv.b); ++i) {
1207                     *b++ = fill;
1208                 }
1209                 for (Py_ssize_t i = sizeof(cv.b); i > 0; --i) {
1210                     *b++ = cv.b[i - 1];
1211                 }
1212             }
1213 #else
1214             if (little_endian) {
1215                 unsigned char *b = (unsigned char *)buffer;
1216                 for (Py_ssize_t i = sizeof(cv.b); i > 0; --i) {
1217                     *b++ = cv.b[i - 1];
1218                 }
1219                 for (Py_ssize_t i = 0; i < n - (int)sizeof(cv.b); ++i) {
1220                     *b++ = fill;
1221                 }
1222             }
1223             else {
1224                 memset(buffer, fill, n - sizeof(cv.b));
1225                 memcpy((char *)buffer + n - sizeof(cv.b), cv.b, sizeof(cv.b));
1226             }
1227 #endif
1228         }
1229     }
1230     else {
1231         if (n > 0) {
1232             _PyLong_AsByteArray(v, buffer, (size_t)n, little_endian, 1, 0);
1233         }
1234 
1235         /* Calculates the number of bits required for the *absolute* value
1236          * of v. This does not take sign into account, only magnitude. */
1237         size_t nb = _PyLong_NumBits((PyObject *)v);
1238         if (nb == (size_t)-1) {
1239             res = -1;
1240         } else {
1241             /* Normally this would be((nb - 1) / 8) + 1 to avoid rounding up
1242              * multiples of 8 to the next byte, but we add an implied bit for
1243              * the sign and it cancels out. */
1244             res = (Py_ssize_t)(nb / 8) + 1;
1245         }
1246 
1247         /* Two edge cases exist that are best handled after extracting the
1248          * bits. These may result in us reporting overflow when the value
1249          * actually fits.
1250          */
1251         if (n > 0 && res == n + 1 && nb % 8 == 0) {
1252             if (_PyLong_IsNegative(v)) {
1253                 /* Values of 0x80...00 from negative values that use every
1254                  * available bit in the buffer do not require an additional
1255                  * bit to store the sign. */
1256                 int is_edge_case = 1;
1257                 unsigned char *b = (unsigned char *)buffer;
1258                 for (Py_ssize_t i = 0; i < n && is_edge_case; ++i, ++b) {
1259                     if (i == 0) {
1260                         is_edge_case = (*b == (little_endian ? 0 : 0x80));
1261                     } else if (i < n - 1) {
1262                         is_edge_case = (*b == 0);
1263                     } else {
1264                         is_edge_case = (*b == (little_endian ? 0x80 : 0));
1265                     }
1266                 }
1267                 if (is_edge_case) {
1268                     res = n;
1269                 }
1270             }
1271             else {
1272                 /* Positive values with the MSB set do not require an
1273                  * additional bit when the caller's intent is to treat them
1274                  * as unsigned. */
1275                 unsigned char *b = (unsigned char *)buffer;
1276                 if (b[little_endian ? n - 1 : 0] & 0x80) {
1277                     if (flags == -1 || (flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER)) {
1278                         res = n;
1279                     } else {
1280                         res = n + 1;
1281                     }
1282                 }
1283             }
1284         }
1285     }
1286 
1287     if (do_decref) {
1288         Py_DECREF(v);
1289     }
1290 
1291     return res;
1292 }
1293 
1294 
1295 PyObject *
PyLong_FromNativeBytes(const void * buffer,size_t n,int flags)1296 PyLong_FromNativeBytes(const void* buffer, size_t n, int flags)
1297 {
1298     if (!buffer) {
1299         PyErr_BadInternalCall();
1300         return NULL;
1301     }
1302 
1303     int little_endian = flags;
1304     if (_resolve_endianness(&little_endian) < 0) {
1305         return NULL;
1306     }
1307 
1308     return _PyLong_FromByteArray(
1309         (const unsigned char *)buffer,
1310         n,
1311         little_endian,
1312         (flags == -1 || !(flags & Py_ASNATIVEBYTES_UNSIGNED_BUFFER)) ? 1 : 0
1313     );
1314 }
1315 
1316 
1317 PyObject *
PyLong_FromUnsignedNativeBytes(const void * buffer,size_t n,int flags)1318 PyLong_FromUnsignedNativeBytes(const void* buffer, size_t n, int flags)
1319 {
1320     if (!buffer) {
1321         PyErr_BadInternalCall();
1322         return NULL;
1323     }
1324 
1325     int little_endian = flags;
1326     if (_resolve_endianness(&little_endian) < 0) {
1327         return NULL;
1328     }
1329 
1330     return _PyLong_FromByteArray((const unsigned char *)buffer, n, little_endian, 0);
1331 }
1332 
1333 
1334 /* Create a new int object from a C pointer */
1335 
1336 PyObject *
PyLong_FromVoidPtr(void * p)1337 PyLong_FromVoidPtr(void *p)
1338 {
1339 #if SIZEOF_VOID_P <= SIZEOF_LONG
1340     return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p);
1341 #else
1342 
1343 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
1344 #   error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)"
1345 #endif
1346     return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p);
1347 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
1348 
1349 }
1350 
1351 /* Get a C pointer from an int object. */
1352 
1353 void *
PyLong_AsVoidPtr(PyObject * vv)1354 PyLong_AsVoidPtr(PyObject *vv)
1355 {
1356 #if SIZEOF_VOID_P <= SIZEOF_LONG
1357     long x;
1358 
1359     if (PyLong_Check(vv) && _PyLong_IsNegative((PyLongObject *)vv)) {
1360         x = PyLong_AsLong(vv);
1361     }
1362     else {
1363         x = PyLong_AsUnsignedLong(vv);
1364     }
1365 #else
1366 
1367 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
1368 #   error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)"
1369 #endif
1370     long long x;
1371 
1372     if (PyLong_Check(vv) && _PyLong_IsNegative((PyLongObject *)vv)) {
1373         x = PyLong_AsLongLong(vv);
1374     }
1375     else {
1376         x = PyLong_AsUnsignedLongLong(vv);
1377     }
1378 
1379 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
1380 
1381     if (x == -1 && PyErr_Occurred())
1382         return NULL;
1383     return (void *)x;
1384 }
1385 
1386 /* Initial long long support by Chris Herborth (chrish@qnx.com), later
1387  * rewritten to use the newer PyLong_{As,From}ByteArray API.
1388  */
1389 
1390 #define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN)
1391 
1392 /* Create a new int object from a C long long int. */
1393 
1394 PyObject *
PyLong_FromLongLong(long long ival)1395 PyLong_FromLongLong(long long ival)
1396 {
1397     PyLongObject *v;
1398     unsigned long long abs_ival, t;
1399     int ndigits;
1400 
1401     /* Handle small and medium cases. */
1402     if (IS_SMALL_INT(ival)) {
1403         return get_small_int((sdigit)ival);
1404     }
1405     if (-(long long)PyLong_MASK <= ival && ival <= (long long)PyLong_MASK) {
1406         return _PyLong_FromMedium((sdigit)ival);
1407     }
1408 
1409     /* Count digits (at least two - smaller cases were handled above). */
1410     abs_ival = ival < 0 ? 0U-(unsigned long long)ival : (unsigned long long)ival;
1411     /* Do shift in two steps to avoid possible undefined behavior. */
1412     t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;
1413     ndigits = 2;
1414     while (t) {
1415         ++ndigits;
1416         t >>= PyLong_SHIFT;
1417     }
1418 
1419     /* Construct output value. */
1420     v = _PyLong_New(ndigits);
1421     if (v != NULL) {
1422         digit *p = v->long_value.ob_digit;
1423         _PyLong_SetSignAndDigitCount(v, ival < 0 ? -1 : 1, ndigits);
1424         t = abs_ival;
1425         while (t) {
1426             *p++ = (digit)(t & PyLong_MASK);
1427             t >>= PyLong_SHIFT;
1428         }
1429     }
1430     return (PyObject *)v;
1431 }
1432 
1433 /* Create a new int object from a C Py_ssize_t. */
1434 
1435 PyObject *
PyLong_FromSsize_t(Py_ssize_t ival)1436 PyLong_FromSsize_t(Py_ssize_t ival)
1437 {
1438     PyLongObject *v;
1439     size_t abs_ival;
1440     size_t t;  /* unsigned so >> doesn't propagate sign bit */
1441     int ndigits = 0;
1442     int negative = 0;
1443 
1444     if (IS_SMALL_INT(ival)) {
1445         return get_small_int((sdigit)ival);
1446     }
1447 
1448     if (ival < 0) {
1449         /* avoid signed overflow when ival = SIZE_T_MIN */
1450         abs_ival = (size_t)(-1-ival)+1;
1451         negative = 1;
1452     }
1453     else {
1454         abs_ival = (size_t)ival;
1455     }
1456 
1457     /* Count the number of Python digits. */
1458     t = abs_ival;
1459     while (t) {
1460         ++ndigits;
1461         t >>= PyLong_SHIFT;
1462     }
1463     v = _PyLong_New(ndigits);
1464     if (v != NULL) {
1465         digit *p = v->long_value.ob_digit;
1466         _PyLong_SetSignAndDigitCount(v, negative ? -1 : 1, ndigits);
1467         t = abs_ival;
1468         while (t) {
1469             *p++ = (digit)(t & PyLong_MASK);
1470             t >>= PyLong_SHIFT;
1471         }
1472     }
1473     return (PyObject *)v;
1474 }
1475 
1476 /* Get a C long long int from an int object or any object that has an
1477    __index__ method.  Return -1 and set an error if overflow occurs. */
1478 
1479 long long
PyLong_AsLongLong(PyObject * vv)1480 PyLong_AsLongLong(PyObject *vv)
1481 {
1482     PyLongObject *v;
1483     long long bytes;
1484     int res;
1485     int do_decref = 0; /* if PyNumber_Index was called */
1486 
1487     if (vv == NULL) {
1488         PyErr_BadInternalCall();
1489         return -1;
1490     }
1491 
1492     if (PyLong_Check(vv)) {
1493         v = (PyLongObject *)vv;
1494     }
1495     else {
1496         v = (PyLongObject *)_PyNumber_Index(vv);
1497         if (v == NULL)
1498             return -1;
1499         do_decref = 1;
1500     }
1501 
1502     if (_PyLong_IsCompact(v)) {
1503         res = 0;
1504         bytes = _PyLong_CompactValue(v);
1505     }
1506     else {
1507         res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes,
1508                                   SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1, 1);
1509     }
1510     if (do_decref) {
1511         Py_DECREF(v);
1512     }
1513 
1514     /* Plan 9 can't handle long long in ? : expressions */
1515     if (res < 0)
1516         return (long long)-1;
1517     else
1518         return bytes;
1519 }
1520 
1521 /* Get a C unsigned long long int from an int object.
1522    Return -1 and set an error if overflow occurs. */
1523 
1524 unsigned long long
PyLong_AsUnsignedLongLong(PyObject * vv)1525 PyLong_AsUnsignedLongLong(PyObject *vv)
1526 {
1527     PyLongObject *v;
1528     unsigned long long bytes;
1529     int res;
1530 
1531     if (vv == NULL) {
1532         PyErr_BadInternalCall();
1533         return (unsigned long long)-1;
1534     }
1535     if (!PyLong_Check(vv)) {
1536         PyErr_SetString(PyExc_TypeError, "an integer is required");
1537         return (unsigned long long)-1;
1538     }
1539 
1540     v = (PyLongObject*)vv;
1541     if (_PyLong_IsNonNegativeCompact(v)) {
1542         res = 0;
1543 #if SIZEOF_LONG_LONG < SIZEOF_SIZE_T
1544         size_t tmp = (size_t)_PyLong_CompactValue(v);
1545         bytes = (unsigned long long)tmp;
1546         if (bytes != tmp) {
1547             PyErr_SetString(PyExc_OverflowError,
1548                             "Python int too large to convert "
1549                             "to C unsigned long long");
1550             res = -1;
1551         }
1552 #else
1553         bytes = (unsigned long long)(size_t)_PyLong_CompactValue(v);
1554 #endif
1555     }
1556     else {
1557         res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
1558                               SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0, 1);
1559     }
1560 
1561     /* Plan 9 can't handle long long in ? : expressions */
1562     if (res < 0)
1563         return (unsigned long long)res;
1564     else
1565         return bytes;
1566 }
1567 
1568 /* Get a C unsigned long int from an int object, ignoring the high bits.
1569    Returns -1 and sets an error condition if an error occurs. */
1570 
1571 static unsigned long long
_PyLong_AsUnsignedLongLongMask(PyObject * vv)1572 _PyLong_AsUnsignedLongLongMask(PyObject *vv)
1573 {
1574     PyLongObject *v;
1575     unsigned long long x;
1576     Py_ssize_t i;
1577     int sign;
1578 
1579     if (vv == NULL || !PyLong_Check(vv)) {
1580         PyErr_BadInternalCall();
1581         return (unsigned long long) -1;
1582     }
1583     v = (PyLongObject *)vv;
1584     if (_PyLong_IsCompact(v)) {
1585 #if SIZEOF_LONG_LONG < SIZEOF_SIZE_T
1586         return (unsigned long long)(size_t)_PyLong_CompactValue(v);
1587 #else
1588         return (unsigned long long)(long long)_PyLong_CompactValue(v);
1589 #endif
1590     }
1591     i = _PyLong_DigitCount(v);
1592     sign = _PyLong_NonCompactSign(v);
1593     x = 0;
1594     while (--i >= 0) {
1595         x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
1596     }
1597     return x * sign;
1598 }
1599 
1600 unsigned long long
PyLong_AsUnsignedLongLongMask(PyObject * op)1601 PyLong_AsUnsignedLongLongMask(PyObject *op)
1602 {
1603     PyLongObject *lo;
1604     unsigned long long val;
1605 
1606     if (op == NULL) {
1607         PyErr_BadInternalCall();
1608         return (unsigned long long)-1;
1609     }
1610 
1611     if (PyLong_Check(op)) {
1612         return _PyLong_AsUnsignedLongLongMask(op);
1613     }
1614 
1615     lo = (PyLongObject *)_PyNumber_Index(op);
1616     if (lo == NULL)
1617         return (unsigned long long)-1;
1618 
1619     val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
1620     Py_DECREF(lo);
1621     return val;
1622 }
1623 
1624 /* Get a C long long int from an int object or any object that has an
1625    __index__ method.
1626 
1627    On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of
1628    the result.  Otherwise *overflow is 0.
1629 
1630    For other errors (e.g., TypeError), return -1 and set an error condition.
1631    In this case *overflow will be 0.
1632 */
1633 
1634 long long
PyLong_AsLongLongAndOverflow(PyObject * vv,int * overflow)1635 PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
1636 {
1637     /* This version by Tim Peters */
1638     PyLongObject *v;
1639     unsigned long long x, prev;
1640     long long res;
1641     Py_ssize_t i;
1642     int sign;
1643     int do_decref = 0; /* if PyNumber_Index was called */
1644 
1645     *overflow = 0;
1646     if (vv == NULL) {
1647         PyErr_BadInternalCall();
1648         return -1;
1649     }
1650 
1651     if (PyLong_Check(vv)) {
1652         v = (PyLongObject *)vv;
1653     }
1654     else {
1655         v = (PyLongObject *)_PyNumber_Index(vv);
1656         if (v == NULL)
1657             return -1;
1658         do_decref = 1;
1659     }
1660     if (_PyLong_IsCompact(v)) {
1661 #if SIZEOF_LONG_LONG < SIZEOF_SIZE_T
1662         Py_ssize_t tmp = _PyLong_CompactValue(v);
1663         if (tmp < LLONG_MIN) {
1664             *overflow = -1;
1665             res = -1;
1666         }
1667         else if (tmp > LLONG_MAX) {
1668             *overflow = 1;
1669             res = -1;
1670         }
1671         else {
1672             res = (long long)tmp;
1673         }
1674 #else
1675         res = _PyLong_CompactValue(v);
1676 #endif
1677     }
1678     else {
1679         i = _PyLong_DigitCount(v);
1680         sign = _PyLong_NonCompactSign(v);
1681         x = 0;
1682         while (--i >= 0) {
1683             prev = x;
1684             x = (x << PyLong_SHIFT) + v->long_value.ob_digit[i];
1685             if ((x >> PyLong_SHIFT) != prev) {
1686                 *overflow = sign;
1687                 res = -1;
1688                 goto exit;
1689             }
1690         }
1691         /* Haven't lost any bits, but casting to long requires extra
1692          * care (see comment above).
1693          */
1694         if (x <= (unsigned long long)LLONG_MAX) {
1695             res = (long long)x * sign;
1696         }
1697         else if (sign < 0 && x == PY_ABS_LLONG_MIN) {
1698             res = LLONG_MIN;
1699         }
1700         else {
1701             *overflow = sign;
1702             res = -1;
1703         }
1704     }
1705   exit:
1706     if (do_decref) {
1707         Py_DECREF(v);
1708     }
1709     return res;
1710 }
1711 
1712 int
_PyLong_UnsignedShort_Converter(PyObject * obj,void * ptr)1713 _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr)
1714 {
1715     unsigned long uval;
1716 
1717     if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1718         PyErr_SetString(PyExc_ValueError, "value must be positive");
1719         return 0;
1720     }
1721     uval = PyLong_AsUnsignedLong(obj);
1722     if (uval == (unsigned long)-1 && PyErr_Occurred())
1723         return 0;
1724     if (uval > USHRT_MAX) {
1725         PyErr_SetString(PyExc_OverflowError,
1726                         "Python int too large for C unsigned short");
1727         return 0;
1728     }
1729 
1730     *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
1731     return 1;
1732 }
1733 
1734 int
_PyLong_UnsignedInt_Converter(PyObject * obj,void * ptr)1735 _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr)
1736 {
1737     unsigned long uval;
1738 
1739     if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1740         PyErr_SetString(PyExc_ValueError, "value must be positive");
1741         return 0;
1742     }
1743     uval = PyLong_AsUnsignedLong(obj);
1744     if (uval == (unsigned long)-1 && PyErr_Occurred())
1745         return 0;
1746     if (uval > UINT_MAX) {
1747         PyErr_SetString(PyExc_OverflowError,
1748                         "Python int too large for C unsigned int");
1749         return 0;
1750     }
1751 
1752     *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
1753     return 1;
1754 }
1755 
1756 int
_PyLong_UnsignedLong_Converter(PyObject * obj,void * ptr)1757 _PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr)
1758 {
1759     unsigned long uval;
1760 
1761     if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1762         PyErr_SetString(PyExc_ValueError, "value must be positive");
1763         return 0;
1764     }
1765     uval = PyLong_AsUnsignedLong(obj);
1766     if (uval == (unsigned long)-1 && PyErr_Occurred())
1767         return 0;
1768 
1769     *(unsigned long *)ptr = uval;
1770     return 1;
1771 }
1772 
1773 int
_PyLong_UnsignedLongLong_Converter(PyObject * obj,void * ptr)1774 _PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr)
1775 {
1776     unsigned long long uval;
1777 
1778     if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1779         PyErr_SetString(PyExc_ValueError, "value must be positive");
1780         return 0;
1781     }
1782     uval = PyLong_AsUnsignedLongLong(obj);
1783     if (uval == (unsigned long long)-1 && PyErr_Occurred())
1784         return 0;
1785 
1786     *(unsigned long long *)ptr = uval;
1787     return 1;
1788 }
1789 
1790 int
_PyLong_Size_t_Converter(PyObject * obj,void * ptr)1791 _PyLong_Size_t_Converter(PyObject *obj, void *ptr)
1792 {
1793     size_t uval;
1794 
1795     if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1796         PyErr_SetString(PyExc_ValueError, "value must be positive");
1797         return 0;
1798     }
1799     uval = PyLong_AsSize_t(obj);
1800     if (uval == (size_t)-1 && PyErr_Occurred())
1801         return 0;
1802 
1803     *(size_t *)ptr = uval;
1804     return 1;
1805 }
1806 
1807 
1808 #define CHECK_BINOP(v,w)                                \
1809     do {                                                \
1810         if (!PyLong_Check(v) || !PyLong_Check(w))       \
1811             Py_RETURN_NOTIMPLEMENTED;                   \
1812     } while(0)
1813 
1814 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n]
1815  * is modified in place, by adding y to it.  Carries are propagated as far as
1816  * x[m-1], and the remaining carry (0 or 1) is returned.
1817  */
1818 static digit
v_iadd(digit * x,Py_ssize_t m,digit * y,Py_ssize_t n)1819 v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1820 {
1821     Py_ssize_t i;
1822     digit carry = 0;
1823 
1824     assert(m >= n);
1825     for (i = 0; i < n; ++i) {
1826         carry += x[i] + y[i];
1827         x[i] = carry & PyLong_MASK;
1828         carry >>= PyLong_SHIFT;
1829         assert((carry & 1) == carry);
1830     }
1831     for (; carry && i < m; ++i) {
1832         carry += x[i];
1833         x[i] = carry & PyLong_MASK;
1834         carry >>= PyLong_SHIFT;
1835         assert((carry & 1) == carry);
1836     }
1837     return carry;
1838 }
1839 
1840 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n]
1841  * is modified in place, by subtracting y from it.  Borrows are propagated as
1842  * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1843  */
1844 static digit
v_isub(digit * x,Py_ssize_t m,digit * y,Py_ssize_t n)1845 v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1846 {
1847     Py_ssize_t i;
1848     digit borrow = 0;
1849 
1850     assert(m >= n);
1851     for (i = 0; i < n; ++i) {
1852         borrow = x[i] - y[i] - borrow;
1853         x[i] = borrow & PyLong_MASK;
1854         borrow >>= PyLong_SHIFT;
1855         borrow &= 1;            /* keep only 1 sign bit */
1856     }
1857     for (; borrow && i < m; ++i) {
1858         borrow = x[i] - borrow;
1859         x[i] = borrow & PyLong_MASK;
1860         borrow >>= PyLong_SHIFT;
1861         borrow &= 1;
1862     }
1863     return borrow;
1864 }
1865 
1866 /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT.  Put
1867  * result in z[0:m], and return the d bits shifted out of the top.
1868  */
1869 static digit
v_lshift(digit * z,digit * a,Py_ssize_t m,int d)1870 v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
1871 {
1872     Py_ssize_t i;
1873     digit carry = 0;
1874 
1875     assert(0 <= d && d < PyLong_SHIFT);
1876     for (i=0; i < m; i++) {
1877         twodigits acc = (twodigits)a[i] << d | carry;
1878         z[i] = (digit)acc & PyLong_MASK;
1879         carry = (digit)(acc >> PyLong_SHIFT);
1880     }
1881     return carry;
1882 }
1883 
1884 /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT.  Put
1885  * result in z[0:m], and return the d bits shifted out of the bottom.
1886  */
1887 static digit
v_rshift(digit * z,digit * a,Py_ssize_t m,int d)1888 v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
1889 {
1890     Py_ssize_t i;
1891     digit carry = 0;
1892     digit mask = ((digit)1 << d) - 1U;
1893 
1894     assert(0 <= d && d < PyLong_SHIFT);
1895     for (i=m; i-- > 0;) {
1896         twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
1897         carry = (digit)acc & mask;
1898         z[i] = (digit)(acc >> d);
1899     }
1900     return carry;
1901 }
1902 
1903 /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1904    in pout, and returning the remainder.  pin and pout point at the LSD.
1905    It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1906    _PyLong_Format, but that should be done with great care since ints are
1907    immutable.
1908 
1909    This version of the code can be 20% faster than the pre-2022 version
1910    on todays compilers on architectures like amd64.  It evolved from Mark
1911    Dickinson observing that a 128:64 divide instruction was always being
1912    generated by the compiler despite us working with 30-bit digit values.
1913    See the thread for full context:
1914 
1915      https://mail.python.org/archives/list/python-dev@python.org/thread/ZICIMX5VFCX4IOFH5NUPVHCUJCQ4Q7QM/#NEUNFZU3TQU4CPTYZNF3WCN7DOJBBTK5
1916 
1917    If you ever want to change this code, pay attention to performance using
1918    different compilers, optimization levels, and cpu architectures. Beware of
1919    PGO/FDO builds doing value specialization such as a fast path for //10. :)
1920 
1921    Verify that 17 isn't specialized and this works as a quick test:
1922      python -m timeit -s 'x = 10**1000; r=x//10; assert r == 10**999, r' 'x//17'
1923 */
1924 static digit
inplace_divrem1(digit * pout,digit * pin,Py_ssize_t size,digit n)1925 inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
1926 {
1927     digit remainder = 0;
1928 
1929     assert(n > 0 && n <= PyLong_MASK);
1930     while (--size >= 0) {
1931         twodigits dividend;
1932         dividend = ((twodigits)remainder << PyLong_SHIFT) | pin[size];
1933         digit quotient;
1934         quotient = (digit)(dividend / n);
1935         remainder = dividend % n;
1936         pout[size] = quotient;
1937     }
1938     return remainder;
1939 }
1940 
1941 
1942 /* Divide an integer by a digit, returning both the quotient
1943    (as function result) and the remainder (through *prem).
1944    The sign of a is ignored; n should not be zero. */
1945 
1946 static PyLongObject *
divrem1(PyLongObject * a,digit n,digit * prem)1947 divrem1(PyLongObject *a, digit n, digit *prem)
1948 {
1949     const Py_ssize_t size = _PyLong_DigitCount(a);
1950     PyLongObject *z;
1951 
1952     assert(n > 0 && n <= PyLong_MASK);
1953     z = _PyLong_New(size);
1954     if (z == NULL)
1955         return NULL;
1956     *prem = inplace_divrem1(z->long_value.ob_digit, a->long_value.ob_digit, size, n);
1957     return long_normalize(z);
1958 }
1959 
1960 /* Remainder of long pin, w/ size digits, by non-zero digit n,
1961    returning the remainder. pin points at the LSD. */
1962 
1963 static digit
inplace_rem1(digit * pin,Py_ssize_t size,digit n)1964 inplace_rem1(digit *pin, Py_ssize_t size, digit n)
1965 {
1966     twodigits rem = 0;
1967 
1968     assert(n > 0 && n <= PyLong_MASK);
1969     while (--size >= 0)
1970         rem = ((rem << PyLong_SHIFT) | pin[size]) % n;
1971     return (digit)rem;
1972 }
1973 
1974 /* Get the remainder of an integer divided by a digit, returning
1975    the remainder as the result of the function. The sign of a is
1976    ignored; n should not be zero. */
1977 
1978 static PyLongObject *
rem1(PyLongObject * a,digit n)1979 rem1(PyLongObject *a, digit n)
1980 {
1981     const Py_ssize_t size = _PyLong_DigitCount(a);
1982 
1983     assert(n > 0 && n <= PyLong_MASK);
1984     return (PyLongObject *)PyLong_FromLong(
1985         (long)inplace_rem1(a->long_value.ob_digit, size, n)
1986     );
1987 }
1988 
1989 #ifdef WITH_PYLONG_MODULE
1990 /* asymptotically faster long_to_decimal_string, using _pylong.py */
1991 static int
pylong_int_to_decimal_string(PyObject * aa,PyObject ** p_output,_PyUnicodeWriter * writer,_PyBytesWriter * bytes_writer,char ** bytes_str)1992 pylong_int_to_decimal_string(PyObject *aa,
1993                              PyObject **p_output,
1994                              _PyUnicodeWriter *writer,
1995                              _PyBytesWriter *bytes_writer,
1996                              char **bytes_str)
1997 {
1998     PyObject *s = NULL;
1999     PyObject *mod = PyImport_ImportModule("_pylong");
2000     if (mod == NULL) {
2001         return -1;
2002     }
2003     s = PyObject_CallMethod(mod, "int_to_decimal_string", "O", aa);
2004     if (s == NULL) {
2005         goto error;
2006     }
2007     if (!PyUnicode_Check(s)) {
2008         PyErr_SetString(PyExc_TypeError,
2009                         "_pylong.int_to_decimal_string did not return a str");
2010         goto error;
2011     }
2012     if (writer) {
2013         Py_ssize_t size = PyUnicode_GET_LENGTH(s);
2014         if (_PyUnicodeWriter_Prepare(writer, size, '9') == -1) {
2015             goto error;
2016         }
2017         if (_PyUnicodeWriter_WriteStr(writer, s) < 0) {
2018             goto error;
2019         }
2020         goto success;
2021     }
2022     else if (bytes_writer) {
2023         Py_ssize_t size = PyUnicode_GET_LENGTH(s);
2024         const void *data = PyUnicode_DATA(s);
2025         int kind = PyUnicode_KIND(s);
2026         *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, size);
2027         if (*bytes_str == NULL) {
2028             goto error;
2029         }
2030         char *p = *bytes_str;
2031         for (Py_ssize_t i=0; i < size; i++) {
2032             Py_UCS4 ch = PyUnicode_READ(kind, data, i);
2033             *p++ = (char) ch;
2034         }
2035         (*bytes_str) = p;
2036         goto success;
2037     }
2038     else {
2039         *p_output = Py_NewRef(s);
2040         goto success;
2041     }
2042 
2043 error:
2044         Py_DECREF(mod);
2045         Py_XDECREF(s);
2046         return -1;
2047 
2048 success:
2049         Py_DECREF(mod);
2050         Py_DECREF(s);
2051         return 0;
2052 }
2053 #endif /* WITH_PYLONG_MODULE */
2054 
2055 /* Convert an integer to a base 10 string.  Returns a new non-shared
2056    string.  (Return value is non-shared so that callers can modify the
2057    returned value if necessary.) */
2058 
2059 static int
long_to_decimal_string_internal(PyObject * aa,PyObject ** p_output,_PyUnicodeWriter * writer,_PyBytesWriter * bytes_writer,char ** bytes_str)2060 long_to_decimal_string_internal(PyObject *aa,
2061                                 PyObject **p_output,
2062                                 _PyUnicodeWriter *writer,
2063                                 _PyBytesWriter *bytes_writer,
2064                                 char **bytes_str)
2065 {
2066     PyLongObject *scratch, *a;
2067     PyObject *str = NULL;
2068     Py_ssize_t size, strlen, size_a, i, j;
2069     digit *pout, *pin, rem, tenpow;
2070     int negative;
2071     int d;
2072 
2073     // writer or bytes_writer can be used, but not both at the same time.
2074     assert(writer == NULL || bytes_writer == NULL);
2075 
2076     a = (PyLongObject *)aa;
2077     if (a == NULL || !PyLong_Check(a)) {
2078         PyErr_BadInternalCall();
2079         return -1;
2080     }
2081     size_a = _PyLong_DigitCount(a);
2082     negative = _PyLong_IsNegative(a);
2083 
2084     /* quick and dirty pre-check for overflowing the decimal digit limit,
2085        based on the inequality 10/3 >= log2(10)
2086 
2087        explanation in https://github.com/python/cpython/pull/96537
2088     */
2089     if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD
2090                   / (3 * PyLong_SHIFT) + 2) {
2091         PyInterpreterState *interp = _PyInterpreterState_GET();
2092         int max_str_digits = interp->long_state.max_str_digits;
2093         if ((max_str_digits > 0) &&
2094             (max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10)) {
2095             PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_STR,
2096                          max_str_digits);
2097             return -1;
2098         }
2099     }
2100 
2101 #if WITH_PYLONG_MODULE
2102     if (size_a > 1000) {
2103         /* Switch to _pylong.int_to_decimal_string(). */
2104         return pylong_int_to_decimal_string(aa,
2105                                          p_output,
2106                                          writer,
2107                                          bytes_writer,
2108                                          bytes_str);
2109     }
2110 #endif
2111 
2112     /* quick and dirty upper bound for the number of digits
2113        required to express a in base _PyLong_DECIMAL_BASE:
2114 
2115          #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE))
2116 
2117        But log2(a) < size_a * PyLong_SHIFT, and
2118        log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
2119                                   > 3.3 * _PyLong_DECIMAL_SHIFT
2120 
2121          size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) =
2122              size_a + size_a / d < size_a + size_a / floor(d),
2123        where d = (3.3 * _PyLong_DECIMAL_SHIFT) /
2124                  (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT)
2125     */
2126     d = (33 * _PyLong_DECIMAL_SHIFT) /
2127         (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT);
2128     assert(size_a < PY_SSIZE_T_MAX/2);
2129     size = 1 + size_a + size_a / d;
2130     scratch = _PyLong_New(size);
2131     if (scratch == NULL)
2132         return -1;
2133 
2134     /* convert array of base _PyLong_BASE digits in pin to an array of
2135        base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP,
2136        Volume 2 (3rd edn), section 4.4, Method 1b). */
2137     pin = a->long_value.ob_digit;
2138     pout = scratch->long_value.ob_digit;
2139     size = 0;
2140     for (i = size_a; --i >= 0; ) {
2141         digit hi = pin[i];
2142         for (j = 0; j < size; j++) {
2143             twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi;
2144             hi = (digit)(z / _PyLong_DECIMAL_BASE);
2145             pout[j] = (digit)(z - (twodigits)hi *
2146                               _PyLong_DECIMAL_BASE);
2147         }
2148         while (hi) {
2149             pout[size++] = hi % _PyLong_DECIMAL_BASE;
2150             hi /= _PyLong_DECIMAL_BASE;
2151         }
2152         /* check for keyboard interrupt */
2153         SIGCHECK({
2154                 Py_DECREF(scratch);
2155                 return -1;
2156             });
2157     }
2158     /* pout should have at least one digit, so that the case when a = 0
2159        works correctly */
2160     if (size == 0)
2161         pout[size++] = 0;
2162 
2163     /* calculate exact length of output string, and allocate */
2164     strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT;
2165     tenpow = 10;
2166     rem = pout[size-1];
2167     while (rem >= tenpow) {
2168         tenpow *= 10;
2169         strlen++;
2170     }
2171     if (strlen > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
2172         PyInterpreterState *interp = _PyInterpreterState_GET();
2173         int max_str_digits = interp->long_state.max_str_digits;
2174         Py_ssize_t strlen_nosign = strlen - negative;
2175         if ((max_str_digits > 0) && (strlen_nosign > max_str_digits)) {
2176             Py_DECREF(scratch);
2177             PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_STR,
2178                          max_str_digits);
2179             return -1;
2180         }
2181     }
2182     if (writer) {
2183         if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) {
2184             Py_DECREF(scratch);
2185             return -1;
2186         }
2187     }
2188     else if (bytes_writer) {
2189         *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
2190         if (*bytes_str == NULL) {
2191             Py_DECREF(scratch);
2192             return -1;
2193         }
2194     }
2195     else {
2196         str = PyUnicode_New(strlen, '9');
2197         if (str == NULL) {
2198             Py_DECREF(scratch);
2199             return -1;
2200         }
2201     }
2202 
2203 #define WRITE_DIGITS(p)                                               \
2204     do {                                                              \
2205         /* pout[0] through pout[size-2] contribute exactly            \
2206            _PyLong_DECIMAL_SHIFT digits each */                       \
2207         for (i=0; i < size - 1; i++) {                                \
2208             rem = pout[i];                                            \
2209             for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) {             \
2210                 *--p = '0' + rem % 10;                                \
2211                 rem /= 10;                                            \
2212             }                                                         \
2213         }                                                             \
2214         /* pout[size-1]: always produce at least one decimal digit */ \
2215         rem = pout[i];                                                \
2216         do {                                                          \
2217             *--p = '0' + rem % 10;                                    \
2218             rem /= 10;                                                \
2219         } while (rem != 0);                                           \
2220                                                                       \
2221         /* and sign */                                                \
2222         if (negative)                                                 \
2223             *--p = '-';                                               \
2224     } while (0)
2225 
2226 #define WRITE_UNICODE_DIGITS(TYPE)                                    \
2227     do {                                                              \
2228         if (writer)                                                   \
2229             p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \
2230         else                                                          \
2231             p = (TYPE*)PyUnicode_DATA(str) + strlen;                  \
2232                                                                       \
2233         WRITE_DIGITS(p);                                              \
2234                                                                       \
2235         /* check we've counted correctly */                           \
2236         if (writer)                                                   \
2237             assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
2238         else                                                          \
2239             assert(p == (TYPE*)PyUnicode_DATA(str));                  \
2240     } while (0)
2241 
2242     /* fill the string right-to-left */
2243     if (bytes_writer) {
2244         char *p = *bytes_str + strlen;
2245         WRITE_DIGITS(p);
2246         assert(p == *bytes_str);
2247     }
2248     else {
2249         int kind = writer ? writer->kind : PyUnicode_KIND(str);
2250         if (kind == PyUnicode_1BYTE_KIND) {
2251             Py_UCS1 *p;
2252             WRITE_UNICODE_DIGITS(Py_UCS1);
2253         }
2254         else if (kind == PyUnicode_2BYTE_KIND) {
2255             Py_UCS2 *p;
2256             WRITE_UNICODE_DIGITS(Py_UCS2);
2257         }
2258         else {
2259             assert (kind == PyUnicode_4BYTE_KIND);
2260             Py_UCS4 *p;
2261             WRITE_UNICODE_DIGITS(Py_UCS4);
2262         }
2263     }
2264 
2265 #undef WRITE_DIGITS
2266 #undef WRITE_UNICODE_DIGITS
2267 
2268     _Py_DECREF_INT(scratch);
2269     if (writer) {
2270         writer->pos += strlen;
2271     }
2272     else if (bytes_writer) {
2273         (*bytes_str) += strlen;
2274     }
2275     else {
2276         assert(_PyUnicode_CheckConsistency(str, 1));
2277         *p_output = (PyObject *)str;
2278     }
2279     return 0;
2280 }
2281 
2282 static PyObject *
long_to_decimal_string(PyObject * aa)2283 long_to_decimal_string(PyObject *aa)
2284 {
2285     PyObject *v;
2286     if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1)
2287         return NULL;
2288     return v;
2289 }
2290 
2291 /* Convert an int object to a string, using a given conversion base,
2292    which should be one of 2, 8 or 16.  Return a string object.
2293    If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'
2294    if alternate is nonzero. */
2295 
2296 static int
long_format_binary(PyObject * aa,int base,int alternate,PyObject ** p_output,_PyUnicodeWriter * writer,_PyBytesWriter * bytes_writer,char ** bytes_str)2297 long_format_binary(PyObject *aa, int base, int alternate,
2298                    PyObject **p_output, _PyUnicodeWriter *writer,
2299                    _PyBytesWriter *bytes_writer, char **bytes_str)
2300 {
2301     PyLongObject *a = (PyLongObject *)aa;
2302     PyObject *v = NULL;
2303     Py_ssize_t sz;
2304     Py_ssize_t size_a;
2305     int negative;
2306     int bits;
2307 
2308     assert(base == 2 || base == 8 || base == 16);
2309     // writer or bytes_writer can be used, but not both at the same time.
2310     assert(writer == NULL || bytes_writer == NULL);
2311     if (a == NULL || !PyLong_Check(a)) {
2312         PyErr_BadInternalCall();
2313         return -1;
2314     }
2315     size_a = _PyLong_DigitCount(a);
2316     negative = _PyLong_IsNegative(a);
2317 
2318     /* Compute a rough upper bound for the length of the string */
2319     switch (base) {
2320     case 16:
2321         bits = 4;
2322         break;
2323     case 8:
2324         bits = 3;
2325         break;
2326     case 2:
2327         bits = 1;
2328         break;
2329     default:
2330         Py_UNREACHABLE();
2331     }
2332 
2333     /* Compute exact length 'sz' of output string. */
2334     if (size_a == 0) {
2335         sz = 1;
2336     }
2337     else {
2338         Py_ssize_t size_a_in_bits;
2339         /* Ensure overflow doesn't occur during computation of sz. */
2340         if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) {
2341             PyErr_SetString(PyExc_OverflowError,
2342                             "int too large to format");
2343             return -1;
2344         }
2345         size_a_in_bits = (size_a - 1) * PyLong_SHIFT +
2346                          bit_length_digit(a->long_value.ob_digit[size_a - 1]);
2347         /* Allow 1 character for a '-' sign. */
2348         sz = negative + (size_a_in_bits + (bits - 1)) / bits;
2349     }
2350     if (alternate) {
2351         /* 2 characters for prefix  */
2352         sz += 2;
2353     }
2354 
2355     if (writer) {
2356         if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
2357             return -1;
2358     }
2359     else if (bytes_writer) {
2360         *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
2361         if (*bytes_str == NULL)
2362             return -1;
2363     }
2364     else {
2365         v = PyUnicode_New(sz, 'x');
2366         if (v == NULL)
2367             return -1;
2368     }
2369 
2370 #define WRITE_DIGITS(p)                                                 \
2371     do {                                                                \
2372         if (size_a == 0) {                                              \
2373             *--p = '0';                                                 \
2374         }                                                               \
2375         else {                                                          \
2376             /* JRH: special case for power-of-2 bases */                \
2377             twodigits accum = 0;                                        \
2378             int accumbits = 0;   /* # of bits in accum */               \
2379             Py_ssize_t i;                                               \
2380             for (i = 0; i < size_a; ++i) {                              \
2381                 accum |= (twodigits)a->long_value.ob_digit[i] << accumbits;        \
2382                 accumbits += PyLong_SHIFT;                              \
2383                 assert(accumbits >= bits);                              \
2384                 do {                                                    \
2385                     char cdigit;                                        \
2386                     cdigit = (char)(accum & (base - 1));                \
2387                     cdigit += (cdigit < 10) ? '0' : 'a'-10;             \
2388                     *--p = cdigit;                                      \
2389                     accumbits -= bits;                                  \
2390                     accum >>= bits;                                     \
2391                 } while (i < size_a-1 ? accumbits >= bits : accum > 0); \
2392             }                                                           \
2393         }                                                               \
2394                                                                         \
2395         if (alternate) {                                                \
2396             if (base == 16)                                             \
2397                 *--p = 'x';                                             \
2398             else if (base == 8)                                         \
2399                 *--p = 'o';                                             \
2400             else /* (base == 2) */                                      \
2401                 *--p = 'b';                                             \
2402             *--p = '0';                                                 \
2403         }                                                               \
2404         if (negative)                                                   \
2405             *--p = '-';                                                 \
2406     } while (0)
2407 
2408 #define WRITE_UNICODE_DIGITS(TYPE)                                      \
2409     do {                                                                \
2410         if (writer)                                                     \
2411             p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \
2412         else                                                            \
2413             p = (TYPE*)PyUnicode_DATA(v) + sz;                          \
2414                                                                         \
2415         WRITE_DIGITS(p);                                                \
2416                                                                         \
2417         if (writer)                                                     \
2418             assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
2419         else                                                            \
2420             assert(p == (TYPE*)PyUnicode_DATA(v));                      \
2421     } while (0)
2422 
2423     if (bytes_writer) {
2424         char *p = *bytes_str + sz;
2425         WRITE_DIGITS(p);
2426         assert(p == *bytes_str);
2427     }
2428     else {
2429         int kind = writer ? writer->kind : PyUnicode_KIND(v);
2430         if (kind == PyUnicode_1BYTE_KIND) {
2431             Py_UCS1 *p;
2432             WRITE_UNICODE_DIGITS(Py_UCS1);
2433         }
2434         else if (kind == PyUnicode_2BYTE_KIND) {
2435             Py_UCS2 *p;
2436             WRITE_UNICODE_DIGITS(Py_UCS2);
2437         }
2438         else {
2439             assert (kind == PyUnicode_4BYTE_KIND);
2440             Py_UCS4 *p;
2441             WRITE_UNICODE_DIGITS(Py_UCS4);
2442         }
2443     }
2444 
2445 #undef WRITE_DIGITS
2446 #undef WRITE_UNICODE_DIGITS
2447 
2448     if (writer) {
2449         writer->pos += sz;
2450     }
2451     else if (bytes_writer) {
2452         (*bytes_str) += sz;
2453     }
2454     else {
2455         assert(_PyUnicode_CheckConsistency(v, 1));
2456         *p_output = v;
2457     }
2458     return 0;
2459 }
2460 
2461 PyObject *
_PyLong_Format(PyObject * obj,int base)2462 _PyLong_Format(PyObject *obj, int base)
2463 {
2464     PyObject *str;
2465     int err;
2466     if (base == 10)
2467         err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL);
2468     else
2469         err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL);
2470     if (err == -1)
2471         return NULL;
2472     return str;
2473 }
2474 
2475 int
_PyLong_FormatWriter(_PyUnicodeWriter * writer,PyObject * obj,int base,int alternate)2476 _PyLong_FormatWriter(_PyUnicodeWriter *writer,
2477                      PyObject *obj,
2478                      int base, int alternate)
2479 {
2480     if (base == 10)
2481         return long_to_decimal_string_internal(obj, NULL, writer,
2482                                                NULL, NULL);
2483     else
2484         return long_format_binary(obj, base, alternate, NULL, writer,
2485                                   NULL, NULL);
2486 }
2487 
2488 char*
_PyLong_FormatBytesWriter(_PyBytesWriter * writer,char * str,PyObject * obj,int base,int alternate)2489 _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str,
2490                           PyObject *obj,
2491                           int base, int alternate)
2492 {
2493     char *str2;
2494     int res;
2495     str2 = str;
2496     if (base == 10)
2497         res = long_to_decimal_string_internal(obj, NULL, NULL,
2498                                               writer, &str2);
2499     else
2500         res = long_format_binary(obj, base, alternate, NULL, NULL,
2501                                  writer, &str2);
2502     if (res < 0)
2503         return NULL;
2504     assert(str2 != NULL);
2505     return str2;
2506 }
2507 
2508 /* Table of digit values for 8-bit string -> integer conversion.
2509  * '0' maps to 0, ..., '9' maps to 9.
2510  * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
2511  * All other indices map to 37.
2512  * Note that when converting a base B string, a char c is a legitimate
2513  * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
2514  */
2515 unsigned char _PyLong_DigitValue[256] = {
2516     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2517     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2518     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2519     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  37, 37, 37, 37, 37, 37,
2520     37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2521     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
2522     37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2523     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
2524     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2525     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2526     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2527     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2528     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2529     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2530     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2531     37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2532 };
2533 
2534 /* `start` and `end` point to the start and end of a string of base `base`
2535  * digits.  base is a power of 2 (2, 4, 8, 16, or 32). An unnormalized int is
2536  * returned in *res. The string should be already validated by the caller and
2537  * consists only of valid digit characters and underscores. `digits` gives the
2538  * number of digit characters.
2539  *
2540  * The point to this routine is that it takes time linear in the
2541  * number of string characters.
2542  *
2543  * Return values:
2544  *   -1 on syntax error (exception needs to be set, *res is untouched)
2545  *   0 else (exception may be set, in that case *res is set to NULL)
2546  */
2547 static int
long_from_binary_base(const char * start,const char * end,Py_ssize_t digits,int base,PyLongObject ** res)2548 long_from_binary_base(const char *start, const char *end, Py_ssize_t digits, int base, PyLongObject **res)
2549 {
2550     const char *p;
2551     int bits_per_char;
2552     Py_ssize_t n;
2553     PyLongObject *z;
2554     twodigits accum;
2555     int bits_in_accum;
2556     digit *pdigit;
2557 
2558     assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
2559     n = base;
2560     for (bits_per_char = -1; n; ++bits_per_char) {
2561         n >>= 1;
2562     }
2563 
2564     /* n <- the number of Python digits needed,
2565             = ceiling((digits * bits_per_char) / PyLong_SHIFT). */
2566     if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) {
2567         PyErr_SetString(PyExc_ValueError,
2568                         "int string too large to convert");
2569         *res = NULL;
2570         return 0;
2571     }
2572     n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT;
2573     z = _PyLong_New(n);
2574     if (z == NULL) {
2575         *res = NULL;
2576         return 0;
2577     }
2578     /* Read string from right, and fill in int from left; i.e.,
2579      * from least to most significant in both.
2580      */
2581     accum = 0;
2582     bits_in_accum = 0;
2583     pdigit = z->long_value.ob_digit;
2584     p = end;
2585     while (--p >= start) {
2586         int k;
2587         if (*p == '_') {
2588             continue;
2589         }
2590         k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)];
2591         assert(k >= 0 && k < base);
2592         accum |= (twodigits)k << bits_in_accum;
2593         bits_in_accum += bits_per_char;
2594         if (bits_in_accum >= PyLong_SHIFT) {
2595             *pdigit++ = (digit)(accum & PyLong_MASK);
2596             assert(pdigit - z->long_value.ob_digit <= n);
2597             accum >>= PyLong_SHIFT;
2598             bits_in_accum -= PyLong_SHIFT;
2599             assert(bits_in_accum < PyLong_SHIFT);
2600         }
2601     }
2602     if (bits_in_accum) {
2603         assert(bits_in_accum <= PyLong_SHIFT);
2604         *pdigit++ = (digit)accum;
2605         assert(pdigit - z->long_value.ob_digit <= n);
2606     }
2607     while (pdigit - z->long_value.ob_digit < n)
2608         *pdigit++ = 0;
2609     *res = z;
2610     return 0;
2611 }
2612 
2613 static PyObject *long_neg(PyLongObject *v);
2614 
2615 #ifdef WITH_PYLONG_MODULE
2616 /* asymptotically faster str-to-long conversion for base 10, using _pylong.py */
2617 static int
pylong_int_from_string(const char * start,const char * end,PyLongObject ** res)2618 pylong_int_from_string(const char *start, const char *end, PyLongObject **res)
2619 {
2620     PyObject *mod = PyImport_ImportModule("_pylong");
2621     if (mod == NULL) {
2622         goto error;
2623     }
2624     PyObject *s = PyUnicode_FromStringAndSize(start, end-start);
2625     if (s == NULL) {
2626         Py_DECREF(mod);
2627         goto error;
2628     }
2629     PyObject *result = PyObject_CallMethod(mod, "int_from_string", "O", s);
2630     Py_DECREF(s);
2631     Py_DECREF(mod);
2632     if (result == NULL) {
2633         goto error;
2634     }
2635     if (!PyLong_Check(result)) {
2636         Py_DECREF(result);
2637         PyErr_SetString(PyExc_TypeError,
2638                         "_pylong.int_from_string did not return an int");
2639         goto error;
2640     }
2641     *res = (PyLongObject *)result;
2642     return 0;
2643 error:
2644     *res = NULL;
2645     return 0;  // See the long_from_string_base() API comment.
2646 }
2647 #endif /* WITH_PYLONG_MODULE */
2648 
2649 /***
2650 long_from_non_binary_base: parameters and return values are the same as
2651 long_from_binary_base.
2652 
2653 Binary bases can be converted in time linear in the number of digits, because
2654 Python's representation base is binary.  Other bases (including decimal!) use
2655 the simple quadratic-time algorithm below, complicated by some speed tricks.
2656 
2657 First some math:  the largest integer that can be expressed in N base-B digits
2658 is B**N-1.  Consequently, if we have an N-digit input in base B, the worst-
2659 case number of Python digits needed to hold it is the smallest integer n s.t.
2660 
2661     BASE**n-1 >= B**N-1  [or, adding 1 to both sides]
2662     BASE**n >= B**N      [taking logs to base BASE]
2663     n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
2664 
2665 The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
2666 this quickly.  A Python int with that much space is reserved near the start,
2667 and the result is computed into it.
2668 
2669 The input string is actually treated as being in base base**i (i.e., i digits
2670 are processed at a time), where two more static arrays hold:
2671 
2672     convwidth_base[base] = the largest integer i such that base**i <= BASE
2673     convmultmax_base[base] = base ** convwidth_base[base]
2674 
2675 The first of these is the largest i such that i consecutive input digits
2676 must fit in a single Python digit.  The second is effectively the input
2677 base we're really using.
2678 
2679 Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
2680 convmultmax_base[base], the result is "simply"
2681 
2682    (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
2683 
2684 where B = convmultmax_base[base].
2685 
2686 Error analysis:  as above, the number of Python digits `n` needed is worst-
2687 case
2688 
2689     n >= N * log(B)/log(BASE)
2690 
2691 where `N` is the number of input digits in base `B`.  This is computed via
2692 
2693     size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
2694 
2695 below.  Two numeric concerns are how much space this can waste, and whether
2696 the computed result can be too small.  To be concrete, assume BASE = 2**15,
2697 which is the default (and it's unlikely anyone changes that).
2698 
2699 Waste isn't a problem:  provided the first input digit isn't 0, the difference
2700 between the worst-case input with N digits and the smallest input with N
2701 digits is about a factor of B, but B is small compared to BASE so at most
2702 one allocated Python digit can remain unused on that count.  If
2703 N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
2704 and adding 1 returns a result 1 larger than necessary.  However, that can't
2705 happen:  whenever B is a power of 2, long_from_binary_base() is called
2706 instead, and it's impossible for B**i to be an integer power of 2**15 when
2707 B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
2708 an exact integer when B is not a power of 2, since B**i has a prime factor
2709 other than 2 in that case, but (2**15)**j's only prime factor is 2).
2710 
2711 The computed result can be too small if the true value of N*log(B)/log(BASE)
2712 is a little bit larger than an exact integer, but due to roundoff errors (in
2713 computing log(B), log(BASE), their quotient, and/or multiplying that by N)
2714 yields a numeric result a little less than that integer.  Unfortunately, "how
2715 close can a transcendental function get to an integer over some range?"
2716 questions are generally theoretically intractable.  Computer analysis via
2717 continued fractions is practical:  expand log(B)/log(BASE) via continued
2718 fractions, giving a sequence i/j of "the best" rational approximations.  Then
2719 j*log(B)/log(BASE) is approximately equal to (the integer) i.  This shows that
2720 we can get very close to being in trouble, but very rarely.  For example,
2721 76573 is a denominator in one of the continued-fraction approximations to
2722 log(10)/log(2**15), and indeed:
2723 
2724     >>> log(10)/log(2**15)*76573
2725     16958.000000654003
2726 
2727 is very close to an integer.  If we were working with IEEE single-precision,
2728 rounding errors could kill us.  Finding worst cases in IEEE double-precision
2729 requires better-than-double-precision log() functions, and Tim didn't bother.
2730 Instead the code checks to see whether the allocated space is enough as each
2731 new Python digit is added, and copies the whole thing to a larger int if not.
2732 This should happen extremely rarely, and in fact I don't have a test case
2733 that triggers it(!).  Instead the code was tested by artificially allocating
2734 just 1 digit at the start, so that the copying code was exercised for every
2735 digit beyond the first.
2736 ***/
2737 static int
long_from_non_binary_base(const char * start,const char * end,Py_ssize_t digits,int base,PyLongObject ** res)2738 long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits, int base, PyLongObject **res)
2739 {
2740     twodigits c;           /* current input character */
2741     Py_ssize_t size_z;
2742     int i;
2743     int convwidth;
2744     twodigits convmultmax, convmult;
2745     digit *pz, *pzstop;
2746     PyLongObject *z;
2747     const char *p;
2748 
2749     static double log_base_BASE[37] = {0.0e0,};
2750     static int convwidth_base[37] = {0,};
2751     static twodigits convmultmax_base[37] = {0,};
2752 
2753     if (log_base_BASE[base] == 0.0) {
2754         twodigits convmax = base;
2755         int i = 1;
2756 
2757         log_base_BASE[base] = (log((double)base) /
2758                                log((double)PyLong_BASE));
2759         for (;;) {
2760             twodigits next = convmax * base;
2761             if (next > PyLong_BASE) {
2762                 break;
2763             }
2764             convmax = next;
2765             ++i;
2766         }
2767         convmultmax_base[base] = convmax;
2768         assert(i > 0);
2769         convwidth_base[base] = i;
2770     }
2771 
2772     /* Create an int object that can contain the largest possible
2773      * integer with this base and length.  Note that there's no
2774      * need to initialize z->long_value.ob_digit -- no slot is read up before
2775      * being stored into.
2776      */
2777     double fsize_z = (double)digits * log_base_BASE[base] + 1.0;
2778     if (fsize_z > (double)MAX_LONG_DIGITS) {
2779         /* The same exception as in _PyLong_New(). */
2780         PyErr_SetString(PyExc_OverflowError,
2781                         "too many digits in integer");
2782         *res = NULL;
2783         return 0;
2784     }
2785     size_z = (Py_ssize_t)fsize_z;
2786     /* Uncomment next line to test exceedingly rare copy code */
2787     /* size_z = 1; */
2788     assert(size_z > 0);
2789     z = _PyLong_New(size_z);
2790     if (z == NULL) {
2791         *res = NULL;
2792         return 0;
2793     }
2794     _PyLong_SetSignAndDigitCount(z, 0, 0);
2795 
2796     /* `convwidth` consecutive input digits are treated as a single
2797      * digit in base `convmultmax`.
2798      */
2799     convwidth = convwidth_base[base];
2800     convmultmax = convmultmax_base[base];
2801 
2802     /* Work ;-) */
2803     p = start;
2804     while (p < end) {
2805         if (*p == '_') {
2806             p++;
2807             continue;
2808         }
2809         /* grab up to convwidth digits from the input string */
2810         c = (digit)_PyLong_DigitValue[Py_CHARMASK(*p++)];
2811         for (i = 1; i < convwidth && p != end; ++p) {
2812             if (*p == '_') {
2813                 continue;
2814             }
2815             i++;
2816             c = (twodigits)(c *  base +
2817                             (int)_PyLong_DigitValue[Py_CHARMASK(*p)]);
2818             assert(c < PyLong_BASE);
2819         }
2820 
2821         convmult = convmultmax;
2822         /* Calculate the shift only if we couldn't get
2823          * convwidth digits.
2824          */
2825         if (i != convwidth) {
2826             convmult = base;
2827             for ( ; i > 1; --i) {
2828                 convmult *= base;
2829             }
2830         }
2831 
2832         /* Multiply z by convmult, and add c. */
2833         pz = z->long_value.ob_digit;
2834         pzstop = pz + _PyLong_DigitCount(z);
2835         for (; pz < pzstop; ++pz) {
2836             c += (twodigits)*pz * convmult;
2837             *pz = (digit)(c & PyLong_MASK);
2838             c >>= PyLong_SHIFT;
2839         }
2840         /* carry off the current end? */
2841         if (c) {
2842             assert(c < PyLong_BASE);
2843             if (_PyLong_DigitCount(z) < size_z) {
2844                 *pz = (digit)c;
2845                 assert(!_PyLong_IsNegative(z));
2846                 _PyLong_SetSignAndDigitCount(z, 1, _PyLong_DigitCount(z) + 1);
2847             }
2848             else {
2849                 PyLongObject *tmp;
2850                 /* Extremely rare.  Get more space. */
2851                 assert(_PyLong_DigitCount(z) == size_z);
2852                 tmp = _PyLong_New(size_z + 1);
2853                 if (tmp == NULL) {
2854                     Py_DECREF(z);
2855                     *res = NULL;
2856                     return 0;
2857                 }
2858                 memcpy(tmp->long_value.ob_digit,
2859                        z->long_value.ob_digit,
2860                        sizeof(digit) * size_z);
2861                 Py_SETREF(z, tmp);
2862                 z->long_value.ob_digit[size_z] = (digit)c;
2863                 ++size_z;
2864             }
2865         }
2866     }
2867     *res = z;
2868     return 0;
2869 }
2870 
2871 /* *str points to the first digit in a string of base `base` digits. base is an
2872  * integer from 2 to 36 inclusive. Here we don't need to worry about prefixes
2873  * like 0x or leading +- signs. The string should be null terminated consisting
2874  * of ASCII digits and separating underscores possibly with trailing whitespace
2875  * but we have to validate all of those points here.
2876  *
2877  * If base is a power of 2 then the complexity is linear in the number of
2878  * characters in the string. Otherwise a quadratic algorithm is used for
2879  * non-binary bases.
2880  *
2881  * Return values:
2882  *
2883  *   - Returns -1 on syntax error (exception needs to be set, *res is untouched)
2884  *   - Returns 0 and sets *res to NULL for MemoryError, OverflowError, or
2885  *     _pylong.int_from_string() errors.
2886  *   - Returns 0 and sets *res to an unsigned, unnormalized PyLong (success!).
2887  *
2888  * Afterwards *str is set to point to the first non-digit (which may be *str!).
2889  */
2890 static int
long_from_string_base(const char ** str,int base,PyLongObject ** res)2891 long_from_string_base(const char **str, int base, PyLongObject **res)
2892 {
2893     const char *start, *end, *p;
2894     char prev = 0;
2895     Py_ssize_t digits = 0;
2896     int is_binary_base = (base & (base - 1)) == 0;
2897 
2898     /* Here we do four things:
2899      *
2900      * - Find the `end` of the string.
2901      * - Validate the string.
2902      * - Count the number of `digits` (rather than underscores)
2903      * - Point *str to the end-of-string or first invalid character.
2904      */
2905     start = p = *str;
2906     /* Leading underscore not allowed. */
2907     if (*start == '_') {
2908         return -1;
2909     }
2910     /* Verify all characters are digits and underscores. */
2911     while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') {
2912         if (*p == '_') {
2913             /* Double underscore not allowed. */
2914             if (prev == '_') {
2915                 *str = p - 1;
2916                 return -1;
2917             }
2918         } else {
2919             ++digits;
2920         }
2921         prev = *p;
2922         ++p;
2923     }
2924     /* Trailing underscore not allowed. */
2925     if (prev == '_') {
2926         *str = p - 1;
2927         return -1;
2928     }
2929     *str = end = p;
2930     /* Reject empty strings */
2931     if (start == end) {
2932         return -1;
2933     }
2934     /* Allow only trailing whitespace after `end` */
2935     while (*p && Py_ISSPACE(*p)) {
2936         p++;
2937     }
2938     *str = p;
2939     if (*p != '\0') {
2940         return -1;
2941     }
2942 
2943     /*
2944      * Pass a validated string consisting of only valid digits and underscores
2945      * to long_from_xxx_base.
2946      */
2947     if (is_binary_base) {
2948         /* Use the linear algorithm for binary bases. */
2949         return long_from_binary_base(start, end, digits, base, res);
2950     }
2951     else {
2952         /* Limit the size to avoid excessive computation attacks exploiting the
2953          * quadratic algorithm. */
2954         if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
2955             PyInterpreterState *interp = _PyInterpreterState_GET();
2956             int max_str_digits = interp->long_state.max_str_digits;
2957             if ((max_str_digits > 0) && (digits > max_str_digits)) {
2958                 PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT,
2959                              max_str_digits, digits);
2960                 *res = NULL;
2961                 return 0;
2962             }
2963         }
2964 #if WITH_PYLONG_MODULE
2965         if (digits > 6000 && base == 10) {
2966             /* Switch to _pylong.int_from_string() */
2967             return pylong_int_from_string(start, end, res);
2968         }
2969 #endif
2970         /* Use the quadratic algorithm for non binary bases. */
2971         return long_from_non_binary_base(start, end, digits, base, res);
2972     }
2973 }
2974 
2975 /* Parses an int from a bytestring. Leading and trailing whitespace will be
2976  * ignored.
2977  *
2978  * If successful, a PyLong object will be returned and 'pend' will be pointing
2979  * to the first unused byte unless it's NULL.
2980  *
2981  * If unsuccessful, NULL will be returned.
2982  */
2983 PyObject *
PyLong_FromString(const char * str,char ** pend,int base)2984 PyLong_FromString(const char *str, char **pend, int base)
2985 {
2986     int sign = 1, error_if_nonzero = 0;
2987     const char *orig_str = str;
2988     PyLongObject *z = NULL;
2989     PyObject *strobj;
2990     Py_ssize_t slen;
2991 
2992     if ((base != 0 && base < 2) || base > 36) {
2993         PyErr_SetString(PyExc_ValueError,
2994                         "int() arg 2 must be >= 2 and <= 36");
2995         return NULL;
2996     }
2997     while (*str != '\0' && Py_ISSPACE(*str)) {
2998         ++str;
2999     }
3000     if (*str == '+') {
3001         ++str;
3002     }
3003     else if (*str == '-') {
3004         ++str;
3005         sign = -1;
3006     }
3007     if (base == 0) {
3008         if (str[0] != '0') {
3009             base = 10;
3010         }
3011         else if (str[1] == 'x' || str[1] == 'X') {
3012             base = 16;
3013         }
3014         else if (str[1] == 'o' || str[1] == 'O') {
3015             base = 8;
3016         }
3017         else if (str[1] == 'b' || str[1] == 'B') {
3018             base = 2;
3019         }
3020         else {
3021             /* "old" (C-style) octal literal, now invalid.
3022                it might still be zero though */
3023             error_if_nonzero = 1;
3024             base = 10;
3025         }
3026     }
3027     if (str[0] == '0' &&
3028         ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
3029          (base == 8  && (str[1] == 'o' || str[1] == 'O')) ||
3030          (base == 2  && (str[1] == 'b' || str[1] == 'B')))) {
3031         str += 2;
3032         /* One underscore allowed here. */
3033         if (*str == '_') {
3034             ++str;
3035         }
3036     }
3037 
3038     /* long_from_string_base is the main workhorse here. */
3039     int ret = long_from_string_base(&str, base, &z);
3040     if (ret == -1) {
3041         /* Syntax error. */
3042         goto onError;
3043     }
3044     if (z == NULL) {
3045         /* Error. exception already set. */
3046         return NULL;
3047     }
3048 
3049     if (error_if_nonzero) {
3050         /* reset the base to 0, else the exception message
3051            doesn't make too much sense */
3052         base = 0;
3053         if (!_PyLong_IsZero(z)) {
3054             goto onError;
3055         }
3056         /* there might still be other problems, therefore base
3057            remains zero here for the same reason */
3058     }
3059 
3060     /* Set sign and normalize */
3061     if (sign < 0) {
3062         _PyLong_FlipSign(z);
3063     }
3064     long_normalize(z);
3065     z = maybe_small_long(z);
3066 
3067     if (pend != NULL) {
3068         *pend = (char *)str;
3069     }
3070     return (PyObject *) z;
3071 
3072   onError:
3073     if (pend != NULL) {
3074         *pend = (char *)str;
3075     }
3076     Py_XDECREF(z);
3077     slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
3078     strobj = PyUnicode_FromStringAndSize(orig_str, slen);
3079     if (strobj == NULL) {
3080         return NULL;
3081     }
3082     PyErr_Format(PyExc_ValueError,
3083                  "invalid literal for int() with base %d: %.200R",
3084                  base, strobj);
3085     Py_DECREF(strobj);
3086     return NULL;
3087 }
3088 
3089 /* Since PyLong_FromString doesn't have a length parameter,
3090  * check here for possible NULs in the string.
3091  *
3092  * Reports an invalid literal as a bytes object.
3093  */
3094 PyObject *
_PyLong_FromBytes(const char * s,Py_ssize_t len,int base)3095 _PyLong_FromBytes(const char *s, Py_ssize_t len, int base)
3096 {
3097     PyObject *result, *strobj;
3098     char *end = NULL;
3099 
3100     result = PyLong_FromString(s, &end, base);
3101     if (end == NULL || (result != NULL && end == s + len))
3102         return result;
3103     Py_XDECREF(result);
3104     strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200));
3105     if (strobj != NULL) {
3106         PyErr_Format(PyExc_ValueError,
3107                      "invalid literal for int() with base %d: %.200R",
3108                      base, strobj);
3109         Py_DECREF(strobj);
3110     }
3111     return NULL;
3112 }
3113 
3114 PyObject *
PyLong_FromUnicodeObject(PyObject * u,int base)3115 PyLong_FromUnicodeObject(PyObject *u, int base)
3116 {
3117     PyObject *result, *asciidig;
3118     const char *buffer;
3119     char *end = NULL;
3120     Py_ssize_t buflen;
3121 
3122     asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
3123     if (asciidig == NULL)
3124         return NULL;
3125     assert(PyUnicode_IS_ASCII(asciidig));
3126     /* Simply get a pointer to existing ASCII characters. */
3127     buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen);
3128     assert(buffer != NULL);
3129 
3130     result = PyLong_FromString(buffer, &end, base);
3131     if (end == NULL || (result != NULL && end == buffer + buflen)) {
3132         Py_DECREF(asciidig);
3133         return result;
3134     }
3135     Py_DECREF(asciidig);
3136     Py_XDECREF(result);
3137     PyErr_Format(PyExc_ValueError,
3138                  "invalid literal for int() with base %d: %.200R",
3139                  base, u);
3140     return NULL;
3141 }
3142 
3143 /* forward */
3144 static PyLongObject *x_divrem
3145     (PyLongObject *, PyLongObject *, PyLongObject **);
3146 static PyObject *long_long(PyObject *v);
3147 
3148 /* Int division with remainder, top-level routine */
3149 
3150 static int
long_divrem(PyLongObject * a,PyLongObject * b,PyLongObject ** pdiv,PyLongObject ** prem)3151 long_divrem(PyLongObject *a, PyLongObject *b,
3152             PyLongObject **pdiv, PyLongObject **prem)
3153 {
3154     Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3155     PyLongObject *z;
3156 
3157     if (size_b == 0) {
3158         PyErr_SetString(PyExc_ZeroDivisionError,
3159                         "integer division or modulo by zero");
3160         return -1;
3161     }
3162     if (size_a < size_b ||
3163         (size_a == size_b &&
3164          a->long_value.ob_digit[size_a-1] < b->long_value.ob_digit[size_b-1])) {
3165         /* |a| < |b|. */
3166         *prem = (PyLongObject *)long_long((PyObject *)a);
3167         if (*prem == NULL) {
3168             return -1;
3169         }
3170         *pdiv = (PyLongObject*)_PyLong_GetZero();
3171         return 0;
3172     }
3173     if (size_b == 1) {
3174         digit rem = 0;
3175         z = divrem1(a, b->long_value.ob_digit[0], &rem);
3176         if (z == NULL)
3177             return -1;
3178         *prem = (PyLongObject *) PyLong_FromLong((long)rem);
3179         if (*prem == NULL) {
3180             Py_DECREF(z);
3181             return -1;
3182         }
3183     }
3184     else {
3185         z = x_divrem(a, b, prem);
3186         *prem = maybe_small_long(*prem);
3187         if (z == NULL)
3188             return -1;
3189     }
3190     /* Set the signs.
3191        The quotient z has the sign of a*b;
3192        the remainder r has the sign of a,
3193        so a = b*z + r. */
3194     if ((_PyLong_IsNegative(a)) != (_PyLong_IsNegative(b))) {
3195         _PyLong_Negate(&z);
3196         if (z == NULL) {
3197             Py_CLEAR(*prem);
3198             return -1;
3199         }
3200     }
3201     if (_PyLong_IsNegative(a) && !_PyLong_IsZero(*prem)) {
3202         _PyLong_Negate(prem);
3203         if (*prem == NULL) {
3204             Py_DECREF(z);
3205             Py_CLEAR(*prem);
3206             return -1;
3207         }
3208     }
3209     *pdiv = maybe_small_long(z);
3210     return 0;
3211 }
3212 
3213 /* Int remainder, top-level routine */
3214 
3215 static int
long_rem(PyLongObject * a,PyLongObject * b,PyLongObject ** prem)3216 long_rem(PyLongObject *a, PyLongObject *b, PyLongObject **prem)
3217 {
3218     Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3219 
3220     if (size_b == 0) {
3221         PyErr_SetString(PyExc_ZeroDivisionError,
3222                         "integer modulo by zero");
3223         return -1;
3224     }
3225     if (size_a < size_b ||
3226         (size_a == size_b &&
3227          a->long_value.ob_digit[size_a-1] < b->long_value.ob_digit[size_b-1])) {
3228         /* |a| < |b|. */
3229         *prem = (PyLongObject *)long_long((PyObject *)a);
3230         return -(*prem == NULL);
3231     }
3232     if (size_b == 1) {
3233         *prem = rem1(a, b->long_value.ob_digit[0]);
3234         if (*prem == NULL)
3235             return -1;
3236     }
3237     else {
3238         /* Slow path using divrem. */
3239         Py_XDECREF(x_divrem(a, b, prem));
3240         *prem = maybe_small_long(*prem);
3241         if (*prem == NULL)
3242             return -1;
3243     }
3244     /* Set the sign. */
3245     if (_PyLong_IsNegative(a) && !_PyLong_IsZero(*prem)) {
3246         _PyLong_Negate(prem);
3247         if (*prem == NULL) {
3248             Py_CLEAR(*prem);
3249             return -1;
3250         }
3251     }
3252     return 0;
3253 }
3254 
3255 /* Unsigned int division with remainder -- the algorithm.  The arguments v1
3256    and w1 should satisfy 2 <= _PyLong_DigitCount(w1) <= _PyLong_DigitCount(v1). */
3257 
3258 static PyLongObject *
x_divrem(PyLongObject * v1,PyLongObject * w1,PyLongObject ** prem)3259 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
3260 {
3261     PyLongObject *v, *w, *a;
3262     Py_ssize_t i, k, size_v, size_w;
3263     int d;
3264     digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
3265     twodigits vv;
3266     sdigit zhi;
3267     stwodigits z;
3268 
3269     /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
3270        edn.), section 4.3.1, Algorithm D], except that we don't explicitly
3271        handle the special case when the initial estimate q for a quotient
3272        digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
3273        that won't overflow a digit. */
3274 
3275     /* allocate space; w will also be used to hold the final remainder */
3276     size_v = _PyLong_DigitCount(v1);
3277     size_w = _PyLong_DigitCount(w1);
3278     assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
3279     v = _PyLong_New(size_v+1);
3280     if (v == NULL) {
3281         *prem = NULL;
3282         return NULL;
3283     }
3284     w = _PyLong_New(size_w);
3285     if (w == NULL) {
3286         Py_DECREF(v);
3287         *prem = NULL;
3288         return NULL;
3289     }
3290 
3291     /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
3292        shift v1 left by the same amount.  Results go into w and v. */
3293     d = PyLong_SHIFT - bit_length_digit(w1->long_value.ob_digit[size_w-1]);
3294     carry = v_lshift(w->long_value.ob_digit, w1->long_value.ob_digit, size_w, d);
3295     assert(carry == 0);
3296     carry = v_lshift(v->long_value.ob_digit, v1->long_value.ob_digit, size_v, d);
3297     if (carry != 0 || v->long_value.ob_digit[size_v-1] >= w->long_value.ob_digit[size_w-1]) {
3298         v->long_value.ob_digit[size_v] = carry;
3299         size_v++;
3300     }
3301 
3302     /* Now v->long_value.ob_digit[size_v-1] < w->long_value.ob_digit[size_w-1], so quotient has
3303        at most (and usually exactly) k = size_v - size_w digits. */
3304     k = size_v - size_w;
3305     assert(k >= 0);
3306     a = _PyLong_New(k);
3307     if (a == NULL) {
3308         Py_DECREF(w);
3309         Py_DECREF(v);
3310         *prem = NULL;
3311         return NULL;
3312     }
3313     v0 = v->long_value.ob_digit;
3314     w0 = w->long_value.ob_digit;
3315     wm1 = w0[size_w-1];
3316     wm2 = w0[size_w-2];
3317     for (vk = v0+k, ak = a->long_value.ob_digit + k; vk-- > v0;) {
3318         /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
3319            single-digit quotient q, remainder in vk[0:size_w]. */
3320 
3321         SIGCHECK({
3322                 Py_DECREF(a);
3323                 Py_DECREF(w);
3324                 Py_DECREF(v);
3325                 *prem = NULL;
3326                 return NULL;
3327             });
3328 
3329         /* estimate quotient digit q; may overestimate by 1 (rare) */
3330         vtop = vk[size_w];
3331         assert(vtop <= wm1);
3332         vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
3333         /* The code used to compute the remainder via
3334          *     r = (digit)(vv - (twodigits)wm1 * q);
3335          * and compilers generally generated code to do the * and -.
3336          * But modern processors generally compute q and r with a single
3337          * instruction, and modern optimizing compilers exploit that if we
3338          * _don't_ try to optimize it.
3339          */
3340         q = (digit)(vv / wm1);
3341         r = (digit)(vv % wm1);
3342         while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
3343                                      | vk[size_w-2])) {
3344             --q;
3345             r += wm1;
3346             if (r >= PyLong_BASE)
3347                 break;
3348         }
3349         assert(q <= PyLong_BASE);
3350 
3351         /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
3352         zhi = 0;
3353         for (i = 0; i < size_w; ++i) {
3354             /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
3355                -PyLong_BASE * q <= z < PyLong_BASE */
3356             z = (sdigit)vk[i] + zhi -
3357                 (stwodigits)q * (stwodigits)w0[i];
3358             vk[i] = (digit)z & PyLong_MASK;
3359             zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
3360                                                     z, PyLong_SHIFT);
3361         }
3362 
3363         /* add w back if q was too large (this branch taken rarely) */
3364         assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
3365         if ((sdigit)vtop + zhi < 0) {
3366             carry = 0;
3367             for (i = 0; i < size_w; ++i) {
3368                 carry += vk[i] + w0[i];
3369                 vk[i] = carry & PyLong_MASK;
3370                 carry >>= PyLong_SHIFT;
3371             }
3372             --q;
3373         }
3374 
3375         /* store quotient digit */
3376         assert(q < PyLong_BASE);
3377         *--ak = q;
3378     }
3379 
3380     /* unshift remainder; we reuse w to store the result */
3381     carry = v_rshift(w0, v0, size_w, d);
3382     assert(carry==0);
3383     Py_DECREF(v);
3384 
3385     *prem = long_normalize(w);
3386     return long_normalize(a);
3387 }
3388 
3389 /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <=
3390    abs(x) < 1.0 and e >= 0; return x and put e in *e.  Here x is
3391    rounded to DBL_MANT_DIG significant bits using round-half-to-even.
3392    If a == 0, return 0.0 and set *e = 0.  If the resulting exponent
3393    e is larger than PY_SSIZE_T_MAX, raise OverflowError and return
3394    -1.0. */
3395 
3396 /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */
3397 #if DBL_MANT_DIG == 53
3398 #define EXP2_DBL_MANT_DIG 9007199254740992.0
3399 #else
3400 #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG))
3401 #endif
3402 
3403 double
_PyLong_Frexp(PyLongObject * a,Py_ssize_t * e)3404 _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
3405 {
3406     Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size;
3407     /* See below for why x_digits is always large enough. */
3408     digit rem;
3409     digit x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT] = {0,};
3410     double dx;
3411     /* Correction term for round-half-to-even rounding.  For a digit x,
3412        "x + half_even_correction[x & 7]" gives x rounded to the nearest
3413        multiple of 4, rounding ties to a multiple of 8. */
3414     static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1};
3415 
3416     a_size = _PyLong_DigitCount(a);
3417     if (a_size == 0) {
3418         /* Special case for 0: significand 0.0, exponent 0. */
3419         *e = 0;
3420         return 0.0;
3421     }
3422     a_bits = bit_length_digit(a->long_value.ob_digit[a_size-1]);
3423     /* The following is an overflow-free version of the check
3424        "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */
3425     if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 &&
3426         (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 ||
3427          a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1))
3428         goto overflow;
3429     a_bits = (a_size - 1) * PyLong_SHIFT + a_bits;
3430 
3431     /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size]
3432        (shifting left if a_bits <= DBL_MANT_DIG + 2).
3433 
3434        Number of digits needed for result: write // for floor division.
3435        Then if shifting left, we end up using
3436 
3437          1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT
3438 
3439        digits.  If shifting right, we use
3440 
3441          a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT
3442 
3443        digits.  Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with
3444        the inequalities
3445 
3446          m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT
3447          m // PyLong_SHIFT - n // PyLong_SHIFT <=
3448                                           1 + (m - n - 1) // PyLong_SHIFT,
3449 
3450        valid for any integers m and n, we find that x_size satisfies
3451 
3452          x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT
3453 
3454        in both cases.
3455     */
3456     if (a_bits <= DBL_MANT_DIG + 2) {
3457         shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT;
3458         shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT;
3459         x_size = shift_digits;
3460         rem = v_lshift(x_digits + x_size, a->long_value.ob_digit, a_size,
3461                        (int)shift_bits);
3462         x_size += a_size;
3463         x_digits[x_size++] = rem;
3464     }
3465     else {
3466         shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT;
3467         shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT;
3468         rem = v_rshift(x_digits, a->long_value.ob_digit + shift_digits,
3469                        a_size - shift_digits, (int)shift_bits);
3470         x_size = a_size - shift_digits;
3471         /* For correct rounding below, we need the least significant
3472            bit of x to be 'sticky' for this shift: if any of the bits
3473            shifted out was nonzero, we set the least significant bit
3474            of x. */
3475         if (rem)
3476             x_digits[0] |= 1;
3477         else
3478             while (shift_digits > 0)
3479                 if (a->long_value.ob_digit[--shift_digits]) {
3480                     x_digits[0] |= 1;
3481                     break;
3482                 }
3483     }
3484     assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits));
3485 
3486     /* Round, and convert to double. */
3487     x_digits[0] += half_even_correction[x_digits[0] & 7];
3488     dx = x_digits[--x_size];
3489     while (x_size > 0)
3490         dx = dx * PyLong_BASE + x_digits[--x_size];
3491 
3492     /* Rescale;  make correction if result is 1.0. */
3493     dx /= 4.0 * EXP2_DBL_MANT_DIG;
3494     if (dx == 1.0) {
3495         if (a_bits == PY_SSIZE_T_MAX)
3496             goto overflow;
3497         dx = 0.5;
3498         a_bits += 1;
3499     }
3500 
3501     *e = a_bits;
3502     return _PyLong_IsNegative(a) ? -dx : dx;
3503 
3504   overflow:
3505     /* exponent > PY_SSIZE_T_MAX */
3506     PyErr_SetString(PyExc_OverflowError,
3507                     "huge integer: number of bits overflows a Py_ssize_t");
3508     *e = 0;
3509     return -1.0;
3510 }
3511 
3512 /* Get a C double from an int object.  Rounds to the nearest double,
3513    using the round-half-to-even rule in the case of a tie. */
3514 
3515 double
PyLong_AsDouble(PyObject * v)3516 PyLong_AsDouble(PyObject *v)
3517 {
3518     Py_ssize_t exponent;
3519     double x;
3520 
3521     if (v == NULL) {
3522         PyErr_BadInternalCall();
3523         return -1.0;
3524     }
3525     if (!PyLong_Check(v)) {
3526         PyErr_SetString(PyExc_TypeError, "an integer is required");
3527         return -1.0;
3528     }
3529     if (_PyLong_IsCompact((PyLongObject *)v)) {
3530         /* Fast path; single digit long (31 bits) will cast safely
3531            to double.  This improves performance of FP/long operations
3532            by 20%.
3533         */
3534         return (double)medium_value((PyLongObject *)v);
3535     }
3536     x = _PyLong_Frexp((PyLongObject *)v, &exponent);
3537     if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) {
3538         PyErr_SetString(PyExc_OverflowError,
3539                         "int too large to convert to float");
3540         return -1.0;
3541     }
3542     return ldexp(x, (int)exponent);
3543 }
3544 
3545 /* Methods */
3546 
3547 /* if a < b, return a negative number
3548    if a == b, return 0
3549    if a > b, return a positive number */
3550 
3551 static Py_ssize_t
long_compare(PyLongObject * a,PyLongObject * b)3552 long_compare(PyLongObject *a, PyLongObject *b)
3553 {
3554     if (_PyLong_BothAreCompact(a, b)) {
3555         return _PyLong_CompactValue(a) - _PyLong_CompactValue(b);
3556     }
3557     Py_ssize_t sign = _PyLong_SignedDigitCount(a) - _PyLong_SignedDigitCount(b);
3558     if (sign == 0) {
3559         Py_ssize_t i = _PyLong_DigitCount(a);
3560         sdigit diff = 0;
3561         while (--i >= 0) {
3562             diff = (sdigit) a->long_value.ob_digit[i] - (sdigit) b->long_value.ob_digit[i];
3563             if (diff) {
3564                 break;
3565             }
3566         }
3567         sign = _PyLong_IsNegative(a) ? -diff : diff;
3568     }
3569     return sign;
3570 }
3571 
3572 static PyObject *
long_richcompare(PyObject * self,PyObject * other,int op)3573 long_richcompare(PyObject *self, PyObject *other, int op)
3574 {
3575     Py_ssize_t result;
3576     CHECK_BINOP(self, other);
3577     if (self == other)
3578         result = 0;
3579     else
3580         result = long_compare((PyLongObject*)self, (PyLongObject*)other);
3581     Py_RETURN_RICHCOMPARE(result, 0, op);
3582 }
3583 
3584 static void
long_dealloc(PyObject * self)3585 long_dealloc(PyObject *self)
3586 {
3587     /* This should never get called, but we also don't want to SEGV if
3588      * we accidentally decref small Ints out of existence. Instead,
3589      * since small Ints are immortal, re-set the reference count.
3590      */
3591     PyLongObject *pylong = (PyLongObject*)self;
3592     if (pylong && _PyLong_IsCompact(pylong)) {
3593         stwodigits ival = medium_value(pylong);
3594         if (IS_SMALL_INT(ival)) {
3595             PyLongObject *small_pylong = (PyLongObject *)get_small_int((sdigit)ival);
3596             if (pylong == small_pylong) {
3597                 _Py_SetImmortal(self);
3598                 return;
3599             }
3600         }
3601     }
3602     Py_TYPE(self)->tp_free(self);
3603 }
3604 
3605 static Py_hash_t
long_hash(PyLongObject * v)3606 long_hash(PyLongObject *v)
3607 {
3608     Py_uhash_t x;
3609     Py_ssize_t i;
3610     int sign;
3611 
3612     if (_PyLong_IsCompact(v)) {
3613         x = (Py_uhash_t)_PyLong_CompactValue(v);
3614         if (x == (Py_uhash_t)-1) {
3615             x = (Py_uhash_t)-2;
3616         }
3617         return x;
3618     }
3619     i = _PyLong_DigitCount(v);
3620     sign = _PyLong_NonCompactSign(v);
3621     x = 0;
3622     while (--i >= 0) {
3623         /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
3624            want to compute x * 2**PyLong_SHIFT + v->long_value.ob_digit[i] modulo
3625            _PyHASH_MODULUS.
3626 
3627            The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS
3628            amounts to a rotation of the bits of x.  To see this, write
3629 
3630              x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z
3631 
3632            where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top
3633            PyLong_SHIFT bits of x (those that are shifted out of the
3634            original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
3635            _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT
3636            bits of x, shifted up.  Then since 2**_PyHASH_BITS is
3637            congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is
3638            congruent to y modulo _PyHASH_MODULUS.  So
3639 
3640              x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS).
3641 
3642            The right-hand side is just the result of rotating the
3643            _PyHASH_BITS bits of x left by PyLong_SHIFT places; since
3644            not all _PyHASH_BITS bits of x are 1s, the same is true
3645            after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is
3646            the reduction of x*2**PyLong_SHIFT modulo
3647            _PyHASH_MODULUS. */
3648         x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) |
3649             (x >> (_PyHASH_BITS - PyLong_SHIFT));
3650         x += v->long_value.ob_digit[i];
3651         if (x >= _PyHASH_MODULUS)
3652             x -= _PyHASH_MODULUS;
3653     }
3654     x = x * sign;
3655     if (x == (Py_uhash_t)-1)
3656         x = (Py_uhash_t)-2;
3657     return (Py_hash_t)x;
3658 }
3659 
3660 
3661 /* Add the absolute values of two integers. */
3662 
3663 static PyLongObject *
x_add(PyLongObject * a,PyLongObject * b)3664 x_add(PyLongObject *a, PyLongObject *b)
3665 {
3666     Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3667     PyLongObject *z;
3668     Py_ssize_t i;
3669     digit carry = 0;
3670 
3671     /* Ensure a is the larger of the two: */
3672     if (size_a < size_b) {
3673         { PyLongObject *temp = a; a = b; b = temp; }
3674         { Py_ssize_t size_temp = size_a;
3675             size_a = size_b;
3676             size_b = size_temp; }
3677     }
3678     z = _PyLong_New(size_a+1);
3679     if (z == NULL)
3680         return NULL;
3681     for (i = 0; i < size_b; ++i) {
3682         carry += a->long_value.ob_digit[i] + b->long_value.ob_digit[i];
3683         z->long_value.ob_digit[i] = carry & PyLong_MASK;
3684         carry >>= PyLong_SHIFT;
3685     }
3686     for (; i < size_a; ++i) {
3687         carry += a->long_value.ob_digit[i];
3688         z->long_value.ob_digit[i] = carry & PyLong_MASK;
3689         carry >>= PyLong_SHIFT;
3690     }
3691     z->long_value.ob_digit[i] = carry;
3692     return long_normalize(z);
3693 }
3694 
3695 /* Subtract the absolute values of two integers. */
3696 
3697 static PyLongObject *
x_sub(PyLongObject * a,PyLongObject * b)3698 x_sub(PyLongObject *a, PyLongObject *b)
3699 {
3700     Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3701     PyLongObject *z;
3702     Py_ssize_t i;
3703     int sign = 1;
3704     digit borrow = 0;
3705 
3706     /* Ensure a is the larger of the two: */
3707     if (size_a < size_b) {
3708         sign = -1;
3709         { PyLongObject *temp = a; a = b; b = temp; }
3710         { Py_ssize_t size_temp = size_a;
3711             size_a = size_b;
3712             size_b = size_temp; }
3713     }
3714     else if (size_a == size_b) {
3715         /* Find highest digit where a and b differ: */
3716         i = size_a;
3717         while (--i >= 0 && a->long_value.ob_digit[i] == b->long_value.ob_digit[i])
3718             ;
3719         if (i < 0)
3720             return (PyLongObject *)PyLong_FromLong(0);
3721         if (a->long_value.ob_digit[i] < b->long_value.ob_digit[i]) {
3722             sign = -1;
3723             { PyLongObject *temp = a; a = b; b = temp; }
3724         }
3725         size_a = size_b = i+1;
3726     }
3727     z = _PyLong_New(size_a);
3728     if (z == NULL)
3729         return NULL;
3730     for (i = 0; i < size_b; ++i) {
3731         /* The following assumes unsigned arithmetic
3732            works module 2**N for some N>PyLong_SHIFT. */
3733         borrow = a->long_value.ob_digit[i] - b->long_value.ob_digit[i] - borrow;
3734         z->long_value.ob_digit[i] = borrow & PyLong_MASK;
3735         borrow >>= PyLong_SHIFT;
3736         borrow &= 1; /* Keep only one sign bit */
3737     }
3738     for (; i < size_a; ++i) {
3739         borrow = a->long_value.ob_digit[i] - borrow;
3740         z->long_value.ob_digit[i] = borrow & PyLong_MASK;
3741         borrow >>= PyLong_SHIFT;
3742         borrow &= 1; /* Keep only one sign bit */
3743     }
3744     assert(borrow == 0);
3745     if (sign < 0) {
3746         _PyLong_FlipSign(z);
3747     }
3748     return maybe_small_long(long_normalize(z));
3749 }
3750 
3751 PyObject *
_PyLong_Add(PyLongObject * a,PyLongObject * b)3752 _PyLong_Add(PyLongObject *a, PyLongObject *b)
3753 {
3754     if (_PyLong_BothAreCompact(a, b)) {
3755         return _PyLong_FromSTwoDigits(medium_value(a) + medium_value(b));
3756     }
3757 
3758     PyLongObject *z;
3759     if (_PyLong_IsNegative(a)) {
3760         if (_PyLong_IsNegative(b)) {
3761             z = x_add(a, b);
3762             if (z != NULL) {
3763                 /* x_add received at least one multiple-digit int,
3764                    and thus z must be a multiple-digit int.
3765                    That also means z is not an element of
3766                    small_ints, so negating it in-place is safe. */
3767                 assert(Py_REFCNT(z) == 1);
3768                 _PyLong_FlipSign(z);
3769             }
3770         }
3771         else
3772             z = x_sub(b, a);
3773     }
3774     else {
3775         if (_PyLong_IsNegative(b))
3776             z = x_sub(a, b);
3777         else
3778             z = x_add(a, b);
3779     }
3780     return (PyObject *)z;
3781 }
3782 
3783 static PyObject *
long_add(PyLongObject * a,PyLongObject * b)3784 long_add(PyLongObject *a, PyLongObject *b)
3785 {
3786     CHECK_BINOP(a, b);
3787     return _PyLong_Add(a, b);
3788 }
3789 
3790 PyObject *
_PyLong_Subtract(PyLongObject * a,PyLongObject * b)3791 _PyLong_Subtract(PyLongObject *a, PyLongObject *b)
3792 {
3793     PyLongObject *z;
3794 
3795     if (_PyLong_BothAreCompact(a, b)) {
3796         return _PyLong_FromSTwoDigits(medium_value(a) - medium_value(b));
3797     }
3798     if (_PyLong_IsNegative(a)) {
3799         if (_PyLong_IsNegative(b)) {
3800             z = x_sub(b, a);
3801         }
3802         else {
3803             z = x_add(a, b);
3804             if (z != NULL) {
3805                 assert(_PyLong_IsZero(z) || Py_REFCNT(z) == 1);
3806                 _PyLong_FlipSign(z);
3807             }
3808         }
3809     }
3810     else {
3811         if (_PyLong_IsNegative(b))
3812             z = x_add(a, b);
3813         else
3814             z = x_sub(a, b);
3815     }
3816     return (PyObject *)z;
3817 }
3818 
3819 static PyObject *
long_sub(PyLongObject * a,PyLongObject * b)3820 long_sub(PyLongObject *a, PyLongObject *b)
3821 {
3822     CHECK_BINOP(a, b);
3823     return _PyLong_Subtract(a, b);
3824 }
3825 
3826 /* Grade school multiplication, ignoring the signs.
3827  * Returns the absolute value of the product, or NULL if error.
3828  */
3829 static PyLongObject *
x_mul(PyLongObject * a,PyLongObject * b)3830 x_mul(PyLongObject *a, PyLongObject *b)
3831 {
3832     PyLongObject *z;
3833     Py_ssize_t size_a = _PyLong_DigitCount(a);
3834     Py_ssize_t size_b = _PyLong_DigitCount(b);
3835     Py_ssize_t i;
3836 
3837     z = _PyLong_New(size_a + size_b);
3838     if (z == NULL)
3839         return NULL;
3840 
3841     memset(z->long_value.ob_digit, 0, _PyLong_DigitCount(z) * sizeof(digit));
3842     if (a == b) {
3843         /* Efficient squaring per HAC, Algorithm 14.16:
3844          * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
3845          * Gives slightly less than a 2x speedup when a == b,
3846          * via exploiting that each entry in the multiplication
3847          * pyramid appears twice (except for the size_a squares).
3848          */
3849         digit *paend = a->long_value.ob_digit + size_a;
3850         for (i = 0; i < size_a; ++i) {
3851             twodigits carry;
3852             twodigits f = a->long_value.ob_digit[i];
3853             digit *pz = z->long_value.ob_digit + (i << 1);
3854             digit *pa = a->long_value.ob_digit + i + 1;
3855 
3856             SIGCHECK({
3857                     Py_DECREF(z);
3858                     return NULL;
3859                 });
3860 
3861             carry = *pz + f * f;
3862             *pz++ = (digit)(carry & PyLong_MASK);
3863             carry >>= PyLong_SHIFT;
3864             assert(carry <= PyLong_MASK);
3865 
3866             /* Now f is added in twice in each column of the
3867              * pyramid it appears.  Same as adding f<<1 once.
3868              */
3869             f <<= 1;
3870             while (pa < paend) {
3871                 carry += *pz + *pa++ * f;
3872                 *pz++ = (digit)(carry & PyLong_MASK);
3873                 carry >>= PyLong_SHIFT;
3874                 assert(carry <= (PyLong_MASK << 1));
3875             }
3876             if (carry) {
3877                 /* See comment below. pz points at the highest possible
3878                  * carry position from the last outer loop iteration, so
3879                  * *pz is at most 1.
3880                  */
3881                 assert(*pz <= 1);
3882                 carry += *pz;
3883                 *pz = (digit)(carry & PyLong_MASK);
3884                 carry >>= PyLong_SHIFT;
3885                 if (carry) {
3886                     /* If there's still a carry, it must be into a position
3887                      * that still holds a 0. Where the base
3888                      ^ B is 1 << PyLong_SHIFT, the last add was of a carry no
3889                      * more than 2*B - 2 to a stored digit no more than 1.
3890                      * So the sum was no more than 2*B - 1, so the current
3891                      * carry no more than floor((2*B - 1)/B) = 1.
3892                      */
3893                     assert(carry == 1);
3894                     assert(pz[1] == 0);
3895                     pz[1] = (digit)carry;
3896                 }
3897             }
3898         }
3899     }
3900     else {      /* a is not the same as b -- gradeschool int mult */
3901         for (i = 0; i < size_a; ++i) {
3902             twodigits carry = 0;
3903             twodigits f = a->long_value.ob_digit[i];
3904             digit *pz = z->long_value.ob_digit + i;
3905             digit *pb = b->long_value.ob_digit;
3906             digit *pbend = b->long_value.ob_digit + size_b;
3907 
3908             SIGCHECK({
3909                     Py_DECREF(z);
3910                     return NULL;
3911                 });
3912 
3913             while (pb < pbend) {
3914                 carry += *pz + *pb++ * f;
3915                 *pz++ = (digit)(carry & PyLong_MASK);
3916                 carry >>= PyLong_SHIFT;
3917                 assert(carry <= PyLong_MASK);
3918             }
3919             if (carry)
3920                 *pz += (digit)(carry & PyLong_MASK);
3921             assert((carry >> PyLong_SHIFT) == 0);
3922         }
3923     }
3924     return long_normalize(z);
3925 }
3926 
3927 /* A helper for Karatsuba multiplication (k_mul).
3928    Takes an int "n" and an integer "size" representing the place to
3929    split, and sets low and high such that abs(n) == (high << size) + low,
3930    viewing the shift as being by digits.  The sign bit is ignored, and
3931    the return values are >= 0.
3932    Returns 0 on success, -1 on failure.
3933 */
3934 static int
kmul_split(PyLongObject * n,Py_ssize_t size,PyLongObject ** high,PyLongObject ** low)3935 kmul_split(PyLongObject *n,
3936            Py_ssize_t size,
3937            PyLongObject **high,
3938            PyLongObject **low)
3939 {
3940     PyLongObject *hi, *lo;
3941     Py_ssize_t size_lo, size_hi;
3942     const Py_ssize_t size_n = _PyLong_DigitCount(n);
3943 
3944     size_lo = Py_MIN(size_n, size);
3945     size_hi = size_n - size_lo;
3946 
3947     if ((hi = _PyLong_New(size_hi)) == NULL)
3948         return -1;
3949     if ((lo = _PyLong_New(size_lo)) == NULL) {
3950         Py_DECREF(hi);
3951         return -1;
3952     }
3953 
3954     memcpy(lo->long_value.ob_digit, n->long_value.ob_digit, size_lo * sizeof(digit));
3955     memcpy(hi->long_value.ob_digit, n->long_value.ob_digit + size_lo, size_hi * sizeof(digit));
3956 
3957     *high = long_normalize(hi);
3958     *low = long_normalize(lo);
3959     return 0;
3960 }
3961 
3962 static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
3963 
3964 /* Karatsuba multiplication.  Ignores the input signs, and returns the
3965  * absolute value of the product (or NULL if error).
3966  * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
3967  */
3968 static PyLongObject *
k_mul(PyLongObject * a,PyLongObject * b)3969 k_mul(PyLongObject *a, PyLongObject *b)
3970 {
3971     Py_ssize_t asize = _PyLong_DigitCount(a);
3972     Py_ssize_t bsize = _PyLong_DigitCount(b);
3973     PyLongObject *ah = NULL;
3974     PyLongObject *al = NULL;
3975     PyLongObject *bh = NULL;
3976     PyLongObject *bl = NULL;
3977     PyLongObject *ret = NULL;
3978     PyLongObject *t1, *t2, *t3;
3979     Py_ssize_t shift;           /* the number of digits we split off */
3980     Py_ssize_t i;
3981 
3982     /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
3983      * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh  + ah*bh + al*bl
3984      * Then the original product is
3985      *     ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
3986      * By picking X to be a power of 2, "*X" is just shifting, and it's
3987      * been reduced to 3 multiplies on numbers half the size.
3988      */
3989 
3990     /* We want to split based on the larger number; fiddle so that b
3991      * is largest.
3992      */
3993     if (asize > bsize) {
3994         t1 = a;
3995         a = b;
3996         b = t1;
3997 
3998         i = asize;
3999         asize = bsize;
4000         bsize = i;
4001     }
4002 
4003     /* Use gradeschool math when either number is too small. */
4004     i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
4005     if (asize <= i) {
4006         if (asize == 0)
4007             return (PyLongObject *)PyLong_FromLong(0);
4008         else
4009             return x_mul(a, b);
4010     }
4011 
4012     /* If a is small compared to b, splitting on b gives a degenerate
4013      * case with ah==0, and Karatsuba may be (even much) less efficient
4014      * than "grade school" then.  However, we can still win, by viewing
4015      * b as a string of "big digits", each of the same width as a. That
4016      * leads to a sequence of balanced calls to k_mul.
4017      */
4018     if (2 * asize <= bsize)
4019         return k_lopsided_mul(a, b);
4020 
4021     /* Split a & b into hi & lo pieces. */
4022     shift = bsize >> 1;
4023     if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
4024     assert(_PyLong_IsPositive(ah));        /* the split isn't degenerate */
4025 
4026     if (a == b) {
4027         bh = (PyLongObject*)Py_NewRef(ah);
4028         bl = (PyLongObject*)Py_NewRef(al);
4029     }
4030     else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
4031 
4032     /* The plan:
4033      * 1. Allocate result space (asize + bsize digits:  that's always
4034      *    enough).
4035      * 2. Compute ah*bh, and copy into result at 2*shift.
4036      * 3. Compute al*bl, and copy into result at 0.  Note that this
4037      *    can't overlap with #2.
4038      * 4. Subtract al*bl from the result, starting at shift.  This may
4039      *    underflow (borrow out of the high digit), but we don't care:
4040      *    we're effectively doing unsigned arithmetic mod
4041      *    BASE**(sizea + sizeb), and so long as the *final* result fits,
4042      *    borrows and carries out of the high digit can be ignored.
4043      * 5. Subtract ah*bh from the result, starting at shift.
4044      * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
4045      *    at shift.
4046      */
4047 
4048     /* 1. Allocate result space. */
4049     ret = _PyLong_New(asize + bsize);
4050     if (ret == NULL) goto fail;
4051 #ifdef Py_DEBUG
4052     /* Fill with trash, to catch reference to uninitialized digits. */
4053     memset(ret->long_value.ob_digit, 0xDF, _PyLong_DigitCount(ret) * sizeof(digit));
4054 #endif
4055 
4056     /* 2. t1 <- ah*bh, and copy into high digits of result. */
4057     if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
4058     assert(!_PyLong_IsNegative(t1));
4059     assert(2*shift + _PyLong_DigitCount(t1) <= _PyLong_DigitCount(ret));
4060     memcpy(ret->long_value.ob_digit + 2*shift, t1->long_value.ob_digit,
4061            _PyLong_DigitCount(t1) * sizeof(digit));
4062 
4063     /* Zero-out the digits higher than the ah*bh copy. */
4064     i = _PyLong_DigitCount(ret) - 2*shift - _PyLong_DigitCount(t1);
4065     if (i)
4066         memset(ret->long_value.ob_digit + 2*shift + _PyLong_DigitCount(t1), 0,
4067                i * sizeof(digit));
4068 
4069     /* 3. t2 <- al*bl, and copy into the low digits. */
4070     if ((t2 = k_mul(al, bl)) == NULL) {
4071         Py_DECREF(t1);
4072         goto fail;
4073     }
4074     assert(!_PyLong_IsNegative(t2));
4075     assert(_PyLong_DigitCount(t2) <= 2*shift); /* no overlap with high digits */
4076     memcpy(ret->long_value.ob_digit, t2->long_value.ob_digit, _PyLong_DigitCount(t2) * sizeof(digit));
4077 
4078     /* Zero out remaining digits. */
4079     i = 2*shift - _PyLong_DigitCount(t2);          /* number of uninitialized digits */
4080     if (i)
4081         memset(ret->long_value.ob_digit + _PyLong_DigitCount(t2), 0, i * sizeof(digit));
4082 
4083     /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2).  We do al*bl first
4084      * because it's fresher in cache.
4085      */
4086     i = _PyLong_DigitCount(ret) - shift;  /* # digits after shift */
4087     (void)v_isub(ret->long_value.ob_digit + shift, i, t2->long_value.ob_digit, _PyLong_DigitCount(t2));
4088     _Py_DECREF_INT(t2);
4089 
4090     (void)v_isub(ret->long_value.ob_digit + shift, i, t1->long_value.ob_digit, _PyLong_DigitCount(t1));
4091     _Py_DECREF_INT(t1);
4092 
4093     /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
4094     if ((t1 = x_add(ah, al)) == NULL) goto fail;
4095     _Py_DECREF_INT(ah);
4096     _Py_DECREF_INT(al);
4097     ah = al = NULL;
4098 
4099     if (a == b) {
4100         t2 = (PyLongObject*)Py_NewRef(t1);
4101     }
4102     else if ((t2 = x_add(bh, bl)) == NULL) {
4103         Py_DECREF(t1);
4104         goto fail;
4105     }
4106     _Py_DECREF_INT(bh);
4107     _Py_DECREF_INT(bl);
4108     bh = bl = NULL;
4109 
4110     t3 = k_mul(t1, t2);
4111     _Py_DECREF_INT(t1);
4112     _Py_DECREF_INT(t2);
4113     if (t3 == NULL) goto fail;
4114     assert(!_PyLong_IsNegative(t3));
4115 
4116     /* Add t3.  It's not obvious why we can't run out of room here.
4117      * See the (*) comment after this function.
4118      */
4119     (void)v_iadd(ret->long_value.ob_digit + shift, i, t3->long_value.ob_digit, _PyLong_DigitCount(t3));
4120     _Py_DECREF_INT(t3);
4121 
4122     return long_normalize(ret);
4123 
4124   fail:
4125     Py_XDECREF(ret);
4126     Py_XDECREF(ah);
4127     Py_XDECREF(al);
4128     Py_XDECREF(bh);
4129     Py_XDECREF(bl);
4130     return NULL;
4131 }
4132 
4133 /* (*) Why adding t3 can't "run out of room" above.
4134 
4135 Let f(x) mean the floor of x and c(x) mean the ceiling of x.  Some facts
4136 to start with:
4137 
4138 1. For any integer i, i = c(i/2) + f(i/2).  In particular,
4139    bsize = c(bsize/2) + f(bsize/2).
4140 2. shift = f(bsize/2)
4141 3. asize <= bsize
4142 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
4143    routine, so asize > bsize/2 >= f(bsize/2) in this routine.
4144 
4145 We allocated asize + bsize result digits, and add t3 into them at an offset
4146 of shift.  This leaves asize+bsize-shift allocated digit positions for t3
4147 to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
4148 asize + c(bsize/2) available digit positions.
4149 
4150 bh has c(bsize/2) digits, and bl at most f(size/2) digits.  So bh+hl has
4151 at most c(bsize/2) digits + 1 bit.
4152 
4153 If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
4154 digits, and al has at most f(bsize/2) digits in any case.  So ah+al has at
4155 most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
4156 
4157 The product (ah+al)*(bh+bl) therefore has at most
4158 
4159     c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
4160 
4161 and we have asize + c(bsize/2) available digit positions.  We need to show
4162 this is always enough.  An instance of c(bsize/2) cancels out in both, so
4163 the question reduces to whether asize digits is enough to hold
4164 (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits.  If asize < bsize,
4165 then we're asking whether asize digits >= f(bsize/2) digits + 2 bits.  By #4,
4166 asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
4167 digit is enough to hold 2 bits.  This is so since PyLong_SHIFT=15 >= 2.  If
4168 asize == bsize, then we're asking whether bsize digits is enough to hold
4169 c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
4170 is enough to hold 2 bits.  This is so if bsize >= 2, which holds because
4171 bsize >= KARATSUBA_CUTOFF >= 2.
4172 
4173 Note that since there's always enough room for (ah+al)*(bh+bl), and that's
4174 clearly >= each of ah*bh and al*bl, there's always enough room to subtract
4175 ah*bh and al*bl too.
4176 */
4177 
4178 /* b has at least twice the digits of a, and a is big enough that Karatsuba
4179  * would pay off *if* the inputs had balanced sizes.  View b as a sequence
4180  * of slices, each with the same number of digits as a, and multiply the
4181  * slices by a, one at a time.  This gives k_mul balanced inputs to work with,
4182  * and is also cache-friendly (we compute one double-width slice of the result
4183  * at a time, then move on, never backtracking except for the helpful
4184  * single-width slice overlap between successive partial sums).
4185  */
4186 static PyLongObject *
k_lopsided_mul(PyLongObject * a,PyLongObject * b)4187 k_lopsided_mul(PyLongObject *a, PyLongObject *b)
4188 {
4189     const Py_ssize_t asize = _PyLong_DigitCount(a);
4190     Py_ssize_t bsize = _PyLong_DigitCount(b);
4191     Py_ssize_t nbdone;          /* # of b digits already multiplied */
4192     PyLongObject *ret;
4193     PyLongObject *bslice = NULL;
4194 
4195     assert(asize > KARATSUBA_CUTOFF);
4196     assert(2 * asize <= bsize);
4197 
4198     /* Allocate result space, and zero it out. */
4199     ret = _PyLong_New(asize + bsize);
4200     if (ret == NULL)
4201         return NULL;
4202     memset(ret->long_value.ob_digit, 0, _PyLong_DigitCount(ret) * sizeof(digit));
4203 
4204     /* Successive slices of b are copied into bslice. */
4205     bslice = _PyLong_New(asize);
4206     if (bslice == NULL)
4207         goto fail;
4208 
4209     nbdone = 0;
4210     while (bsize > 0) {
4211         PyLongObject *product;
4212         const Py_ssize_t nbtouse = Py_MIN(bsize, asize);
4213 
4214         /* Multiply the next slice of b by a. */
4215         memcpy(bslice->long_value.ob_digit, b->long_value.ob_digit + nbdone,
4216                nbtouse * sizeof(digit));
4217         assert(nbtouse >= 0);
4218         _PyLong_SetSignAndDigitCount(bslice, 1, nbtouse);
4219         product = k_mul(a, bslice);
4220         if (product == NULL)
4221             goto fail;
4222 
4223         /* Add into result. */
4224         (void)v_iadd(ret->long_value.ob_digit + nbdone, _PyLong_DigitCount(ret) - nbdone,
4225                      product->long_value.ob_digit, _PyLong_DigitCount(product));
4226         _Py_DECREF_INT(product);
4227 
4228         bsize -= nbtouse;
4229         nbdone += nbtouse;
4230     }
4231 
4232     _Py_DECREF_INT(bslice);
4233     return long_normalize(ret);
4234 
4235   fail:
4236     Py_DECREF(ret);
4237     Py_XDECREF(bslice);
4238     return NULL;
4239 }
4240 
4241 PyObject *
_PyLong_Multiply(PyLongObject * a,PyLongObject * b)4242 _PyLong_Multiply(PyLongObject *a, PyLongObject *b)
4243 {
4244     PyLongObject *z;
4245 
4246     /* fast path for single-digit multiplication */
4247     if (_PyLong_BothAreCompact(a, b)) {
4248         stwodigits v = medium_value(a) * medium_value(b);
4249         return _PyLong_FromSTwoDigits(v);
4250     }
4251 
4252     z = k_mul(a, b);
4253     /* Negate if exactly one of the inputs is negative. */
4254     if (!_PyLong_SameSign(a, b) && z) {
4255         _PyLong_Negate(&z);
4256         if (z == NULL)
4257             return NULL;
4258     }
4259     return (PyObject *)z;
4260 }
4261 
4262 static PyObject *
long_mul(PyLongObject * a,PyLongObject * b)4263 long_mul(PyLongObject *a, PyLongObject *b)
4264 {
4265     CHECK_BINOP(a, b);
4266     return _PyLong_Multiply(a, b);
4267 }
4268 
4269 /* Fast modulo division for single-digit longs. */
4270 static PyObject *
fast_mod(PyLongObject * a,PyLongObject * b)4271 fast_mod(PyLongObject *a, PyLongObject *b)
4272 {
4273     sdigit left = a->long_value.ob_digit[0];
4274     sdigit right = b->long_value.ob_digit[0];
4275     sdigit mod;
4276 
4277     assert(_PyLong_DigitCount(a) == 1);
4278     assert(_PyLong_DigitCount(b) == 1);
4279     sdigit sign = _PyLong_CompactSign(b);
4280     if (_PyLong_SameSign(a, b)) {
4281         mod = left % right;
4282     }
4283     else {
4284         /* Either 'a' or 'b' is negative. */
4285         mod = right - 1 - (left - 1) % right;
4286     }
4287 
4288     return PyLong_FromLong(mod * sign);
4289 }
4290 
4291 /* Fast floor division for single-digit longs. */
4292 static PyObject *
fast_floor_div(PyLongObject * a,PyLongObject * b)4293 fast_floor_div(PyLongObject *a, PyLongObject *b)
4294 {
4295     sdigit left = a->long_value.ob_digit[0];
4296     sdigit right = b->long_value.ob_digit[0];
4297     sdigit div;
4298 
4299     assert(_PyLong_DigitCount(a) == 1);
4300     assert(_PyLong_DigitCount(b) == 1);
4301 
4302     if (_PyLong_SameSign(a, b)) {
4303         div = left / right;
4304     }
4305     else {
4306         /* Either 'a' or 'b' is negative. */
4307         div = -1 - (left - 1) / right;
4308     }
4309 
4310     return PyLong_FromLong(div);
4311 }
4312 
4313 #ifdef WITH_PYLONG_MODULE
4314 /* asymptotically faster divmod, using _pylong.py */
4315 static int
pylong_int_divmod(PyLongObject * v,PyLongObject * w,PyLongObject ** pdiv,PyLongObject ** pmod)4316 pylong_int_divmod(PyLongObject *v, PyLongObject *w,
4317                   PyLongObject **pdiv, PyLongObject **pmod)
4318 {
4319     PyObject *mod = PyImport_ImportModule("_pylong");
4320     if (mod == NULL) {
4321         return -1;
4322     }
4323     PyObject *result = PyObject_CallMethod(mod, "int_divmod", "OO", v, w);
4324     Py_DECREF(mod);
4325     if (result == NULL) {
4326         return -1;
4327     }
4328     if (!PyTuple_Check(result)) {
4329         Py_DECREF(result);
4330         PyErr_SetString(PyExc_ValueError,
4331                         "tuple is required from int_divmod()");
4332         return -1;
4333     }
4334     PyObject *q = PyTuple_GET_ITEM(result, 0);
4335     PyObject *r = PyTuple_GET_ITEM(result, 1);
4336     if (!PyLong_Check(q) || !PyLong_Check(r)) {
4337         Py_DECREF(result);
4338         PyErr_SetString(PyExc_ValueError,
4339                         "tuple of int is required from int_divmod()");
4340         return -1;
4341     }
4342     if (pdiv != NULL) {
4343         *pdiv = (PyLongObject *)Py_NewRef(q);
4344     }
4345     if (pmod != NULL) {
4346         *pmod = (PyLongObject *)Py_NewRef(r);
4347     }
4348     Py_DECREF(result);
4349     return 0;
4350 }
4351 #endif /* WITH_PYLONG_MODULE */
4352 
4353 /* The / and % operators are now defined in terms of divmod().
4354    The expression a mod b has the value a - b*floor(a/b).
4355    The long_divrem function gives the remainder after division of
4356    |a| by |b|, with the sign of a.  This is also expressed
4357    as a - b*trunc(a/b), if trunc truncates towards zero.
4358    Some examples:
4359      a           b      a rem b         a mod b
4360      13          10      3               3
4361     -13          10     -3               7
4362      13         -10      3              -7
4363     -13         -10     -3              -3
4364    So, to get from rem to mod, we have to add b if a and b
4365    have different signs.  We then subtract one from the 'div'
4366    part of the outcome to keep the invariant intact. */
4367 
4368 /* Compute
4369  *     *pdiv, *pmod = divmod(v, w)
4370  * NULL can be passed for pdiv or pmod, in which case that part of
4371  * the result is simply thrown away.  The caller owns a reference to
4372  * each of these it requests (does not pass NULL for).
4373  */
4374 static int
l_divmod(PyLongObject * v,PyLongObject * w,PyLongObject ** pdiv,PyLongObject ** pmod)4375 l_divmod(PyLongObject *v, PyLongObject *w,
4376          PyLongObject **pdiv, PyLongObject **pmod)
4377 {
4378     PyLongObject *div, *mod;
4379 
4380     if (_PyLong_DigitCount(v) == 1 && _PyLong_DigitCount(w) == 1) {
4381         /* Fast path for single-digit longs */
4382         div = NULL;
4383         if (pdiv != NULL) {
4384             div = (PyLongObject *)fast_floor_div(v, w);
4385             if (div == NULL) {
4386                 return -1;
4387             }
4388         }
4389         if (pmod != NULL) {
4390             mod = (PyLongObject *)fast_mod(v, w);
4391             if (mod == NULL) {
4392                 Py_XDECREF(div);
4393                 return -1;
4394             }
4395             *pmod = mod;
4396         }
4397         if (pdiv != NULL) {
4398             /* We only want to set `*pdiv` when `*pmod` is
4399                set successfully. */
4400             *pdiv = div;
4401         }
4402         return 0;
4403     }
4404 #if WITH_PYLONG_MODULE
4405     Py_ssize_t size_v = _PyLong_DigitCount(v); /* digits in numerator */
4406     Py_ssize_t size_w = _PyLong_DigitCount(w); /* digits in denominator */
4407     if (size_w > 300 && (size_v - size_w) > 150) {
4408         /* Switch to _pylong.int_divmod().  If the quotient is small then
4409           "schoolbook" division is linear-time so don't use in that case.
4410           These limits are empirically determined and should be slightly
4411           conservative so that _pylong is used in cases it is likely
4412           to be faster. See Tools/scripts/divmod_threshold.py. */
4413         return pylong_int_divmod(v, w, pdiv, pmod);
4414     }
4415 #endif
4416     if (long_divrem(v, w, &div, &mod) < 0)
4417         return -1;
4418     if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) ||
4419         (_PyLong_IsPositive(mod) && _PyLong_IsNegative(w))) {
4420         PyLongObject *temp;
4421         temp = (PyLongObject *) long_add(mod, w);
4422         Py_SETREF(mod, temp);
4423         if (mod == NULL) {
4424             Py_DECREF(div);
4425             return -1;
4426         }
4427         temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_GetOne());
4428         if (temp == NULL) {
4429             Py_DECREF(mod);
4430             Py_DECREF(div);
4431             return -1;
4432         }
4433         Py_SETREF(div, temp);
4434     }
4435     if (pdiv != NULL)
4436         *pdiv = div;
4437     else
4438         Py_DECREF(div);
4439 
4440     if (pmod != NULL)
4441         *pmod = mod;
4442     else
4443         Py_DECREF(mod);
4444 
4445     return 0;
4446 }
4447 
4448 /* Compute
4449  *     *pmod = v % w
4450  * pmod cannot be NULL. The caller owns a reference to pmod.
4451  */
4452 static int
l_mod(PyLongObject * v,PyLongObject * w,PyLongObject ** pmod)4453 l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod)
4454 {
4455     PyLongObject *mod;
4456 
4457     assert(pmod);
4458     if (_PyLong_DigitCount(v) == 1 && _PyLong_DigitCount(w) == 1) {
4459         /* Fast path for single-digit longs */
4460         *pmod = (PyLongObject *)fast_mod(v, w);
4461         return -(*pmod == NULL);
4462     }
4463     if (long_rem(v, w, &mod) < 0)
4464         return -1;
4465     if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) ||
4466         (_PyLong_IsPositive(mod) && _PyLong_IsNegative(w))) {
4467         PyLongObject *temp;
4468         temp = (PyLongObject *) long_add(mod, w);
4469         Py_SETREF(mod, temp);
4470         if (mod == NULL)
4471             return -1;
4472     }
4473     *pmod = mod;
4474 
4475     return 0;
4476 }
4477 
4478 static PyObject *
long_div(PyObject * a,PyObject * b)4479 long_div(PyObject *a, PyObject *b)
4480 {
4481     PyLongObject *div;
4482 
4483     CHECK_BINOP(a, b);
4484 
4485     if (_PyLong_DigitCount((PyLongObject*)a) == 1 && _PyLong_DigitCount((PyLongObject*)b) == 1) {
4486         return fast_floor_div((PyLongObject*)a, (PyLongObject*)b);
4487     }
4488 
4489     if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
4490         div = NULL;
4491     return (PyObject *)div;
4492 }
4493 
4494 /* PyLong/PyLong -> float, with correctly rounded result. */
4495 
4496 #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT)
4497 #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT)
4498 
4499 static PyObject *
long_true_divide(PyObject * v,PyObject * w)4500 long_true_divide(PyObject *v, PyObject *w)
4501 {
4502     PyLongObject *a, *b, *x;
4503     Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits;
4504     digit mask, low;
4505     int inexact, negate, a_is_small, b_is_small;
4506     double dx, result;
4507 
4508     CHECK_BINOP(v, w);
4509     a = (PyLongObject *)v;
4510     b = (PyLongObject *)w;
4511 
4512     /*
4513        Method in a nutshell:
4514 
4515          0. reduce to case a, b > 0; filter out obvious underflow/overflow
4516          1. choose a suitable integer 'shift'
4517          2. use integer arithmetic to compute x = floor(2**-shift*a/b)
4518          3. adjust x for correct rounding
4519          4. convert x to a double dx with the same value
4520          5. return ldexp(dx, shift).
4521 
4522        In more detail:
4523 
4524        0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b
4525        returns either 0.0 or -0.0, depending on the sign of b.  For a and
4526        b both nonzero, ignore signs of a and b, and add the sign back in
4527        at the end.  Now write a_bits and b_bits for the bit lengths of a
4528        and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise
4529        for b).  Then
4530 
4531           2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1).
4532 
4533        So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and
4534        so overflows.  Similarly, if a_bits - b_bits < DBL_MIN_EXP -
4535        DBL_MANT_DIG - 1 then a/b underflows to 0.  With these cases out of
4536        the way, we can assume that
4537 
4538           DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP.
4539 
4540        1. The integer 'shift' is chosen so that x has the right number of
4541        bits for a double, plus two or three extra bits that will be used
4542        in the rounding decisions.  Writing a_bits and b_bits for the
4543        number of significant bits in a and b respectively, a
4544        straightforward formula for shift is:
4545 
4546           shift = a_bits - b_bits - DBL_MANT_DIG - 2
4547 
4548        This is fine in the usual case, but if a/b is smaller than the
4549        smallest normal float then it can lead to double rounding on an
4550        IEEE 754 platform, giving incorrectly rounded results.  So we
4551        adjust the formula slightly.  The actual formula used is:
4552 
4553            shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2
4554 
4555        2. The quantity x is computed by first shifting a (left -shift bits
4556        if shift <= 0, right shift bits if shift > 0) and then dividing by
4557        b.  For both the shift and the division, we keep track of whether
4558        the result is inexact, in a flag 'inexact'; this information is
4559        needed at the rounding stage.
4560 
4561        With the choice of shift above, together with our assumption that
4562        a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows
4563        that x >= 1.
4564 
4565        3. Now x * 2**shift <= a/b < (x+1) * 2**shift.  We want to replace
4566        this with an exactly representable float of the form
4567 
4568           round(x/2**extra_bits) * 2**(extra_bits+shift).
4569 
4570        For float representability, we need x/2**extra_bits <
4571        2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP -
4572        DBL_MANT_DIG.  This translates to the condition:
4573 
4574           extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG
4575 
4576        To round, we just modify the bottom digit of x in-place; this can
4577        end up giving a digit with value > PyLONG_MASK, but that's not a
4578        problem since digits can hold values up to 2*PyLONG_MASK+1.
4579 
4580        With the original choices for shift above, extra_bits will always
4581        be 2 or 3.  Then rounding under the round-half-to-even rule, we
4582        round up iff the most significant of the extra bits is 1, and
4583        either: (a) the computation of x in step 2 had an inexact result,
4584        or (b) at least one other of the extra bits is 1, or (c) the least
4585        significant bit of x (above those to be rounded) is 1.
4586 
4587        4. Conversion to a double is straightforward; all floating-point
4588        operations involved in the conversion are exact, so there's no
4589        danger of rounding errors.
4590 
4591        5. Use ldexp(x, shift) to compute x*2**shift, the final result.
4592        The result will always be exactly representable as a double, except
4593        in the case that it overflows.  To avoid dependence on the exact
4594        behaviour of ldexp on overflow, we check for overflow before
4595        applying ldexp.  The result of ldexp is adjusted for sign before
4596        returning.
4597     */
4598 
4599     /* Reduce to case where a and b are both positive. */
4600     a_size = _PyLong_DigitCount(a);
4601     b_size = _PyLong_DigitCount(b);
4602     negate = (_PyLong_IsNegative(a)) != (_PyLong_IsNegative(b));
4603     if (b_size == 0) {
4604         PyErr_SetString(PyExc_ZeroDivisionError,
4605                         "division by zero");
4606         goto error;
4607     }
4608     if (a_size == 0)
4609         goto underflow_or_zero;
4610 
4611     /* Fast path for a and b small (exactly representable in a double).
4612        Relies on floating-point division being correctly rounded; results
4613        may be subject to double rounding on x86 machines that operate with
4614        the x87 FPU set to 64-bit precision. */
4615     a_is_small = a_size <= MANT_DIG_DIGITS ||
4616         (a_size == MANT_DIG_DIGITS+1 &&
4617          a->long_value.ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4618     b_is_small = b_size <= MANT_DIG_DIGITS ||
4619         (b_size == MANT_DIG_DIGITS+1 &&
4620          b->long_value.ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4621     if (a_is_small && b_is_small) {
4622         double da, db;
4623         da = a->long_value.ob_digit[--a_size];
4624         while (a_size > 0)
4625             da = da * PyLong_BASE + a->long_value.ob_digit[--a_size];
4626         db = b->long_value.ob_digit[--b_size];
4627         while (b_size > 0)
4628             db = db * PyLong_BASE + b->long_value.ob_digit[--b_size];
4629         result = da / db;
4630         goto success;
4631     }
4632 
4633     /* Catch obvious cases of underflow and overflow */
4634     diff = a_size - b_size;
4635     if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1)
4636         /* Extreme overflow */
4637         goto overflow;
4638     else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT)
4639         /* Extreme underflow */
4640         goto underflow_or_zero;
4641     /* Next line is now safe from overflowing a Py_ssize_t */
4642     diff = diff * PyLong_SHIFT + bit_length_digit(a->long_value.ob_digit[a_size - 1]) -
4643         bit_length_digit(b->long_value.ob_digit[b_size - 1]);
4644     /* Now diff = a_bits - b_bits. */
4645     if (diff > DBL_MAX_EXP)
4646         goto overflow;
4647     else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1)
4648         goto underflow_or_zero;
4649 
4650     /* Choose value for shift; see comments for step 1 above. */
4651     shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2;
4652 
4653     inexact = 0;
4654 
4655     /* x = abs(a * 2**-shift) */
4656     if (shift <= 0) {
4657         Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT;
4658         digit rem;
4659         /* x = a << -shift */
4660         if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) {
4661             /* In practice, it's probably impossible to end up
4662                here.  Both a and b would have to be enormous,
4663                using close to SIZE_T_MAX bytes of memory each. */
4664             PyErr_SetString(PyExc_OverflowError,
4665                             "intermediate overflow during division");
4666             goto error;
4667         }
4668         x = _PyLong_New(a_size + shift_digits + 1);
4669         if (x == NULL)
4670             goto error;
4671         for (i = 0; i < shift_digits; i++)
4672             x->long_value.ob_digit[i] = 0;
4673         rem = v_lshift(x->long_value.ob_digit + shift_digits, a->long_value.ob_digit,
4674                        a_size, -shift % PyLong_SHIFT);
4675         x->long_value.ob_digit[a_size + shift_digits] = rem;
4676     }
4677     else {
4678         Py_ssize_t shift_digits = shift / PyLong_SHIFT;
4679         digit rem;
4680         /* x = a >> shift */
4681         assert(a_size >= shift_digits);
4682         x = _PyLong_New(a_size - shift_digits);
4683         if (x == NULL)
4684             goto error;
4685         rem = v_rshift(x->long_value.ob_digit, a->long_value.ob_digit + shift_digits,
4686                        a_size - shift_digits, shift % PyLong_SHIFT);
4687         /* set inexact if any of the bits shifted out is nonzero */
4688         if (rem)
4689             inexact = 1;
4690         while (!inexact && shift_digits > 0)
4691             if (a->long_value.ob_digit[--shift_digits])
4692                 inexact = 1;
4693     }
4694     long_normalize(x);
4695     x_size = _PyLong_SignedDigitCount(x);
4696 
4697     /* x //= b. If the remainder is nonzero, set inexact.  We own the only
4698        reference to x, so it's safe to modify it in-place. */
4699     if (b_size == 1) {
4700         digit rem = inplace_divrem1(x->long_value.ob_digit, x->long_value.ob_digit, x_size,
4701                               b->long_value.ob_digit[0]);
4702         long_normalize(x);
4703         if (rem)
4704             inexact = 1;
4705     }
4706     else {
4707         PyLongObject *div, *rem;
4708         div = x_divrem(x, b, &rem);
4709         Py_SETREF(x, div);
4710         if (x == NULL)
4711             goto error;
4712         if (!_PyLong_IsZero(rem))
4713             inexact = 1;
4714         Py_DECREF(rem);
4715     }
4716     x_size = _PyLong_DigitCount(x);
4717     assert(x_size > 0); /* result of division is never zero */
4718     x_bits = (x_size-1)*PyLong_SHIFT+bit_length_digit(x->long_value.ob_digit[x_size-1]);
4719 
4720     /* The number of extra bits that have to be rounded away. */
4721     extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG;
4722     assert(extra_bits == 2 || extra_bits == 3);
4723 
4724     /* Round by directly modifying the low digit of x. */
4725     mask = (digit)1 << (extra_bits - 1);
4726     low = x->long_value.ob_digit[0] | inexact;
4727     if ((low & mask) && (low & (3U*mask-1U)))
4728         low += mask;
4729     x->long_value.ob_digit[0] = low & ~(2U*mask-1U);
4730 
4731     /* Convert x to a double dx; the conversion is exact. */
4732     dx = x->long_value.ob_digit[--x_size];
4733     while (x_size > 0)
4734         dx = dx * PyLong_BASE + x->long_value.ob_digit[--x_size];
4735     Py_DECREF(x);
4736 
4737     /* Check whether ldexp result will overflow a double. */
4738     if (shift + x_bits >= DBL_MAX_EXP &&
4739         (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits)))
4740         goto overflow;
4741     result = ldexp(dx, (int)shift);
4742 
4743   success:
4744     return PyFloat_FromDouble(negate ? -result : result);
4745 
4746   underflow_or_zero:
4747     return PyFloat_FromDouble(negate ? -0.0 : 0.0);
4748 
4749   overflow:
4750     PyErr_SetString(PyExc_OverflowError,
4751                     "integer division result too large for a float");
4752   error:
4753     return NULL;
4754 }
4755 
4756 static PyObject *
long_mod(PyObject * a,PyObject * b)4757 long_mod(PyObject *a, PyObject *b)
4758 {
4759     PyLongObject *mod;
4760 
4761     CHECK_BINOP(a, b);
4762 
4763     if (l_mod((PyLongObject*)a, (PyLongObject*)b, &mod) < 0)
4764         mod = NULL;
4765     return (PyObject *)mod;
4766 }
4767 
4768 static PyObject *
long_divmod(PyObject * a,PyObject * b)4769 long_divmod(PyObject *a, PyObject *b)
4770 {
4771     PyLongObject *div, *mod;
4772     PyObject *z;
4773 
4774     CHECK_BINOP(a, b);
4775 
4776     if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
4777         return NULL;
4778     }
4779     z = PyTuple_New(2);
4780     if (z != NULL) {
4781         PyTuple_SET_ITEM(z, 0, (PyObject *) div);
4782         PyTuple_SET_ITEM(z, 1, (PyObject *) mod);
4783     }
4784     else {
4785         Py_DECREF(div);
4786         Py_DECREF(mod);
4787     }
4788     return z;
4789 }
4790 
4791 
4792 /* Compute an inverse to a modulo n, or raise ValueError if a is not
4793    invertible modulo n. Assumes n is positive. The inverse returned
4794    is whatever falls out of the extended Euclidean algorithm: it may
4795    be either positive or negative, but will be smaller than n in
4796    absolute value.
4797 
4798    Pure Python equivalent for long_invmod:
4799 
4800         def invmod(a, n):
4801             b, c = 1, 0
4802             while n:
4803                 q, r = divmod(a, n)
4804                 a, b, c, n = n, c, b - q*c, r
4805 
4806             # at this point a is the gcd of the original inputs
4807             if a == 1:
4808                 return b
4809             raise ValueError("Not invertible")
4810 */
4811 
4812 static PyLongObject *
long_invmod(PyLongObject * a,PyLongObject * n)4813 long_invmod(PyLongObject *a, PyLongObject *n)
4814 {
4815     PyLongObject *b, *c;
4816 
4817     /* Should only ever be called for positive n */
4818     assert(_PyLong_IsPositive(n));
4819 
4820     b = (PyLongObject *)PyLong_FromLong(1L);
4821     if (b == NULL) {
4822         return NULL;
4823     }
4824     c = (PyLongObject *)PyLong_FromLong(0L);
4825     if (c == NULL) {
4826         Py_DECREF(b);
4827         return NULL;
4828     }
4829     Py_INCREF(a);
4830     Py_INCREF(n);
4831 
4832     /* references now owned: a, b, c, n */
4833     while (!_PyLong_IsZero(n)) {
4834         PyLongObject *q, *r, *s, *t;
4835 
4836         if (l_divmod(a, n, &q, &r) == -1) {
4837             goto Error;
4838         }
4839         Py_SETREF(a, n);
4840         n = r;
4841         t = (PyLongObject *)long_mul(q, c);
4842         Py_DECREF(q);
4843         if (t == NULL) {
4844             goto Error;
4845         }
4846         s = (PyLongObject *)long_sub(b, t);
4847         Py_DECREF(t);
4848         if (s == NULL) {
4849             goto Error;
4850         }
4851         Py_SETREF(b, c);
4852         c = s;
4853     }
4854     /* references now owned: a, b, c, n */
4855 
4856     Py_DECREF(c);
4857     Py_DECREF(n);
4858     if (long_compare(a, (PyLongObject *)_PyLong_GetOne())) {
4859         /* a != 1; we don't have an inverse. */
4860         Py_DECREF(a);
4861         Py_DECREF(b);
4862         PyErr_SetString(PyExc_ValueError,
4863                         "base is not invertible for the given modulus");
4864         return NULL;
4865     }
4866     else {
4867         /* a == 1; b gives an inverse modulo n */
4868         Py_DECREF(a);
4869         return b;
4870     }
4871 
4872   Error:
4873     Py_DECREF(a);
4874     Py_DECREF(b);
4875     Py_DECREF(c);
4876     Py_DECREF(n);
4877     return NULL;
4878 }
4879 
4880 
4881 /* pow(v, w, x) */
4882 static PyObject *
long_pow(PyObject * v,PyObject * w,PyObject * x)4883 long_pow(PyObject *v, PyObject *w, PyObject *x)
4884 {
4885     PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
4886     int negativeOutput = 0;  /* if x<0 return negative output */
4887 
4888     PyLongObject *z = NULL;  /* accumulated result */
4889     Py_ssize_t i, j;             /* counters */
4890     PyLongObject *temp = NULL;
4891     PyLongObject *a2 = NULL; /* may temporarily hold a**2 % c */
4892 
4893     /* k-ary values.  If the exponent is large enough, table is
4894      * precomputed so that table[i] == a**(2*i+1) % c for i in
4895      * range(EXP_TABLE_LEN).
4896      * Note: this is uninitialized stack trash: don't pay to set it to known
4897      * values unless it's needed. Instead ensure that num_table_entries is
4898      * set to the number of entries actually filled whenever a branch to the
4899      * Error or Done labels is possible.
4900      */
4901     PyLongObject *table[EXP_TABLE_LEN];
4902     Py_ssize_t num_table_entries = 0;
4903 
4904     /* a, b, c = v, w, x */
4905     CHECK_BINOP(v, w);
4906     a = (PyLongObject*)Py_NewRef(v);
4907     b = (PyLongObject*)Py_NewRef(w);
4908     if (PyLong_Check(x)) {
4909         c = (PyLongObject *)Py_NewRef(x);
4910     }
4911     else if (x == Py_None)
4912         c = NULL;
4913     else {
4914         Py_DECREF(a);
4915         Py_DECREF(b);
4916         Py_RETURN_NOTIMPLEMENTED;
4917     }
4918 
4919     if (_PyLong_IsNegative(b) && c == NULL) {
4920         /* if exponent is negative and there's no modulus:
4921                return a float.  This works because we know
4922                that this calls float_pow() which converts its
4923                arguments to double. */
4924         Py_DECREF(a);
4925         Py_DECREF(b);
4926         return PyFloat_Type.tp_as_number->nb_power(v, w, x);
4927     }
4928 
4929     if (c) {
4930         /* if modulus == 0:
4931                raise ValueError() */
4932         if (_PyLong_IsZero(c)) {
4933             PyErr_SetString(PyExc_ValueError,
4934                             "pow() 3rd argument cannot be 0");
4935             goto Error;
4936         }
4937 
4938         /* if modulus < 0:
4939                negativeOutput = True
4940                modulus = -modulus */
4941         if (_PyLong_IsNegative(c)) {
4942             negativeOutput = 1;
4943             temp = (PyLongObject *)_PyLong_Copy(c);
4944             if (temp == NULL)
4945                 goto Error;
4946             Py_SETREF(c, temp);
4947             temp = NULL;
4948             _PyLong_Negate(&c);
4949             if (c == NULL)
4950                 goto Error;
4951         }
4952 
4953         /* if modulus == 1:
4954                return 0 */
4955         if (_PyLong_IsNonNegativeCompact(c) && (c->long_value.ob_digit[0] == 1)) {
4956             z = (PyLongObject *)PyLong_FromLong(0L);
4957             goto Done;
4958         }
4959 
4960         /* if exponent is negative, negate the exponent and
4961            replace the base with a modular inverse */
4962         if (_PyLong_IsNegative(b)) {
4963             temp = (PyLongObject *)_PyLong_Copy(b);
4964             if (temp == NULL)
4965                 goto Error;
4966             Py_SETREF(b, temp);
4967             temp = NULL;
4968             _PyLong_Negate(&b);
4969             if (b == NULL)
4970                 goto Error;
4971 
4972             temp = long_invmod(a, c);
4973             if (temp == NULL)
4974                 goto Error;
4975             Py_SETREF(a, temp);
4976             temp = NULL;
4977         }
4978 
4979         /* Reduce base by modulus in some cases:
4980            1. If base < 0.  Forcing the base non-negative makes things easier.
4981            2. If base is obviously larger than the modulus.  The "small
4982               exponent" case later can multiply directly by base repeatedly,
4983               while the "large exponent" case multiplies directly by base 31
4984               times.  It can be unboundedly faster to multiply by
4985               base % modulus instead.
4986            We could _always_ do this reduction, but l_mod() isn't cheap,
4987            so we only do it when it buys something. */
4988         if (_PyLong_IsNegative(a) || _PyLong_DigitCount(a) > _PyLong_DigitCount(c)) {
4989             if (l_mod(a, c, &temp) < 0)
4990                 goto Error;
4991             Py_SETREF(a, temp);
4992             temp = NULL;
4993         }
4994     }
4995 
4996     /* At this point a, b, and c are guaranteed non-negative UNLESS
4997        c is NULL, in which case a may be negative. */
4998 
4999     z = (PyLongObject *)PyLong_FromLong(1L);
5000     if (z == NULL)
5001         goto Error;
5002 
5003     /* Perform a modular reduction, X = X % c, but leave X alone if c
5004      * is NULL.
5005      */
5006 #define REDUCE(X)                                       \
5007     do {                                                \
5008         if (c != NULL) {                                \
5009             if (l_mod(X, c, &temp) < 0)                 \
5010                 goto Error;                             \
5011             Py_XDECREF(X);                              \
5012             X = temp;                                   \
5013             temp = NULL;                                \
5014         }                                               \
5015     } while(0)
5016 
5017     /* Multiply two values, then reduce the result:
5018        result = X*Y % c.  If c is NULL, skip the mod. */
5019 #define MULT(X, Y, result)                      \
5020     do {                                        \
5021         temp = (PyLongObject *)long_mul(X, Y);  \
5022         if (temp == NULL)                       \
5023             goto Error;                         \
5024         Py_XDECREF(result);                     \
5025         result = temp;                          \
5026         temp = NULL;                            \
5027         REDUCE(result);                         \
5028     } while(0)
5029 
5030     i = _PyLong_SignedDigitCount(b);
5031     digit bi = i ? b->long_value.ob_digit[i-1] : 0;
5032     digit bit;
5033     if (i <= 1 && bi <= 3) {
5034         /* aim for minimal overhead */
5035         if (bi >= 2) {
5036             MULT(a, a, z);
5037             if (bi == 3) {
5038                 MULT(z, a, z);
5039             }
5040         }
5041         else if (bi == 1) {
5042             /* Multiplying by 1 serves two purposes: if `a` is of an int
5043              * subclass, makes the result an int (e.g., pow(False, 1) returns
5044              * 0 instead of False), and potentially reduces `a` by the modulus.
5045              */
5046             MULT(a, z, z);
5047         }
5048         /* else bi is 0, and z==1 is correct */
5049     }
5050     else if (i <= HUGE_EXP_CUTOFF / PyLong_SHIFT ) {
5051         /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
5052         /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf    */
5053 
5054         /* Find the first significant exponent bit. Search right to left
5055          * because we're primarily trying to cut overhead for small powers.
5056          */
5057         assert(bi);  /* else there is no significant bit */
5058         Py_SETREF(z, (PyLongObject*)Py_NewRef(a));
5059         for (bit = 2; ; bit <<= 1) {
5060             if (bit > bi) { /* found the first bit */
5061                 assert((bi & bit) == 0);
5062                 bit >>= 1;
5063                 assert(bi & bit);
5064                 break;
5065             }
5066         }
5067         for (--i, bit >>= 1;;) {
5068             for (; bit != 0; bit >>= 1) {
5069                 MULT(z, z, z);
5070                 if (bi & bit) {
5071                     MULT(z, a, z);
5072                 }
5073             }
5074             if (--i < 0) {
5075                 break;
5076             }
5077             bi = b->long_value.ob_digit[i];
5078             bit = (digit)1 << (PyLong_SHIFT-1);
5079         }
5080     }
5081     else {
5082         /* Left-to-right k-ary sliding window exponentiation
5083          * (Handbook of Applied Cryptography (HAC) Algorithm 14.85)
5084          */
5085         table[0] = (PyLongObject*)Py_NewRef(a);
5086         num_table_entries = 1;
5087         MULT(a, a, a2);
5088         /* table[i] == a**(2*i + 1) % c */
5089         for (i = 1; i < EXP_TABLE_LEN; ++i) {
5090             table[i] = NULL; /* must set to known value for MULT */
5091             MULT(table[i-1], a2, table[i]);
5092             ++num_table_entries; /* incremented iff MULT succeeded */
5093         }
5094         Py_CLEAR(a2);
5095 
5096         /* Repeatedly extract the next (no more than) EXP_WINDOW_SIZE bits
5097          * into `pending`, starting with the next 1 bit.  The current bit
5098          * length of `pending` is `blen`.
5099          */
5100         int pending = 0, blen = 0;
5101 #define ABSORB_PENDING  do { \
5102             int ntz = 0; /* number of trailing zeroes in `pending` */ \
5103             assert(pending && blen); \
5104             assert(pending >> (blen - 1)); \
5105             assert(pending >> blen == 0); \
5106             while ((pending & 1) == 0) { \
5107                 ++ntz; \
5108                 pending >>= 1; \
5109             } \
5110             assert(ntz < blen); \
5111             blen -= ntz; \
5112             do { \
5113                 MULT(z, z, z); \
5114             } while (--blen); \
5115             MULT(z, table[pending >> 1], z); \
5116             while (ntz-- > 0) \
5117                 MULT(z, z, z); \
5118             assert(blen == 0); \
5119             pending = 0; \
5120         } while(0)
5121 
5122         for (i = _PyLong_SignedDigitCount(b) - 1; i >= 0; --i) {
5123             const digit bi = b->long_value.ob_digit[i];
5124             for (j = PyLong_SHIFT - 1; j >= 0; --j) {
5125                 const int bit = (bi >> j) & 1;
5126                 pending = (pending << 1) | bit;
5127                 if (pending) {
5128                     ++blen;
5129                     if (blen == EXP_WINDOW_SIZE)
5130                         ABSORB_PENDING;
5131                 }
5132                 else /* absorb strings of 0 bits */
5133                     MULT(z, z, z);
5134             }
5135         }
5136         if (pending)
5137             ABSORB_PENDING;
5138     }
5139 
5140     if (negativeOutput && !_PyLong_IsZero(z)) {
5141         temp = (PyLongObject *)long_sub(z, c);
5142         if (temp == NULL)
5143             goto Error;
5144         Py_SETREF(z, temp);
5145         temp = NULL;
5146     }
5147     goto Done;
5148 
5149   Error:
5150     Py_CLEAR(z);
5151     /* fall through */
5152   Done:
5153     for (i = 0; i < num_table_entries; ++i)
5154         Py_DECREF(table[i]);
5155     Py_DECREF(a);
5156     Py_DECREF(b);
5157     Py_XDECREF(c);
5158     Py_XDECREF(a2);
5159     Py_XDECREF(temp);
5160     return (PyObject *)z;
5161 }
5162 
5163 static PyObject *
long_invert(PyLongObject * v)5164 long_invert(PyLongObject *v)
5165 {
5166     /* Implement ~x as -(x+1) */
5167     PyLongObject *x;
5168     if (_PyLong_IsCompact(v))
5169         return _PyLong_FromSTwoDigits(~medium_value(v));
5170     x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_GetOne());
5171     if (x == NULL)
5172         return NULL;
5173     _PyLong_Negate(&x);
5174     /* No need for maybe_small_long here, since any small longs
5175        will have been caught in the _PyLong_IsCompact() fast path. */
5176     return (PyObject *)x;
5177 }
5178 
5179 static PyObject *
long_neg(PyLongObject * v)5180 long_neg(PyLongObject *v)
5181 {
5182     PyLongObject *z;
5183     if (_PyLong_IsCompact(v))
5184         return _PyLong_FromSTwoDigits(-medium_value(v));
5185     z = (PyLongObject *)_PyLong_Copy(v);
5186     if (z != NULL)
5187         _PyLong_FlipSign(z);
5188     return (PyObject *)z;
5189 }
5190 
5191 static PyObject *
long_abs(PyLongObject * v)5192 long_abs(PyLongObject *v)
5193 {
5194     if (_PyLong_IsNegative(v))
5195         return long_neg(v);
5196     else
5197         return long_long((PyObject *)v);
5198 }
5199 
5200 static int
long_bool(PyLongObject * v)5201 long_bool(PyLongObject *v)
5202 {
5203     return !_PyLong_IsZero(v);
5204 }
5205 
5206 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
5207 static int
divmod_shift(PyObject * shiftby,Py_ssize_t * wordshift,digit * remshift)5208 divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift)
5209 {
5210     assert(PyLong_Check(shiftby));
5211     assert(!_PyLong_IsNegative((PyLongObject *)shiftby));
5212     Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby);
5213     if (lshiftby >= 0) {
5214         *wordshift = lshiftby / PyLong_SHIFT;
5215         *remshift = lshiftby % PyLong_SHIFT;
5216         return 0;
5217     }
5218     /* PyLong_Check(shiftby) is true and shiftby is not negative, so it must
5219        be that PyLong_AsSsize_t raised an OverflowError. */
5220     assert(PyErr_ExceptionMatches(PyExc_OverflowError));
5221     PyErr_Clear();
5222     PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift);
5223     if (wordshift_obj == NULL) {
5224         return -1;
5225     }
5226     *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj);
5227     Py_DECREF(wordshift_obj);
5228     if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) {
5229         return 0;
5230     }
5231     PyErr_Clear();
5232     /* Clip the value.  With such large wordshift the right shift
5233        returns 0 and the left shift raises an error in _PyLong_New(). */
5234     *wordshift = PY_SSIZE_T_MAX / sizeof(digit);
5235     *remshift = 0;
5236     return 0;
5237 }
5238 
5239 /* Inner function for both long_rshift and _PyLong_Rshift, shifting an
5240    integer right by PyLong_SHIFT*wordshift + remshift bits.
5241    wordshift should be nonnegative. */
5242 
5243 static PyObject *
long_rshift1(PyLongObject * a,Py_ssize_t wordshift,digit remshift)5244 long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
5245 {
5246     PyLongObject *z = NULL;
5247     Py_ssize_t newsize, hishift, size_a;
5248     twodigits accum;
5249     int a_negative;
5250 
5251     /* Total number of bits shifted must be nonnegative. */
5252     assert(wordshift >= 0);
5253     assert(remshift < PyLong_SHIFT);
5254 
5255     /* Fast path for small a. */
5256     if (_PyLong_IsCompact(a)) {
5257         stwodigits m, x;
5258         digit shift;
5259         m = medium_value(a);
5260         shift = wordshift == 0 ? remshift : PyLong_SHIFT;
5261         x = m < 0 ? ~(~m >> shift) : m >> shift;
5262         return _PyLong_FromSTwoDigits(x);
5263     }
5264 
5265     a_negative = _PyLong_IsNegative(a);
5266     size_a = _PyLong_DigitCount(a);
5267 
5268     if (a_negative) {
5269         /* For negative 'a', adjust so that 0 < remshift <= PyLong_SHIFT,
5270            while keeping PyLong_SHIFT*wordshift + remshift the same. This
5271            ensures that 'newsize' is computed correctly below. */
5272         if (remshift == 0) {
5273             if (wordshift == 0) {
5274                 /* Can only happen if the original shift was 0. */
5275                 return long_long((PyObject *)a);
5276             }
5277             remshift = PyLong_SHIFT;
5278             --wordshift;
5279         }
5280     }
5281 
5282     assert(wordshift >= 0);
5283     newsize = size_a - wordshift;
5284     if (newsize <= 0) {
5285         /* Shifting all the bits of 'a' out gives either -1 or 0. */
5286         return PyLong_FromLong(-a_negative);
5287     }
5288     z = _PyLong_New(newsize);
5289     if (z == NULL) {
5290         return NULL;
5291     }
5292     hishift = PyLong_SHIFT - remshift;
5293 
5294     accum = a->long_value.ob_digit[wordshift];
5295     if (a_negative) {
5296         /*
5297             For a positive integer a and nonnegative shift, we have:
5298 
5299                 (-a) >> shift == -((a + 2**shift - 1) >> shift).
5300 
5301             In the addition `a + (2**shift - 1)`, the low `wordshift` digits of
5302             `2**shift - 1` all have value `PyLong_MASK`, so we get a carry out
5303             from the bottom `wordshift` digits when at least one of the least
5304             significant `wordshift` digits of `a` is nonzero. Digit `wordshift`
5305             of `2**shift - 1` has value `PyLong_MASK >> hishift`.
5306         */
5307         _PyLong_SetSignAndDigitCount(z, -1, newsize);
5308 
5309         digit sticky = 0;
5310         for (Py_ssize_t j = 0; j < wordshift; j++) {
5311             sticky |= a->long_value.ob_digit[j];
5312         }
5313         accum += (PyLong_MASK >> hishift) + (digit)(sticky != 0);
5314     }
5315 
5316     accum >>= remshift;
5317     for (Py_ssize_t i = 0, j = wordshift + 1; j < size_a; i++, j++) {
5318         accum += (twodigits)a->long_value.ob_digit[j] << hishift;
5319         z->long_value.ob_digit[i] = (digit)(accum & PyLong_MASK);
5320         accum >>= PyLong_SHIFT;
5321     }
5322     assert(accum <= PyLong_MASK);
5323     z->long_value.ob_digit[newsize - 1] = (digit)accum;
5324 
5325     z = maybe_small_long(long_normalize(z));
5326     return (PyObject *)z;
5327 }
5328 
5329 static PyObject *
long_rshift(PyObject * a,PyObject * b)5330 long_rshift(PyObject *a, PyObject *b)
5331 {
5332     Py_ssize_t wordshift;
5333     digit remshift;
5334 
5335     CHECK_BINOP(a, b);
5336 
5337     if (_PyLong_IsNegative((PyLongObject *)b)) {
5338         PyErr_SetString(PyExc_ValueError, "negative shift count");
5339         return NULL;
5340     }
5341     if (_PyLong_IsZero((PyLongObject *)a)) {
5342         return PyLong_FromLong(0);
5343     }
5344     if (divmod_shift(b, &wordshift, &remshift) < 0)
5345         return NULL;
5346     return long_rshift1((PyLongObject *)a, wordshift, remshift);
5347 }
5348 
5349 /* Return a >> shiftby. */
5350 PyObject *
_PyLong_Rshift(PyObject * a,size_t shiftby)5351 _PyLong_Rshift(PyObject *a, size_t shiftby)
5352 {
5353     Py_ssize_t wordshift;
5354     digit remshift;
5355 
5356     assert(PyLong_Check(a));
5357     if (_PyLong_IsZero((PyLongObject *)a)) {
5358         return PyLong_FromLong(0);
5359     }
5360     wordshift = shiftby / PyLong_SHIFT;
5361     remshift = shiftby % PyLong_SHIFT;
5362     return long_rshift1((PyLongObject *)a, wordshift, remshift);
5363 }
5364 
5365 static PyObject *
long_lshift1(PyLongObject * a,Py_ssize_t wordshift,digit remshift)5366 long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
5367 {
5368     PyLongObject *z = NULL;
5369     Py_ssize_t oldsize, newsize, i, j;
5370     twodigits accum;
5371 
5372     if (wordshift == 0 && _PyLong_IsCompact(a)) {
5373         stwodigits m = medium_value(a);
5374         // bypass undefined shift operator behavior
5375         stwodigits x = m < 0 ? -(-m << remshift) : m << remshift;
5376         return _PyLong_FromSTwoDigits(x);
5377     }
5378 
5379     oldsize = _PyLong_DigitCount(a);
5380     newsize = oldsize + wordshift;
5381     if (remshift)
5382         ++newsize;
5383     z = _PyLong_New(newsize);
5384     if (z == NULL)
5385         return NULL;
5386     if (_PyLong_IsNegative(a)) {
5387         assert(Py_REFCNT(z) == 1);
5388         _PyLong_FlipSign(z);
5389     }
5390     for (i = 0; i < wordshift; i++)
5391         z->long_value.ob_digit[i] = 0;
5392     accum = 0;
5393     for (j = 0; j < oldsize; i++, j++) {
5394         accum |= (twodigits)a->long_value.ob_digit[j] << remshift;
5395         z->long_value.ob_digit[i] = (digit)(accum & PyLong_MASK);
5396         accum >>= PyLong_SHIFT;
5397     }
5398     if (remshift)
5399         z->long_value.ob_digit[newsize-1] = (digit)accum;
5400     else
5401         assert(!accum);
5402     z = long_normalize(z);
5403     return (PyObject *) maybe_small_long(z);
5404 }
5405 
5406 static PyObject *
long_lshift(PyObject * a,PyObject * b)5407 long_lshift(PyObject *a, PyObject *b)
5408 {
5409     Py_ssize_t wordshift;
5410     digit remshift;
5411 
5412     CHECK_BINOP(a, b);
5413 
5414     if (_PyLong_IsNegative((PyLongObject *)b)) {
5415         PyErr_SetString(PyExc_ValueError, "negative shift count");
5416         return NULL;
5417     }
5418     if (_PyLong_IsZero((PyLongObject *)a)) {
5419         return PyLong_FromLong(0);
5420     }
5421     if (divmod_shift(b, &wordshift, &remshift) < 0)
5422         return NULL;
5423     return long_lshift1((PyLongObject *)a, wordshift, remshift);
5424 }
5425 
5426 /* Return a << shiftby. */
5427 PyObject *
_PyLong_Lshift(PyObject * a,size_t shiftby)5428 _PyLong_Lshift(PyObject *a, size_t shiftby)
5429 {
5430     Py_ssize_t wordshift;
5431     digit remshift;
5432 
5433     assert(PyLong_Check(a));
5434     if (_PyLong_IsZero((PyLongObject *)a)) {
5435         return PyLong_FromLong(0);
5436     }
5437     wordshift = shiftby / PyLong_SHIFT;
5438     remshift = shiftby % PyLong_SHIFT;
5439     return long_lshift1((PyLongObject *)a, wordshift, remshift);
5440 }
5441 
5442 /* Compute two's complement of digit vector a[0:m], writing result to
5443    z[0:m].  The digit vector a need not be normalized, but should not
5444    be entirely zero.  a and z may point to the same digit vector. */
5445 
5446 static void
v_complement(digit * z,digit * a,Py_ssize_t m)5447 v_complement(digit *z, digit *a, Py_ssize_t m)
5448 {
5449     Py_ssize_t i;
5450     digit carry = 1;
5451     for (i = 0; i < m; ++i) {
5452         carry += a[i] ^ PyLong_MASK;
5453         z[i] = carry & PyLong_MASK;
5454         carry >>= PyLong_SHIFT;
5455     }
5456     assert(carry == 0);
5457 }
5458 
5459 /* Bitwise and/xor/or operations */
5460 
5461 static PyObject *
long_bitwise(PyLongObject * a,char op,PyLongObject * b)5462 long_bitwise(PyLongObject *a,
5463              char op,  /* '&', '|', '^' */
5464              PyLongObject *b)
5465 {
5466     int nega, negb, negz;
5467     Py_ssize_t size_a, size_b, size_z, i;
5468     PyLongObject *z;
5469 
5470     /* Bitwise operations for negative numbers operate as though
5471        on a two's complement representation.  So convert arguments
5472        from sign-magnitude to two's complement, and convert the
5473        result back to sign-magnitude at the end. */
5474 
5475     /* If a is negative, replace it by its two's complement. */
5476     size_a = _PyLong_DigitCount(a);
5477     nega = _PyLong_IsNegative(a);
5478     if (nega) {
5479         z = _PyLong_New(size_a);
5480         if (z == NULL)
5481             return NULL;
5482         v_complement(z->long_value.ob_digit, a->long_value.ob_digit, size_a);
5483         a = z;
5484     }
5485     else
5486         /* Keep reference count consistent. */
5487         Py_INCREF(a);
5488 
5489     /* Same for b. */
5490     size_b = _PyLong_DigitCount(b);
5491     negb = _PyLong_IsNegative(b);
5492     if (negb) {
5493         z = _PyLong_New(size_b);
5494         if (z == NULL) {
5495             Py_DECREF(a);
5496             return NULL;
5497         }
5498         v_complement(z->long_value.ob_digit, b->long_value.ob_digit, size_b);
5499         b = z;
5500     }
5501     else
5502         Py_INCREF(b);
5503 
5504     /* Swap a and b if necessary to ensure size_a >= size_b. */
5505     if (size_a < size_b) {
5506         z = a; a = b; b = z;
5507         size_z = size_a; size_a = size_b; size_b = size_z;
5508         negz = nega; nega = negb; negb = negz;
5509     }
5510 
5511     /* JRH: The original logic here was to allocate the result value (z)
5512        as the longer of the two operands.  However, there are some cases
5513        where the result is guaranteed to be shorter than that: AND of two
5514        positives, OR of two negatives: use the shorter number.  AND with
5515        mixed signs: use the positive number.  OR with mixed signs: use the
5516        negative number.
5517     */
5518     switch (op) {
5519     case '^':
5520         negz = nega ^ negb;
5521         size_z = size_a;
5522         break;
5523     case '&':
5524         negz = nega & negb;
5525         size_z = negb ? size_a : size_b;
5526         break;
5527     case '|':
5528         negz = nega | negb;
5529         size_z = negb ? size_b : size_a;
5530         break;
5531     default:
5532         Py_UNREACHABLE();
5533     }
5534 
5535     /* We allow an extra digit if z is negative, to make sure that
5536        the final two's complement of z doesn't overflow. */
5537     z = _PyLong_New(size_z + negz);
5538     if (z == NULL) {
5539         Py_DECREF(a);
5540         Py_DECREF(b);
5541         return NULL;
5542     }
5543 
5544     /* Compute digits for overlap of a and b. */
5545     switch(op) {
5546     case '&':
5547         for (i = 0; i < size_b; ++i)
5548             z->long_value.ob_digit[i] = a->long_value.ob_digit[i] & b->long_value.ob_digit[i];
5549         break;
5550     case '|':
5551         for (i = 0; i < size_b; ++i)
5552             z->long_value.ob_digit[i] = a->long_value.ob_digit[i] | b->long_value.ob_digit[i];
5553         break;
5554     case '^':
5555         for (i = 0; i < size_b; ++i)
5556             z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ b->long_value.ob_digit[i];
5557         break;
5558     default:
5559         Py_UNREACHABLE();
5560     }
5561 
5562     /* Copy any remaining digits of a, inverting if necessary. */
5563     if (op == '^' && negb)
5564         for (; i < size_z; ++i)
5565             z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ PyLong_MASK;
5566     else if (i < size_z)
5567         memcpy(&z->long_value.ob_digit[i], &a->long_value.ob_digit[i],
5568                (size_z-i)*sizeof(digit));
5569 
5570     /* Complement result if negative. */
5571     if (negz) {
5572         _PyLong_FlipSign(z);
5573         z->long_value.ob_digit[size_z] = PyLong_MASK;
5574         v_complement(z->long_value.ob_digit, z->long_value.ob_digit, size_z+1);
5575     }
5576 
5577     Py_DECREF(a);
5578     Py_DECREF(b);
5579     return (PyObject *)maybe_small_long(long_normalize(z));
5580 }
5581 
5582 static PyObject *
long_and(PyObject * a,PyObject * b)5583 long_and(PyObject *a, PyObject *b)
5584 {
5585     CHECK_BINOP(a, b);
5586     PyLongObject *x = (PyLongObject*)a;
5587     PyLongObject *y = (PyLongObject*)b;
5588     if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
5589         return _PyLong_FromSTwoDigits(medium_value(x) & medium_value(y));
5590     }
5591     return long_bitwise(x, '&', y);
5592 }
5593 
5594 static PyObject *
long_xor(PyObject * a,PyObject * b)5595 long_xor(PyObject *a, PyObject *b)
5596 {
5597     CHECK_BINOP(a, b);
5598     PyLongObject *x = (PyLongObject*)a;
5599     PyLongObject *y = (PyLongObject*)b;
5600     if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
5601         return _PyLong_FromSTwoDigits(medium_value(x) ^ medium_value(y));
5602     }
5603     return long_bitwise(x, '^', y);
5604 }
5605 
5606 static PyObject *
long_or(PyObject * a,PyObject * b)5607 long_or(PyObject *a, PyObject *b)
5608 {
5609     CHECK_BINOP(a, b);
5610     PyLongObject *x = (PyLongObject*)a;
5611     PyLongObject *y = (PyLongObject*)b;
5612     if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
5613         return _PyLong_FromSTwoDigits(medium_value(x) | medium_value(y));
5614     }
5615     return long_bitwise(x, '|', y);
5616 }
5617 
5618 static PyObject *
long_long(PyObject * v)5619 long_long(PyObject *v)
5620 {
5621     if (PyLong_CheckExact(v)) {
5622         return Py_NewRef(v);
5623     }
5624     else {
5625         return _PyLong_Copy((PyLongObject *)v);
5626     }
5627 }
5628 
5629 PyObject *
_PyLong_GCD(PyObject * aarg,PyObject * barg)5630 _PyLong_GCD(PyObject *aarg, PyObject *barg)
5631 {
5632     PyLongObject *a, *b, *c = NULL, *d = NULL, *r;
5633     stwodigits x, y, q, s, t, c_carry, d_carry;
5634     stwodigits A, B, C, D, T;
5635     int nbits, k;
5636     digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end;
5637 
5638     a = (PyLongObject *)aarg;
5639     b = (PyLongObject *)barg;
5640     if (_PyLong_DigitCount(a) <= 2 && _PyLong_DigitCount(b) <= 2) {
5641         Py_INCREF(a);
5642         Py_INCREF(b);
5643         goto simple;
5644     }
5645 
5646     /* Initial reduction: make sure that 0 <= b <= a. */
5647     a = (PyLongObject *)long_abs(a);
5648     if (a == NULL)
5649         return NULL;
5650     b = (PyLongObject *)long_abs(b);
5651     if (b == NULL) {
5652         Py_DECREF(a);
5653         return NULL;
5654     }
5655     if (long_compare(a, b) < 0) {
5656         r = a;
5657         a = b;
5658         b = r;
5659     }
5660     /* We now own references to a and b */
5661 
5662     Py_ssize_t size_a, size_b, alloc_a, alloc_b;
5663     alloc_a = _PyLong_DigitCount(a);
5664     alloc_b = _PyLong_DigitCount(b);
5665     /* reduce until a fits into 2 digits */
5666     while ((size_a = _PyLong_DigitCount(a)) > 2) {
5667         nbits = bit_length_digit(a->long_value.ob_digit[size_a-1]);
5668         /* extract top 2*PyLong_SHIFT bits of a into x, along with
5669            corresponding bits of b into y */
5670         size_b = _PyLong_DigitCount(b);
5671         assert(size_b <= size_a);
5672         if (size_b == 0) {
5673             if (size_a < alloc_a) {
5674                 r = (PyLongObject *)_PyLong_Copy(a);
5675                 Py_DECREF(a);
5676             }
5677             else
5678                 r = a;
5679             Py_DECREF(b);
5680             Py_XDECREF(c);
5681             Py_XDECREF(d);
5682             return (PyObject *)r;
5683         }
5684         x = (((twodigits)a->long_value.ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) |
5685              ((twodigits)a->long_value.ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) |
5686              (a->long_value.ob_digit[size_a-3] >> nbits));
5687 
5688         y = ((size_b >= size_a - 2 ? b->long_value.ob_digit[size_a-3] >> nbits : 0) |
5689              (size_b >= size_a - 1 ? (twodigits)b->long_value.ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) |
5690              (size_b >= size_a ? (twodigits)b->long_value.ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0));
5691 
5692         /* inner loop of Lehmer's algorithm; A, B, C, D never grow
5693            larger than PyLong_MASK during the algorithm. */
5694         A = 1; B = 0; C = 0; D = 1;
5695         for (k=0;; k++) {
5696             if (y-C == 0)
5697                 break;
5698             q = (x+(A-1))/(y-C);
5699             s = B+q*D;
5700             t = x-q*y;
5701             if (s > t)
5702                 break;
5703             x = y; y = t;
5704             t = A+q*C; A = D; B = C; C = s; D = t;
5705         }
5706 
5707         if (k == 0) {
5708             /* no progress; do a Euclidean step */
5709             if (l_mod(a, b, &r) < 0)
5710                 goto error;
5711             Py_SETREF(a, b);
5712             b = r;
5713             alloc_a = alloc_b;
5714             alloc_b = _PyLong_DigitCount(b);
5715             continue;
5716         }
5717 
5718         /*
5719           a, b = A*b-B*a, D*a-C*b if k is odd
5720           a, b = A*a-B*b, D*b-C*a if k is even
5721         */
5722         if (k&1) {
5723             T = -A; A = -B; B = T;
5724             T = -C; C = -D; D = T;
5725         }
5726         if (c != NULL) {
5727             assert(size_a >= 0);
5728             _PyLong_SetSignAndDigitCount(c, 1, size_a);
5729         }
5730         else if (Py_REFCNT(a) == 1) {
5731             c = (PyLongObject*)Py_NewRef(a);
5732         }
5733         else {
5734             alloc_a = size_a;
5735             c = _PyLong_New(size_a);
5736             if (c == NULL)
5737                 goto error;
5738         }
5739 
5740         if (d != NULL) {
5741             assert(size_a >= 0);
5742             _PyLong_SetSignAndDigitCount(d, 1, size_a);
5743         }
5744         else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
5745             d = (PyLongObject*)Py_NewRef(b);
5746             assert(size_a >= 0);
5747             _PyLong_SetSignAndDigitCount(d, 1, size_a);
5748         }
5749         else {
5750             alloc_b = size_a;
5751             d = _PyLong_New(size_a);
5752             if (d == NULL)
5753                 goto error;
5754         }
5755         a_end = a->long_value.ob_digit + size_a;
5756         b_end = b->long_value.ob_digit + size_b;
5757 
5758         /* compute new a and new b in parallel */
5759         a_digit = a->long_value.ob_digit;
5760         b_digit = b->long_value.ob_digit;
5761         c_digit = c->long_value.ob_digit;
5762         d_digit = d->long_value.ob_digit;
5763         c_carry = 0;
5764         d_carry = 0;
5765         while (b_digit < b_end) {
5766             c_carry += (A * *a_digit) - (B * *b_digit);
5767             d_carry += (D * *b_digit++) - (C * *a_digit++);
5768             *c_digit++ = (digit)(c_carry & PyLong_MASK);
5769             *d_digit++ = (digit)(d_carry & PyLong_MASK);
5770             c_carry >>= PyLong_SHIFT;
5771             d_carry >>= PyLong_SHIFT;
5772         }
5773         while (a_digit < a_end) {
5774             c_carry += A * *a_digit;
5775             d_carry -= C * *a_digit++;
5776             *c_digit++ = (digit)(c_carry & PyLong_MASK);
5777             *d_digit++ = (digit)(d_carry & PyLong_MASK);
5778             c_carry >>= PyLong_SHIFT;
5779             d_carry >>= PyLong_SHIFT;
5780         }
5781         assert(c_carry == 0);
5782         assert(d_carry == 0);
5783 
5784         Py_INCREF(c);
5785         Py_INCREF(d);
5786         Py_DECREF(a);
5787         Py_DECREF(b);
5788         a = long_normalize(c);
5789         b = long_normalize(d);
5790     }
5791     Py_XDECREF(c);
5792     Py_XDECREF(d);
5793 
5794 simple:
5795     assert(Py_REFCNT(a) > 0);
5796     assert(Py_REFCNT(b) > 0);
5797 /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid
5798    undefined behaviour when LONG_MAX type is smaller than 60 bits */
5799 #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
5800     /* a fits into a long, so b must too */
5801     x = PyLong_AsLong((PyObject *)a);
5802     y = PyLong_AsLong((PyObject *)b);
5803 #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
5804     x = PyLong_AsLongLong((PyObject *)a);
5805     y = PyLong_AsLongLong((PyObject *)b);
5806 #else
5807 # error "_PyLong_GCD"
5808 #endif
5809     x = Py_ABS(x);
5810     y = Py_ABS(y);
5811     Py_DECREF(a);
5812     Py_DECREF(b);
5813 
5814     /* usual Euclidean algorithm for longs */
5815     while (y != 0) {
5816         t = y;
5817         y = x % y;
5818         x = t;
5819     }
5820 #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
5821     return PyLong_FromLong(x);
5822 #elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
5823     return PyLong_FromLongLong(x);
5824 #else
5825 # error "_PyLong_GCD"
5826 #endif
5827 
5828 error:
5829     Py_DECREF(a);
5830     Py_DECREF(b);
5831     Py_XDECREF(c);
5832     Py_XDECREF(d);
5833     return NULL;
5834 }
5835 
5836 static PyObject *
long_float(PyObject * v)5837 long_float(PyObject *v)
5838 {
5839     double result;
5840     result = PyLong_AsDouble(v);
5841     if (result == -1.0 && PyErr_Occurred())
5842         return NULL;
5843     return PyFloat_FromDouble(result);
5844 }
5845 
5846 static PyObject *
5847 long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase);
5848 
5849 /*[clinic input]
5850 @classmethod
5851 int.__new__ as long_new
5852     x: object(c_default="NULL") = 0
5853     /
5854     base as obase: object(c_default="NULL") = 10
5855 [clinic start generated code]*/
5856 
5857 static PyObject *
long_new_impl(PyTypeObject * type,PyObject * x,PyObject * obase)5858 long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase)
5859 /*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/
5860 {
5861     Py_ssize_t base;
5862 
5863     if (type != &PyLong_Type)
5864         return long_subtype_new(type, x, obase); /* Wimp out */
5865     if (x == NULL) {
5866         if (obase != NULL) {
5867             PyErr_SetString(PyExc_TypeError,
5868                             "int() missing string argument");
5869             return NULL;
5870         }
5871         return PyLong_FromLong(0L);
5872     }
5873     /* default base and limit, forward to standard implementation */
5874     if (obase == NULL)
5875         return PyNumber_Long(x);
5876 
5877     base = PyNumber_AsSsize_t(obase, NULL);
5878     if (base == -1 && PyErr_Occurred())
5879         return NULL;
5880     if ((base != 0 && base < 2) || base > 36) {
5881         PyErr_SetString(PyExc_ValueError,
5882                         "int() base must be >= 2 and <= 36, or 0");
5883         return NULL;
5884     }
5885 
5886     if (PyUnicode_Check(x))
5887         return PyLong_FromUnicodeObject(x, (int)base);
5888     else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
5889         const char *string;
5890         if (PyByteArray_Check(x))
5891             string = PyByteArray_AS_STRING(x);
5892         else
5893             string = PyBytes_AS_STRING(x);
5894         return _PyLong_FromBytes(string, Py_SIZE(x), (int)base);
5895     }
5896     else {
5897         PyErr_SetString(PyExc_TypeError,
5898                         "int() can't convert non-string with explicit base");
5899         return NULL;
5900     }
5901 }
5902 
5903 /* Wimpy, slow approach to tp_new calls for subtypes of int:
5904    first create a regular int from whatever arguments we got,
5905    then allocate a subtype instance and initialize it from
5906    the regular int.  The regular int is then thrown away.
5907 */
5908 static PyObject *
long_subtype_new(PyTypeObject * type,PyObject * x,PyObject * obase)5909 long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase)
5910 {
5911     PyLongObject *tmp, *newobj;
5912     Py_ssize_t i, n;
5913 
5914     assert(PyType_IsSubtype(type, &PyLong_Type));
5915     tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase);
5916     if (tmp == NULL)
5917         return NULL;
5918     assert(PyLong_Check(tmp));
5919     n = _PyLong_DigitCount(tmp);
5920     /* Fast operations for single digit integers (including zero)
5921      * assume that there is always at least one digit present. */
5922     if (n == 0) {
5923         n = 1;
5924     }
5925     newobj = (PyLongObject *)type->tp_alloc(type, n);
5926     if (newobj == NULL) {
5927         Py_DECREF(tmp);
5928         return NULL;
5929     }
5930     assert(PyLong_Check(newobj));
5931     newobj->long_value.lv_tag = tmp->long_value.lv_tag;
5932     for (i = 0; i < n; i++) {
5933         newobj->long_value.ob_digit[i] = tmp->long_value.ob_digit[i];
5934     }
5935     Py_DECREF(tmp);
5936     return (PyObject *)newobj;
5937 }
5938 
5939 /*[clinic input]
5940 int.__getnewargs__
5941 [clinic start generated code]*/
5942 
5943 static PyObject *
int___getnewargs___impl(PyObject * self)5944 int___getnewargs___impl(PyObject *self)
5945 /*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/
5946 {
5947     return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self));
5948 }
5949 
5950 static PyObject *
long_get0(PyObject * Py_UNUSED (self),void * Py_UNUSED (context))5951 long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context))
5952 {
5953     return PyLong_FromLong(0L);
5954 }
5955 
5956 static PyObject *
long_get1(PyObject * Py_UNUSED (self),void * Py_UNUSED (ignored))5957 long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored))
5958 {
5959     return PyLong_FromLong(1L);
5960 }
5961 
5962 /*[clinic input]
5963 int.__format__
5964 
5965     format_spec: unicode
5966     /
5967 
5968 Convert to a string according to format_spec.
5969 [clinic start generated code]*/
5970 
5971 static PyObject *
int___format___impl(PyObject * self,PyObject * format_spec)5972 int___format___impl(PyObject *self, PyObject *format_spec)
5973 /*[clinic end generated code: output=b4929dee9ae18689 input=d5e1254a47e8d1dc]*/
5974 {
5975     _PyUnicodeWriter writer;
5976     int ret;
5977 
5978     _PyUnicodeWriter_Init(&writer);
5979     ret = _PyLong_FormatAdvancedWriter(
5980         &writer,
5981         self,
5982         format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
5983     if (ret == -1) {
5984         _PyUnicodeWriter_Dealloc(&writer);
5985         return NULL;
5986     }
5987     return _PyUnicodeWriter_Finish(&writer);
5988 }
5989 
5990 /* Return a pair (q, r) such that a = b * q + r, and
5991    abs(r) <= abs(b)/2, with equality possible only if q is even.
5992    In other words, q == a / b, rounded to the nearest integer using
5993    round-half-to-even. */
5994 
5995 PyObject *
_PyLong_DivmodNear(PyObject * a,PyObject * b)5996 _PyLong_DivmodNear(PyObject *a, PyObject *b)
5997 {
5998     PyLongObject *quo = NULL, *rem = NULL;
5999     PyObject *twice_rem, *result, *temp;
6000     int quo_is_odd, quo_is_neg;
6001     Py_ssize_t cmp;
6002 
6003     /* Equivalent Python code:
6004 
6005        def divmod_near(a, b):
6006            q, r = divmod(a, b)
6007            # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
6008            # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
6009            # positive, 2 * r < b if b negative.
6010            greater_than_half = 2*r > b if b > 0 else 2*r < b
6011            exactly_half = 2*r == b
6012            if greater_than_half or exactly_half and q % 2 == 1:
6013                q += 1
6014                r -= b
6015            return q, r
6016 
6017     */
6018     if (!PyLong_Check(a) || !PyLong_Check(b)) {
6019         PyErr_SetString(PyExc_TypeError,
6020                         "non-integer arguments in division");
6021         return NULL;
6022     }
6023 
6024     /* Do a and b have different signs?  If so, quotient is negative. */
6025     quo_is_neg = (_PyLong_IsNegative((PyLongObject *)a)) != (_PyLong_IsNegative((PyLongObject *)b));
6026 
6027     if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
6028         goto error;
6029 
6030     /* compare twice the remainder with the divisor, to see
6031        if we need to adjust the quotient and remainder */
6032     PyObject *one = _PyLong_GetOne();  // borrowed reference
6033     twice_rem = long_lshift((PyObject *)rem, one);
6034     if (twice_rem == NULL)
6035         goto error;
6036     if (quo_is_neg) {
6037         temp = long_neg((PyLongObject*)twice_rem);
6038         Py_SETREF(twice_rem, temp);
6039         if (twice_rem == NULL)
6040             goto error;
6041     }
6042     cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b);
6043     Py_DECREF(twice_rem);
6044 
6045     quo_is_odd = (quo->long_value.ob_digit[0] & 1) != 0;
6046     if ((_PyLong_IsNegative((PyLongObject *)b) ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
6047         /* fix up quotient */
6048         if (quo_is_neg)
6049             temp = long_sub(quo, (PyLongObject *)one);
6050         else
6051             temp = long_add(quo, (PyLongObject *)one);
6052         Py_SETREF(quo, (PyLongObject *)temp);
6053         if (quo == NULL)
6054             goto error;
6055         /* and remainder */
6056         if (quo_is_neg)
6057             temp = long_add(rem, (PyLongObject *)b);
6058         else
6059             temp = long_sub(rem, (PyLongObject *)b);
6060         Py_SETREF(rem, (PyLongObject *)temp);
6061         if (rem == NULL)
6062             goto error;
6063     }
6064 
6065     result = PyTuple_New(2);
6066     if (result == NULL)
6067         goto error;
6068 
6069     /* PyTuple_SET_ITEM steals references */
6070     PyTuple_SET_ITEM(result, 0, (PyObject *)quo);
6071     PyTuple_SET_ITEM(result, 1, (PyObject *)rem);
6072     return result;
6073 
6074   error:
6075     Py_XDECREF(quo);
6076     Py_XDECREF(rem);
6077     return NULL;
6078 }
6079 
6080 /*[clinic input]
6081 int.__round__
6082 
6083     ndigits as o_ndigits: object = NULL
6084     /
6085 
6086 Rounding an Integral returns itself.
6087 
6088 Rounding with an ndigits argument also returns an integer.
6089 [clinic start generated code]*/
6090 
6091 static PyObject *
int___round___impl(PyObject * self,PyObject * o_ndigits)6092 int___round___impl(PyObject *self, PyObject *o_ndigits)
6093 /*[clinic end generated code: output=954fda6b18875998 input=1614cf23ec9e18c3]*/
6094 {
6095     PyObject *temp, *result, *ndigits;
6096 
6097     /* To round an integer m to the nearest 10**n (n positive), we make use of
6098      * the divmod_near operation, defined by:
6099      *
6100      *   divmod_near(a, b) = (q, r)
6101      *
6102      * where q is the nearest integer to the quotient a / b (the
6103      * nearest even integer in the case of a tie) and r == a - q * b.
6104      * Hence q * b = a - r is the nearest multiple of b to a,
6105      * preferring even multiples in the case of a tie.
6106      *
6107      * So the nearest multiple of 10**n to m is:
6108      *
6109      *   m - divmod_near(m, 10**n)[1].
6110      */
6111     if (o_ndigits == NULL)
6112         return long_long(self);
6113 
6114     ndigits = _PyNumber_Index(o_ndigits);
6115     if (ndigits == NULL)
6116         return NULL;
6117 
6118     /* if ndigits >= 0 then no rounding is necessary; return self unchanged */
6119     if (!_PyLong_IsNegative((PyLongObject *)ndigits)) {
6120         Py_DECREF(ndigits);
6121         return long_long(self);
6122     }
6123 
6124     /* result = self - divmod_near(self, 10 ** -ndigits)[1] */
6125     temp = long_neg((PyLongObject*)ndigits);
6126     Py_SETREF(ndigits, temp);
6127     if (ndigits == NULL)
6128         return NULL;
6129 
6130     result = PyLong_FromLong(10L);
6131     if (result == NULL) {
6132         Py_DECREF(ndigits);
6133         return NULL;
6134     }
6135 
6136     temp = long_pow(result, ndigits, Py_None);
6137     Py_DECREF(ndigits);
6138     Py_SETREF(result, temp);
6139     if (result == NULL)
6140         return NULL;
6141 
6142     temp = _PyLong_DivmodNear(self, result);
6143     Py_SETREF(result, temp);
6144     if (result == NULL)
6145         return NULL;
6146 
6147     temp = long_sub((PyLongObject *)self,
6148                     (PyLongObject *)PyTuple_GET_ITEM(result, 1));
6149     Py_SETREF(result, temp);
6150 
6151     return result;
6152 }
6153 
6154 /*[clinic input]
6155 int.__sizeof__ -> Py_ssize_t
6156 
6157 Returns size in memory, in bytes.
6158 [clinic start generated code]*/
6159 
6160 static Py_ssize_t
int___sizeof___impl(PyObject * self)6161 int___sizeof___impl(PyObject *self)
6162 /*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/
6163 {
6164     /* using Py_MAX(..., 1) because we always allocate space for at least
6165        one digit, even though the integer zero has a digit count of 0 */
6166     Py_ssize_t ndigits = Py_MAX(_PyLong_DigitCount((PyLongObject *)self), 1);
6167     return Py_TYPE(self)->tp_basicsize + Py_TYPE(self)->tp_itemsize * ndigits;
6168 }
6169 
6170 /*[clinic input]
6171 int.bit_length
6172 
6173 Number of bits necessary to represent self in binary.
6174 
6175 >>> bin(37)
6176 '0b100101'
6177 >>> (37).bit_length()
6178 6
6179 [clinic start generated code]*/
6180 
6181 static PyObject *
int_bit_length_impl(PyObject * self)6182 int_bit_length_impl(PyObject *self)
6183 /*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/
6184 {
6185     PyLongObject *result, *x, *y;
6186     Py_ssize_t ndigits;
6187     int msd_bits;
6188     digit msd;
6189 
6190     assert(self != NULL);
6191     assert(PyLong_Check(self));
6192 
6193     ndigits = _PyLong_DigitCount((PyLongObject *)self);
6194     if (ndigits == 0)
6195         return PyLong_FromLong(0);
6196 
6197     msd = ((PyLongObject *)self)->long_value.ob_digit[ndigits-1];
6198     msd_bits = bit_length_digit(msd);
6199 
6200     if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
6201         return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
6202 
6203     /* expression above may overflow; use Python integers instead */
6204     result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
6205     if (result == NULL)
6206         return NULL;
6207     x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
6208     if (x == NULL)
6209         goto error;
6210     y = (PyLongObject *)long_mul(result, x);
6211     Py_DECREF(x);
6212     if (y == NULL)
6213         goto error;
6214     Py_SETREF(result, y);
6215 
6216     x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
6217     if (x == NULL)
6218         goto error;
6219     y = (PyLongObject *)long_add(result, x);
6220     Py_DECREF(x);
6221     if (y == NULL)
6222         goto error;
6223     Py_SETREF(result, y);
6224 
6225     return (PyObject *)result;
6226 
6227   error:
6228     Py_DECREF(result);
6229     return NULL;
6230 }
6231 
6232 static int
popcount_digit(digit d)6233 popcount_digit(digit d)
6234 {
6235     // digit can be larger than uint32_t, but only PyLong_SHIFT bits
6236     // of it will be ever used.
6237     static_assert(PyLong_SHIFT <= 32, "digit is larger than uint32_t");
6238     return _Py_popcount32((uint32_t)d);
6239 }
6240 
6241 /*[clinic input]
6242 int.bit_count
6243 
6244 Number of ones in the binary representation of the absolute value of self.
6245 
6246 Also known as the population count.
6247 
6248 >>> bin(13)
6249 '0b1101'
6250 >>> (13).bit_count()
6251 3
6252 [clinic start generated code]*/
6253 
6254 static PyObject *
int_bit_count_impl(PyObject * self)6255 int_bit_count_impl(PyObject *self)
6256 /*[clinic end generated code: output=2e571970daf1e5c3 input=7e0adef8e8ccdf2e]*/
6257 {
6258     assert(self != NULL);
6259     assert(PyLong_Check(self));
6260 
6261     PyLongObject *z = (PyLongObject *)self;
6262     Py_ssize_t ndigits = _PyLong_DigitCount(z);
6263     Py_ssize_t bit_count = 0;
6264 
6265     /* Each digit has up to PyLong_SHIFT ones, so the accumulated bit count
6266        from the first PY_SSIZE_T_MAX/PyLong_SHIFT digits can't overflow a
6267        Py_ssize_t. */
6268     Py_ssize_t ndigits_fast = Py_MIN(ndigits, PY_SSIZE_T_MAX/PyLong_SHIFT);
6269     for (Py_ssize_t i = 0; i < ndigits_fast; i++) {
6270         bit_count += popcount_digit(z->long_value.ob_digit[i]);
6271     }
6272 
6273     PyObject *result = PyLong_FromSsize_t(bit_count);
6274     if (result == NULL) {
6275         return NULL;
6276     }
6277 
6278     /* Use Python integers if bit_count would overflow. */
6279     for (Py_ssize_t i = ndigits_fast; i < ndigits; i++) {
6280         PyObject *x = PyLong_FromLong(popcount_digit(z->long_value.ob_digit[i]));
6281         if (x == NULL) {
6282             goto error;
6283         }
6284         PyObject *y = long_add((PyLongObject *)result, (PyLongObject *)x);
6285         Py_DECREF(x);
6286         if (y == NULL) {
6287             goto error;
6288         }
6289         Py_SETREF(result, y);
6290     }
6291 
6292     return result;
6293 
6294   error:
6295     Py_DECREF(result);
6296     return NULL;
6297 }
6298 
6299 /*[clinic input]
6300 int.as_integer_ratio
6301 
6302 Return a pair of integers, whose ratio is equal to the original int.
6303 
6304 The ratio is in lowest terms and has a positive denominator.
6305 
6306 >>> (10).as_integer_ratio()
6307 (10, 1)
6308 >>> (-10).as_integer_ratio()
6309 (-10, 1)
6310 >>> (0).as_integer_ratio()
6311 (0, 1)
6312 [clinic start generated code]*/
6313 
6314 static PyObject *
int_as_integer_ratio_impl(PyObject * self)6315 int_as_integer_ratio_impl(PyObject *self)
6316 /*[clinic end generated code: output=e60803ae1cc8621a input=384ff1766634bec2]*/
6317 {
6318     PyObject *ratio_tuple;
6319     PyObject *numerator = long_long(self);
6320     if (numerator == NULL) {
6321         return NULL;
6322     }
6323     ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_GetOne());
6324     Py_DECREF(numerator);
6325     return ratio_tuple;
6326 }
6327 
6328 /*[clinic input]
6329 int.to_bytes
6330 
6331     length: Py_ssize_t = 1
6332         Length of bytes object to use.  An OverflowError is raised if the
6333         integer is not representable with the given number of bytes.  Default
6334         is length 1.
6335     byteorder: unicode(c_default="NULL") = "big"
6336         The byte order used to represent the integer.  If byteorder is 'big',
6337         the most significant byte is at the beginning of the byte array.  If
6338         byteorder is 'little', the most significant byte is at the end of the
6339         byte array.  To request the native byte order of the host system, use
6340         sys.byteorder as the byte order value.  Default is to use 'big'.
6341     *
6342     signed as is_signed: bool = False
6343         Determines whether two's complement is used to represent the integer.
6344         If signed is False and a negative integer is given, an OverflowError
6345         is raised.
6346 
6347 Return an array of bytes representing an integer.
6348 [clinic start generated code]*/
6349 
6350 static PyObject *
int_to_bytes_impl(PyObject * self,Py_ssize_t length,PyObject * byteorder,int is_signed)6351 int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
6352                   int is_signed)
6353 /*[clinic end generated code: output=89c801df114050a3 input=a0103d0e9ad85c2b]*/
6354 {
6355     int little_endian;
6356     PyObject *bytes;
6357 
6358     if (byteorder == NULL)
6359         little_endian = 0;
6360     else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
6361         little_endian = 1;
6362     else if (_PyUnicode_Equal(byteorder, &_Py_ID(big)))
6363         little_endian = 0;
6364     else {
6365         PyErr_SetString(PyExc_ValueError,
6366             "byteorder must be either 'little' or 'big'");
6367         return NULL;
6368     }
6369 
6370     if (length < 0) {
6371         PyErr_SetString(PyExc_ValueError,
6372                         "length argument must be non-negative");
6373         return NULL;
6374     }
6375 
6376     bytes = PyBytes_FromStringAndSize(NULL, length);
6377     if (bytes == NULL)
6378         return NULL;
6379 
6380     if (_PyLong_AsByteArray((PyLongObject *)self,
6381                             (unsigned char *)PyBytes_AS_STRING(bytes),
6382                             length, little_endian, is_signed, 1) < 0) {
6383         Py_DECREF(bytes);
6384         return NULL;
6385     }
6386 
6387     return bytes;
6388 }
6389 
6390 /*[clinic input]
6391 @classmethod
6392 int.from_bytes
6393 
6394     bytes as bytes_obj: object
6395         Holds the array of bytes to convert.  The argument must either
6396         support the buffer protocol or be an iterable object producing bytes.
6397         Bytes and bytearray are examples of built-in objects that support the
6398         buffer protocol.
6399     byteorder: unicode(c_default="NULL") = "big"
6400         The byte order used to represent the integer.  If byteorder is 'big',
6401         the most significant byte is at the beginning of the byte array.  If
6402         byteorder is 'little', the most significant byte is at the end of the
6403         byte array.  To request the native byte order of the host system, use
6404         sys.byteorder as the byte order value.  Default is to use 'big'.
6405     *
6406     signed as is_signed: bool = False
6407         Indicates whether two's complement is used to represent the integer.
6408 
6409 Return the integer represented by the given array of bytes.
6410 [clinic start generated code]*/
6411 
6412 static PyObject *
int_from_bytes_impl(PyTypeObject * type,PyObject * bytes_obj,PyObject * byteorder,int is_signed)6413 int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
6414                     PyObject *byteorder, int is_signed)
6415 /*[clinic end generated code: output=efc5d68e31f9314f input=2ff527997fe7b0c5]*/
6416 {
6417     int little_endian;
6418     PyObject *long_obj, *bytes;
6419 
6420     if (byteorder == NULL)
6421         little_endian = 0;
6422     else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
6423         little_endian = 1;
6424     else if (_PyUnicode_Equal(byteorder, &_Py_ID(big)))
6425         little_endian = 0;
6426     else {
6427         PyErr_SetString(PyExc_ValueError,
6428             "byteorder must be either 'little' or 'big'");
6429         return NULL;
6430     }
6431 
6432     bytes = PyObject_Bytes(bytes_obj);
6433     if (bytes == NULL)
6434         return NULL;
6435 
6436     long_obj = _PyLong_FromByteArray(
6437         (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
6438         little_endian, is_signed);
6439     Py_DECREF(bytes);
6440 
6441     if (long_obj != NULL && type != &PyLong_Type) {
6442         Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj));
6443     }
6444 
6445     return long_obj;
6446 }
6447 
6448 static PyObject *
long_long_meth(PyObject * self,PyObject * Py_UNUSED (ignored))6449 long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
6450 {
6451     return long_long(self);
6452 }
6453 
6454 /*[clinic input]
6455 int.is_integer
6456 
6457 Returns True. Exists for duck type compatibility with float.is_integer.
6458 [clinic start generated code]*/
6459 
6460 static PyObject *
int_is_integer_impl(PyObject * self)6461 int_is_integer_impl(PyObject *self)
6462 /*[clinic end generated code: output=90f8e794ce5430ef input=7e41c4d4416e05f2]*/
6463 {
6464     Py_RETURN_TRUE;
6465 }
6466 
6467 static PyObject *
long_vectorcall(PyObject * type,PyObject * const * args,size_t nargsf,PyObject * kwnames)6468 long_vectorcall(PyObject *type, PyObject * const*args,
6469                  size_t nargsf, PyObject *kwnames)
6470 {
6471     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
6472     if (kwnames != NULL) {
6473         PyThreadState *tstate = PyThreadState_GET();
6474         return _PyObject_MakeTpCall(tstate, type, args, nargs, kwnames);
6475     }
6476     switch (nargs) {
6477         case 0:
6478             return _PyLong_GetZero();
6479         case 1:
6480             return PyNumber_Long(args[0]);
6481         case 2:
6482             return long_new_impl(_PyType_CAST(type), args[0], args[1]);
6483         default:
6484             return PyErr_Format(PyExc_TypeError,
6485                                 "int expected at most 2 arguments, got %zd",
6486                                 nargs);
6487     }
6488 }
6489 
6490 static PyMethodDef long_methods[] = {
6491     {"conjugate",       long_long_meth, METH_NOARGS,
6492      "Returns self, the complex conjugate of any int."},
6493     INT_BIT_LENGTH_METHODDEF
6494     INT_BIT_COUNT_METHODDEF
6495     INT_TO_BYTES_METHODDEF
6496     INT_FROM_BYTES_METHODDEF
6497     INT_AS_INTEGER_RATIO_METHODDEF
6498     {"__trunc__",       long_long_meth, METH_NOARGS,
6499      "Truncating an Integral returns itself."},
6500     {"__floor__",       long_long_meth, METH_NOARGS,
6501      "Flooring an Integral returns itself."},
6502     {"__ceil__",        long_long_meth, METH_NOARGS,
6503      "Ceiling of an Integral returns itself."},
6504     INT___ROUND___METHODDEF
6505     INT___GETNEWARGS___METHODDEF
6506     INT___FORMAT___METHODDEF
6507     INT___SIZEOF___METHODDEF
6508     INT_IS_INTEGER_METHODDEF
6509     {NULL,              NULL}           /* sentinel */
6510 };
6511 
6512 static PyGetSetDef long_getset[] = {
6513     {"real",
6514      (getter)long_long_meth, (setter)NULL,
6515      "the real part of a complex number",
6516      NULL},
6517     {"imag",
6518      long_get0, (setter)NULL,
6519      "the imaginary part of a complex number",
6520      NULL},
6521     {"numerator",
6522      (getter)long_long_meth, (setter)NULL,
6523      "the numerator of a rational number in lowest terms",
6524      NULL},
6525     {"denominator",
6526      long_get1, (setter)NULL,
6527      "the denominator of a rational number in lowest terms",
6528      NULL},
6529     {NULL}  /* Sentinel */
6530 };
6531 
6532 PyDoc_STRVAR(long_doc,
6533 "int([x]) -> integer\n\
6534 int(x, base=10) -> integer\n\
6535 \n\
6536 Convert a number or string to an integer, or return 0 if no arguments\n\
6537 are given.  If x is a number, return x.__int__().  For floating-point\n\
6538 numbers, this truncates towards zero.\n\
6539 \n\
6540 If x is not a number or if base is given, then x must be a string,\n\
6541 bytes, or bytearray instance representing an integer literal in the\n\
6542 given base.  The literal can be preceded by '+' or '-' and be surrounded\n\
6543 by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\n\
6544 Base 0 means to interpret the base from the string as an integer literal.\n\
6545 >>> int('0b100', base=0)\n\
6546 4");
6547 
6548 static PyNumberMethods long_as_number = {
6549     (binaryfunc)long_add,       /*nb_add*/
6550     (binaryfunc)long_sub,       /*nb_subtract*/
6551     (binaryfunc)long_mul,       /*nb_multiply*/
6552     long_mod,                   /*nb_remainder*/
6553     long_divmod,                /*nb_divmod*/
6554     long_pow,                   /*nb_power*/
6555     (unaryfunc)long_neg,        /*nb_negative*/
6556     long_long,                  /*tp_positive*/
6557     (unaryfunc)long_abs,        /*tp_absolute*/
6558     (inquiry)long_bool,         /*tp_bool*/
6559     (unaryfunc)long_invert,     /*nb_invert*/
6560     long_lshift,                /*nb_lshift*/
6561     long_rshift,                /*nb_rshift*/
6562     long_and,                   /*nb_and*/
6563     long_xor,                   /*nb_xor*/
6564     long_or,                    /*nb_or*/
6565     long_long,                  /*nb_int*/
6566     0,                          /*nb_reserved*/
6567     long_float,                 /*nb_float*/
6568     0,                          /* nb_inplace_add */
6569     0,                          /* nb_inplace_subtract */
6570     0,                          /* nb_inplace_multiply */
6571     0,                          /* nb_inplace_remainder */
6572     0,                          /* nb_inplace_power */
6573     0,                          /* nb_inplace_lshift */
6574     0,                          /* nb_inplace_rshift */
6575     0,                          /* nb_inplace_and */
6576     0,                          /* nb_inplace_xor */
6577     0,                          /* nb_inplace_or */
6578     long_div,                   /* nb_floor_divide */
6579     long_true_divide,           /* nb_true_divide */
6580     0,                          /* nb_inplace_floor_divide */
6581     0,                          /* nb_inplace_true_divide */
6582     long_long,                  /* nb_index */
6583 };
6584 
6585 PyTypeObject PyLong_Type = {
6586     PyVarObject_HEAD_INIT(&PyType_Type, 0)
6587     "int",                                      /* tp_name */
6588     offsetof(PyLongObject, long_value.ob_digit),  /* tp_basicsize */
6589     sizeof(digit),                              /* tp_itemsize */
6590     long_dealloc,                               /* tp_dealloc */
6591     0,                                          /* tp_vectorcall_offset */
6592     0,                                          /* tp_getattr */
6593     0,                                          /* tp_setattr */
6594     0,                                          /* tp_as_async */
6595     long_to_decimal_string,                     /* tp_repr */
6596     &long_as_number,                            /* tp_as_number */
6597     0,                                          /* tp_as_sequence */
6598     0,                                          /* tp_as_mapping */
6599     (hashfunc)long_hash,                        /* tp_hash */
6600     0,                                          /* tp_call */
6601     0,                                          /* tp_str */
6602     PyObject_GenericGetAttr,                    /* tp_getattro */
6603     0,                                          /* tp_setattro */
6604     0,                                          /* tp_as_buffer */
6605     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
6606         Py_TPFLAGS_LONG_SUBCLASS |
6607         _Py_TPFLAGS_MATCH_SELF,               /* tp_flags */
6608     long_doc,                                   /* tp_doc */
6609     0,                                          /* tp_traverse */
6610     0,                                          /* tp_clear */
6611     long_richcompare,                           /* tp_richcompare */
6612     0,                                          /* tp_weaklistoffset */
6613     0,                                          /* tp_iter */
6614     0,                                          /* tp_iternext */
6615     long_methods,                               /* tp_methods */
6616     0,                                          /* tp_members */
6617     long_getset,                                /* tp_getset */
6618     0,                                          /* tp_base */
6619     0,                                          /* tp_dict */
6620     0,                                          /* tp_descr_get */
6621     0,                                          /* tp_descr_set */
6622     0,                                          /* tp_dictoffset */
6623     0,                                          /* tp_init */
6624     0,                                          /* tp_alloc */
6625     long_new,                                   /* tp_new */
6626     PyObject_Free,                              /* tp_free */
6627     .tp_vectorcall = long_vectorcall,
6628 };
6629 
6630 static PyTypeObject Int_InfoType;
6631 
6632 PyDoc_STRVAR(int_info__doc__,
6633 "sys.int_info\n\
6634 \n\
6635 A named tuple that holds information about Python's\n\
6636 internal representation of integers.  The attributes are read only.");
6637 
6638 static PyStructSequence_Field int_info_fields[] = {
6639     {"bits_per_digit", "size of a digit in bits"},
6640     {"sizeof_digit", "size in bytes of the C type used to represent a digit"},
6641     {"default_max_str_digits", "maximum string conversion digits limitation"},
6642     {"str_digits_check_threshold", "minimum positive value for int_max_str_digits"},
6643     {NULL, NULL}
6644 };
6645 
6646 static PyStructSequence_Desc int_info_desc = {
6647     "sys.int_info",   /* name */
6648     int_info__doc__,  /* doc */
6649     int_info_fields,  /* fields */
6650     4                 /* number of fields */
6651 };
6652 
6653 PyObject *
PyLong_GetInfo(void)6654 PyLong_GetInfo(void)
6655 {
6656     PyObject* int_info;
6657     int field = 0;
6658     int_info = PyStructSequence_New(&Int_InfoType);
6659     if (int_info == NULL)
6660         return NULL;
6661     PyStructSequence_SET_ITEM(int_info, field++,
6662                               PyLong_FromLong(PyLong_SHIFT));
6663     PyStructSequence_SET_ITEM(int_info, field++,
6664                               PyLong_FromLong(sizeof(digit)));
6665     /*
6666      * The following two fields were added after investigating uses of
6667      * sys.int_info in the wild: Exceedingly rarely used. The ONLY use found was
6668      * numba using sys.int_info.bits_per_digit as attribute access rather than
6669      * sequence unpacking. Cython and sympy also refer to sys.int_info but only
6670      * as info for debugging. No concern about adding these in a backport.
6671      */
6672     PyStructSequence_SET_ITEM(int_info, field++,
6673                               PyLong_FromLong(_PY_LONG_DEFAULT_MAX_STR_DIGITS));
6674     PyStructSequence_SET_ITEM(int_info, field++,
6675                               PyLong_FromLong(_PY_LONG_MAX_STR_DIGITS_THRESHOLD));
6676     if (PyErr_Occurred()) {
6677         Py_CLEAR(int_info);
6678         return NULL;
6679     }
6680     return int_info;
6681 }
6682 
6683 
6684 /* runtime lifecycle */
6685 
6686 PyStatus
_PyLong_InitTypes(PyInterpreterState * interp)6687 _PyLong_InitTypes(PyInterpreterState *interp)
6688 {
6689     /* initialize int_info */
6690     if (_PyStructSequence_InitBuiltin(interp, &Int_InfoType,
6691                                       &int_info_desc) < 0)
6692     {
6693         return _PyStatus_ERR("can't init int info type");
6694     }
6695 
6696     return _PyStatus_OK();
6697 }
6698 
6699 
6700 void
_PyLong_FiniTypes(PyInterpreterState * interp)6701 _PyLong_FiniTypes(PyInterpreterState *interp)
6702 {
6703     _PyStructSequence_FiniBuiltin(interp, &Int_InfoType);
6704 }
6705 
6706 #undef PyUnstable_Long_IsCompact
6707 
6708 int
PyUnstable_Long_IsCompact(const PyLongObject * op)6709 PyUnstable_Long_IsCompact(const PyLongObject* op) {
6710     return _PyLong_IsCompact(op);
6711 }
6712 
6713 #undef PyUnstable_Long_CompactValue
6714 
6715 Py_ssize_t
PyUnstable_Long_CompactValue(const PyLongObject * op)6716 PyUnstable_Long_CompactValue(const PyLongObject* op) {
6717     return _PyLong_CompactValue(op);
6718 }
6719