Coverage Report

Created: 2025-08-26 06:26

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