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