Coverage Report

Created: 2025-07-11 06:24

/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
990M
#define medium_value(x) ((stwodigits)_PyLong_CompactValue(x))
27
28
1.06G
#define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
29
1.88M
#define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)
30
31
49
#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
14.3M
{
47
14.3M
    assert(PyLong_CheckExact(op));
48
14.3M
    _Py_DECREF_SPECIALIZED((PyObject *)op, _PyLong_ExactDealloc);
49
14.3M
}
50
51
static inline int
52
is_medium_int(stwodigits x)
53
255M
{
54
    /* Take care that we are comparing unsigned values. */
55
255M
    twodigits x_plus_mask = ((twodigits)x) + PyLong_MASK;
56
255M
    return x_plus_mask < ((twodigits)PyLong_MASK) + PyLong_BASE;
57
255M
}
58
59
static PyObject *
60
get_small_int(sdigit ival)
61
405M
{
62
405M
    assert(IS_SMALL_INT(ival));
63
405M
    return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + ival];
64
405M
}
65
66
static PyLongObject *
67
maybe_small_long(PyLongObject *v)
68
6.64M
{
69
6.64M
    if (v && _PyLong_IsCompact(v)) {
70
6.33M
        stwodigits ival = medium_value(v);
71
6.33M
        if (IS_SMALL_INT(ival)) {
72
6.24M
            _Py_DECREF_INT(v);
73
6.24M
            return (PyLongObject *)get_small_int((sdigit)ival);
74
6.24M
        }
75
6.33M
    }
76
394k
    return v;
77
6.64M
}
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
279k
#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
47
#define HUGE_EXP_CUTOFF 60
114
115
#define SIGCHECK(PyTryBlock)                    \
116
7.73M
    do {                                        \
117
7.73M
        if (PyErr_CheckSignals()) PyTryBlock    \
118
7.73M
    } 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
6.86M
{
127
6.86M
    Py_ssize_t j = _PyLong_DigitCount(v);
128
6.86M
    Py_ssize_t i = j;
129
130
7.05M
    while (i > 0 && v->long_value.ob_digit[i-1] == 0)
131
193k
        --i;
132
6.86M
    if (i != j) {
133
191k
        if (i == 0) {
134
1.65k
            _PyLong_SetSignAndDigitCount(v, 0, 0);
135
1.65k
        }
136
190k
        else {
137
190k
            _PyLong_SetDigitCount(v, i);
138
190k
        }
139
191k
    }
140
6.86M
    return v;
141
6.86M
}
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
23.1M
# define MAX_LONG_DIGITS ((INT64_MAX-1) / PyLong_SHIFT)
155
#endif
156
157
static PyLongObject *
158
long_alloc(Py_ssize_t size)
159
16.8M
{
160
16.8M
    assert(size >= 0);
161
16.8M
    PyLongObject *result = NULL;
162
16.8M
    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
16.8M
    Py_ssize_t ndigits = size ? size : 1;
170
171
16.8M
    if (ndigits == 1) {
172
6.87M
        result = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints);
173
6.87M
    }
174
16.8M
    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
9.96M
        result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) +
181
9.96M
                                ndigits*sizeof(digit));
182
9.96M
        if (!result) {
183
0
            PyErr_NoMemory();
184
0
            return NULL;
185
0
        }
186
9.96M
        _PyObject_Init((PyObject*)result, &PyLong_Type);
187
9.96M
    }
188
16.8M
    _PyLong_SetSignAndDigitCount(result, size != 0, size);
189
    /* The digit has to be initialized explicitly to avoid
190
     * use-of-uninitialized-value. */
191
16.8M
    result->long_value.ob_digit[0] = 0;
192
16.8M
    return result;
193
16.8M
}
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
400M
{
249
400M
    assert(!IS_SMALL_INT(x));
250
400M
    assert(is_medium_int(x));
251
252
400M
    PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints);
253
400M
    if (v == NULL) {
254
77.2M
        v = PyObject_Malloc(sizeof(PyLongObject));
255
77.2M
        if (v == NULL) {
256
0
            PyErr_NoMemory();
257
0
            return NULL;
258
0
        }
259
77.2M
        _PyObject_Init((PyObject*)v, &PyLong_Type);
260
77.2M
    }
261
400M
    digit abs_x = x < 0 ? -x : x;
262
400M
    _PyLong_SetSignAndDigitCount(v, x<0?-1:1, 1);
263
400M
    v->long_value.ob_digit[0] = abs_x;
264
400M
    return (PyObject*)v;
265
400M
}
266
267
static PyObject *
268
_PyLong_FromLarge(stwodigits ival)
269
708
{
270
708
    twodigits abs_ival;
271
708
    int sign;
272
708
    assert(!is_medium_int(ival));
273
274
708
    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
708
    else {
281
708
        abs_ival = (twodigits)ival;
282
708
        sign = 1;
283
708
    }
284
    /* Must be at least two digits */
285
708
    assert(abs_ival >> PyLong_SHIFT != 0);
286
708
    twodigits t = abs_ival >> (PyLong_SHIFT * 2);
287
708
    Py_ssize_t ndigits = 2;
288
708
    while (t) {
289
0
        ++ndigits;
290
0
        t >>= PyLong_SHIFT;
291
0
    }
292
708
    PyLongObject *v = long_alloc(ndigits);
293
708
    if (v != NULL) {
294
708
        digit *p = v->long_value.ob_digit;
295
708
        _PyLong_SetSignAndDigitCount(v, sign, ndigits);
296
708
        t = abs_ival;
297
2.12k
        while (t) {
298
1.41k
            *p++ = Py_SAFE_DOWNCAST(
299
1.41k
                t & PyLong_MASK, twodigits, digit);
300
1.41k
            t >>= PyLong_SHIFT;
301
1.41k
        }
302
708
    }
303
708
    return (PyObject *)v;
304
708
}
305
306
/* Create a new int object from a C word-sized int */
307
static inline PyLongObject *
308
_PyLong_FromSTwoDigits(stwodigits x)
309
59.2k
{
310
59.2k
    if (IS_SMALL_INT(x)) {
311
58.0k
        return (PyLongObject*)get_small_int((sdigit)x);
312
58.0k
    }
313
1.25k
    assert(x != 0);
314
1.25k
    if (is_medium_int(x)) {
315
542
        return (PyLongObject*)_PyLong_FromMedium((sdigit)x);
316
542
    }
317
708
    return (PyLongObject*)_PyLong_FromLarge(x);
318
1.25k
}
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
491M
{
325
491M
    if (IS_SMALL_INT(x)) {
326
236M
        return PyStackRef_FromPyObjectBorrow(get_small_int((sdigit)x));
327
236M
    }
328
255M
    assert(x != 0);
329
255M
    if(!is_medium_int(x)) {
330
669
        return PyStackRef_NULL;
331
669
    }
332
255M
    PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints);
333
255M
    if (v == NULL) {
334
127k
        v = PyObject_Malloc(sizeof(PyLongObject));
335
127k
        if (v == NULL) {
336
0
            return PyStackRef_NULL;
337
0
        }
338
127k
        _PyObject_Init((PyObject*)v, &PyLong_Type);
339
127k
    }
340
255M
    digit abs_x = x < 0 ? (digit)(-x) : (digit)x;
341
255M
    _PyLong_SetSignAndDigitCount(v, x<0?-1:1, 1);
342
255M
    v->long_value.ob_digit[0] = abs_x;
343
255M
    return PyStackRef_FromPyObjectStealMortal((PyObject *)v);
344
255M
}
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
15
{
352
15
    PyLongObject *x;
353
354
15
    x = (PyLongObject *)*x_p;
355
15
    if (Py_REFCNT(x) == 1) {
356
0
         _PyLong_FlipSign(x);
357
0
        return;
358
0
    }
359
360
15
    *x_p = _PyLong_FromSTwoDigits(-medium_value(x));
361
15
    Py_DECREF(x);
362
15
}
363
364
#define PYLONG_FROM_INT(UINT_TYPE, INT_TYPE, ival)                                  \
365
563M
    do {                                                                            \
366
563M
        /* Handle small and medium cases. */                                        \
367
563M
        if (IS_SMALL_INT(ival)) {                                                   \
368
162M
            return get_small_int((sdigit)(ival));                                   \
369
162M
        }                                                                           \
370
563M
        if (-(INT_TYPE)PyLong_MASK <= (ival) && (ival) <= (INT_TYPE)PyLong_MASK) {  \
371
400M
            return _PyLong_FromMedium((sdigit)(ival));                              \
372
400M
        }                                                                           \
373
400M
        UINT_TYPE abs_ival = (ival) < 0 ? 0U-(UINT_TYPE)(ival) : (UINT_TYPE)(ival); \
374
17.0k
        /* Do shift in two steps to avoid possible undefined behavior. */           \
375
17.0k
        UINT_TYPE t = abs_ival >> PyLong_SHIFT >> PyLong_SHIFT;                     \
376
17.0k
        /* Count digits (at least two - smaller cases were handled above). */       \
377
17.0k
        Py_ssize_t ndigits = 2;                                                     \
378
17.4k
        while (t) {                                                                 \
379
436
            ++ndigits;                                                              \
380
436
            t >>= PyLong_SHIFT;                                                     \
381
436
        }                                                                           \
382
17.0k
        /* Construct output value. */                                               \
383
17.0k
        PyLongObject *v = long_alloc(ndigits);                                      \
384
17.0k
        if (v == NULL) {                                                            \
385
0
            return NULL;                                                            \
386
0
        }                                                                           \
387
17.0k
        digit *p = v->long_value.ob_digit;                                          \
388
17.0k
        _PyLong_SetSignAndDigitCount(v, (ival) < 0 ? -1 : 1, ndigits);              \
389
17.0k
        t = abs_ival;                                                               \
390
51.4k
        while (t) {                                                                 \
391
34.4k
            *p++ = (digit)(t & PyLong_MASK);                                        \
392
34.4k
            t >>= PyLong_SHIFT;                                                     \
393
34.4k
        }                                                                           \
394
17.0k
        return (PyObject *)v;                                                       \
395
17.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
318M
{
403
318M
    PYLONG_FROM_INT(unsigned long, long, ival);
404
318M
}
405
406
#define PYLONG_FROM_UINT(INT_TYPE, ival) \
407
1.88M
    do { \
408
1.88M
        /* Handle small and medium cases. */ \
409
1.88M
        if (IS_SMALL_UINT(ival)) { \
410
9.76k
            return get_small_int((sdigit)(ival)); \
411
9.76k
        } \
412
1.88M
        if ((ival) <= PyLong_MASK) { \
413
22.2k
            return _PyLong_FromMedium((sdigit)(ival)); \
414
22.2k
        } \
415
1.87M
        /* Do shift in two steps to avoid possible undefined behavior. */ \
416
1.87M
        INT_TYPE t = (ival) >> PyLong_SHIFT >> PyLong_SHIFT; \
417
1.85M
        /* Count digits (at least two - smaller cases were handled above). */ \
418
1.85M
        Py_ssize_t ndigits = 2; \
419
1.85M
        while (t) { \
420
0
            ++ndigits; \
421
0
            t >>= PyLong_SHIFT; \
422
0
        } \
423
1.85M
        /* Construct output value. */ \
424
1.85M
        PyLongObject *v = long_alloc(ndigits); \
425
1.85M
        if (v == NULL) { \
426
0
            return NULL; \
427
0
        } \
428
1.85M
        digit *p = v->long_value.ob_digit; \
429
5.55M
        while ((ival)) { \
430
3.70M
            *p++ = (digit)((ival) & PyLong_MASK); \
431
3.70M
            (ival) >>= PyLong_SHIFT; \
432
3.70M
        } \
433
1.85M
        return (PyObject *)v; \
434
1.85M
    } 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
1.87M
{
441
1.87M
    PYLONG_FROM_UINT(unsigned long, ival);
442
1.87M
}
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
14.0k
{
449
14.0k
    PYLONG_FROM_UINT(unsigned long long, ival);
450
14.0k
}
451
452
/* Create a new int object from a C size_t. */
453
454
PyObject *
455
PyLong_FromSize_t(size_t ival)
456
810
{
457
810
    PYLONG_FROM_UINT(size_t, ival);
458
810
}
459
460
/* Create a new int object from a C double */
461
462
PyObject *
463
PyLong_FromDouble(double dval)
464
12.3k
{
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
12.3k
    const double int_max = (unsigned long)LONG_MAX + 1;
476
12.3k
    if (-int_max < dval && dval < int_max) {
477
12.3k
        return PyLong_FromLong((long)dval);
478
12.3k
    }
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
445
{
532
445
    assert(ULONG_MAX >= ((1UL << PyLong_SHIFT) - 1));
533
534
445
    Py_ssize_t i = *iptr;
535
445
    assert(i >= 2);
536
537
    /* unroll 1 digit */
538
445
    --i;
539
445
    digit *digits = v->long_value.ob_digit;
540
445
    unsigned long x = digits[i];
541
542
445
#if (ULONG_MAX >> PyLong_SHIFT) >= ((1UL << PyLong_SHIFT) - 1)
543
    /* unroll another digit */
544
445
    x <<= PyLong_SHIFT;
545
445
    --i;
546
445
    x |= digits[i];
547
445
#endif
548
549
445
    *iptr = i;
550
445
    return x;
551
445
}
552
553
static inline size_t
554
unroll_digits_size_t(PyLongObject *v, Py_ssize_t *iptr)
555
1.02k
{
556
1.02k
    assert(SIZE_MAX >= ((1UL << PyLong_SHIFT) - 1));
557
558
1.02k
    Py_ssize_t i = *iptr;
559
1.02k
    assert(i >= 2);
560
561
    /* unroll 1 digit */
562
1.02k
    --i;
563
1.02k
    digit *digits = v->long_value.ob_digit;
564
1.02k
    size_t x = digits[i];
565
566
1.02k
#if (SIZE_MAX >> PyLong_SHIFT) >= ((1 << PyLong_SHIFT) - 1)
567
    /* unroll another digit */
568
1.02k
    x <<= PyLong_SHIFT;
569
1.02k
    --i;
570
1.02k
    x |= digits[i];
571
1.02k
#endif
572
573
1.02k
    *iptr = i;
574
1.02k
    return x;
575
1.02k
}
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
49.8M
{
589
    /* This version originally by Tim Peters */
590
49.8M
    PyLongObject *v;
591
49.8M
    long res;
592
49.8M
    Py_ssize_t i;
593
49.8M
    int sign;
594
49.8M
    int do_decref = 0; /* if PyNumber_Index was called */
595
596
49.8M
    *overflow = 0;
597
49.8M
    if (vv == NULL) {
598
0
        PyErr_BadInternalCall();
599
0
        return -1;
600
0
    }
601
602
49.8M
    if (PyLong_Check(vv)) {
603
49.8M
        v = (PyLongObject *)vv;
604
49.8M
    }
605
952
    else {
606
952
        v = (PyLongObject *)_PyNumber_Index(vv);
607
952
        if (v == NULL)
608
952
            return -1;
609
0
        do_decref = 1;
610
0
    }
611
49.8M
    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
49.8M
        res = _PyLong_CompactValue(v);
627
49.8M
#endif
628
49.8M
    }
629
79
    else {
630
79
        res = -1;
631
79
        i = _PyLong_DigitCount(v);
632
79
        sign = _PyLong_NonCompactSign(v);
633
634
79
        unsigned long x = unroll_digits_ulong(v, &i);
635
82
        while (--i >= 0) {
636
44
            if (x > (ULONG_MAX >> PyLong_SHIFT)) {
637
41
                *overflow = sign;
638
41
                goto exit;
639
41
            }
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
38
        if (x <= (unsigned long)LONG_MAX) {
646
35
            res = (long)x * sign;
647
35
        }
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
38
    }
656
49.8M
  exit:
657
49.8M
    if (do_decref) {
658
0
        Py_DECREF(v);
659
0
    }
660
49.8M
    return res;
661
49.8M
}
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
21.1M
{
669
21.1M
    int overflow;
670
21.1M
    long result = PyLong_AsLongAndOverflow(obj, &overflow);
671
21.1M
    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
21.1M
    return result;
678
21.1M
}
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
6.65M
{
686
6.65M
    int overflow;
687
6.65M
    long result = PyLong_AsLongAndOverflow(obj, &overflow);
688
6.65M
    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
6.65M
    return (int)result;
696
6.65M
}
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
402M
PyLong_AsSsize_t(PyObject *vv) {
703
402M
    PyLongObject *v;
704
402M
    Py_ssize_t i;
705
402M
    int sign;
706
707
402M
    if (vv == NULL) {
708
0
        PyErr_BadInternalCall();
709
0
        return -1;
710
0
    }
711
402M
    if (!PyLong_Check(vv)) {
712
0
        PyErr_SetString(PyExc_TypeError, "an integer is required");
713
0
        return -1;
714
0
    }
715
716
402M
    v = (PyLongObject *)vv;
717
402M
    if (_PyLong_IsCompact(v)) {
718
402M
        return _PyLong_CompactValue(v);
719
402M
    }
720
1.02k
    i = _PyLong_DigitCount(v);
721
1.02k
    sign = _PyLong_NonCompactSign(v);
722
723
1.02k
    size_t x = unroll_digits_size_t(v, &i);
724
1.21k
    while (--i >= 0) {
725
278
        if (x > (SIZE_MAX >> PyLong_SHIFT)) {
726
88
            goto overflow;
727
88
        }
728
190
        x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
729
190
    }
730
    /* Haven't lost any bits, but casting to a signed type requires
731
     * extra care (see comment above).
732
     */
733
933
    if (x <= (size_t)PY_SSIZE_T_MAX) {
734
929
        return (Py_ssize_t)x * sign;
735
929
    }
736
4
    else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
737
0
        return PY_SSIZE_T_MIN;
738
0
    }
739
    /* else overflow */
740
741
92
  overflow:
742
92
    PyErr_SetString(PyExc_OverflowError,
743
92
                    "Python int too large to convert to C ssize_t");
744
92
    return -1;
745
933
}
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
8.56k
{
753
8.56k
    PyLongObject *v;
754
8.56k
    Py_ssize_t i;
755
756
8.56k
    if (vv == NULL) {
757
0
        PyErr_BadInternalCall();
758
0
        return (unsigned long)-1;
759
0
    }
760
8.56k
    if (!PyLong_Check(vv)) {
761
0
        PyErr_SetString(PyExc_TypeError, "an integer is required");
762
0
        return (unsigned long)-1;
763
0
    }
764
765
8.56k
    v = (PyLongObject *)vv;
766
8.56k
    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.19k
        return (unsigned long)(size_t)_PyLong_CompactValue(v);
776
8.19k
#endif
777
8.19k
    }
778
366
    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
366
    i = _PyLong_DigitCount(v);
784
785
366
    unsigned long x = unroll_digits_ulong(v, &i);
786
366
    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
366
    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
366
}
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
14
{
806
14
    PyLongObject *v;
807
14
    Py_ssize_t i;
808
809
14
    if (vv == NULL) {
810
0
        PyErr_BadInternalCall();
811
0
        return (size_t) -1;
812
0
    }
813
14
    if (!PyLong_Check(vv)) {
814
0
        PyErr_SetString(PyExc_TypeError, "an integer is required");
815
0
        return (size_t)-1;
816
0
    }
817
818
14
    v = (PyLongObject *)vv;
819
14
    if (_PyLong_IsNonNegativeCompact(v)) {
820
14
        return (size_t)_PyLong_CompactValue(v);
821
14
    }
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.26M
{
920
2.26M
    assert(obj != NULL);
921
2.26M
    if (!PyLong_Check(obj)) {
922
0
        PyErr_Format(PyExc_TypeError, "expected int, got %T", obj);
923
0
        return -1;
924
0
    }
925
2.26M
    return _PyLong_IsZero((PyLongObject *)obj);
926
2.26M
}
927
928
static int
929
long_sign(PyObject *vv)
930
598
{
931
598
    assert(vv != NULL);
932
598
    assert(PyLong_Check(vv));
933
598
    PyLongObject *v = (PyLongObject *)vv;
934
935
598
    if (_PyLong_IsCompact(v)) {
936
598
        return _PyLong_CompactSign(v);
937
598
    }
938
0
    return _PyLong_NonCompactSign(v);
939
598
}
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
598
{
950
598
    if (!PyLong_Check(vv)) {
951
0
        PyErr_Format(PyExc_TypeError, "expect int, got %T", vv);
952
0
        return -1;
953
0
    }
954
955
598
    *sign = long_sign(vv);
956
598
    return 0;
957
598
}
958
959
static int
960
bit_length_digit(digit x)
961
1.95k
{
962
    // digit can be larger than unsigned long, but only PyLong_SHIFT bits
963
    // of it will be ever used.
964
1.95k
    static_assert(PyLong_SHIFT <= sizeof(unsigned long) * 8,
965
1.95k
                  "digit is larger than unsigned long");
966
1.95k
    return _Py_bit_length((unsigned long)x);
967
1.95k
}
968
969
int64_t
970
_PyLong_NumBits(PyObject *vv)
971
47
{
972
47
    PyLongObject *v = (PyLongObject *)vv;
973
47
    int64_t result = 0;
974
47
    Py_ssize_t ndigits;
975
47
    int msd_bits;
976
977
47
    assert(v != NULL);
978
47
    assert(PyLong_Check(v));
979
47
    ndigits = _PyLong_DigitCount(v);
980
47
    assert(ndigits == 0 || v->long_value.ob_digit[ndigits - 1] != 0);
981
47
    if (ndigits > 0) {
982
47
        digit msd = v->long_value.ob_digit[ndigits - 1];
983
47
#if SIZEOF_SIZE_T == 8
984
47
        assert(ndigits <= INT64_MAX / PyLong_SHIFT);
985
47
#endif
986
47
        result = (int64_t)(ndigits - 1) * PyLong_SHIFT;
987
47
        msd_bits = bit_length_digit(msd);
988
47
        result += msd_bits;
989
47
    }
990
47
    return result;
991
47
}
992
993
PyObject *
994
_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
995
                      int little_endian, int is_signed)
996
2.09k
{
997
2.09k
    const unsigned char* pstartbyte;    /* LSB of bytes */
998
2.09k
    int incr;                           /* direction to move pstartbyte */
999
2.09k
    const unsigned char* pendbyte;      /* MSB of bytes */
1000
2.09k
    size_t numsignificantbytes;         /* number of bytes that matter */
1001
2.09k
    Py_ssize_t ndigits;                 /* number of Python int digits */
1002
2.09k
    PyLongObject* v;                    /* result */
1003
2.09k
    Py_ssize_t idigit = 0;              /* next free index in v->long_value.ob_digit */
1004
1005
2.09k
    if (n == 0)
1006
0
        return PyLong_FromLong(0L);
1007
1008
2.09k
    if (little_endian) {
1009
1.95k
        pstartbyte = bytes;
1010
1.95k
        pendbyte = bytes + n - 1;
1011
1.95k
        incr = 1;
1012
1.95k
    }
1013
132
    else {
1014
132
        pstartbyte = bytes + n - 1;
1015
132
        pendbyte = bytes;
1016
132
        incr = -1;
1017
132
    }
1018
1019
2.09k
    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.09k
    {
1026
2.09k
        size_t i;
1027
2.09k
        const unsigned char* p = pendbyte;
1028
2.09k
        const int pincr = -incr;  /* search MSB to LSB */
1029
2.09k
        const unsigned char insignificant = is_signed ? 0xff : 0x00;
1030
1031
6.06k
        for (i = 0; i < n; ++i, p += pincr) {
1032
5.31k
            if (*p != insignificant)
1033
1.34k
                break;
1034
5.31k
        }
1035
2.09k
        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.09k
        if (is_signed && numsignificantbytes < n)
1042
0
            ++numsignificantbytes;
1043
2.09k
    }
1044
1045
    /* avoid integer overflow */
1046
2.09k
    ndigits = numsignificantbytes / PyLong_SHIFT * 8
1047
2.09k
        + (numsignificantbytes % PyLong_SHIFT * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
1048
2.09k
    v = long_alloc(ndigits);
1049
2.09k
    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.09k
    {
1056
2.09k
        size_t i;
1057
2.09k
        twodigits carry = 1;                    /* for 2's-comp calculation */
1058
2.09k
        twodigits accum = 0;                    /* sliding register */
1059
2.09k
        unsigned int accumbits = 0;             /* number of bits in accum */
1060
2.09k
        const unsigned char* p = pstartbyte;
1061
1062
6.47k
        for (i = 0; i < numsignificantbytes; ++i, p += incr) {
1063
4.38k
            twodigits thisbyte = *p;
1064
            /* Compute correction for 2's comp, if needed. */
1065
4.38k
            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.38k
            accum |= thisbyte << accumbits;
1074
4.38k
            accumbits += 8;
1075
4.38k
            if (accumbits >= PyLong_SHIFT) {
1076
                /* There's enough to fill a Python digit. */
1077
847
                assert(idigit < ndigits);
1078
847
                v->long_value.ob_digit[idigit] = (digit)(accum & PyLong_MASK);
1079
847
                ++idigit;
1080
847
                accum >>= PyLong_SHIFT;
1081
847
                accumbits -= PyLong_SHIFT;
1082
847
                assert(accumbits < PyLong_SHIFT);
1083
847
            }
1084
4.38k
        }
1085
2.09k
        assert(accumbits < PyLong_SHIFT);
1086
2.09k
        if (accumbits) {
1087
1.34k
            assert(idigit < ndigits);
1088
1.34k
            v->long_value.ob_digit[idigit] = (digit)accum;
1089
1.34k
            ++idigit;
1090
1.34k
        }
1091
2.09k
    }
1092
1093
2.09k
    int sign = is_signed ? -1: 1;
1094
2.09k
    if (idigit == 0) {
1095
750
        sign = 0;
1096
750
    }
1097
2.09k
    _PyLong_SetSignAndDigitCount(v, sign, idigit);
1098
2.09k
    return (PyObject *)maybe_small_long(long_normalize(v));
1099
2.09k
}
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
685
{
1107
685
    Py_ssize_t i;               /* index into v->long_value.ob_digit */
1108
685
    Py_ssize_t ndigits;         /* number of digits */
1109
685
    twodigits accum;            /* sliding register */
1110
685
    unsigned int accumbits;     /* # bits in accum */
1111
685
    int do_twos_comp;           /* store 2's-comp?  is_signed and v < 0 */
1112
685
    digit carry;                /* for computing 2's-comp */
1113
685
    size_t j;                   /* # bytes filled */
1114
685
    unsigned char* p;           /* pointer to next byte in bytes */
1115
685
    int pincr;                  /* direction to move p */
1116
1117
685
    assert(v != NULL && PyLong_Check(v));
1118
1119
685
    ndigits = _PyLong_DigitCount(v);
1120
685
    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
685
    else {
1131
685
        do_twos_comp = 0;
1132
685
    }
1133
1134
685
    if (little_endian) {
1135
685
        p = bytes;
1136
685
        pincr = 1;
1137
685
    }
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
685
    assert(ndigits == 0 || v->long_value.ob_digit[ndigits - 1] != 0);
1153
685
    j = 0;
1154
685
    accum = 0;
1155
685
    accumbits = 0;
1156
685
    carry = do_twos_comp ? 1 : 0;
1157
1.36k
    for (i = 0; i < ndigits; ++i) {
1158
682
        digit thisdigit = v->long_value.ob_digit[i];
1159
682
        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
682
        accum |= (twodigits)thisdigit << accumbits;
1168
1169
        /* The most-significant digit may be (probably is) at least
1170
           partly empty. */
1171
682
        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
459
            digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit;
1176
4.12k
            while (s != 0) {
1177
3.66k
                s >>= 1;
1178
3.66k
                accumbits++;
1179
3.66k
            }
1180
459
        }
1181
223
        else
1182
223
            accumbits += PyLong_SHIFT;
1183
1184
        /* Store as many bytes as possible. */
1185
1.66k
        while (accumbits >= 8) {
1186
981
            if (j >= n)
1187
0
                goto Overflow;
1188
981
            ++j;
1189
981
            *p = (unsigned char)(accum & 0xff);
1190
981
            p += pincr;
1191
981
            accumbits -= 8;
1192
981
            accum >>= 8;
1193
981
        }
1194
682
    }
1195
1196
    /* Store the straggler (if any). */
1197
685
    assert(accumbits < 8);
1198
685
    assert(carry == 0);  /* else do_twos_comp and *every* digit was 0 */
1199
685
    if (accumbits > 0) {
1200
425
        if (j >= n)
1201
0
            goto Overflow;
1202
425
        ++j;
1203
425
        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
425
        *p = (unsigned char)(accum & 0xff);
1210
425
        p += pincr;
1211
425
    }
1212
260
    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
685
    {
1228
685
        unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
1229
2.01k
        for ( ; j < n; ++j, p += pincr)
1230
1.33k
            *p = signbyte;
1231
685
    }
1232
1233
685
    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
685
}
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
461
{
1247
461
    if (n >= (Py_ssize_t)sizeof(Py_ssize_t) * 8) {
1248
461
        return 1;
1249
461
    }
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
461
}
1255
1256
static inline int
1257
_resolve_endianness(int *endianness)
1258
461
{
1259
461
    if (*endianness == -1 || (*endianness & 2)) {
1260
461
        *endianness = PY_LITTLE_ENDIAN;
1261
461
    } else {
1262
0
        *endianness &= 1;
1263
0
    }
1264
461
    assert(*endianness == 0 || *endianness == 1);
1265
461
    return 0;
1266
461
}
1267
1268
Py_ssize_t
1269
PyLong_AsNativeBytes(PyObject* vv, void* buffer, Py_ssize_t n, int flags)
1270
461
{
1271
461
    PyLongObject *v;
1272
461
    union {
1273
461
        Py_ssize_t v;
1274
461
        unsigned char b[sizeof(Py_ssize_t)];
1275
461
    } cv;
1276
461
    int do_decref = 0;
1277
461
    Py_ssize_t res = 0;
1278
1279
461
    if (vv == NULL || n < 0) {
1280
0
        PyErr_BadInternalCall();
1281
0
        return -1;
1282
0
    }
1283
1284
461
    int little_endian = flags;
1285
461
    if (_resolve_endianness(&little_endian) < 0) {
1286
0
        return -1;
1287
0
    }
1288
1289
461
    if (PyLong_Check(vv)) {
1290
461
        v = (PyLongObject *)vv;
1291
461
    }
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
461
    if ((flags != -1 && (flags & Py_ASNATIVEBYTES_REJECT_NEGATIVE))
1305
461
        && _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
461
    if (_PyLong_IsCompact(v)) {
1314
461
        res = 0;
1315
461
        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
461
        res = sizeof(cv.b);
1320
461
        if (n <= 0) {
1321
            // nothing to do!
1322
0
        }
1323
461
        else if (n <= (Py_ssize_t)sizeof(cv.b)) {
1324
461
#if PY_LITTLE_ENDIAN
1325
461
            if (little_endian) {
1326
461
                memcpy(buffer, cv.b, n);
1327
461
            }
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
461
            if (_fits_in_n_bits(cv.v, n * 8)) {
1346
461
                res = n;
1347
461
            } 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
461
        }
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
461
    }
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
461
    if (do_decref) {
1446
0
        Py_DECREF(v);
1447
0
    }
1448
1449
461
    return res;
1450
461
}
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
1.84M
{
1497
1.84M
#if SIZEOF_VOID_P <= SIZEOF_LONG
1498
1.84M
    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
1.84M
}
1508
1509
/* Get a C pointer from an int object. */
1510
1511
void *
1512
PyLong_AsVoidPtr(PyObject *vv)
1513
0
{
1514
0
#if SIZEOF_VOID_P <= SIZEOF_LONG
1515
0
    long x;
1516
1517
0
    if (PyLong_Check(vv) && _PyLong_IsNegative((PyLongObject *)vv)) {
1518
0
        x = PyLong_AsLong(vv);
1519
0
    }
1520
0
    else {
1521
0
        x = PyLong_AsUnsignedLong(vv);
1522
0
    }
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
0
    if (x == -1 && PyErr_Occurred())
1540
0
        return NULL;
1541
0
    return (void *)x;
1542
0
}
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
17.5k
{
1555
17.5k
    PYLONG_FROM_INT(unsigned long long, long long, ival);
1556
17.5k
}
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
245M
{
1563
245M
    PYLONG_FROM_INT(size_t, Py_ssize_t, ival);
1564
245M
}
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
38.1M
    do {                                                \
1832
38.1M
        if (!PyLong_Check(v) || !PyLong_Check(w))       \
1833
38.1M
            Py_RETURN_NOTIMPLEMENTED;                   \
1834
38.1M
    } 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
442
{
1949
442
    digit remainder = 0;
1950
1951
442
    assert(n > 0 && n <= PyLong_MASK);
1952
6.70k
    while (--size >= 0) {
1953
6.26k
        twodigits dividend;
1954
6.26k
        dividend = ((twodigits)remainder << PyLong_SHIFT) | pin[size];
1955
6.26k
        digit quotient;
1956
6.26k
        quotient = (digit)(dividend / n);
1957
6.26k
        remainder = dividend % n;
1958
6.26k
        pout[size] = quotient;
1959
6.26k
    }
1960
442
    return remainder;
1961
442
}
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
442
{
1971
442
    const Py_ssize_t size = _PyLong_DigitCount(a);
1972
442
    PyLongObject *z;
1973
1974
442
    assert(n > 0 && n <= PyLong_MASK);
1975
442
    z = long_alloc(size);
1976
442
    if (z == NULL)
1977
0
        return NULL;
1978
442
    *prem = inplace_divrem1(z->long_value.ob_digit, a->long_value.ob_digit, size, n);
1979
442
    return long_normalize(z);
1980
442
}
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
78
{
1988
78
    twodigits rem = 0;
1989
1990
78
    assert(n > 0 && n <= PyLong_MASK);
1991
234
    while (--size >= 0)
1992
156
        rem = ((rem << PyLong_SHIFT) | pin[size]) % n;
1993
78
    return (digit)rem;
1994
78
}
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
78
{
2003
78
    const Py_ssize_t size = _PyLong_DigitCount(a);
2004
2005
78
    assert(n > 0 && n <= PyLong_MASK);
2006
78
    return (PyLongObject *)PyLong_FromLong(
2007
78
        (long)inplace_rem1(a->long_value.ob_digit, size, n)
2008
78
    );
2009
78
}
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
8.10M
{
2088
8.10M
    PyLongObject *scratch, *a;
2089
8.10M
    PyObject *str = NULL;
2090
8.10M
    Py_ssize_t size, strlen, size_a, i, j;
2091
8.10M
    digit *pout, *pin, rem, tenpow;
2092
8.10M
    int negative;
2093
8.10M
    int d;
2094
2095
    // writer or bytes_writer can be used, but not both at the same time.
2096
8.10M
    assert(writer == NULL || bytes_writer == NULL);
2097
2098
8.10M
    a = (PyLongObject *)aa;
2099
8.10M
    if (a == NULL || !PyLong_Check(a)) {
2100
0
        PyErr_BadInternalCall();
2101
0
        return -1;
2102
0
    }
2103
8.10M
    size_a = _PyLong_DigitCount(a);
2104
8.10M
    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
8.10M
    if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD
2112
8.10M
                  / (3 * PyLong_SHIFT) + 2) {
2113
299
        PyInterpreterState *interp = _PyInterpreterState_GET();
2114
299
        int max_str_digits = interp->long_state.max_str_digits;
2115
299
        if ((max_str_digits > 0) &&
2116
299
            (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
299
    }
2122
2123
8.10M
#if WITH_PYLONG_MODULE
2124
8.10M
    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
8.10M
#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
8.10M
    d = (33 * _PyLong_DECIMAL_SHIFT) /
2149
8.10M
        (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT);
2150
8.10M
    assert(size_a < PY_SSIZE_T_MAX/2);
2151
8.10M
    size = 1 + size_a + size_a / d;
2152
8.10M
    scratch = long_alloc(size);
2153
8.10M
    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
8.10M
    pin = a->long_value.ob_digit;
2160
8.10M
    pout = scratch->long_value.ob_digit;
2161
8.10M
    size = 0;
2162
15.6M
    for (i = size_a; --i >= 0; ) {
2163
7.59M
        digit hi = pin[i];
2164
9.43M
        for (j = 0; j < size; j++) {
2165
1.83M
            twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi;
2166
1.83M
            hi = (digit)(z / _PyLong_DECIMAL_BASE);
2167
1.83M
            pout[j] = (digit)(z - (twodigits)hi *
2168
1.83M
                              _PyLong_DECIMAL_BASE);
2169
1.83M
        }
2170
15.1M
        while (hi) {
2171
7.59M
            pout[size++] = hi % _PyLong_DECIMAL_BASE;
2172
7.59M
            hi /= _PyLong_DECIMAL_BASE;
2173
7.59M
        }
2174
        /* check for keyboard interrupt */
2175
7.59M
        SIGCHECK({
2176
7.59M
                Py_DECREF(scratch);
2177
7.59M
                return -1;
2178
7.59M
            });
2179
7.59M
    }
2180
    /* pout should have at least one digit, so that the case when a = 0
2181
       works correctly */
2182
8.10M
    if (size == 0)
2183
547k
        pout[size++] = 0;
2184
2185
    /* calculate exact length of output string, and allocate */
2186
8.10M
    strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT;
2187
8.10M
    tenpow = 10;
2188
8.10M
    rem = pout[size-1];
2189
28.2M
    while (rem >= tenpow) {
2190
20.1M
        tenpow *= 10;
2191
20.1M
        strlen++;
2192
20.1M
    }
2193
8.10M
    if (strlen > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
2194
313
        PyInterpreterState *interp = _PyInterpreterState_GET();
2195
313
        int max_str_digits = interp->long_state.max_str_digits;
2196
313
        Py_ssize_t strlen_nosign = strlen - negative;
2197
313
        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
313
    }
2204
8.10M
    if (writer) {
2205
8.07M
        if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) {
2206
0
            Py_DECREF(scratch);
2207
0
            return -1;
2208
0
        }
2209
8.07M
    }
2210
26.8k
    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
26.8k
    else {
2218
26.8k
        str = PyUnicode_New(strlen, '9');
2219
26.8k
        if (str == NULL) {
2220
0
            Py_DECREF(scratch);
2221
0
            return -1;
2222
0
        }
2223
26.8k
    }
2224
2225
8.10M
#define WRITE_DIGITS(p)                                               \
2226
8.10M
    do {                                                              \
2227
        /* pout[0] through pout[size-2] contribute exactly            \
2228
           _PyLong_DECIMAL_SHIFT digits each */                       \
2229
8.14M
        for (i=0; i < size - 1; i++) {                                \
2230
45.3k
            rem = pout[i];                                            \
2231
453k
            for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) {             \
2232
408k
                *--p = '0' + rem % 10;                                \
2233
408k
                rem /= 10;                                            \
2234
408k
            }                                                         \
2235
45.3k
        }                                                             \
2236
        /* pout[size-1]: always produce at least one decimal digit */ \
2237
8.10M
        rem = pout[i];                                                \
2238
28.2M
        do {                                                          \
2239
28.2M
            *--p = '0' + rem % 10;                                    \
2240
28.2M
            rem /= 10;                                                \
2241
28.2M
        } while (rem != 0);                                           \
2242
8.10M
                                                                      \
2243
        /* and sign */                                                \
2244
8.10M
        if (negative)                                                 \
2245
8.10M
            *--p = '-';                                               \
2246
8.10M
    } while (0)
2247
2248
8.10M
#define WRITE_UNICODE_DIGITS(TYPE)                                    \
2249
8.10M
    do {                                                              \
2250
8.10M
        if (writer)                                                   \
2251
8.10M
            p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \
2252
8.10M
        else                                                          \
2253
8.10M
            p = (TYPE*)PyUnicode_DATA(str) + strlen;                  \
2254
8.10M
                                                                      \
2255
8.10M
        WRITE_DIGITS(p);                                              \
2256
8.10M
                                                                      \
2257
        /* check we've counted correctly */                           \
2258
8.10M
        if (writer)                                                   \
2259
8.10M
            assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
2260
8.10M
        else                                                          \
2261
8.10M
            assert(p == (TYPE*)PyUnicode_DATA(str));                  \
2262
8.10M
    } while (0)
2263
2264
    /* fill the string right-to-left */
2265
8.10M
    if (bytes_writer) {
2266
0
        char *p = *bytes_str + strlen;
2267
0
        WRITE_DIGITS(p);
2268
0
        assert(p == *bytes_str);
2269
0
    }
2270
8.10M
    else {
2271
8.10M
        int kind = writer ? writer->kind : PyUnicode_KIND(str);
2272
8.10M
        if (kind == PyUnicode_1BYTE_KIND) {
2273
8.10M
            Py_UCS1 *p;
2274
8.10M
            WRITE_UNICODE_DIGITS(Py_UCS1);
2275
8.10M
        }
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
8.10M
    }
2286
2287
8.10M
#undef WRITE_DIGITS
2288
8.10M
#undef WRITE_UNICODE_DIGITS
2289
2290
8.10M
    _Py_DECREF_INT(scratch);
2291
8.10M
    if (writer) {
2292
8.07M
        writer->pos += strlen;
2293
8.07M
    }
2294
26.8k
    else if (bytes_writer) {
2295
0
        (*bytes_str) += strlen;
2296
0
    }
2297
26.8k
    else {
2298
26.8k
        assert(_PyUnicode_CheckConsistency(str, 1));
2299
26.8k
        *p_output = (PyObject *)str;
2300
26.8k
    }
2301
8.10M
    return 0;
2302
8.10M
}
2303
2304
static PyObject *
2305
long_to_decimal_string(PyObject *aa)
2306
26.8k
{
2307
26.8k
    PyObject *v;
2308
26.8k
    if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1)
2309
2
        return NULL;
2310
26.8k
    return v;
2311
26.8k
}
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.91k
{
2323
1.91k
    PyLongObject *a = (PyLongObject *)aa;
2324
1.91k
    PyObject *v = NULL;
2325
1.91k
    Py_ssize_t sz;
2326
1.91k
    Py_ssize_t size_a;
2327
1.91k
    int negative;
2328
1.91k
    int bits;
2329
2330
1.91k
    assert(base == 2 || base == 8 || base == 16);
2331
    // writer or bytes_writer can be used, but not both at the same time.
2332
1.91k
    assert(writer == NULL || bytes_writer == NULL);
2333
1.91k
    if (a == NULL || !PyLong_Check(a)) {
2334
0
        PyErr_BadInternalCall();
2335
0
        return -1;
2336
0
    }
2337
1.91k
    size_a = _PyLong_DigitCount(a);
2338
1.91k
    negative = _PyLong_IsNegative(a);
2339
2340
    /* Compute a rough upper bound for the length of the string */
2341
1.91k
    switch (base) {
2342
1.91k
    case 16:
2343
1.91k
        bits = 4;
2344
1.91k
        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.91k
    }
2354
2355
    /* Compute exact length 'sz' of output string. */
2356
1.91k
    if (size_a == 0) {
2357
8
        sz = 1;
2358
8
    }
2359
1.90k
    else {
2360
1.90k
        Py_ssize_t size_a_in_bits;
2361
        /* Ensure overflow doesn't occur during computation of sz. */
2362
1.90k
        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.90k
        size_a_in_bits = (size_a - 1) * PyLong_SHIFT +
2368
1.90k
                         bit_length_digit(a->long_value.ob_digit[size_a - 1]);
2369
        /* Allow 1 character for a '-' sign. */
2370
1.90k
        sz = negative + (size_a_in_bits + (bits - 1)) / bits;
2371
1.90k
    }
2372
1.91k
    if (alternate) {
2373
        /* 2 characters for prefix  */
2374
1.60k
        sz += 2;
2375
1.60k
    }
2376
2377
1.91k
    if (writer) {
2378
311
        if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
2379
0
            return -1;
2380
311
    }
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.91k
#define WRITE_DIGITS(p)                                                 \
2393
1.91k
    do {                                                                \
2394
1.91k
        if (size_a == 0) {                                              \
2395
8
            *--p = '0';                                                 \
2396
8
        }                                                               \
2397
1.91k
        else {                                                          \
2398
            /* JRH: special case for power-of-2 bases */                \
2399
1.90k
            twodigits accum = 0;                                        \
2400
1.90k
            int accumbits = 0;   /* # of bits in accum */               \
2401
1.90k
            Py_ssize_t i;                                               \
2402
8.88k
            for (i = 0; i < size_a; ++i) {                              \
2403
6.97k
                accum |= (twodigits)a->long_value.ob_digit[i] << accumbits;        \
2404
6.97k
                accumbits += PyLong_SHIFT;                              \
2405
6.97k
                assert(accumbits >= bits);                              \
2406
42.4k
                do {                                                    \
2407
42.4k
                    char cdigit;                                        \
2408
42.4k
                    cdigit = (char)(accum & (base - 1));                \
2409
42.4k
                    cdigit += (cdigit < 10) ? '0' : 'a'-10;             \
2410
42.4k
                    *--p = cdigit;                                      \
2411
42.4k
                    accumbits -= bits;                                  \
2412
42.4k
                    accum >>= bits;                                     \
2413
42.4k
                } while (i < size_a-1 ? accumbits >= bits : accum > 0); \
2414
6.97k
            }                                                           \
2415
1.90k
        }                                                               \
2416
1.91k
                                                                        \
2417
1.91k
        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.91k
        if (negative)                                                   \
2427
1.91k
            *--p = '-';                                                 \
2428
1.91k
    } while (0)
2429
2430
1.91k
#define WRITE_UNICODE_DIGITS(TYPE)                                      \
2431
1.91k
    do {                                                                \
2432
1.91k
        if (writer)                                                     \
2433
1.91k
            p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \
2434
1.91k
        else                                                            \
2435
1.91k
            p = (TYPE*)PyUnicode_DATA(v) + sz;                          \
2436
1.91k
                                                                        \
2437
1.91k
        WRITE_DIGITS(p);                                                \
2438
1.91k
                                                                        \
2439
1.91k
        if (writer)                                                     \
2440
1.91k
            assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
2441
1.91k
        else                                                            \
2442
1.91k
            assert(p == (TYPE*)PyUnicode_DATA(v));                      \
2443
1.91k
    } while (0)
2444
2445
1.91k
    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.91k
    else {
2451
1.91k
        int kind = writer ? writer->kind : PyUnicode_KIND(v);
2452
1.91k
        if (kind == PyUnicode_1BYTE_KIND) {
2453
1.91k
            Py_UCS1 *p;
2454
1.91k
            WRITE_UNICODE_DIGITS(Py_UCS1);
2455
1.91k
        }
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.91k
    }
2466
2467
1.91k
#undef WRITE_DIGITS
2468
1.91k
#undef WRITE_UNICODE_DIGITS
2469
2470
1.91k
    if (writer) {
2471
311
        writer->pos += sz;
2472
311
    }
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.91k
    return 0;
2481
1.91k
}
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
8.07M
{
2502
8.07M
    if (base == 10)
2503
8.07M
        return long_to_decimal_string_internal(obj, NULL, writer,
2504
8.07M
                                               NULL, NULL);
2505
311
    else
2506
311
        return long_format_binary(obj, base, alternate, NULL, writer,
2507
311
                                  NULL, NULL);
2508
8.07M
}
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
278k
{
2572
278k
    const char *p;
2573
278k
    int bits_per_char;
2574
278k
    Py_ssize_t n;
2575
278k
    PyLongObject *z;
2576
278k
    twodigits accum;
2577
278k
    int bits_in_accum;
2578
278k
    digit *pdigit;
2579
2580
278k
    assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
2581
278k
    n = base;
2582
1.66M
    for (bits_per_char = -1; n; ++bits_per_char) {
2583
1.38M
        n >>= 1;
2584
1.38M
    }
2585
2586
    /* n <- the number of Python digits needed,
2587
            = ceiling((digits * bits_per_char) / PyLong_SHIFT). */
2588
278k
    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
278k
    n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT;
2595
278k
    z = long_alloc(n);
2596
278k
    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
278k
    accum = 0;
2604
278k
    bits_in_accum = 0;
2605
278k
    pdigit = z->long_value.ob_digit;
2606
278k
    p = end;
2607
5.93M
    while (--p >= start) {
2608
5.66M
        int k;
2609
5.66M
        if (*p == '_') {
2610
0
            continue;
2611
0
        }
2612
5.66M
        k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)];
2613
5.66M
        assert(k >= 0 && k < base);
2614
5.66M
        accum |= (twodigits)k << bits_in_accum;
2615
5.66M
        bits_in_accum += bits_per_char;
2616
5.66M
        if (bits_in_accum >= PyLong_SHIFT) {
2617
704k
            *pdigit++ = (digit)(accum & PyLong_MASK);
2618
704k
            assert(pdigit - z->long_value.ob_digit <= n);
2619
704k
            accum >>= PyLong_SHIFT;
2620
704k
            bits_in_accum -= PyLong_SHIFT;
2621
704k
            assert(bits_in_accum < PyLong_SHIFT);
2622
704k
        }
2623
5.66M
    }
2624
278k
    if (bits_in_accum) {
2625
277k
        assert(bits_in_accum <= PyLong_SHIFT);
2626
277k
        *pdigit++ = (digit)accum;
2627
277k
        assert(pdigit - z->long_value.ob_digit <= n);
2628
277k
    }
2629
278k
    while (pdigit - z->long_value.ob_digit < n)
2630
0
        *pdigit++ = 0;
2631
278k
    *res = z;
2632
278k
    return 0;
2633
278k
}
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
6.35M
{
2812
6.35M
    twodigits c;           /* current input character */
2813
6.35M
    Py_ssize_t size_z;
2814
6.35M
    int i;
2815
6.35M
    int convwidth;
2816
6.35M
    twodigits convmultmax, convmult;
2817
6.35M
    digit *pz, *pzstop;
2818
6.35M
    PyLongObject *z;
2819
6.35M
    const char *p;
2820
2821
6.35M
    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
6.35M
    double fsize_z = (double)digits * log_base_BASE[base] + 1.0;
2829
6.35M
    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
6.35M
    size_z = (Py_ssize_t)fsize_z;
2837
    /* Uncomment next line to test exceedingly rare copy code */
2838
    /* size_z = 1; */
2839
6.35M
    assert(size_z > 0);
2840
6.35M
    z = long_alloc(size_z);
2841
6.35M
    if (z == NULL) {
2842
0
        *res = NULL;
2843
0
        return 0;
2844
0
    }
2845
6.35M
    _PyLong_SetSignAndDigitCount(z, 0, 0);
2846
2847
    /* `convwidth` consecutive input digits are treated as a single
2848
     * digit in base `convmultmax`.
2849
     */
2850
6.35M
    convwidth = convwidth_base[base];
2851
6.35M
    convmultmax = convmultmax_base[base];
2852
2853
    /* Work ;-) */
2854
6.35M
    p = start;
2855
12.9M
    while (p < end) {
2856
6.59M
        if (*p == '_') {
2857
37
            p++;
2858
37
            continue;
2859
37
        }
2860
        /* grab up to convwidth digits from the input string */
2861
6.59M
        c = (digit)_PyLong_DigitValue[Py_CHARMASK(*p++)];
2862
8.89M
        for (i = 1; i < convwidth && p != end; ++p) {
2863
2.30M
            if (*p == '_') {
2864
179
                continue;
2865
179
            }
2866
2.30M
            i++;
2867
2.30M
            c = (twodigits)(c *  base +
2868
2.30M
                            (int)_PyLong_DigitValue[Py_CHARMASK(*p)]);
2869
2.30M
            assert(c < PyLong_BASE);
2870
2.30M
        }
2871
2872
6.59M
        convmult = convmultmax;
2873
        /* Calculate the shift only if we couldn't get
2874
         * convwidth digits.
2875
         */
2876
6.59M
        if (i != convwidth) {
2877
6.35M
            convmult = base;
2878
6.75M
            for ( ; i > 1; --i) {
2879
401k
                convmult *= base;
2880
401k
            }
2881
6.35M
        }
2882
2883
        /* Multiply z by convmult, and add c. */
2884
6.59M
        pz = z->long_value.ob_digit;
2885
6.59M
        pzstop = pz + _PyLong_DigitCount(z);
2886
12.9M
        for (; pz < pzstop; ++pz) {
2887
6.30M
            c += (twodigits)*pz * convmult;
2888
6.30M
            *pz = (digit)(c & PyLong_MASK);
2889
6.30M
            c >>= PyLong_SHIFT;
2890
6.30M
        }
2891
        /* carry off the current end? */
2892
6.59M
        if (c) {
2893
4.84M
            assert(c < PyLong_BASE);
2894
4.84M
            if (_PyLong_DigitCount(z) < size_z) {
2895
4.84M
                *pz = (digit)c;
2896
4.84M
                assert(!_PyLong_IsNegative(z));
2897
4.84M
                _PyLong_SetSignAndDigitCount(z, 1, _PyLong_DigitCount(z) + 1);
2898
4.84M
            }
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
4.84M
        }
2917
6.59M
    }
2918
6.35M
    *res = z;
2919
6.35M
    return 0;
2920
6.35M
}
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
6.63M
{
2944
6.63M
    const char *start, *end, *p;
2945
6.63M
    char prev = 0;
2946
6.63M
    Py_ssize_t digits = 0;
2947
6.63M
    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
6.63M
    start = p = *str;
2957
    /* Leading underscore not allowed. */
2958
6.63M
    if (*start == '_') {
2959
1
        return -1;
2960
1
    }
2961
    /* Verify all characters are digits and underscores. */
2962
27.5M
    while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') {
2963
20.8M
        if (*p == '_') {
2964
            /* Double underscore not allowed. */
2965
261
            if (prev == '_') {
2966
1
                *str = p - 1;
2967
1
                return -1;
2968
1
            }
2969
20.8M
        } else {
2970
20.8M
            ++digits;
2971
20.8M
        }
2972
20.8M
        prev = *p;
2973
20.8M
        ++p;
2974
20.8M
    }
2975
    /* Trailing underscore not allowed. */
2976
6.63M
    if (prev == '_') {
2977
9
        *str = p - 1;
2978
9
        return -1;
2979
9
    }
2980
6.63M
    *str = end = p;
2981
    /* Reject empty strings */
2982
6.63M
    if (start == end) {
2983
206
        return -1;
2984
206
    }
2985
    /* Allow only trailing whitespace after `end` */
2986
6.64M
    while (*p && Py_ISSPACE(*p)) {
2987
12.4k
        p++;
2988
12.4k
    }
2989
6.63M
    *str = p;
2990
6.63M
    if (*p != '\0') {
2991
57
        return -1;
2992
57
    }
2993
2994
    /*
2995
     * Pass a validated string consisting of only valid digits and underscores
2996
     * to long_from_xxx_base.
2997
     */
2998
6.63M
    if (is_binary_base) {
2999
        /* Use the linear algorithm for binary bases. */
3000
278k
        return long_from_binary_base(start, end, digits, base, res);
3001
278k
    }
3002
6.35M
    else {
3003
        /* Limit the size to avoid excessive computation attacks exploiting the
3004
         * quadratic algorithm. */
3005
6.35M
        if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
3006
1.15k
            PyInterpreterState *interp = _PyInterpreterState_GET();
3007
1.15k
            int max_str_digits = interp->long_state.max_str_digits;
3008
1.15k
            if ((max_str_digits > 0) && (digits > max_str_digits)) {
3009
49
                PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT,
3010
49
                             max_str_digits, digits);
3011
49
                *res = NULL;
3012
49
                return 0;
3013
49
            }
3014
1.15k
        }
3015
6.35M
#if WITH_PYLONG_MODULE
3016
6.35M
        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
6.35M
#endif
3021
        /* Use the quadratic algorithm for non binary bases. */
3022
6.35M
        return long_from_non_binary_base(start, end, digits, base, res);
3023
6.35M
    }
3024
6.63M
}
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
6.63M
{
3037
6.63M
    int sign = 1, error_if_nonzero = 0;
3038
6.63M
    const char *orig_str = str;
3039
6.63M
    PyLongObject *z = NULL;
3040
6.63M
    PyObject *strobj;
3041
6.63M
    Py_ssize_t slen;
3042
3043
6.63M
    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
6.63M
    while (*str != '\0' && Py_ISSPACE(*str)) {
3049
521
        ++str;
3050
521
    }
3051
6.63M
    if (*str == '+') {
3052
20
        ++str;
3053
20
    }
3054
6.63M
    else if (*str == '-') {
3055
18.3k
        ++str;
3056
18.3k
        sign = -1;
3057
18.3k
    }
3058
6.63M
    if (base == 0) {
3059
3.44k
        if (str[0] != '0') {
3060
1.66k
            base = 10;
3061
1.66k
        }
3062
1.78k
        else if (str[1] == 'x' || str[1] == 'X') {
3063
1.30k
            base = 16;
3064
1.30k
        }
3065
479
        else if (str[1] == 'o' || str[1] == 'O') {
3066
336
            base = 8;
3067
336
        }
3068
143
        else if (str[1] == 'b' || str[1] == 'B') {
3069
143
            base = 2;
3070
143
        }
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.44k
    }
3078
6.63M
    if (str[0] == '0' &&
3079
6.63M
        ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
3080
1.76M
         (base == 8  && (str[1] == 'o' || str[1] == 'O')) ||
3081
1.76M
         (base == 2  && (str[1] == 'b' || str[1] == 'B')))) {
3082
1.78k
        str += 2;
3083
        /* One underscore allowed here. */
3084
1.78k
        if (*str == '_') {
3085
0
            ++str;
3086
0
        }
3087
1.78k
    }
3088
3089
    /* long_from_string_base is the main workhorse here. */
3090
6.63M
    int ret = long_from_string_base(&str, base, &z);
3091
6.63M
    if (ret == -1) {
3092
        /* Syntax error. */
3093
274
        goto onError;
3094
274
    }
3095
6.63M
    if (z == NULL) {
3096
        /* Error. exception already set. */
3097
49
        return NULL;
3098
49
    }
3099
3100
6.63M
    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
6.63M
    if (sign < 0) {
3113
18.3k
        _PyLong_FlipSign(z);
3114
18.3k
    }
3115
6.63M
    long_normalize(z);
3116
6.63M
    z = maybe_small_long(z);
3117
3118
6.63M
    if (pend != NULL) {
3119
3.05M
        *pend = (char *)str;
3120
3.05M
    }
3121
6.63M
    return (PyObject *) z;
3122
3123
274
  onError:
3124
274
    if (pend != NULL) {
3125
274
        *pend = (char *)str;
3126
274
    }
3127
274
    Py_XDECREF(z);
3128
274
    slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
3129
274
    strobj = PyUnicode_FromStringAndSize(orig_str, slen);
3130
274
    if (strobj == NULL) {
3131
0
        return NULL;
3132
0
    }
3133
274
    PyErr_Format(PyExc_ValueError,
3134
274
                 "invalid literal for int() with base %d: %.200R",
3135
274
                 base, strobj);
3136
274
    Py_DECREF(strobj);
3137
274
    return NULL;
3138
274
}
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.12k
{
3148
1.12k
    PyObject *result, *strobj;
3149
1.12k
    char *end = NULL;
3150
3151
1.12k
    result = PyLong_FromString(s, &end, base);
3152
1.12k
    if (end == NULL || (result != NULL && end == s + len))
3153
1.12k
        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.12k
}
3164
3165
PyObject *
3166
PyLong_FromUnicodeObject(PyObject *u, int base)
3167
3.05M
{
3168
3.05M
    PyObject *result, *asciidig;
3169
3.05M
    const char *buffer;
3170
3.05M
    char *end = NULL;
3171
3.05M
    Py_ssize_t buflen;
3172
3173
3.05M
    asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
3174
3.05M
    if (asciidig == NULL)
3175
0
        return NULL;
3176
3.05M
    assert(PyUnicode_IS_ASCII(asciidig));
3177
    /* Simply get a pointer to existing ASCII characters. */
3178
3.05M
    buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen);
3179
3.05M
    assert(buffer != NULL);
3180
3181
3.05M
    result = PyLong_FromString(buffer, &end, base);
3182
3.05M
    if (end == NULL || (result != NULL && end == buffer + buflen)) {
3183
3.05M
        Py_DECREF(asciidig);
3184
3.05M
        return result;
3185
3.05M
    }
3186
290
    Py_DECREF(asciidig);
3187
290
    Py_XDECREF(result);
3188
290
    PyErr_Format(PyExc_ValueError,
3189
290
                 "invalid literal for int() with base %d: %.200R",
3190
290
                 base, u);
3191
290
    return NULL;
3192
3.05M
}
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
318k
{
3200
318k
    Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3201
318k
    PyLongObject *z;
3202
3203
318k
    if (size_b == 0) {
3204
0
        PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
3205
0
        return -1;
3206
0
    }
3207
318k
    if (size_a < size_b ||
3208
318k
        (size_a == size_b &&
3209
318k
         a->long_value.ob_digit[size_a-1] < b->long_value.ob_digit[size_b-1])) {
3210
        /* |a| < |b|. */
3211
318k
        *prem = (PyLongObject *)long_long((PyObject *)a);
3212
318k
        if (*prem == NULL) {
3213
0
            return -1;
3214
0
        }
3215
318k
        *pdiv = (PyLongObject*)_PyLong_GetZero();
3216
318k
        return 0;
3217
318k
    }
3218
442
    if (size_b == 1) {
3219
442
        digit rem = 0;
3220
442
        z = divrem1(a, b->long_value.ob_digit[0], &rem);
3221
442
        if (z == NULL)
3222
0
            return -1;
3223
442
        *prem = (PyLongObject *) PyLong_FromLong((long)rem);
3224
442
        if (*prem == NULL) {
3225
0
            Py_DECREF(z);
3226
0
            return -1;
3227
0
        }
3228
442
    }
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
442
    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
442
    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
442
    *pdiv = maybe_small_long(z);
3255
442
    return 0;
3256
442
}
3257
3258
/* Int remainder, top-level routine */
3259
3260
static int
3261
long_rem(PyLongObject *a, PyLongObject *b, PyLongObject **prem)
3262
2.85M
{
3263
2.85M
    Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3264
3265
2.85M
    if (size_b == 0) {
3266
0
        PyErr_SetString(PyExc_ZeroDivisionError,
3267
0
                        "division by zero");
3268
0
        return -1;
3269
0
    }
3270
2.85M
    if (size_a < size_b ||
3271
2.85M
        (size_a == size_b &&
3272
2.85M
         a->long_value.ob_digit[size_a-1] < b->long_value.ob_digit[size_b-1])) {
3273
        /* |a| < |b|. */
3274
2.85M
        *prem = (PyLongObject *)long_long((PyObject *)a);
3275
2.85M
        return -(*prem == NULL);
3276
2.85M
    }
3277
78
    if (size_b == 1) {
3278
78
        *prem = rem1(a, b->long_value.ob_digit[0]);
3279
78
        if (*prem == NULL)
3280
0
            return -1;
3281
78
    }
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
78
    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
78
    return 0;
3298
78
}
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
25.7M
{
3586
25.7M
    if (_PyLong_BothAreCompact(a, b)) {
3587
23.8M
        return _PyLong_CompactValue(a) - _PyLong_CompactValue(b);
3588
23.8M
    }
3589
1.87M
    Py_ssize_t sign = _PyLong_SignedDigitCount(a) - _PyLong_SignedDigitCount(b);
3590
1.87M
    if (sign == 0) {
3591
576k
        Py_ssize_t i = _PyLong_DigitCount(a);
3592
576k
        sdigit diff = 0;
3593
1.73M
        while (--i >= 0) {
3594
1.18M
            diff = (sdigit) a->long_value.ob_digit[i] - (sdigit) b->long_value.ob_digit[i];
3595
1.18M
            if (diff) {
3596
28.0k
                break;
3597
28.0k
            }
3598
1.18M
        }
3599
576k
        sign = _PyLong_IsNegative(a) ? -diff : diff;
3600
576k
    }
3601
1.87M
    return sign;
3602
25.7M
}
3603
3604
static PyObject *
3605
long_richcompare(PyObject *self, PyObject *other, int op)
3606
32.2M
{
3607
32.2M
    Py_ssize_t result;
3608
32.2M
    CHECK_BINOP(self, other);
3609
31.9M
    if (self == other)
3610
6.18M
        result = 0;
3611
25.7M
    else
3612
25.7M
        result = long_compare((PyLongObject*)self, (PyLongObject*)other);
3613
31.9M
    Py_RETURN_RICHCOMPARE(result, 0, op);
3614
31.9M
}
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
673M
{
3620
673M
    PyLongObject *long_object = (PyLongObject *)op;
3621
673M
    int is_small_int = (long_object->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
3622
673M
    assert((!is_small_int) || PyLong_CheckExact(op));
3623
673M
    return is_small_int;
3624
673M
}
3625
3626
void
3627
_PyLong_ExactDealloc(PyObject *self)
3628
102M
{
3629
102M
    assert(PyLong_CheckExact(self));
3630
102M
    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
102M
    if (_PyLong_IsCompact((PyLongObject *)self)) {
3636
95.3M
        _Py_FREELIST_FREE(ints, self, PyObject_Free);
3637
95.3M
        return;
3638
95.3M
    }
3639
7.55M
    PyObject_Free(self);
3640
7.55M
}
3641
3642
static void
3643
long_dealloc(PyObject *self)
3644
570M
{
3645
570M
    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
570M
    if (PyLong_CheckExact(self) && _PyLong_IsCompact((PyLongObject *)self)) {
3656
567M
        _Py_FREELIST_FREE(ints, self, PyObject_Free);
3657
567M
        return;
3658
567M
    }
3659
2.38M
    Py_TYPE(self)->tp_free(self);
3660
2.38M
}
3661
3662
static Py_hash_t
3663
long_hash(PyObject *obj)
3664
210M
{
3665
210M
    PyLongObject *v = (PyLongObject *)obj;
3666
210M
    Py_uhash_t x;
3667
210M
    Py_ssize_t i;
3668
210M
    int sign;
3669
3670
210M
    if (_PyLong_IsCompact(v)) {
3671
206M
        x = (Py_uhash_t)_PyLong_CompactValue(v);
3672
206M
        if (x == (Py_uhash_t)-1) {
3673
323k
            x = (Py_uhash_t)-2;
3674
323k
        }
3675
206M
        return x;
3676
206M
    }
3677
3.82M
    i = _PyLong_DigitCount(v);
3678
3.82M
    sign = _PyLong_NonCompactSign(v);
3679
3.82M
    x = 0;
3680
11.9M
    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
8.13M
        x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) |
3707
8.13M
            (x >> (_PyHASH_BITS - PyLong_SHIFT));
3708
8.13M
        x += v->long_value.ob_digit[i];
3709
8.13M
        if (x >= _PyHASH_MODULUS)
3710
11.4k
            x -= _PyHASH_MODULUS;
3711
8.13M
    }
3712
3.82M
    x = x * sign;
3713
3.82M
    if (x == (Py_uhash_t)-1)
3714
0
        x = (Py_uhash_t)-2;
3715
3.82M
    return (Py_hash_t)x;
3716
210M
}
3717
3718
3719
/* Add the absolute values of two integers. */
3720
3721
static PyLongObject *
3722
x_add(PyLongObject *a, PyLongObject *b)
3723
78.3k
{
3724
78.3k
    Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3725
78.3k
    PyLongObject *z;
3726
78.3k
    Py_ssize_t i;
3727
78.3k
    digit carry = 0;
3728
3729
    /* Ensure a is the larger of the two: */
3730
78.3k
    if (size_a < size_b) {
3731
7.92k
        { PyLongObject *temp = a; a = b; b = temp; }
3732
7.92k
        { Py_ssize_t size_temp = size_a;
3733
7.92k
            size_a = size_b;
3734
7.92k
            size_b = size_temp; }
3735
7.92k
    }
3736
78.3k
    z = long_alloc(size_a+1);
3737
78.3k
    if (z == NULL)
3738
0
        return NULL;
3739
12.7M
    for (i = 0; i < size_b; ++i) {
3740
12.7M
        carry += a->long_value.ob_digit[i] + b->long_value.ob_digit[i];
3741
12.7M
        z->long_value.ob_digit[i] = carry & PyLong_MASK;
3742
12.7M
        carry >>= PyLong_SHIFT;
3743
12.7M
    }
3744
124k
    for (; i < size_a; ++i) {
3745
46.1k
        carry += a->long_value.ob_digit[i];
3746
46.1k
        z->long_value.ob_digit[i] = carry & PyLong_MASK;
3747
46.1k
        carry >>= PyLong_SHIFT;
3748
46.1k
    }
3749
78.3k
    z->long_value.ob_digit[i] = carry;
3750
78.3k
    return long_normalize(z);
3751
78.3k
}
3752
3753
/* Subtract the absolute values of two integers. */
3754
3755
static PyLongObject *
3756
x_sub(PyLongObject *a, PyLongObject *b)
3757
643
{
3758
643
    Py_ssize_t size_a = _PyLong_DigitCount(a), size_b = _PyLong_DigitCount(b);
3759
643
    PyLongObject *z;
3760
643
    Py_ssize_t i;
3761
643
    int sign = 1;
3762
643
    digit borrow = 0;
3763
3764
    /* Ensure a is the larger of the two: */
3765
643
    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
643
    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
643
    z = long_alloc(size_a);
3786
643
    if (z == NULL)
3787
0
        return NULL;
3788
1.27k
    for (i = 0; i < size_b; ++i) {
3789
        /* The following assumes unsigned arithmetic
3790
           works module 2**N for some N>PyLong_SHIFT. */
3791
627
        borrow = a->long_value.ob_digit[i] - b->long_value.ob_digit[i] - borrow;
3792
627
        z->long_value.ob_digit[i] = borrow & PyLong_MASK;
3793
627
        borrow >>= PyLong_SHIFT;
3794
627
        borrow &= 1; /* Keep only one sign bit */
3795
627
    }
3796
11.7k
    for (; i < size_a; ++i) {
3797
11.1k
        borrow = a->long_value.ob_digit[i] - borrow;
3798
11.1k
        z->long_value.ob_digit[i] = borrow & PyLong_MASK;
3799
11.1k
        borrow >>= PyLong_SHIFT;
3800
11.1k
        borrow &= 1; /* Keep only one sign bit */
3801
11.1k
    }
3802
643
    assert(borrow == 0);
3803
643
    if (sign < 0) {
3804
0
        _PyLong_FlipSign(z);
3805
0
    }
3806
643
    return maybe_small_long(long_normalize(z));
3807
643
}
3808
3809
static PyLongObject *
3810
long_add(PyLongObject *a, PyLongObject *b)
3811
127k
{
3812
127k
    if (_PyLong_BothAreCompact(a, b)) {
3813
49.0k
        stwodigits z = medium_value(a) + medium_value(b);
3814
49.0k
        return _PyLong_FromSTwoDigits(z);
3815
49.0k
    }
3816
3817
78.5k
    PyLongObject *z;
3818
78.5k
    if (_PyLong_IsNegative(a)) {
3819
189
        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
189
        else
3831
189
            z = x_sub(b, a);
3832
189
    }
3833
78.3k
    else {
3834
78.3k
        if (_PyLong_IsNegative(b))
3835
25
            z = x_sub(a, b);
3836
78.3k
        else
3837
78.3k
            z = x_add(a, b);
3838
78.3k
    }
3839
78.5k
    return z;
3840
127k
}
3841
3842
_PyStackRef
3843
_PyCompactLong_Add(PyLongObject *a, PyLongObject *b)
3844
315M
{
3845
315M
    assert(_PyLong_BothAreCompact(a, b));
3846
315M
    stwodigits v = medium_value(a) + medium_value(b);
3847
315M
    return medium_from_stwodigits(v);
3848
315M
}
3849
3850
static PyObject *
3851
long_add_method(PyObject *a, PyObject *b)
3852
127k
{
3853
127k
    CHECK_BINOP(a, b);
3854
127k
    return (PyObject*)long_add((PyLongObject*)a, (PyLongObject*)b);
3855
127k
}
3856
3857
3858
static PyLongObject *
3859
long_sub(PyLongObject *a, PyLongObject *b)
3860
734
{
3861
734
    if (_PyLong_BothAreCompact(a, b)) {
3862
305
        return _PyLong_FromSTwoDigits(medium_value(a) - medium_value(b));
3863
305
    }
3864
3865
429
    PyLongObject *z;
3866
429
    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
429
    else {
3879
429
        if (_PyLong_IsNegative(b))
3880
0
            z = x_add(a, b);
3881
429
        else
3882
429
            z = x_sub(a, b);
3883
429
    }
3884
429
    return z;
3885
734
}
3886
3887
_PyStackRef
3888
_PyCompactLong_Subtract(PyLongObject *a, PyLongObject *b)
3889
174M
{
3890
174M
    assert(_PyLong_BothAreCompact(a, b));
3891
174M
    stwodigits v = medium_value(a) - medium_value(b);
3892
174M
    return medium_from_stwodigits(v);
3893
174M
}
3894
3895
static PyObject *
3896
long_sub_method(PyObject *a, PyObject *b)
3897
734
{
3898
734
    CHECK_BINOP(a, b);
3899
734
    return (PyObject*)long_sub((PyLongObject*)a, (PyLongObject*)b);
3900
734
}
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
139k
{
3909
139k
    PyLongObject *z;
3910
139k
    Py_ssize_t size_a = _PyLong_DigitCount(a);
3911
139k
    Py_ssize_t size_b = _PyLong_DigitCount(b);
3912
139k
    Py_ssize_t i;
3913
3914
139k
    z = long_alloc(size_a + size_b);
3915
139k
    if (z == NULL)
3916
0
        return NULL;
3917
3918
139k
    memset(z->long_value.ob_digit, 0, _PyLong_DigitCount(z) * sizeof(digit));
3919
139k
    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
139k
    else {      /* a is not the same as b -- gradeschool int mult */
3978
279k
        for (i = 0; i < size_a; ++i) {
3979
139k
            twodigits carry = 0;
3980
139k
            twodigits f = a->long_value.ob_digit[i];
3981
139k
            digit *pz = z->long_value.ob_digit + i;
3982
139k
            digit *pb = b->long_value.ob_digit;
3983
139k
            digit *pbend = b->long_value.ob_digit + size_b;
3984
3985
139k
            SIGCHECK({
3986
139k
                    Py_DECREF(z);
3987
139k
                    return NULL;
3988
139k
                });
3989
3990
25.5M
            while (pb < pbend) {
3991
25.4M
                carry += *pz + *pb++ * f;
3992
25.4M
                *pz++ = (digit)(carry & PyLong_MASK);
3993
25.4M
                carry >>= PyLong_SHIFT;
3994
25.4M
                assert(carry <= PyLong_MASK);
3995
25.4M
            }
3996
139k
            if (carry)
3997
30.1k
                *pz += (digit)(carry & PyLong_MASK);
3998
139k
            assert((carry >> PyLong_SHIFT) == 0);
3999
139k
        }
4000
139k
    }
4001
139k
    return long_normalize(z);
4002
139k
}
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
139k
{
4048
139k
    Py_ssize_t asize = _PyLong_DigitCount(a);
4049
139k
    Py_ssize_t bsize = _PyLong_DigitCount(b);
4050
139k
    PyLongObject *ah = NULL;
4051
139k
    PyLongObject *al = NULL;
4052
139k
    PyLongObject *bh = NULL;
4053
139k
    PyLongObject *bl = NULL;
4054
139k
    PyLongObject *ret = NULL;
4055
139k
    PyLongObject *t1, *t2, *t3;
4056
139k
    Py_ssize_t shift;           /* the number of digits we split off */
4057
139k
    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
139k
    if (asize > bsize) {
4071
76.3k
        t1 = a;
4072
76.3k
        a = b;
4073
76.3k
        b = t1;
4074
4075
76.3k
        i = asize;
4076
76.3k
        asize = bsize;
4077
76.3k
        bsize = i;
4078
76.3k
    }
4079
4080
    /* Use gradeschool math when either number is too small. */
4081
139k
    i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
4082
139k
    if (asize <= i) {
4083
139k
        if (asize == 0)
4084
15
            return (PyLongObject *)PyLong_FromLong(0);
4085
139k
        else
4086
139k
            return x_mul(a, b);
4087
139k
    }
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
141k
{
4322
    /* fast path for single-digit multiplication */
4323
141k
    if (_PyLong_BothAreCompact(a, b)) {
4324
1.38k
        stwodigits v = medium_value(a) * medium_value(b);
4325
1.38k
        return _PyLong_FromSTwoDigits(v);
4326
1.38k
    }
4327
4328
139k
    PyLongObject *z = k_mul(a, b);
4329
    /* Negate if exactly one of the inputs is negative. */
4330
139k
    if (!_PyLong_SameSign(a, b) && z) {
4331
15
        _PyLong_Negate(&z);
4332
15
    }
4333
139k
    return z;
4334
141k
}
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
1.57M
{
4341
1.57M
    assert(_PyLong_BothAreCompact(a, b));
4342
1.57M
    stwodigits v = medium_value(a) * medium_value(b);
4343
1.57M
    return medium_from_stwodigits(v);
4344
1.57M
}
4345
4346
static PyObject *
4347
long_mul_method(PyObject *a, PyObject *b)
4348
492k
{
4349
492k
    CHECK_BINOP(a, b);
4350
140k
    return (PyObject*)long_mul((PyLongObject*)a, (PyLongObject*)b);
4351
492k
}
4352
4353
/* Fast modulo division for single-digit longs. */
4354
static PyObject *
4355
fast_mod(PyLongObject *a, PyLongObject *b)
4356
638k
{
4357
638k
    sdigit left = a->long_value.ob_digit[0];
4358
638k
    sdigit right = b->long_value.ob_digit[0];
4359
638k
    sdigit mod;
4360
4361
638k
    assert(_PyLong_DigitCount(a) == 1);
4362
638k
    assert(_PyLong_DigitCount(b) == 1);
4363
638k
    sdigit sign = _PyLong_CompactSign(b);
4364
638k
    if (_PyLong_SameSign(a, b)) {
4365
638k
        mod = left % right;
4366
638k
    }
4367
0
    else {
4368
        /* Either 'a' or 'b' is negative. */
4369
0
        mod = right - 1 - (left - 1) % right;
4370
0
    }
4371
4372
638k
    return PyLong_FromLong(mod * sign);
4373
638k
}
4374
4375
/* Fast floor division for single-digit longs. */
4376
static PyObject *
4377
fast_floor_div(PyLongObject *a, PyLongObject *b)
4378
1.42M
{
4379
1.42M
    sdigit left = a->long_value.ob_digit[0];
4380
1.42M
    sdigit right = b->long_value.ob_digit[0];
4381
1.42M
    sdigit div;
4382
4383
1.42M
    assert(_PyLong_DigitCount(a) == 1);
4384
1.42M
    assert(_PyLong_DigitCount(b) == 1);
4385
4386
1.42M
    if (_PyLong_SameSign(a, b)) {
4387
1.42M
        div = left / right;
4388
1.42M
    }
4389
0
    else {
4390
        /* Either 'a' or 'b' is negative. */
4391
0
        div = -1 - (left - 1) / right;
4392
0
    }
4393
4394
1.42M
    return PyLong_FromLong(div);
4395
1.42M
}
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
318k
{
4462
318k
    PyLongObject *div, *mod;
4463
4464
318k
    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
318k
#if WITH_PYLONG_MODULE
4489
318k
    Py_ssize_t size_v = _PyLong_DigitCount(v); /* digits in numerator */
4490
318k
    Py_ssize_t size_w = _PyLong_DigitCount(w); /* digits in denominator */
4491
318k
    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
318k
#endif
4500
318k
    if (long_divrem(v, w, &div, &mod) < 0)
4501
0
        return -1;
4502
318k
    if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) ||
4503
318k
        (_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
318k
    if (pdiv != NULL)
4520
318k
        *pdiv = div;
4521
0
    else
4522
0
        Py_DECREF(div);
4523
4524
318k
    if (pmod != NULL)
4525
0
        *pmod = mod;
4526
318k
    else
4527
318k
        Py_DECREF(mod);
4528
4529
318k
    return 0;
4530
318k
}
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
3.49M
{
4539
3.49M
    PyLongObject *mod;
4540
4541
3.49M
    assert(pmod);
4542
3.49M
    if (_PyLong_DigitCount(v) == 1 && _PyLong_DigitCount(w) == 1) {
4543
        /* Fast path for single-digit longs */
4544
638k
        *pmod = (PyLongObject *)fast_mod(v, w);
4545
638k
        return -(*pmod == NULL);
4546
638k
    }
4547
2.85M
    if (long_rem(v, w, &mod) < 0)
4548
0
        return -1;
4549
2.85M
    if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) ||
4550
2.85M
        (_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
2.85M
    *pmod = mod;
4558
4559
2.85M
    return 0;
4560
2.85M
}
4561
4562
static PyObject *
4563
long_div(PyObject *a, PyObject *b)
4564
1.73M
{
4565
1.73M
    PyLongObject *div;
4566
4567
1.73M
    CHECK_BINOP(a, b);
4568
4569
1.73M
    if (_PyLong_DigitCount((PyLongObject*)a) == 1 && _PyLong_DigitCount((PyLongObject*)b) == 1) {
4570
1.42M
        return fast_floor_div((PyLongObject*)a, (PyLongObject*)b);
4571
1.42M
    }
4572
4573
318k
    if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
4574
0
        div = NULL;
4575
318k
    return (PyObject *)div;
4576
1.73M
}
4577
4578
/* PyLong/PyLong -> float, with correctly rounded result. */
4579
4580
46.5k
#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
11.6k
{
4586
11.6k
    PyLongObject *a, *b, *x;
4587
11.6k
    Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits;
4588
11.6k
    digit mask, low;
4589
11.6k
    int inexact, negate, a_is_small, b_is_small;
4590
11.6k
    double dx, result;
4591
4592
11.6k
    CHECK_BINOP(v, w);
4593
11.6k
    a = (PyLongObject *)v;
4594
11.6k
    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
11.6k
    a_size = _PyLong_DigitCount(a);
4685
11.6k
    b_size = _PyLong_DigitCount(b);
4686
11.6k
    negate = (_PyLong_IsNegative(a)) != (_PyLong_IsNegative(b));
4687
11.6k
    if (b_size == 0) {
4688
0
        PyErr_SetString(PyExc_ZeroDivisionError,
4689
0
                        "division by zero");
4690
0
        goto error;
4691
0
    }
4692
11.6k
    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
11.6k
    a_is_small = a_size <= MANT_DIG_DIGITS ||
4700
11.6k
        (a_size == MANT_DIG_DIGITS+1 &&
4701
0
         a->long_value.ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4702
11.6k
    b_is_small = b_size <= MANT_DIG_DIGITS ||
4703
11.6k
        (b_size == MANT_DIG_DIGITS+1 &&
4704
0
         b->long_value.ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4705
11.6k
    if (a_is_small && b_is_small) {
4706
11.6k
        double da, db;
4707
11.6k
        da = a->long_value.ob_digit[--a_size];
4708
11.6k
        while (a_size > 0)
4709
0
            da = da * PyLong_BASE + a->long_value.ob_digit[--a_size];
4710
11.6k
        db = b->long_value.ob_digit[--b_size];
4711
11.6k
        while (b_size > 0)
4712
0
            db = db * PyLong_BASE + b->long_value.ob_digit[--b_size];
4713
11.6k
        result = da / db;
4714
11.6k
        goto success;
4715
11.6k
    }
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
11.6k
  success:
4828
11.6k
    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
3.49M
{
4843
3.49M
    PyLongObject *mod;
4844
4845
3.49M
    CHECK_BINOP(a, b);
4846
4847
3.49M
    if (l_mod((PyLongObject*)a, (PyLongObject*)b, &mod) < 0)
4848
0
        mod = NULL;
4849
3.49M
    return (PyObject *)mod;
4850
3.49M
}
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
49
{
4960
49
    PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
4961
49
    int negativeOutput = 0;  /* if x<0 return negative output */
4962
4963
49
    PyLongObject *z = NULL;  /* accumulated result */
4964
49
    Py_ssize_t i, j;             /* counters */
4965
49
    PyLongObject *temp = NULL;
4966
49
    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
49
    PyLongObject *table[EXP_TABLE_LEN];
4977
49
    Py_ssize_t num_table_entries = 0;
4978
4979
    /* a, b, c = v, w, x */
4980
49
    CHECK_BINOP(v, w);
4981
49
    a = (PyLongObject*)Py_NewRef(v);
4982
49
    b = (PyLongObject*)Py_NewRef(w);
4983
49
    if (PyLong_Check(x)) {
4984
0
        c = (PyLongObject *)Py_NewRef(x);
4985
0
    }
4986
49
    else if (x == Py_None)
4987
49
        c = NULL;
4988
0
    else {
4989
0
        Py_DECREF(a);
4990
0
        Py_DECREF(b);
4991
0
        Py_RETURN_NOTIMPLEMENTED;
4992
0
    }
4993
4994
49
    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
47
    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
47
    z = (PyLongObject *)PyLong_FromLong(1L);
5075
47
    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
47
#define REDUCE(X)                                       \
5082
256
    do {                                                \
5083
256
        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
256
    } while(0)
5091
5092
    /* Multiply two values, then reduce the result:
5093
       result = X*Y % c.  If c is NULL, skip the mod. */
5094
47
#define MULT(X, Y, result)                      \
5095
256
    do {                                        \
5096
256
        temp = (PyLongObject *)long_mul(X, Y);  \
5097
256
        if (temp == NULL)                       \
5098
256
            goto Error;                         \
5099
256
        Py_XDECREF(result);                     \
5100
256
        result = temp;                          \
5101
256
        temp = NULL;                            \
5102
256
        REDUCE(result);                         \
5103
256
    } while(0)
5104
5105
47
    i = _PyLong_SignedDigitCount(b);
5106
47
    digit bi = i ? b->long_value.ob_digit[i-1] : 0;
5107
47
    digit bit;
5108
47
    if (i <= 1 && bi <= 3) {
5109
        /* aim for minimal overhead */
5110
0
        if (bi >= 2) {
5111
0
            MULT(a, a, z);
5112
0
            if (bi == 3) {
5113
0
                MULT(z, a, z);
5114
0
            }
5115
0
        }
5116
0
        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
0
            MULT(a, z, z);
5122
0
        }
5123
        /* else bi is 0, and z==1 is correct */
5124
0
    }
5125
47
    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
47
        assert(bi);  /* else there is no significant bit */
5133
47
        Py_SETREF(z, (PyLongObject*)Py_NewRef(a));
5134
251
        for (bit = 2; ; bit <<= 1) {
5135
251
            if (bit > bi) { /* found the first bit */
5136
47
                assert((bi & bit) == 0);
5137
47
                bit >>= 1;
5138
47
                assert(bi & bit);
5139
47
                break;
5140
47
            }
5141
251
        }
5142
47
        for (--i, bit >>= 1;;) {
5143
251
            for (; bit != 0; bit >>= 1) {
5144
204
                MULT(z, z, z);
5145
204
                if (bi & bit) {
5146
52
                    MULT(z, a, z);
5147
52
                }
5148
204
            }
5149
47
            if (--i < 0) {
5150
47
                break;
5151
47
            }
5152
0
            bi = b->long_value.ob_digit[i];
5153
0
            bit = (digit)1 << (PyLong_SHIFT-1);
5154
0
        }
5155
47
    }
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
47
    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
47
    goto Done;
5223
5224
47
  Error:
5225
0
    Py_CLEAR(z);
5226
    /* fall through */
5227
47
  Done:
5228
47
    for (i = 0; i < num_table_entries; ++i)
5229
0
        Py_DECREF(table[i]);
5230
47
    Py_DECREF(a);
5231
47
    Py_DECREF(b);
5232
47
    Py_XDECREF(c);
5233
47
    Py_XDECREF(a2);
5234
47
    Py_XDECREF(temp);
5235
47
    return (PyObject *)z;
5236
0
}
5237
5238
static PyObject *
5239
long_invert(PyObject *self)
5240
270
{
5241
270
    PyLongObject *v = _PyLong_CAST(self);
5242
5243
    /* Implement ~x as -(x+1) */
5244
270
    if (_PyLong_IsCompact(v))
5245
270
        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
7.50k
{
5259
7.50k
    if (_PyLong_IsCompact(v)) {
5260
7.50k
        return _PyLong_FromSTwoDigits(-medium_value(v));
5261
7.50k
    }
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
7.50k
}
5269
5270
static PyObject *
5271
long_neg_method(PyObject *v)
5272
7.50k
{
5273
7.50k
    return (PyObject*)long_neg(_PyLong_CAST(v));
5274
7.50k
}
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
518k
{
5294
518k
    return !_PyLong_IsZero(_PyLong_CAST(v));
5295
518k
}
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
305
{
5446
305
    PyLongObject *z = NULL;
5447
305
    Py_ssize_t oldsize, newsize, i, j;
5448
305
    twodigits accum;
5449
5450
305
    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
236
    oldsize = _PyLong_DigitCount(a);
5458
236
    newsize = oldsize + wordshift;
5459
236
    if (remshift)
5460
236
        ++newsize;
5461
236
    z = long_alloc(newsize);
5462
236
    if (z == NULL)
5463
0
        return NULL;
5464
236
    if (_PyLong_IsNegative(a)) {
5465
0
        assert(Py_REFCNT(z) == 1);
5466
0
        _PyLong_FlipSign(z);
5467
0
    }
5468
1.31k
    for (i = 0; i < wordshift; i++)
5469
1.08k
        z->long_value.ob_digit[i] = 0;
5470
236
    accum = 0;
5471
526
    for (j = 0; j < oldsize; i++, j++) {
5472
290
        accum |= (twodigits)a->long_value.ob_digit[j] << remshift;
5473
290
        z->long_value.ob_digit[i] = (digit)(accum & PyLong_MASK);
5474
290
        accum >>= PyLong_SHIFT;
5475
290
    }
5476
236
    if (remshift)
5477
236
        z->long_value.ob_digit[newsize-1] = (digit)accum;
5478
0
    else
5479
0
        assert(!accum);
5480
236
    z = long_normalize(z);
5481
236
    return (PyObject *) maybe_small_long(z);
5482
236
}
5483
5484
5485
static PyObject *
5486
long_lshift_method(PyObject *aa, PyObject *bb)
5487
527
{
5488
527
    CHECK_BINOP(aa, bb);
5489
527
    PyLongObject *a = (PyLongObject*)aa;
5490
527
    PyLongObject *b = (PyLongObject*)bb;
5491
5492
527
    if (_PyLong_IsNegative(b)) {
5493
0
        PyErr_SetString(PyExc_ValueError, "negative shift count");
5494
0
        return NULL;
5495
0
    }
5496
527
    if (_PyLong_IsZero(a)) {
5497
222
        return PyLong_FromLong(0);
5498
222
    }
5499
5500
305
    int64_t shiftby;
5501
305
    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
305
    return long_lshift_int64(a, shiftby);
5509
305
}
5510
5511
/* Return a << shiftby. */
5512
static PyObject *
5513
long_lshift_int64(PyLongObject *a, int64_t shiftby)
5514
305
{
5515
305
    assert(shiftby >= 0);
5516
5517
305
    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
305
    Py_ssize_t wordshift = (Py_ssize_t)(shiftby / PyLong_SHIFT);
5528
305
    digit remshift = (digit)(shiftby % PyLong_SHIFT);
5529
305
    return long_lshift1(a, wordshift, remshift);
5530
305
}
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.46k
{
5563
2.46k
    int nega, negb, negz;
5564
2.46k
    Py_ssize_t size_a, size_b, size_z, i;
5565
2.46k
    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.46k
    size_a = _PyLong_DigitCount(a);
5574
2.46k
    nega = _PyLong_IsNegative(a);
5575
2.46k
    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.46k
    else
5583
        /* Keep reference count consistent. */
5584
2.46k
        Py_INCREF(a);
5585
5586
    /* Same for b. */
5587
2.46k
    size_b = _PyLong_DigitCount(b);
5588
2.46k
    negb = _PyLong_IsNegative(b);
5589
2.46k
    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.46k
    else
5599
2.46k
        Py_INCREF(b);
5600
5601
    /* Swap a and b if necessary to ensure size_a >= size_b. */
5602
2.46k
    if (size_a < size_b) {
5603
1.00k
        z = a; a = b; b = z;
5604
1.00k
        size_z = size_a; size_a = size_b; size_b = size_z;
5605
1.00k
        negz = nega; nega = negb; negb = negz;
5606
1.00k
    }
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.46k
    switch (op) {
5616
156
    case '^':
5617
156
        negz = nega ^ negb;
5618
156
        size_z = size_a;
5619
156
        break;
5620
2.24k
    case '&':
5621
2.24k
        negz = nega & negb;
5622
2.24k
        size_z = negb ? size_a : size_b;
5623
2.24k
        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.46k
    }
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.46k
    z = long_alloc(size_z + negz);
5635
2.46k
    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.46k
    switch(op) {
5643
2.24k
    case '&':
5644
6.05k
        for (i = 0; i < size_b; ++i)
5645
3.80k
            z->long_value.ob_digit[i] = a->long_value.ob_digit[i] & b->long_value.ob_digit[i];
5646
2.24k
        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.46k
    }
5658
5659
    /* Copy any remaining digits of a, inverting if necessary. */
5660
2.46k
    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.46k
    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.46k
    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.46k
    Py_DECREF(a);
5675
2.46k
    Py_DECREF(b);
5676
2.46k
    return (PyObject *)maybe_small_long(long_normalize(z));
5677
2.46k
}
5678
5679
static PyObject *
5680
long_and(PyObject *a, PyObject *b)
5681
2.63k
{
5682
2.63k
    CHECK_BINOP(a, b);
5683
2.63k
    PyLongObject *x = (PyLongObject*)a;
5684
2.63k
    PyLongObject *y = (PyLongObject*)b;
5685
2.63k
    if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
5686
388
        return (PyObject*)_PyLong_FromSTwoDigits(medium_value(x) & medium_value(y));
5687
388
    }
5688
2.24k
    return long_bitwise(x, '&', y);
5689
2.63k
}
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
306
{
5706
306
    CHECK_BINOP(a, b);
5707
306
    PyLongObject *x = (PyLongObject*)a;
5708
306
    PyLongObject *y = (PyLongObject*)b;
5709
306
    if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
5710
242
        return (PyObject*)_PyLong_FromSTwoDigits(medium_value(x) | medium_value(y));
5711
242
    }
5712
64
    return long_bitwise(x, '|', y);
5713
306
}
5714
5715
static PyObject *
5716
long_long(PyObject *v)
5717
3.17M
{
5718
3.17M
    if (PyLong_CheckExact(v)) {
5719
3.17M
        return Py_NewRef(v);
5720
3.17M
    }
5721
0
    else {
5722
0
        return _PyLong_Copy((PyLongObject *)v);
5723
0
    }
5724
3.17M
}
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
281k
{
5958
281k
    Py_ssize_t base;
5959
5960
281k
    if (type != &PyLong_Type)
5961
2.10k
        return long_subtype_new(type, x, obase); /* Wimp out */
5962
279k
    if (x == NULL) {
5963
11
        if (obase != NULL) {
5964
0
            PyErr_SetString(PyExc_TypeError,
5965
0
                            "int() missing string argument");
5966
0
            return NULL;
5967
0
        }
5968
11
        return PyLong_FromLong(0L);
5969
11
    }
5970
    /* default base and limit, forward to standard implementation */
5971
279k
    if (obase == NULL)
5972
2.09k
        return PyNumber_Long(x);
5973
5974
277k
    base = PyNumber_AsSsize_t(obase, NULL);
5975
277k
    if (base == -1 && PyErr_Occurred())
5976
0
        return NULL;
5977
277k
    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
277k
    if (PyUnicode_Check(x))
5984
275k
        return PyLong_FromUnicodeObject(x, (int)base);
5985
1.12k
    else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
5986
1.12k
        const char *string;
5987
1.12k
        if (PyByteArray_Check(x))
5988
1.12k
            string = PyByteArray_AS_STRING(x);
5989
0
        else
5990
0
            string = PyBytes_AS_STRING(x);
5991
1.12k
        return _PyLong_FromBytes(string, Py_SIZE(x), (int)base);
5992
1.12k
    }
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
277k
}
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.10k
{
6008
2.10k
    PyLongObject *tmp, *newobj;
6009
2.10k
    Py_ssize_t i, n;
6010
6011
2.10k
    assert(PyType_IsSubtype(type, &PyLong_Type));
6012
2.10k
    tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase);
6013
2.10k
    if (tmp == NULL)
6014
0
        return NULL;
6015
2.10k
    assert(PyLong_Check(tmp));
6016
2.10k
    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.10k
    if (n == 0) {
6020
91
        n = 1;
6021
91
    }
6022
2.10k
    newobj = (PyLongObject *)type->tp_alloc(type, n);
6023
2.10k
    if (newobj == NULL) {
6024
0
        Py_DECREF(tmp);
6025
0
        return NULL;
6026
0
    }
6027
2.10k
    assert(PyLong_Check(newobj));
6028
2.10k
    newobj->long_value.lv_tag = tmp->long_value.lv_tag & ~IMMORTALITY_BIT_MASK;
6029
4.22k
    for (i = 0; i < n; i++) {
6030
2.12k
        newobj->long_value.ob_digit[i] = tmp->long_value.ob_digit[i];
6031
2.12k
    }
6032
2.10k
    Py_DECREF(tmp);
6033
2.10k
    return (PyObject *)newobj;
6034
2.10k
}
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
311
{
6072
311
    _PyUnicodeWriter writer;
6073
311
    int ret;
6074
6075
311
    _PyUnicodeWriter_Init(&writer);
6076
311
    ret = _PyLong_FormatAdvancedWriter(
6077
311
        &writer,
6078
311
        self,
6079
311
        format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
6080
311
    if (ret == -1) {
6081
0
        _PyUnicodeWriter_Dealloc(&writer);
6082
0
        return NULL;
6083
0
    }
6084
311
    return _PyUnicodeWriter_Finish(&writer);
6085
311
}
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
27
{
6280
27
    int64_t nbits = _PyLong_NumBits(self);
6281
27
    assert(nbits >= 0);
6282
27
    assert(!PyErr_Occurred());
6283
27
    return PyLong_FromInt64(nbits);
6284
27
}
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
int.bit_count
6297
6298
Number of ones in the binary representation of the absolute value of self.
6299
6300
Also known as the population count.
6301
6302
>>> bin(13)
6303
'0b1101'
6304
>>> (13).bit_count()
6305
3
6306
[clinic start generated code]*/
6307
6308
static PyObject *
6309
int_bit_count_impl(PyObject *self)
6310
/*[clinic end generated code: output=2e571970daf1e5c3 input=7e0adef8e8ccdf2e]*/
6311
0
{
6312
0
    assert(self != NULL);
6313
0
    assert(PyLong_Check(self));
6314
6315
0
    PyLongObject *z = (PyLongObject *)self;
6316
0
    Py_ssize_t ndigits = _PyLong_DigitCount(z);
6317
0
    int64_t bit_count = 0;
6318
6319
0
    for (Py_ssize_t i = 0; i < ndigits; i++) {
6320
0
        bit_count += popcount_digit(z->long_value.ob_digit[i]);
6321
0
    }
6322
6323
0
    return PyLong_FromInt64(bit_count);
6324
0
}
6325
6326
/*[clinic input]
6327
int.as_integer_ratio
6328
6329
Return a pair of integers, whose ratio is equal to the original int.
6330
6331
The ratio is in lowest terms and has a positive denominator.
6332
6333
>>> (10).as_integer_ratio()
6334
(10, 1)
6335
>>> (-10).as_integer_ratio()
6336
(-10, 1)
6337
>>> (0).as_integer_ratio()
6338
(0, 1)
6339
[clinic start generated code]*/
6340
6341
static PyObject *
6342
int_as_integer_ratio_impl(PyObject *self)
6343
/*[clinic end generated code: output=e60803ae1cc8621a input=384ff1766634bec2]*/
6344
0
{
6345
0
    PyObject *ratio_tuple;
6346
0
    PyObject *numerator = long_long(self);
6347
0
    if (numerator == NULL) {
6348
0
        return NULL;
6349
0
    }
6350
0
    ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_GetOne());
6351
0
    Py_DECREF(numerator);
6352
0
    return ratio_tuple;
6353
0
}
6354
6355
/*[clinic input]
6356
int.to_bytes
6357
6358
    length: Py_ssize_t = 1
6359
        Length of bytes object to use.  An OverflowError is raised if the
6360
        integer is not representable with the given number of bytes.  Default
6361
        is length 1.
6362
    byteorder: unicode(c_default="NULL") = "big"
6363
        The byte order used to represent the integer.  If byteorder is 'big',
6364
        the most significant byte is at the beginning of the byte array.  If
6365
        byteorder is 'little', the most significant byte is at the end of the
6366
        byte array.  To request the native byte order of the host system, use
6367
        sys.byteorder as the byte order value.  Default is to use 'big'.
6368
    *
6369
    signed as is_signed: bool = False
6370
        Determines whether two's complement is used to represent the integer.
6371
        If signed is False and a negative integer is given, an OverflowError
6372
        is raised.
6373
6374
Return an array of bytes representing an integer.
6375
[clinic start generated code]*/
6376
6377
static PyObject *
6378
int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
6379
                  int is_signed)
6380
/*[clinic end generated code: output=89c801df114050a3 input=a0103d0e9ad85c2b]*/
6381
685
{
6382
685
    int little_endian;
6383
685
    PyObject *bytes;
6384
6385
685
    if (byteorder == NULL)
6386
0
        little_endian = 0;
6387
685
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
6388
685
        little_endian = 1;
6389
0
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(big)))
6390
0
        little_endian = 0;
6391
0
    else {
6392
0
        PyErr_SetString(PyExc_ValueError,
6393
0
            "byteorder must be either 'little' or 'big'");
6394
0
        return NULL;
6395
0
    }
6396
6397
685
    if (length < 0) {
6398
0
        PyErr_SetString(PyExc_ValueError,
6399
0
                        "length argument must be non-negative");
6400
0
        return NULL;
6401
0
    }
6402
6403
685
    bytes = PyBytes_FromStringAndSize(NULL, length);
6404
685
    if (bytes == NULL)
6405
0
        return NULL;
6406
6407
685
    if (_PyLong_AsByteArray((PyLongObject *)self,
6408
685
                            (unsigned char *)PyBytes_AS_STRING(bytes),
6409
685
                            length, little_endian, is_signed, 1) < 0) {
6410
0
        Py_DECREF(bytes);
6411
0
        return NULL;
6412
0
    }
6413
6414
685
    return bytes;
6415
685
}
6416
6417
/*[clinic input]
6418
@classmethod
6419
int.from_bytes
6420
6421
    bytes as bytes_obj: object
6422
        Holds the array of bytes to convert.  The argument must either
6423
        support the buffer protocol or be an iterable object producing bytes.
6424
        Bytes and bytearray are examples of built-in objects that support the
6425
        buffer protocol.
6426
    byteorder: unicode(c_default="NULL") = "big"
6427
        The byte order used to represent the integer.  If byteorder is 'big',
6428
        the most significant byte is at the beginning of the byte array.  If
6429
        byteorder is 'little', the most significant byte is at the end of the
6430
        byte array.  To request the native byte order of the host system, use
6431
        sys.byteorder as the byte order value.  Default is to use 'big'.
6432
    *
6433
    signed as is_signed: bool = False
6434
        Indicates whether two's complement is used to represent the integer.
6435
6436
Return the integer represented by the given array of bytes.
6437
[clinic start generated code]*/
6438
6439
static PyObject *
6440
int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
6441
                    PyObject *byteorder, int is_signed)
6442
/*[clinic end generated code: output=efc5d68e31f9314f input=2ff527997fe7b0c5]*/
6443
2.09k
{
6444
2.09k
    int little_endian;
6445
2.09k
    PyObject *long_obj, *bytes;
6446
6447
2.09k
    if (byteorder == NULL)
6448
0
        little_endian = 0;
6449
2.09k
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
6450
1.95k
        little_endian = 1;
6451
132
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(big)))
6452
132
        little_endian = 0;
6453
0
    else {
6454
0
        PyErr_SetString(PyExc_ValueError,
6455
0
            "byteorder must be either 'little' or 'big'");
6456
0
        return NULL;
6457
0
    }
6458
6459
2.09k
    bytes = PyObject_Bytes(bytes_obj);
6460
2.09k
    if (bytes == NULL)
6461
0
        return NULL;
6462
6463
2.09k
    long_obj = _PyLong_FromByteArray(
6464
2.09k
        (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
6465
2.09k
        little_endian, is_signed);
6466
2.09k
    Py_DECREF(bytes);
6467
6468
2.09k
    if (long_obj != NULL && type != &PyLong_Type) {
6469
0
        Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj));
6470
0
    }
6471
6472
2.09k
    return long_obj;
6473
2.09k
}
6474
6475
static PyObject *
6476
long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
6477
0
{
6478
0
    return long_long(self);
6479
0
}
6480
6481
static PyObject *
6482
long_long_getter(PyObject *self, void *Py_UNUSED(ignored))
6483
0
{
6484
0
    return long_long(self);
6485
0
}
6486
6487
/*[clinic input]
6488
int.is_integer
6489
6490
Returns True. Exists for duck type compatibility with float.is_integer.
6491
[clinic start generated code]*/
6492
6493
static PyObject *
6494
int_is_integer_impl(PyObject *self)
6495
/*[clinic end generated code: output=90f8e794ce5430ef input=7e41c4d4416e05f2]*/
6496
0
{
6497
0
    Py_RETURN_TRUE;
6498
0
}
6499
6500
static PyObject *
6501
long_vectorcall(PyObject *type, PyObject * const*args,
6502
                 size_t nargsf, PyObject *kwnames)
6503
3.07M
{
6504
3.07M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
6505
3.07M
    if (kwnames != NULL) {
6506
0
        PyThreadState *tstate = PyThreadState_GET();
6507
0
        return _PyObject_MakeTpCall(tstate, type, args, nargs, kwnames);
6508
0
    }
6509
3.07M
    switch (nargs) {
6510
0
        case 0:
6511
0
            return _PyLong_GetZero();
6512
2.79M
        case 1:
6513
2.79M
            return PyNumber_Long(args[0]);
6514
277k
        case 2:
6515
277k
            return long_new_impl(_PyType_CAST(type), args[0], args[1]);
6516
0
        default:
6517
0
            return PyErr_Format(PyExc_TypeError,
6518
0
                                "int expected at most 2 arguments, got %zd",
6519
0
                                nargs);
6520
3.07M
    }
6521
3.07M
}
6522
6523
static PyMethodDef long_methods[] = {
6524
    {"conjugate",       long_long_meth, METH_NOARGS,
6525
     "Returns self, the complex conjugate of any int."},
6526
    INT_BIT_LENGTH_METHODDEF
6527
    INT_BIT_COUNT_METHODDEF
6528
    INT_TO_BYTES_METHODDEF
6529
    INT_FROM_BYTES_METHODDEF
6530
    INT_AS_INTEGER_RATIO_METHODDEF
6531
    {"__trunc__",       long_long_meth, METH_NOARGS,
6532
     "Truncating an Integral returns itself."},
6533
    {"__floor__",       long_long_meth, METH_NOARGS,
6534
     "Flooring an Integral returns itself."},
6535
    {"__ceil__",        long_long_meth, METH_NOARGS,
6536
     "Ceiling of an Integral returns itself."},
6537
    INT___ROUND___METHODDEF
6538
    INT___GETNEWARGS___METHODDEF
6539
    INT___FORMAT___METHODDEF
6540
    INT___SIZEOF___METHODDEF
6541
    INT_IS_INTEGER_METHODDEF
6542
    {NULL,              NULL}           /* sentinel */
6543
};
6544
6545
static PyGetSetDef long_getset[] = {
6546
    {"real",
6547
     long_long_getter, NULL,
6548
     "the real part of a complex number",
6549
     NULL},
6550
    {"imag",
6551
     long_get0, NULL,
6552
     "the imaginary part of a complex number",
6553
     NULL},
6554
    {"numerator",
6555
     long_long_getter, NULL,
6556
     "the numerator of a rational number in lowest terms",
6557
     NULL},
6558
    {"denominator",
6559
     long_get1, NULL,
6560
     "the denominator of a rational number in lowest terms",
6561
     NULL},
6562
    {NULL}  /* Sentinel */
6563
};
6564
6565
PyDoc_STRVAR(long_doc,
6566
"int([x]) -> integer\n\
6567
int(x, base=10) -> integer\n\
6568
\n\
6569
Convert a number or string to an integer, or return 0 if no arguments\n\
6570
are given.  If x is a number, return x.__int__().  For floating-point\n\
6571
numbers, this truncates towards zero.\n\
6572
\n\
6573
If x is not a number or if base is given, then x must be a string,\n\
6574
bytes, or bytearray instance representing an integer literal in the\n\
6575
given base.  The literal can be preceded by '+' or '-' and be surrounded\n\
6576
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\n\
6577
Base 0 means to interpret the base from the string as an integer literal.\n\
6578
>>> int('0b100', base=0)\n\
6579
4");
6580
6581
static PyNumberMethods long_as_number = {
6582
    long_add_method,            /*nb_add*/
6583
    long_sub_method,            /*nb_subtract*/
6584
    long_mul_method,            /*nb_multiply*/
6585
    long_mod,                   /*nb_remainder*/
6586
    long_divmod,                /*nb_divmod*/
6587
    long_pow,                   /*nb_power*/
6588
    long_neg_method,            /*nb_negative*/
6589
    long_long,                  /*tp_positive*/
6590
    long_abs_method,            /*tp_absolute*/
6591
    long_bool,                  /*tp_bool*/
6592
    long_invert,                /*nb_invert*/
6593
    long_lshift_method,         /*nb_lshift*/
6594
    long_rshift,                /*nb_rshift*/
6595
    long_and,                   /*nb_and*/
6596
    long_xor,                   /*nb_xor*/
6597
    long_or,                    /*nb_or*/
6598
    long_long,                  /*nb_int*/
6599
    0,                          /*nb_reserved*/
6600
    long_float,                 /*nb_float*/
6601
    0,                          /* nb_inplace_add */
6602
    0,                          /* nb_inplace_subtract */
6603
    0,                          /* nb_inplace_multiply */
6604
    0,                          /* nb_inplace_remainder */
6605
    0,                          /* nb_inplace_power */
6606
    0,                          /* nb_inplace_lshift */
6607
    0,                          /* nb_inplace_rshift */
6608
    0,                          /* nb_inplace_and */
6609
    0,                          /* nb_inplace_xor */
6610
    0,                          /* nb_inplace_or */
6611
    long_div,                   /* nb_floor_divide */
6612
    long_true_divide,           /* nb_true_divide */
6613
    0,                          /* nb_inplace_floor_divide */
6614
    0,                          /* nb_inplace_true_divide */
6615
    long_long,                  /* nb_index */
6616
};
6617
6618
PyTypeObject PyLong_Type = {
6619
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
6620
    "int",                                      /* tp_name */
6621
    offsetof(PyLongObject, long_value.ob_digit),  /* tp_basicsize */
6622
    sizeof(digit),                              /* tp_itemsize */
6623
    long_dealloc,                               /* tp_dealloc */
6624
    0,                                          /* tp_vectorcall_offset */
6625
    0,                                          /* tp_getattr */
6626
    0,                                          /* tp_setattr */
6627
    0,                                          /* tp_as_async */
6628
    long_to_decimal_string,                     /* tp_repr */
6629
    &long_as_number,                            /* tp_as_number */
6630
    0,                                          /* tp_as_sequence */
6631
    0,                                          /* tp_as_mapping */
6632
    long_hash,                                  /* tp_hash */
6633
    0,                                          /* tp_call */
6634
    0,                                          /* tp_str */
6635
    PyObject_GenericGetAttr,                    /* tp_getattro */
6636
    0,                                          /* tp_setattro */
6637
    0,                                          /* tp_as_buffer */
6638
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
6639
        Py_TPFLAGS_LONG_SUBCLASS |
6640
        _Py_TPFLAGS_MATCH_SELF,               /* tp_flags */
6641
    long_doc,                                   /* tp_doc */
6642
    0,                                          /* tp_traverse */
6643
    0,                                          /* tp_clear */
6644
    long_richcompare,                           /* tp_richcompare */
6645
    0,                                          /* tp_weaklistoffset */
6646
    0,                                          /* tp_iter */
6647
    0,                                          /* tp_iternext */
6648
    long_methods,                               /* tp_methods */
6649
    0,                                          /* tp_members */
6650
    long_getset,                                /* tp_getset */
6651
    0,                                          /* tp_base */
6652
    0,                                          /* tp_dict */
6653
    0,                                          /* tp_descr_get */
6654
    0,                                          /* tp_descr_set */
6655
    0,                                          /* tp_dictoffset */
6656
    0,                                          /* tp_init */
6657
    0,                                          /* tp_alloc */
6658
    long_new,                                   /* tp_new */
6659
    PyObject_Free,                              /* tp_free */
6660
    .tp_vectorcall = long_vectorcall,
6661
    .tp_version_tag = _Py_TYPE_VERSION_INT,
6662
};
6663
6664
static PyTypeObject Int_InfoType;
6665
6666
PyDoc_STRVAR(int_info__doc__,
6667
"sys.int_info\n\
6668
\n\
6669
A named tuple that holds information about Python's\n\
6670
internal representation of integers.  The attributes are read only.");
6671
6672
static PyStructSequence_Field int_info_fields[] = {
6673
    {"bits_per_digit", "size of a digit in bits"},
6674
    {"sizeof_digit", "size in bytes of the C type used to represent a digit"},
6675
    {"default_max_str_digits", "maximum string conversion digits limitation"},
6676
    {"str_digits_check_threshold", "minimum positive value for int_max_str_digits"},
6677
    {NULL, NULL}
6678
};
6679
6680
static PyStructSequence_Desc int_info_desc = {
6681
    "sys.int_info",   /* name */
6682
    int_info__doc__,  /* doc */
6683
    int_info_fields,  /* fields */
6684
    4                 /* number of fields */
6685
};
6686
6687
PyObject *
6688
PyLong_GetInfo(void)
6689
16
{
6690
16
    PyObject* int_info;
6691
16
    int field = 0;
6692
16
    int_info = PyStructSequence_New(&Int_InfoType);
6693
16
    if (int_info == NULL)
6694
0
        return NULL;
6695
16
    PyStructSequence_SET_ITEM(int_info, field++,
6696
16
                              PyLong_FromLong(PyLong_SHIFT));
6697
16
    PyStructSequence_SET_ITEM(int_info, field++,
6698
16
                              PyLong_FromLong(sizeof(digit)));
6699
    /*
6700
     * The following two fields were added after investigating uses of
6701
     * sys.int_info in the wild: Exceedingly rarely used. The ONLY use found was
6702
     * numba using sys.int_info.bits_per_digit as attribute access rather than
6703
     * sequence unpacking. Cython and sympy also refer to sys.int_info but only
6704
     * as info for debugging. No concern about adding these in a backport.
6705
     */
6706
16
    PyStructSequence_SET_ITEM(int_info, field++,
6707
16
                              PyLong_FromLong(_PY_LONG_DEFAULT_MAX_STR_DIGITS));
6708
16
    PyStructSequence_SET_ITEM(int_info, field++,
6709
16
                              PyLong_FromLong(_PY_LONG_MAX_STR_DIGITS_THRESHOLD));
6710
16
    if (PyErr_Occurred()) {
6711
0
        Py_CLEAR(int_info);
6712
0
        return NULL;
6713
0
    }
6714
16
    return int_info;
6715
16
}
6716
6717
6718
/* runtime lifecycle */
6719
6720
PyStatus
6721
_PyLong_InitTypes(PyInterpreterState *interp)
6722
16
{
6723
    /* initialize int_info */
6724
16
    if (_PyStructSequence_InitBuiltin(interp, &Int_InfoType,
6725
16
                                      &int_info_desc) < 0)
6726
0
    {
6727
0
        return _PyStatus_ERR("can't init int info type");
6728
0
    }
6729
6730
16
    return _PyStatus_OK();
6731
16
}
6732
6733
6734
void
6735
_PyLong_FiniTypes(PyInterpreterState *interp)
6736
0
{
6737
0
    _PyStructSequence_FiniBuiltin(interp, &Int_InfoType);
6738
0
}
6739
6740
#undef PyUnstable_Long_IsCompact
6741
6742
int
6743
0
PyUnstable_Long_IsCompact(const PyLongObject* op) {
6744
0
    return _PyLong_IsCompact((PyLongObject*)op);
6745
0
}
6746
6747
#undef PyUnstable_Long_CompactValue
6748
6749
Py_ssize_t
6750
0
PyUnstable_Long_CompactValue(const PyLongObject* op) {
6751
0
    return _PyLong_CompactValue((PyLongObject*)op);
6752
0
}
6753
6754
6755
PyObject* PyLong_FromInt32(int32_t value)
6756
0
{
6757
0
    PYLONG_FROM_INT(uint32_t, int32_t, value);
6758
0
}
6759
6760
PyObject* PyLong_FromUInt32(uint32_t value)
6761
0
{
6762
0
    PYLONG_FROM_UINT(uint32_t, value);
6763
0
}
6764
6765
PyObject* PyLong_FromInt64(int64_t value)
6766
27
{
6767
27
    PYLONG_FROM_INT(uint64_t, int64_t, value);
6768
27
}
6769
6770
PyObject* PyLong_FromUInt64(uint64_t value)
6771
0
{
6772
0
    PYLONG_FROM_UINT(uint64_t, value);
6773
0
}
6774
6775
#define LONG_TO_INT(obj, value, type_name) \
6776
461
    do { \
6777
461
        int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
6778
461
                     | Py_ASNATIVEBYTES_ALLOW_INDEX); \
6779
461
        Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
6780
461
        if (bytes < 0) { \
6781
0
            return -1; \
6782
0
        } \
6783
461
        if ((size_t)bytes > sizeof(*value)) { \
6784
0
            PyErr_SetString(PyExc_OverflowError, \
6785
0
                            "Python int too large to convert to " type_name); \
6786
0
            return -1; \
6787
0
        } \
6788
461
        return 0; \
6789
461
    } while (0)
6790
6791
int PyLong_AsInt32(PyObject *obj, int32_t *value)
6792
0
{
6793
0
    LONG_TO_INT(obj, value, "C int32_t");
6794
0
}
6795
6796
int PyLong_AsInt64(PyObject *obj, int64_t *value)
6797
461
{
6798
461
    LONG_TO_INT(obj, value, "C int64_t");
6799
461
}
6800
6801
#define LONG_TO_UINT(obj, value, type_name) \
6802
0
    do { \
6803
0
        int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
6804
0
                     | Py_ASNATIVEBYTES_UNSIGNED_BUFFER \
6805
0
                     | Py_ASNATIVEBYTES_REJECT_NEGATIVE \
6806
0
                     | Py_ASNATIVEBYTES_ALLOW_INDEX); \
6807
0
        Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
6808
0
        if (bytes < 0) { \
6809
0
            return -1; \
6810
0
        } \
6811
0
        if ((size_t)bytes > sizeof(*value)) { \
6812
0
            PyErr_SetString(PyExc_OverflowError, \
6813
0
                            "Python int too large to convert to " type_name); \
6814
0
            return -1; \
6815
0
        } \
6816
0
        return 0; \
6817
0
    } while (0)
6818
6819
int PyLong_AsUInt32(PyObject *obj, uint32_t *value)
6820
0
{
6821
0
    LONG_TO_UINT(obj, value, "C uint32_t");
6822
0
}
6823
6824
int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
6825
0
{
6826
0
    LONG_TO_UINT(obj, value, "C uint64_t");
6827
0
}
6828
6829
6830
static const PyLongLayout PyLong_LAYOUT = {
6831
    .bits_per_digit = PyLong_SHIFT,
6832
    .digits_order = -1,  // least significant first
6833
    .digit_endianness = PY_LITTLE_ENDIAN ? -1 : 1,
6834
    .digit_size = sizeof(digit),
6835
};
6836
6837
6838
const PyLongLayout*
6839
PyLong_GetNativeLayout(void)
6840
82
{
6841
82
    return &PyLong_LAYOUT;
6842
82
}
6843
6844
6845
int
6846
PyLong_Export(PyObject *obj, PyLongExport *export_long)
6847
9
{
6848
9
    if (!PyLong_Check(obj)) {
6849
0
        memset(export_long, 0, sizeof(*export_long));
6850
0
        PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
6851
0
        return -1;
6852
0
    }
6853
6854
    // Fast-path: try to convert to a int64_t
6855
9
    int overflow;
6856
9
#if SIZEOF_LONG == 8
6857
9
    long value = PyLong_AsLongAndOverflow(obj, &overflow);
6858
#else
6859
    // Windows has 32-bit long, so use 64-bit long long instead
6860
    long long value = PyLong_AsLongLongAndOverflow(obj, &overflow);
6861
#endif
6862
9
    Py_BUILD_ASSERT(sizeof(value) == sizeof(int64_t));
6863
    // the function cannot fail since obj is a PyLongObject
6864
9
    assert(!(value == -1 && PyErr_Occurred()));
6865
6866
9
    if (!overflow) {
6867
5
        export_long->value = value;
6868
5
        export_long->negative = 0;
6869
5
        export_long->ndigits = 0;
6870
5
        export_long->digits = NULL;
6871
5
        export_long->_reserved = 0;
6872
5
    }
6873
4
    else {
6874
4
        PyLongObject *self = (PyLongObject*)obj;
6875
4
        export_long->value = 0;
6876
4
        export_long->negative = _PyLong_IsNegative(self);
6877
4
        export_long->ndigits = _PyLong_DigitCount(self);
6878
4
        if (export_long->ndigits == 0) {
6879
0
            export_long->ndigits = 1;
6880
0
        }
6881
4
        export_long->digits = self->long_value.ob_digit;
6882
4
        export_long->_reserved = (Py_uintptr_t)Py_NewRef(obj);
6883
4
    }
6884
9
    return 0;
6885
9
}
6886
6887
6888
void
6889
PyLong_FreeExport(PyLongExport *export_long)
6890
4
{
6891
4
    PyObject *obj = (PyObject*)export_long->_reserved;
6892
4
    if (obj) {
6893
4
        export_long->_reserved = 0;
6894
4
        Py_DECREF(obj);
6895
4
    }
6896
4
}
6897
6898
6899
/* --- PyLongWriter API --------------------------------------------------- */
6900
6901
PyLongWriter*
6902
PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
6903
78
{
6904
78
    if (ndigits <= 0) {
6905
0
        PyErr_SetString(PyExc_ValueError, "ndigits must be positive");
6906
0
        goto error;
6907
0
    }
6908
78
    assert(digits != NULL);
6909
6910
78
    PyLongObject *obj = long_alloc(ndigits);
6911
78
    if (obj == NULL) {
6912
0
        goto error;
6913
0
    }
6914
78
    if (negative) {
6915
0
        _PyLong_FlipSign(obj);
6916
0
    }
6917
6918
78
    *digits = obj->long_value.ob_digit;
6919
78
    return (PyLongWriter*)obj;
6920
6921
0
error:
6922
0
    *digits = NULL;
6923
0
    return NULL;
6924
78
}
6925
6926
6927
void
6928
PyLongWriter_Discard(PyLongWriter *writer)
6929
0
{
6930
0
    if (writer == NULL) {
6931
0
        return;
6932
0
    }
6933
6934
0
    PyLongObject *obj = (PyLongObject *)writer;
6935
0
    assert(Py_REFCNT(obj) == 1);
6936
0
    Py_DECREF(obj);
6937
0
}
6938
6939
6940
PyObject*
6941
PyLongWriter_Finish(PyLongWriter *writer)
6942
78
{
6943
78
    PyLongObject *obj = (PyLongObject *)writer;
6944
78
    assert(Py_REFCNT(obj) == 1);
6945
6946
    // Normalize and get singleton if possible
6947
78
    obj = maybe_small_long(long_normalize(obj));
6948
6949
78
    return (PyObject*)obj;
6950
78
}