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