Coverage Report

Created: 2026-05-30 06:18

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