Coverage Report

Created: 2026-03-23 06:45

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