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