### Bug Summary

File: | Objects/longobject.c
Location: | line 1814, column 9
Description: | Division by zero

### Annotated Source Code

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 "longintrepr.h"
7 |
8 | #include <float.h>
9 | #include <ctype.h>
10 | #include <stddef.h>
11 |
12 | #ifndef NSMALLPOSINTS257
13 | #define NSMALLPOSINTS257 257
14 | #endif
15 | #ifndef NSMALLNEGINTS5
16 | #define NSMALLNEGINTS5 5
17 | #endif
18 |
19 | /* convert a PyLong of size 1, 0 or -1 to an sdigit */
20 | #define MEDIUM_VALUE(x)((((PyVarObject*)(x))->ob_size) < 0 ? -(sdigit)(x)-> ob_digit[0] : ((((PyVarObject*)(x))->ob_size) == 0 ? (sdigit )0 : (sdigit)(x)->ob_digit[0])) (Py_SIZE(x)(((PyVarObject*)(x))->ob_size) < 0 ? -(sdigit)(x)->ob_digit[0] : \
21 | (Py_SIZE(x)(((PyVarObject*)(x))->ob_size) == 0 ? (sdigit)0 : \
22 | (sdigit)(x)->ob_digit[0]))
23 | #define ABS(x)((x) < 0 ? -(x) : (x)) ((x) < 0 ? -(x) : (x))
24 |
25 | #if NSMALLNEGINTS5 + NSMALLPOSINTS257 > 0
26 | /* Small integers are preallocated in this array so that they
27 | can be shared.
28 | The integers that are preallocated are those in the range
29 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
30 | */
31 | static PyLongObject small_ints[NSMALLNEGINTS5 + NSMALLPOSINTS257];
32 | #ifdef COUNT_ALLOCS
33 | int quick_int_allocs, quick_neg_int_allocs;
34 | #endif
35 |
36 | static PyObject *
37 | get_small_int(sdigit ival)
38 | {
39 | PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS5);
40 | Py_INCREF(v)( _Py_RefTotal++ , ((PyObject*)(v))->ob_refcnt++);
41 | #ifdef COUNT_ALLOCS
42 | if (ival >= 0)
43 | quick_int_allocs++;
44 | else
45 | quick_neg_int_allocs++;
46 | #endif
47 | return v;
48 | }
49 | #define CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0) \
50 | do if (-NSMALLNEGINTS5 <= ival && ival < NSMALLPOSINTS257) { \
51 | return get_small_int((sdigit)ival); \
52 | } while(0)
53 |
54 | static PyLongObject *
55 | maybe_small_long(PyLongObject *v)
56 | {
57 | if (v && ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size))) <= 1) {
58 | sdigit ival = MEDIUM_VALUE(v)((((PyVarObject*)(v))->ob_size) < 0 ? -(sdigit)(v)-> ob_digit[0] : ((((PyVarObject*)(v))->ob_size) == 0 ? (sdigit )0 : (sdigit)(v)->ob_digit[0]));
59 | if (-NSMALLNEGINTS5 <= ival && ival < NSMALLPOSINTS257) {
60 | Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 60, (PyObject *)(v)); } else _Py_Dealloc ((PyObject *)(v)); } while (0);
61 | return (PyLongObject *)get_small_int(ival);
62 | }
63 | }
64 | return v;
65 | }
66 | #else
67 | #define CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0)
68 | #define maybe_small_long(val) (val)
69 | #endif
70 |
71 | /* If a freshly-allocated long is already shared, it must
72 | be a small integer, so negating it must go to PyLong_FromLong */
73 | #define NEGATE(x)do if ((((PyObject*)(x))->ob_refcnt) == 1) (((PyVarObject* )(x))->ob_size) = -(((PyVarObject*)(x))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(x))->ob_size ) < 0 ? -(sdigit)(x)->ob_digit[0] : ((((PyVarObject*)(x ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(x)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 73, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0); (x) = (PyLongObject*)tmp; } while (0) \
74 | do if (Py_REFCNT(x)(((PyObject*)(x))->ob_refcnt) == 1) Py_SIZE(x)(((PyVarObject*)(x))->ob_size) = -Py_SIZE(x)(((PyVarObject*)(x))->ob_size); \
75 | else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)((((PyVarObject*)(x))->ob_size) < 0 ? -(sdigit)(x)-> ob_digit[0] : ((((PyVarObject*)(x))->ob_size) == 0 ? (sdigit )0 : (sdigit)(x)->ob_digit[0]))); \
76 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 76, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0); (x) = (PyLongObject*)tmp; } \
77 | while(0)
78 | /* For long multiplication, use the O(N**2) school algorithm unless
79 | * both operands contain more than KARATSUBA_CUTOFF digits (this
80 | * being an internal Python long digit, in base BASE).
81 | */
82 | #define KARATSUBA_CUTOFF70 70
83 | #define KARATSUBA_SQUARE_CUTOFF(2 * 70) (2 * KARATSUBA_CUTOFF70)
84 |
85 | /* For exponentiation, use the binary left-to-right algorithm
86 | * unless the exponent contains more than FIVEARY_CUTOFF digits.
87 | * In that case, do 5 bits at a time. The potential drawback is that
88 | * a table of 2**5 intermediate results is computed.
89 | */
90 | #define FIVEARY_CUTOFF8 8
91 |
92 | #undef MIN
93 | #undef MAX
94 | #define MAX(x, y)((x) < (y) ? (y) : (x)) ((x) < (y) ? (y) : (x))
95 | #define MIN(x, y)((x) > (y) ? (y) : (x)) ((x) > (y) ? (y) : (x))
96 |
97 | #define SIGCHECK(PyTryBlock)do { if (PyErr_CheckSignals()) PyTryBlock } while(0) \
98 | do { \
99 | if (PyErr_CheckSignals()) PyTryBlock \
100 | } while(0)
101 |
102 | /* Normalize (remove leading zeros from) a long int object.
103 | Doesn't attempt to free the storage--in most cases, due to the nature
104 | of the algorithms used, this could save at most be one word anyway. */
105 |
106 | static PyLongObject *
107 | long_normalize(register PyLongObject *v)
108 | {
109 | Py_ssize_t j = ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size)));
110 | Py_ssize_t i = j;
111 |
112 | while (i > 0 && v->ob_digit[i-1] == 0)
113 | --i;
114 | if (i != j)
115 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = (Py_SIZE(v)(((PyVarObject*)(v))->ob_size) < 0) ? -(i) : i;
116 | return v;
117 | }
118 |
119 | /* Allocate a new long int object with size digits.
120 | Return NULL and set exception if we run out of memory. */
121 |
122 | #define MAX_LONG_DIGITS((((Py_ssize_t)(((size_t)-1)>>1)) - __builtin_offsetof( PyLongObject, ob_digit))/sizeof(digit)) \
123 | ((PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - offsetof(PyLongObject, ob_digit)__builtin_offsetof(PyLongObject, ob_digit))/sizeof(digit))
124 |
125 | PyLongObject *
126 | _PyLong_New(Py_ssize_t size)
127 | {
128 | PyLongObject *result;
129 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
130 | sizeof(digit)*size. Previous incarnations of this code used
131 | sizeof(PyVarObject) instead of the offsetof, but this risks being
132 | incorrect in the presence of padding between the PyVarObject header
133 | and the digits. */
134 | if (size > (Py_ssize_t)MAX_LONG_DIGITS((((Py_ssize_t)(((size_t)-1)>>1)) - __builtin_offsetof( PyLongObject, ob_digit))/sizeof(digit))) {
135 | PyErr_SetString(PyExc_OverflowError,
136 | "too many digits in integer");
137 | return NULL((void*)0);
138 | }
139 | result = PyObject_MALLOC_PyObject_DebugMalloc(offsetof(PyLongObject, ob_digit)__builtin_offsetof(PyLongObject, ob_digit) +
140 | size*sizeof(digit));
141 | if (!result) {
142 | PyErr_NoMemory();
143 | return NULL((void*)0);
144 | }
145 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size)( (((PyVarObject*)(result))->ob_size) = (size), ( (((PyObject *)((result)))->ob_type) = ((&PyLong_Type)), _Py_NewReference ((PyObject *)((result))), ((result)) ) );
146 | }
147 |
148 | PyObject *
149 | _PyLong_Copy(PyLongObject *src)
150 | {
151 | PyLongObject *result;
152 | Py_ssize_t i;
153 |
154 | assert(src != NULL)(__builtin_expect(!(src != ((void*)0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 154, "src != NULL") : (void)0);
155 | i = Py_SIZE(src)(((PyVarObject*)(src))->ob_size);
156 | if (i < 0)
157 | i = -(i);
158 | if (i < 2) {
159 | sdigit ival = src->ob_digit[0];
160 | if (Py_SIZE(src)(((PyVarObject*)(src))->ob_size) < 0)
161 | ival = -ival;
162 | CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0);
163 | }
164 | result = _PyLong_New(i);
165 | if (result != NULL((void*)0)) {
166 | Py_SIZE(result)(((PyVarObject*)(result))->ob_size) = Py_SIZE(src)(((PyVarObject*)(src))->ob_size);
167 | while (--i >= 0)
168 | result->ob_digit[i] = src->ob_digit[i];
169 | }
170 | return (PyObject *)result;
171 | }
172 |
173 | /* Create a new long int object from a C long int */
174 |
175 | PyObject *
176 | PyLong_FromLong(long ival)
177 | {
178 | PyLongObject *v;
179 | unsigned long abs_ival;
180 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */
181 | int ndigits = 0;
182 | int sign = 1;
183 |
184 | CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0);
185 |
186 | if (ival < 0) {
187 | /* negate: can't write this as abs_ival = -ival since that
188 | invokes undefined behaviour when ival is LONG_MIN */
189 | abs_ival = 0U-(unsigned long)ival;
190 | sign = -1;
191 | }
192 | else {
193 | abs_ival = (unsigned long)ival;
194 | }
195 |
196 | /* Fast path for single-digit ints */
197 | if (!(abs_ival >> PyLong_SHIFT30)) {
198 | v = _PyLong_New(1);
199 | if (v) {
200 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = sign;
201 | v->ob_digit[0] = Py_SAFE_DOWNCAST(((__builtin_expect(!((unsigned long)(digit)(abs_ival) == (abs_ival )), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 202, "(unsigned long)(digit)( abs_ival) == ( abs_ival)" ) : (void)0), (digit)(abs_ival))
202 | abs_ival, unsigned long, digit)((__builtin_expect(!((unsigned long)(digit)(abs_ival) == (abs_ival )), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 202, "(unsigned long)(digit)( abs_ival) == ( abs_ival)" ) : (void)0), (digit)(abs_ival));
203 | }
204 | return (PyObject*)v;
205 | }
206 |
207 | #if PyLong_SHIFT30==15
208 | /* 2 digits */
209 | if (!(abs_ival >> 2*PyLong_SHIFT30)) {
210 | v = _PyLong_New(2);
211 | if (v) {
212 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = 2*sign;
213 | v->ob_digit[0] = Py_SAFE_DOWNCAST(((__builtin_expect(!((unsigned long)(digit)(abs_ival & (( digit)(((digit)1 << 30) - 1))) == (abs_ival & ((digit )(((digit)1 << 30) - 1)))), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 214, "(unsigned long)(digit)( abs_ival & ((digit)(((digit)1 << 30) - 1))) == ( abs_ival & ((digit)(((digit)1 << 30) - 1)))" ) : (void)0), (digit)(abs_ival & ((digit)(((digit)1 << 30) - 1))))
214 | abs_ival & PyLong_MASK, unsigned long, digit)((__builtin_expect(!((unsigned long)(digit)(abs_ival & (( digit)(((digit)1 << 30) - 1))) == (abs_ival & ((digit )(((digit)1 << 30) - 1)))), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 214, "(unsigned long)(digit)( abs_ival & ((digit)(((digit)1 << 30) - 1))) == ( abs_ival & ((digit)(((digit)1 << 30) - 1)))" ) : (void)0), (digit)(abs_ival & ((digit)(((digit)1 << 30) - 1))));
215 | v->ob_digit[1] = Py_SAFE_DOWNCAST(((__builtin_expect(!((unsigned long)(digit)(abs_ival >> 30) == (abs_ival >> 30)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 216, "(unsigned long)(digit)( abs_ival >> 30) == ( abs_ival >> 30)" ) : (void)0), (digit)(abs_ival >> 30))
216 | abs_ival >> PyLong_SHIFT, unsigned long, digit)((__builtin_expect(!((unsigned long)(digit)(abs_ival >> 30) == (abs_ival >> 30)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 216, "(unsigned long)(digit)( abs_ival >> 30) == ( abs_ival >> 30)" ) : (void)0), (digit)(abs_ival >> 30));
217 | }
218 | return (PyObject*)v;
219 | }
220 | #endif
221 |
222 | /* Larger numbers: loop to determine number of digits */
223 | t = abs_ival;
224 | while (t) {
225 | ++ndigits;
226 | t >>= PyLong_SHIFT30;
227 | }
228 | v = _PyLong_New(ndigits);
229 | if (v != NULL((void*)0)) {
230 | digit *p = v->ob_digit;
231 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = ndigits*sign;
232 | t = abs_ival;
233 | while (t) {
234 | *p++ = Py_SAFE_DOWNCAST(((__builtin_expect(!((unsigned long)(digit)(t & ((digit)( ((digit)1 << 30) - 1))) == (t & ((digit)(((digit)1 << 30) - 1)))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 235, "(unsigned long)(digit)( t & ((digit)(((digit)1 << 30) - 1))) == ( t & ((digit)(((digit)1 << 30) - 1)))" ) : (void)0), (digit)(t & ((digit)(((digit)1 << 30) - 1))))
235 | t & PyLong_MASK, unsigned long, digit)((__builtin_expect(!((unsigned long)(digit)(t & ((digit)( ((digit)1 << 30) - 1))) == (t & ((digit)(((digit)1 << 30) - 1)))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 235, "(unsigned long)(digit)( t & ((digit)(((digit)1 << 30) - 1))) == ( t & ((digit)(((digit)1 << 30) - 1)))" ) : (void)0), (digit)(t & ((digit)(((digit)1 << 30) - 1))));
236 | t >>= PyLong_SHIFT30;
237 | }
238 | }
239 | return (PyObject *)v;
240 | }
241 |
242 | /* Create a new long int object from a C unsigned long int */
243 |
244 | PyObject *
245 | PyLong_FromUnsignedLong(unsigned long ival)
246 | {
247 | PyLongObject *v;
248 | unsigned long t;
249 | int ndigits = 0;
250 |
251 | if (ival < PyLong_BASE((digit)1 << 30))
252 | return PyLong_FromLong(ival);
253 | /* Count the number of Python digits. */
254 | t = (unsigned long)ival;
255 | while (t) {
256 | ++ndigits;
257 | t >>= PyLong_SHIFT30;
258 | }
259 | v = _PyLong_New(ndigits);
260 | if (v != NULL((void*)0)) {
261 | digit *p = v->ob_digit;
262 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = ndigits;
263 | while (ival) {
264 | *p++ = (digit)(ival & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
265 | ival >>= PyLong_SHIFT30;
266 | }
267 | }
268 | return (PyObject *)v;
269 | }
270 |
271 | /* Create a new long int object from a C double */
272 |
273 | PyObject *
274 | PyLong_FromDouble(double dval)
275 | {
276 | PyLongObject *v;
277 | double frac;
278 | int i, ndig, expo, neg;
279 | neg = 0;
280 | if (Py_IS_INFINITY(dval)( sizeof (dval) == sizeof(float ) ? __inline_isinff((float)(dval )) : sizeof (dval) == sizeof(double) ? __inline_isinfd((double )(dval)) : __inline_isinf ((long double)(dval)))) {
281 | PyErr_SetString(PyExc_OverflowError,
282 | "cannot convert float infinity to integer");
283 | return NULL((void*)0);
284 | }
285 | if (Py_IS_NAN(dval)( sizeof (dval) == sizeof(float ) ? __inline_isnanf((float)(dval )) : sizeof (dval) == sizeof(double) ? __inline_isnand((double )(dval)) : __inline_isnan ((long double)(dval)))) {
286 | PyErr_SetString(PyExc_ValueError,
287 | "cannot convert float NaN to integer");
288 | return NULL((void*)0);
289 | }
290 | if (dval < 0.0) {
291 | neg = 1;
292 | dval = -dval;
293 | }
294 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
295 | if (expo <= 0)
296 | return PyLong_FromLong(0L);
297 | ndig = (expo-1) / PyLong_SHIFT30 + 1; /* Number of 'digits' in result */
298 | v = _PyLong_New(ndig);
299 | if (v == NULL((void*)0))
300 | return NULL((void*)0);
301 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT30 + 1);
302 | for (i = ndig; --i >= 0; ) {
303 | digit bits = (digit)frac;
304 | v->ob_digit[i] = bits;
305 | frac = frac - (double)bits;
306 | frac = ldexp(frac, PyLong_SHIFT30);
307 | }
308 | if (neg)
309 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = -(Py_SIZE(v)(((PyVarObject*)(v))->ob_size));
310 | return (PyObject *)v;
311 | }
312 |
313 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
314 | * anything about what happens when a signed integer operation overflows,
315 | * and some compilers think they're doing you a favor by being "clever"
316 | * then. The bit pattern for the largest postive signed long is
317 | * (unsigned long)LONG_MAX, and for the smallest negative signed long
318 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
319 | * However, some other compilers warn about applying unary minus to an
320 | * unsigned operand. Hence the weird "0-".
321 | */
322 | #define PY_ABS_LONG_MIN(0 -(unsigned long)(-9223372036854775807L -1L)) (0-(unsigned long)LONG_MIN(-9223372036854775807L -1L))
323 | #define PY_ABS_SSIZE_T_MIN(0 -(size_t)(-((Py_ssize_t)(((size_t)-1)>>1))-1)) (0-(size_t)PY_SSIZE_T_MIN(-((Py_ssize_t)(((size_t)-1)>>1))-1))
324 |
325 | /* Get a C long int from a long int object.
326 | Returns -1 and sets an error condition if overflow occurs. */
327 |
328 | long
329 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
330 | {
331 | /* This version by Tim Peters */
332 | register PyLongObject *v;
333 | unsigned long x, prev;
334 | long res;
335 | Py_ssize_t i;
336 | int sign;
337 | int do_decref = 0; /* if nb_int was called */
338 |
339 | *overflow = 0;
340 | if (vv == NULL((void*)0)) {
341 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 341);
342 | return -1;
343 | }
344 |
345 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
346 | PyNumberMethods *nb;
347 | nb = vv->ob_type->tp_as_number;
348 | if (nb == NULL((void*)0) || nb->nb_int == NULL((void*)0)) {
349 | PyErr_SetString(PyExc_TypeError,
350 | "an integer is required");
351 | return -1;
352 | }
353 | vv = (*nb->nb_int) (vv);
354 | if (vv == NULL((void*)0))
355 | return -1;
356 | do_decref = 1;
357 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
358 | Py_DECREF(vv)do { if (_Py_RefTotal-- , --((PyObject*)(vv))->ob_refcnt != 0) { if (((PyObject*)vv)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 358, (PyObject *)(vv)); } else _Py_Dealloc ((PyObject *)(vv)); } while (0);
359 | PyErr_SetString(PyExc_TypeError,
360 | "nb_int should return int object");
361 | return -1;
362 | }
363 | }
364 |
365 | res = -1;
366 | v = (PyLongObject *)vv;
367 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
368 |
369 | switch (i) {
370 | case -1:
371 | res = -(sdigit)v->ob_digit[0];
372 | break;
373 | case 0:
374 | res = 0;
375 | break;
376 | case 1:
377 | res = v->ob_digit[0];
378 | break;
379 | default:
380 | sign = 1;
381 | x = 0;
382 | if (i < 0) {
383 | sign = -1;
384 | i = -(i);
385 | }
386 | while (--i >= 0) {
387 | prev = x;
388 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
389 | if ((x >> PyLong_SHIFT30) != prev) {
390 | *overflow = sign;
391 | goto exit;
392 | }
393 | }
394 | /* Haven't lost any bits, but casting to long requires extra
395 | * care (see comment above).
396 | */
397 | if (x <= (unsigned long)LONG_MAX9223372036854775807L) {
398 | res = (long)x * sign;
399 | }
400 | else if (sign < 0 && x == PY_ABS_LONG_MIN(0 -(unsigned long)(-9223372036854775807L -1L))) {
401 | res = LONG_MIN(-9223372036854775807L -1L);
402 | }
403 | else {
404 | *overflow = sign;
405 | /* res is already set to -1 */
406 | }
407 | }
408 | exit:
409 | if (do_decref) {
410 | Py_DECREF(vv)do { if (_Py_RefTotal-- , --((PyObject*)(vv))->ob_refcnt != 0) { if (((PyObject*)vv)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 410, (PyObject *)(vv)); } else _Py_Dealloc ((PyObject *)(vv)); } while (0);
411 | }
412 | return res;
413 | }
414 |
415 | long
416 | PyLong_AsLong(PyObject *obj)
417 | {
418 | int overflow;
419 | long result = PyLong_AsLongAndOverflow(obj, &overflow);
420 | if (overflow) {
421 | /* XXX: could be cute and give a different
422 | message for overflow == -1 */
423 | PyErr_SetString(PyExc_OverflowError,
424 | "Python int too large to convert to C long");
425 | }
426 | return result;
427 | }
428 |
429 | /* Get a Py_ssize_t from a long int object.
430 | Returns -1 and sets an error condition if overflow occurs. */
431 |
432 | Py_ssize_t
433 | PyLong_AsSsize_t(PyObject *vv) {
434 | register PyLongObject *v;
435 | size_t x, prev;
436 | Py_ssize_t i;
437 | int sign;
438 |
439 | if (vv == NULL((void*)0)) {
440 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 440);
441 | return -1;
442 | }
443 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
444 | PyErr_SetString(PyExc_TypeError, "an integer is required");
445 | return -1;
446 | }
447 |
448 | v = (PyLongObject *)vv;
449 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
450 | switch (i) {
451 | case -1: return -(sdigit)v->ob_digit[0];
452 | case 0: return 0;
453 | case 1: return v->ob_digit[0];
454 | }
455 | sign = 1;
456 | x = 0;
457 | if (i < 0) {
458 | sign = -1;
459 | i = -(i);
460 | }
461 | while (--i >= 0) {
462 | prev = x;
463 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
464 | if ((x >> PyLong_SHIFT30) != prev)
465 | goto overflow;
466 | }
467 | /* Haven't lost any bits, but casting to a signed type requires
468 | * extra care (see comment above).
469 | */
470 | if (x <= (size_t)PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))) {
471 | return (Py_ssize_t)x * sign;
472 | }
473 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN(0 -(size_t)(-((Py_ssize_t)(((size_t)-1)>>1))-1))) {
474 | return PY_SSIZE_T_MIN(-((Py_ssize_t)(((size_t)-1)>>1))-1);
475 | }
476 | /* else overflow */
477 |
478 | overflow:
479 | PyErr_SetString(PyExc_OverflowError,
480 | "Python int too large to convert to C ssize_t");
481 | return -1;
482 | }
483 |
484 | /* Get a C unsigned long int from a long int object.
485 | Returns -1 and sets an error condition if overflow occurs. */
486 |
487 | unsigned long
488 | PyLong_AsUnsignedLong(PyObject *vv)
489 | {
490 | register PyLongObject *v;
491 | unsigned long x, prev;
492 | Py_ssize_t i;
493 |
494 | if (vv == NULL((void*)0)) {
495 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 495);
496 | return (unsigned long)-1;
497 | }
498 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
499 | PyErr_SetString(PyExc_TypeError, "an integer is required");
500 | return (unsigned long)-1;
501 | }
502 |
503 | v = (PyLongObject *)vv;
504 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
505 | x = 0;
506 | if (i < 0) {
507 | PyErr_SetString(PyExc_OverflowError,
508 | "can't convert negative value to unsigned int");
509 | return (unsigned long) -1;
510 | }
511 | switch (i) {
512 | case 0: return 0;
513 | case 1: return v->ob_digit[0];
514 | }
515 | while (--i >= 0) {
516 | prev = x;
517 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
518 | if ((x >> PyLong_SHIFT30) != prev) {
519 | PyErr_SetString(PyExc_OverflowError,
520 | "python int too large to convert "
521 | "to C unsigned long");
522 | return (unsigned long) -1;
523 | }
524 | }
525 | return x;
526 | }
527 |
528 | /* Get a C unsigned long int from a long int object.
529 | Returns -1 and sets an error condition if overflow occurs. */
530 |
531 | size_t
532 | PyLong_AsSize_t(PyObject *vv)
533 | {
534 | register PyLongObject *v;
535 | size_t x, prev;
536 | Py_ssize_t i;
537 |
538 | if (vv == NULL((void*)0)) {
539 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 539);
540 | return (size_t) -1;
541 | }
542 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
543 | PyErr_SetString(PyExc_TypeError, "an integer is required");
544 | return (size_t)-1;
545 | }
546 |
547 | v = (PyLongObject *)vv;
548 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
549 | x = 0;
550 | if (i < 0) {
551 | PyErr_SetString(PyExc_OverflowError,
552 | "can't convert negative value to size_t");
553 | return (size_t) -1;
554 | }
555 | switch (i) {
556 | case 0: return 0;
557 | case 1: return v->ob_digit[0];
558 | }
559 | while (--i >= 0) {
560 | prev = x;
561 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
562 | if ((x >> PyLong_SHIFT30) != prev) {
563 | PyErr_SetString(PyExc_OverflowError,
564 | "Python int too large to convert to C size_t");
565 | return (unsigned long) -1;
566 | }
567 | }
568 | return x;
569 | }
570 |
571 | /* Get a C unsigned long int from a long int object, ignoring the high bits.
572 | Returns -1 and sets an error condition if an error occurs. */
573 |
574 | static unsigned long
575 | _PyLong_AsUnsignedLongMask(PyObject *vv)
576 | {
577 | register PyLongObject *v;
578 | unsigned long x;
579 | Py_ssize_t i;
580 | int sign;
581 |
582 | if (vv == NULL((void*)0) || !PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
583 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 583);
584 | return (unsigned long) -1;
585 | }
586 | v = (PyLongObject *)vv;
587 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
588 | switch (i) {
589 | case 0: return 0;
590 | case 1: return v->ob_digit[0];
591 | }
592 | sign = 1;
593 | x = 0;
594 | if (i < 0) {
595 | sign = -1;
596 | i = -i;
597 | }
598 | while (--i >= 0) {
599 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
600 | }
601 | return x * sign;
602 | }
603 |
604 | unsigned long
605 | PyLong_AsUnsignedLongMask(register PyObject *op)
606 | {
607 | PyNumberMethods *nb;
608 | PyLongObject *lo;
609 | unsigned long val;
610 |
611 | if (op && PyLong_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<< 24))) != 0))
612 | return _PyLong_AsUnsignedLongMask(op);
613 |
614 | if (op == NULL((void*)0) || (nb = op->ob_type->tp_as_number) == NULL((void*)0) ||
615 | nb->nb_int == NULL((void*)0)) {
616 | PyErr_SetString(PyExc_TypeError, "an integer is required");
617 | return (unsigned long)-1;
618 | }
619 |
620 | lo = (PyLongObject*) (*nb->nb_int) (op);
621 | if (lo == NULL((void*)0))
622 | return (unsigned long)-1;
623 | if (PyLong_Check(lo)((((((PyObject*)(lo))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
624 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
625 | Py_DECREF(lo)do { if (_Py_RefTotal-- , --((PyObject*)(lo))->ob_refcnt != 0) { if (((PyObject*)lo)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 625, (PyObject *)(lo)); } else _Py_Dealloc ((PyObject *)(lo)); } while (0);
626 | if (PyErr_Occurred())
627 | return (unsigned long)-1;
628 | return val;
629 | }
630 | else
631 | {
632 | Py_DECREF(lo)do { if (_Py_RefTotal-- , --((PyObject*)(lo))->ob_refcnt != 0) { if (((PyObject*)lo)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 632, (PyObject *)(lo)); } else _Py_Dealloc ((PyObject *)(lo)); } while (0);
633 | PyErr_SetString(PyExc_TypeError,
634 | "nb_int should return int object");
635 | return (unsigned long)-1;
636 | }
637 | }
638 |
639 | int
640 | _PyLong_Sign(PyObject *vv)
641 | {
642 | PyLongObject *v = (PyLongObject *)vv;
643 |
644 | assert(v != NULL)(__builtin_expect(!(v != ((void*)0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 644, "v != NULL") : (void)0);
645 | assert(PyLong_Check(v))(__builtin_expect(!(((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 645, "PyLong_Check(v)") : (void)0);
646 |
647 | return Py_SIZE(v)(((PyVarObject*)(v))->ob_size) == 0 ? 0 : (Py_SIZE(v)(((PyVarObject*)(v))->ob_size) < 0 ? -1 : 1);
648 | }
649 |
650 | size_t
651 | _PyLong_NumBits(PyObject *vv)
652 | {
653 | PyLongObject *v = (PyLongObject *)vv;
654 | size_t result = 0;
655 | Py_ssize_t ndigits;
656 |
657 | assert(v != NULL)(__builtin_expect(!(v != ((void*)0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 657, "v != NULL") : (void)0);
658 | assert(PyLong_Check(v))(__builtin_expect(!(((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 658, "PyLong_Check(v)") : (void)0);
659 | ndigits = ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size)));
660 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0)(__builtin_expect(!(ndigits == 0 || v->ob_digit[ndigits - 1 ] != 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 660 , "ndigits == 0 || v->ob_digit[ndigits - 1] != 0") : (void )0);
661 | if (ndigits > 0) {
662 | digit msd = v->ob_digit[ndigits - 1];
663 |
664 | result = (ndigits - 1) * PyLong_SHIFT30;
665 | if (result / PyLong_SHIFT30 != (size_t)(ndigits - 1))
666 | goto Overflow;
667 | do {
668 | ++result;
669 | if (result == 0)
670 | goto Overflow;
671 | msd >>= 1;
672 | } while (msd);
673 | }
674 | return result;
675 |
676 | Overflow:
677 | PyErr_SetString(PyExc_OverflowError, "int has too many bits "
678 | "to express in a platform size_t");
679 | return (size_t)-1;
680 | }
681 |
682 | PyObject *
683 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
684 | int little_endian, int is_signed)
685 | {
686 | const unsigned char* pstartbyte; /* LSB of bytes */
687 | int incr; /* direction to move pstartbyte */
688 | const unsigned char* pendbyte; /* MSB of bytes */
689 | size_t numsignificantbytes; /* number of bytes that matter */
690 | Py_ssize_t ndigits; /* number of Python long digits */
691 | PyLongObject* v; /* result */
692 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
693 |
694 | if (n == 0)
695 | return PyLong_FromLong(0L);
696 |
697 | if (little_endian) {
698 | pstartbyte = bytes;
699 | pendbyte = bytes + n - 1;
700 | incr = 1;
701 | }
702 | else {
703 | pstartbyte = bytes + n - 1;
704 | pendbyte = bytes;
705 | incr = -1;
706 | }
707 |
708 | if (is_signed)
709 | is_signed = *pendbyte >= 0x80;
710 |
711 | /* Compute numsignificantbytes. This consists of finding the most
712 | significant byte. Leading 0 bytes are insignficant if the number
713 | is positive, and leading 0xff bytes if negative. */
714 | {
715 | size_t i;
716 | const unsigned char* p = pendbyte;
717 | const int pincr = -incr; /* search MSB to LSB */
718 | const unsigned char insignficant = is_signed ? 0xff : 0x00;
719 |
720 | for (i = 0; i < n; ++i, p += pincr) {
721 | if (*p != insignficant)
722 | break;
723 | }
724 | numsignificantbytes = n - i;
725 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
726 | actually has 2 significant bytes. OTOH, 0xff0001 ==
727 | -0x00ffff, so we wouldn't *need* to bump it there; but we
728 | do for 0xffff = -0x0001. To be safe without bothering to
729 | check every case, bump it regardless. */
730 | if (is_signed && numsignificantbytes < n)
731 | ++numsignificantbytes;
732 | }
733 |
734 | /* How many Python long digits do we need? We have
735 | 8*numsignificantbytes bits, and each Python long digit has
736 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */
737 | /* catch overflow before it happens */
738 | if (numsignificantbytes > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - PyLong_SHIFT30) / 8) {
739 | PyErr_SetString(PyExc_OverflowError,
740 | "byte array too long to convert to int");
741 | return NULL((void*)0);
742 | }
743 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT30 - 1) / PyLong_SHIFT30;
744 | v = _PyLong_New(ndigits);
745 | if (v == NULL((void*)0))
746 | return NULL((void*)0);
747 |
748 | /* Copy the bits over. The tricky parts are computing 2's-comp on
749 | the fly for signed numbers, and dealing with the mismatch between
750 | 8-bit bytes and (probably) 15-bit Python digits.*/
751 | {
752 | size_t i;
753 | twodigits carry = 1; /* for 2's-comp calculation */
754 | twodigits accum = 0; /* sliding register */
755 | unsigned int accumbits = 0; /* number of bits in accum */
756 | const unsigned char* p = pstartbyte;
757 |
758 | for (i = 0; i < numsignificantbytes; ++i, p += incr) {
759 | twodigits thisbyte = *p;
760 | /* Compute correction for 2's comp, if needed. */
761 | if (is_signed) {
762 | thisbyte = (0xff ^ thisbyte) + carry;
763 | carry = thisbyte >> 8;
764 | thisbyte &= 0xff;
765 | }
766 | /* Because we're going LSB to MSB, thisbyte is
767 | more significant than what's already in accum,
768 | so needs to be prepended to accum. */
769 | accum |= (twodigits)thisbyte << accumbits;
770 | accumbits += 8;
771 | if (accumbits >= PyLong_SHIFT30) {
772 | /* There's enough to fill a Python digit. */
773 | assert(idigit < ndigits)(__builtin_expect(!(idigit < ndigits), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 773, "idigit < ndigits") : (void )0);
774 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
775 | ++idigit;
776 | accum >>= PyLong_SHIFT30;
777 | accumbits -= PyLong_SHIFT30;
778 | assert(accumbits < PyLong_SHIFT)(__builtin_expect(!(accumbits < 30), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 778, "accumbits < PyLong_SHIFT") : (void)0);
779 | }
780 | }
781 | assert(accumbits < PyLong_SHIFT)(__builtin_expect(!(accumbits < 30), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 781, "accumbits < PyLong_SHIFT") : (void)0);
782 | if (accumbits) {
783 | assert(idigit < ndigits)(__builtin_expect(!(idigit < ndigits), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 783, "idigit < ndigits") : (void )0);
784 | v->ob_digit[idigit] = (digit)accum;
785 | ++idigit;
786 | }
787 | }
788 |
789 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = is_signed ? -idigit : idigit;
790 | return (PyObject *)long_normalize(v);
791 | }
792 |
793 | int
794 | _PyLong_AsByteArray(PyLongObject* v,
795 | unsigned char* bytes, size_t n,
796 | int little_endian, int is_signed)
797 | {
798 | Py_ssize_t i; /* index into v->ob_digit */
799 | Py_ssize_t ndigits; /* |v->ob_size| */
800 | twodigits accum; /* sliding register */
801 | unsigned int accumbits; /* # bits in accum */
802 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
803 | digit carry; /* for computing 2's-comp */
804 | size_t j; /* # bytes filled */
805 | unsigned char* p; /* pointer to next byte in bytes */
806 | int pincr; /* direction to move p */
807 |
808 | assert(v != NULL && PyLong_Check(v))(__builtin_expect(!(v != ((void*)0) && ((((((PyObject *)(v))->ob_type))->tp_flags & ((1L<<24))) != 0 )), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 808, "v != NULL && PyLong_Check(v)" ) : (void)0);
809 |
810 | if (Py_SIZE(v)(((PyVarObject*)(v))->ob_size) < 0) {
811 | ndigits = -(Py_SIZE(v)(((PyVarObject*)(v))->ob_size));
812 | if (!is_signed) {
813 | PyErr_SetString(PyExc_OverflowError,
814 | "can't convert negative int to unsigned");
815 | return -1;
816 | }
817 | do_twos_comp = 1;
818 | }
819 | else {
820 | ndigits = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
821 | do_twos_comp = 0;
822 | }
823 |
824 | if (little_endian) {
825 | p = bytes;
826 | pincr = 1;
827 | }
828 | else {
829 | p = bytes + n - 1;
830 | pincr = -1;
831 | }
832 |
833 | /* Copy over all the Python digits.
834 | It's crucial that every Python digit except for the MSD contribute
835 | exactly PyLong_SHIFT bits to the total, so first assert that the long is
836 | normalized. */
837 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0)(__builtin_expect(!(ndigits == 0 || v->ob_digit[ndigits - 1 ] != 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 837 , "ndigits == 0 || v->ob_digit[ndigits - 1] != 0") : (void )0);
838 | j = 0;
839 | accum = 0;
840 | accumbits = 0;
841 | carry = do_twos_comp ? 1 : 0;
842 | for (i = 0; i < ndigits; ++i) {
843 | digit thisdigit = v->ob_digit[i];
844 | if (do_twos_comp) {
845 | thisdigit = (thisdigit ^ PyLong_MASK((digit)(((digit)1 << 30) - 1))) + carry;
846 | carry = thisdigit >> PyLong_SHIFT30;
847 | thisdigit &= PyLong_MASK((digit)(((digit)1 << 30) - 1));
848 | }
849 | /* Because we're going LSB to MSB, thisdigit is more
850 | significant than what's already in accum, so needs to be
851 | prepended to accum. */
852 | accum |= (twodigits)thisdigit << accumbits;
853 |
854 | /* The most-significant digit may be (probably is) at least
855 | partly empty. */
856 | if (i == ndigits - 1) {
857 | /* Count # of sign bits -- they needn't be stored,
858 | * although for signed conversion we need later to
859 | * make sure at least one sign bit gets stored. */
860 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK((digit)(((digit)1 << 30) - 1)) : thisdigit;
861 | while (s != 0) {
862 | s >>= 1;
863 | accumbits++;
864 | }
865 | }
866 | else
867 | accumbits += PyLong_SHIFT30;
868 |
869 | /* Store as many bytes as possible. */
870 | while (accumbits >= 8) {
871 | if (j >= n)
872 | goto Overflow;
873 | ++j;
874 | *p = (unsigned char)(accum & 0xff);
875 | p += pincr;
876 | accumbits -= 8;
877 | accum >>= 8;
878 | }
879 | }
880 |
881 | /* Store the straggler (if any). */
882 | assert(accumbits < 8)(__builtin_expect(!(accumbits < 8), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 882, "accumbits < 8") : (void)0);
883 | assert(carry == 0)(__builtin_expect(!(carry == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 883, "carry == 0") : (void)0); /* else do_twos_comp and *every* digit was 0 */
884 | if (accumbits > 0) {
885 | if (j >= n)
886 | goto Overflow;
887 | ++j;
888 | if (do_twos_comp) {
889 | /* Fill leading bits of the byte with sign bits
890 | (appropriately pretending that the long had an
891 | infinite supply of sign bits). */
892 | accum |= (~(twodigits)0) << accumbits;
893 | }
894 | *p = (unsigned char)(accum & 0xff);
895 | p += pincr;
896 | }
897 | else if (j == n && n > 0 && is_signed) {
898 | /* The main loop filled the byte array exactly, so the code
899 | just above didn't get to ensure there's a sign bit, and the
900 | loop below wouldn't add one either. Make sure a sign bit
901 | exists. */
902 | unsigned char msb = *(p - pincr);
903 | int sign_bit_set = msb >= 0x80;
904 | assert(accumbits == 0)(__builtin_expect(!(accumbits == 0), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 904, "accumbits == 0") : (void)0);
905 | if (sign_bit_set == do_twos_comp)
906 | return 0;
907 | else
908 | goto Overflow;
909 | }
910 |
911 | /* Fill remaining bytes with copies of the sign bit. */
912 | {
913 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
914 | for ( ; j < n; ++j, p += pincr)
915 | *p = signbyte;
916 | }
917 |
918 | return 0;
919 |
920 | Overflow:
921 | PyErr_SetString(PyExc_OverflowError, "int too big to convert");
922 | return -1;
923 |
924 | }
925 |
926 | /* Create a new long (or int) object from a C pointer */
927 |
928 | PyObject *
929 | PyLong_FromVoidPtr(void *p)
930 | {
931 | #ifndef HAVE_LONG_LONG1
932 | # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
933 | #endif
934 | #if SIZEOF_LONG_LONG8 < SIZEOF_VOID_P8
935 | # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
936 | #endif
937 | /* special-case null pointer */
938 | if (!p)
939 | return PyLong_FromLong(0);
940 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONGlong long)(Py_uintptr_t)p);
941 |
942 | }
943 |
944 | /* Get a C pointer from a long object (or an int object in some cases) */
945 |
946 | void *
947 | PyLong_AsVoidPtr(PyObject *vv)
948 | {
949 | /* This function will allow int or long objects. If vv is neither,
950 | then the PyLong_AsLong*() functions will raise the exception:
951 | PyExc_SystemError, "bad argument to internal function"
952 | */
953 | #if SIZEOF_VOID_P8 <= SIZEOF_LONG8
954 | long x;
955 |
956 | if (PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0) && _PyLong_Sign(vv) < 0)
957 | x = PyLong_AsLong(vv);
958 | else
959 | x = PyLong_AsUnsignedLong(vv);
960 | #else
961 |
962 | #ifndef HAVE_LONG_LONG1
963 | # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
964 | #endif
965 | #if SIZEOF_LONG_LONG8 < SIZEOF_VOID_P8
966 | # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
967 | #endif
968 | PY_LONG_LONGlong long x;
969 |
970 | if (PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0) && _PyLong_Sign(vv) < 0)
971 | x = PyLong_AsLongLong(vv);
972 | else
973 | x = PyLong_AsUnsignedLongLong(vv);
974 |
975 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
976 |
977 | if (x == -1 && PyErr_Occurred())
978 | return NULL((void*)0);
979 | return (void *)x;
980 | }
981 |
982 | #ifdef HAVE_LONG_LONG1
983 |
984 | /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
985 | * rewritten to use the newer PyLong_{As,From}ByteArray API.
986 | */
987 |
988 | #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
989 | #define PY_ABS_LLONG_MIN(0 -(unsigned long long)(-9223372036854775807LL -1LL)) (0-(unsigned PY_LONG_LONGlong long)PY_LLONG_MIN(-9223372036854775807LL -1LL))
990 |
991 | /* Create a new long int object from a C PY_LONG_LONG int. */
992 |
993 | PyObject *
994 | PyLong_FromLongLong(PY_LONG_LONGlong long ival)
995 | {
996 | PyLongObject *v;
997 | unsigned PY_LONG_LONGlong long abs_ival;
998 | unsigned PY_LONG_LONGlong long t; /* unsigned so >> doesn't propagate sign bit */
999 | int ndigits = 0;
1000 | int negative = 0;
1001 |
1002 | CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0);
1003 | if (ival < 0) {
1004 | /* avoid signed overflow on negation; see comments
1005 | in PyLong_FromLong above. */
1006 | abs_ival = (unsigned PY_LONG_LONGlong long)(-1-ival) + 1;
1007 | negative = 1;
1008 | }
1009 | else {
1010 | abs_ival = (unsigned PY_LONG_LONGlong long)ival;
1011 | }
1012 |
1013 | /* Count the number of Python digits.
1014 | We used to pick 5 ("big enough for anything"), but that's a
1015 | waste of time and space given that 5*15 = 75 bits are rarely
1016 | needed. */
1017 | t = abs_ival;
1018 | while (t) {
1019 | ++ndigits;
1020 | t >>= PyLong_SHIFT30;
1021 | }
1022 | v = _PyLong_New(ndigits);
1023 | if (v != NULL((void*)0)) {
1024 | digit *p = v->ob_digit;
1025 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = negative ? -ndigits : ndigits;
1026 | t = abs_ival;
1027 | while (t) {
1028 | *p++ = (digit)(t & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
1029 | t >>= PyLong_SHIFT30;
1030 | }
1031 | }
1032 | return (PyObject *)v;
1033 | }
1034 |
1035 | /* Create a new long int object from a C unsigned PY_LONG_LONG int. */
1036 |
1037 | PyObject *
1038 | PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONGlong long ival)
1039 | {
1040 | PyLongObject *v;
1041 | unsigned PY_LONG_LONGlong long t;
1042 | int ndigits = 0;
1043 |
1044 | if (ival < PyLong_BASE((digit)1 << 30))
1045 | return PyLong_FromLong((long)ival);
1046 | /* Count the number of Python digits. */
1047 | t = (unsigned PY_LONG_LONGlong long)ival;
1048 | while (t) {
1049 | ++ndigits;
1050 | t >>= PyLong_SHIFT30;
1051 | }
1052 | v = _PyLong_New(ndigits);
1053 | if (v != NULL((void*)0)) {
1054 | digit *p = v->ob_digit;
1055 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = ndigits;
1056 | while (ival) {
1057 | *p++ = (digit)(ival & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
1058 | ival >>= PyLong_SHIFT30;
1059 | }
1060 | }
1061 | return (PyObject *)v;
1062 | }
1063 |
1064 | /* Create a new long int object from a C Py_ssize_t. */
1065 |
1066 | PyObject *
1067 | PyLong_FromSsize_t(Py_ssize_t ival)
1068 | {
1069 | PyLongObject *v;
1070 | size_t abs_ival;
1071 | size_t t; /* unsigned so >> doesn't propagate sign bit */
1072 | int ndigits = 0;
1073 | int negative = 0;
1074 |
1075 | CHECK_SMALL_INT(ival)do if (-5 <= ival && ival < 257) { return get_small_int ((sdigit)ival); } while(0);
1076 | if (ival < 0) {
1077 | /* avoid signed overflow when ival = SIZE_T_MIN */
1078 | abs_ival = (size_t)(-1-ival)+1;
1079 | negative = 1;
1080 | }
1081 | else {
1082 | abs_ival = (size_t)ival;
1083 | }
1084 |
1085 | /* Count the number of Python digits. */
1086 | t = abs_ival;
1087 | while (t) {
1088 | ++ndigits;
1089 | t >>= PyLong_SHIFT30;
1090 | }
1091 | v = _PyLong_New(ndigits);
1092 | if (v != NULL((void*)0)) {
1093 | digit *p = v->ob_digit;
1094 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = negative ? -ndigits : ndigits;
1095 | t = abs_ival;
1096 | while (t) {
1097 | *p++ = (digit)(t & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
1098 | t >>= PyLong_SHIFT30;
1099 | }
1100 | }
1101 | return (PyObject *)v;
1102 | }
1103 |
1104 | /* Create a new long int object from a C size_t. */
1105 |
1106 | PyObject *
1107 | PyLong_FromSize_t(size_t ival)
1108 | {
1109 | PyLongObject *v;
1110 | size_t t;
1111 | int ndigits = 0;
1112 |
1113 | if (ival < PyLong_BASE((digit)1 << 30))
1114 | return PyLong_FromLong((long)ival);
1115 | /* Count the number of Python digits. */
1116 | t = ival;
1117 | while (t) {
1118 | ++ndigits;
1119 | t >>= PyLong_SHIFT30;
1120 | }
1121 | v = _PyLong_New(ndigits);
1122 | if (v != NULL((void*)0)) {
1123 | digit *p = v->ob_digit;
1124 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = ndigits;
1125 | while (ival) {
1126 | *p++ = (digit)(ival & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
1127 | ival >>= PyLong_SHIFT30;
1128 | }
1129 | }
1130 | return (PyObject *)v;
1131 | }
1132 |
1133 | /* Get a C PY_LONG_LONG int from a long int object.
1134 | Return -1 and set an error if overflow occurs. */
1135 |
1136 | PY_LONG_LONGlong long
1137 | PyLong_AsLongLong(PyObject *vv)
1138 | {
1139 | PyLongObject *v;
1140 | PY_LONG_LONGlong long bytes;
1141 | int one = 1;
1142 | int res;
1143 |
1144 | if (vv == NULL((void*)0)) {
1145 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1145);
1146 | return -1;
1147 | }
1148 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1149 | PyNumberMethods *nb;
1150 | PyObject *io;
1151 | if ((nb = vv->ob_type->tp_as_number) == NULL((void*)0) ||
1152 | nb->nb_int == NULL((void*)0)) {
1153 | PyErr_SetString(PyExc_TypeError, "an integer is required");
1154 | return -1;
1155 | }
1156 | io = (*nb->nb_int) (vv);
1157 | if (io == NULL((void*)0))
1158 | return -1;
1159 | if (PyLong_Check(io)((((((PyObject*)(io))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1160 | bytes = PyLong_AsLongLong(io);
1161 | Py_DECREF(io)do { if (_Py_RefTotal-- , --((PyObject*)(io))->ob_refcnt != 0) { if (((PyObject*)io)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1161, (PyObject *)(io)); } else _Py_Dealloc ((PyObject *)(io)); } while (0);
1162 | return bytes;
1163 | }
1164 | Py_DECREF(io)do { if (_Py_RefTotal-- , --((PyObject*)(io))->ob_refcnt != 0) { if (((PyObject*)io)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1164, (PyObject *)(io)); } else _Py_Dealloc ((PyObject *)(io)); } while (0);
1165 | PyErr_SetString(PyExc_TypeError, "integer conversion failed");
1166 | return -1;
1167 | }
1168 |
1169 | v = (PyLongObject*)vv;
1170 | switch(Py_SIZE(v)(((PyVarObject*)(v))->ob_size)) {
1171 | case -1: return -(sdigit)v->ob_digit[0];
1172 | case 0: return 0;
1173 | case 1: return v->ob_digit[0];
1174 | }
1175 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
1176 | SIZEOF_LONG_LONG8, IS_LITTLE_ENDIAN, 1);
1177 |
1178 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
1179 | if (res < 0)
1180 | return (PY_LONG_LONGlong long)-1;
1181 | else
1182 | return bytes;
1183 | }
1184 |
1185 | /* Get a C unsigned PY_LONG_LONG int from a long int object.
1186 | Return -1 and set an error if overflow occurs. */
1187 |
1188 | unsigned PY_LONG_LONGlong long
1189 | PyLong_AsUnsignedLongLong(PyObject *vv)
1190 | {
1191 | PyLongObject *v;
1192 | unsigned PY_LONG_LONGlong long bytes;
1193 | int one = 1;
1194 | int res;
1195 |
1196 | if (vv == NULL((void*)0) || !PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1197 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1197);
1198 | return (unsigned PY_LONG_LONGlong long)-1;
1199 | }
1200 |
1201 | v = (PyLongObject*)vv;
1202 | switch(Py_SIZE(v)(((PyVarObject*)(v))->ob_size)) {
1203 | case 0: return 0;
1204 | case 1: return v->ob_digit[0];
1205 | }
1206 |
1207 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
1208 | SIZEOF_LONG_LONG8, IS_LITTLE_ENDIAN, 0);
1209 |
1210 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
1211 | if (res < 0)
1212 | return (unsigned PY_LONG_LONGlong long)res;
1213 | else
1214 | return bytes;
1215 | }
1216 |
1217 | /* Get a C unsigned long int from a long int object, ignoring the high bits.
1218 | Returns -1 and sets an error condition if an error occurs. */
1219 |
1220 | static unsigned PY_LONG_LONGlong long
1221 | _PyLong_AsUnsignedLongLongMask(PyObject *vv)
1222 | {
1223 | register PyLongObject *v;
1224 | unsigned PY_LONG_LONGlong long x;
1225 | Py_ssize_t i;
1226 | int sign;
1227 |
1228 | if (vv == NULL((void*)0) || !PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1229 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1229);
1230 | return (unsigned long) -1;
1231 | }
1232 | v = (PyLongObject *)vv;
1233 | switch(Py_SIZE(v)(((PyVarObject*)(v))->ob_size)) {
1234 | case 0: return 0;
1235 | case 1: return v->ob_digit[0];
1236 | }
1237 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
1238 | sign = 1;
1239 | x = 0;
1240 | if (i < 0) {
1241 | sign = -1;
1242 | i = -i;
1243 | }
1244 | while (--i >= 0) {
1245 | x = (x << PyLong_SHIFT30) | v->ob_digit[i];
1246 | }
1247 | return x * sign;
1248 | }
1249 |
1250 | unsigned PY_LONG_LONGlong long
1251 | PyLong_AsUnsignedLongLongMask(register PyObject *op)
1252 | {
1253 | PyNumberMethods *nb;
1254 | PyLongObject *lo;
1255 | unsigned PY_LONG_LONGlong long val;
1256 |
1257 | if (op && PyLong_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<< 24))) != 0))
1258 | return _PyLong_AsUnsignedLongLongMask(op);
1259 |
1260 | if (op == NULL((void*)0) || (nb = op->ob_type->tp_as_number) == NULL((void*)0) ||
1261 | nb->nb_int == NULL((void*)0)) {
1262 | PyErr_SetString(PyExc_TypeError, "an integer is required");
1263 | return (unsigned PY_LONG_LONGlong long)-1;
1264 | }
1265 |
1266 | lo = (PyLongObject*) (*nb->nb_int) (op);
1267 | if (lo == NULL((void*)0))
1268 | return (unsigned PY_LONG_LONGlong long)-1;
1269 | if (PyLong_Check(lo)((((((PyObject*)(lo))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1270 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
1271 | Py_DECREF(lo)do { if (_Py_RefTotal-- , --((PyObject*)(lo))->ob_refcnt != 0) { if (((PyObject*)lo)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1271, (PyObject *)(lo)); } else _Py_Dealloc ((PyObject *)(lo)); } while (0);
1272 | if (PyErr_Occurred())
1273 | return (unsigned PY_LONG_LONGlong long)-1;
1274 | return val;
1275 | }
1276 | else
1277 | {
1278 | Py_DECREF(lo)do { if (_Py_RefTotal-- , --((PyObject*)(lo))->ob_refcnt != 0) { if (((PyObject*)lo)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1278, (PyObject *)(lo)); } else _Py_Dealloc ((PyObject *)(lo)); } while (0);
1279 | PyErr_SetString(PyExc_TypeError,
1280 | "nb_int should return int object");
1281 | return (unsigned PY_LONG_LONGlong long)-1;
1282 | }
1283 | }
1284 | #undef IS_LITTLE_ENDIAN
1285 |
1286 | /* Get a C long long int from a Python long or Python int object.
1287 | On overflow, returns -1 and sets *overflow to 1 or -1 depending
1288 | on the sign of the result. Otherwise *overflow is 0.
1289 |
1290 | For other errors (e.g., type error), returns -1 and sets an error
1291 | condition.
1292 | */
1293 |
1294 | PY_LONG_LONGlong long
1295 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
1296 | {
1297 | /* This version by Tim Peters */
1298 | register PyLongObject *v;
1299 | unsigned PY_LONG_LONGlong long x, prev;
1300 | PY_LONG_LONGlong long res;
1301 | Py_ssize_t i;
1302 | int sign;
1303 | int do_decref = 0; /* if nb_int was called */
1304 |
1305 | *overflow = 0;
1306 | if (vv == NULL((void*)0)) {
1307 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1307);
1308 | return -1;
1309 | }
1310 |
1311 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1312 | PyNumberMethods *nb;
1313 | nb = vv->ob_type->tp_as_number;
1314 | if (nb == NULL((void*)0) || nb->nb_int == NULL((void*)0)) {
1315 | PyErr_SetString(PyExc_TypeError,
1316 | "an integer is required");
1317 | return -1;
1318 | }
1319 | vv = (*nb->nb_int) (vv);
1320 | if (vv == NULL((void*)0))
1321 | return -1;
1322 | do_decref = 1;
1323 | if (!PyLong_Check(vv)((((((PyObject*)(vv))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1324 | Py_DECREF(vv)do { if (_Py_RefTotal-- , --((PyObject*)(vv))->ob_refcnt != 0) { if (((PyObject*)vv)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1324, (PyObject *)(vv)); } else _Py_Dealloc ((PyObject *)(vv)); } while (0);
1325 | PyErr_SetString(PyExc_TypeError,
1326 | "nb_int should return int object");
1327 | return -1;
1328 | }
1329 | }
1330 |
1331 | res = -1;
1332 | v = (PyLongObject *)vv;
1333 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
1334 |
1335 | switch (i) {
1336 | case -1:
1337 | res = -(sdigit)v->ob_digit[0];
1338 | break;
1339 | case 0:
1340 | res = 0;
1341 | break;
1342 | case 1:
1343 | res = v->ob_digit[0];
1344 | break;
1345 | default:
1346 | sign = 1;
1347 | x = 0;
1348 | if (i < 0) {
1349 | sign = -1;
1350 | i = -(i);
1351 | }
1352 | while (--i >= 0) {
1353 | prev = x;
1354 | x = (x << PyLong_SHIFT30) + v->ob_digit[i];
1355 | if ((x >> PyLong_SHIFT30) != prev) {
1356 | *overflow = sign;
1357 | goto exit;
1358 | }
1359 | }
1360 | /* Haven't lost any bits, but casting to long requires extra
1361 | * care (see comment above).
1362 | */
1363 | if (x <= (unsigned PY_LONG_LONGlong long)PY_LLONG_MAX9223372036854775807LL) {
1364 | res = (PY_LONG_LONGlong long)x * sign;
1365 | }
1366 | else if (sign < 0 && x == PY_ABS_LLONG_MIN(0 -(unsigned long long)(-9223372036854775807LL -1LL))) {
1367 | res = PY_LLONG_MIN(-9223372036854775807LL -1LL);
1368 | }
1369 | else {
1370 | *overflow = sign;
1371 | /* res is already set to -1 */
1372 | }
1373 | }
1374 | exit:
1375 | if (do_decref) {
1376 | Py_DECREF(vv)do { if (_Py_RefTotal-- , --((PyObject*)(vv))->ob_refcnt != 0) { if (((PyObject*)vv)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1376, (PyObject *)(vv)); } else _Py_Dealloc ((PyObject *)(vv)); } while (0);
1377 | }
1378 | return res;
1379 | }
1380 |
1381 | #endif /* HAVE_LONG_LONG */
1382 |
1383 | #define CHECK_BINOP(v,w)do { if (!((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(w))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0) \
1384 | do { \
1385 | if (!PyLong_Check(v)((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<< 24))) != 0) || !PyLong_Check(w)((((((PyObject*)(w))->ob_type))->tp_flags & ((1L<< 24))) != 0)) { \
1386 | Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct )))->ob_refcnt++); \
1387 | return Py_NotImplemented(&_Py_NotImplementedStruct); \
1388 | } \
1389 | } while(0)
1390 |
1391 | /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
1392 | 2**k if d is nonzero, else 0. */
1393 |
1394 | static const unsigned char BitLengthTable[32] = {
1395 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
1396 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
1397 | };
1398 |
1399 | static int
1400 | bits_in_digit(digit d)
1401 | {
1402 | int d_bits = 0;
1403 | while (d >= 32) {
1404 | d_bits += 6;
1405 | d >>= 6;
1406 | }
1407 | d_bits += (int)BitLengthTable[d];
1408 | return d_bits;
1409 | }
1410 |
1411 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1412 | * is modified in place, by adding y to it. Carries are propagated as far as
1413 | * x[m-1], and the remaining carry (0 or 1) is returned.
1414 | */
1415 | static digit
1416 | v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1417 | {
1418 | Py_ssize_t i;
1419 | digit carry = 0;
1420 |
1421 | assert(m >= n)(__builtin_expect(!(m >= n), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1421, "m >= n") : (void)0);
1422 | for (i = 0; i < n; ++i) {
1423 | carry += x[i] + y[i];
1424 | x[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
1425 | carry >>= PyLong_SHIFT30;
1426 | assert((carry & 1) == carry)(__builtin_expect(!((carry & 1) == carry), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1426, "(carry & 1) == carry" ) : (void)0);
1427 | }
1428 | for (; carry && i < m; ++i) {
1429 | carry += x[i];
1430 | x[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
1431 | carry >>= PyLong_SHIFT30;
1432 | assert((carry & 1) == carry)(__builtin_expect(!((carry & 1) == carry), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1432, "(carry & 1) == carry" ) : (void)0);
1433 | }
1434 | return carry;
1435 | }
1436 |
1437 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1438 | * is modified in place, by subtracting y from it. Borrows are propagated as
1439 | * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1440 | */
1441 | static digit
1442 | v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1443 | {
1444 | Py_ssize_t i;
1445 | digit borrow = 0;
1446 |
1447 | assert(m >= n)(__builtin_expect(!(m >= n), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1447, "m >= n") : (void)0);
1448 | for (i = 0; i < n; ++i) {
1449 | borrow = x[i] - y[i] - borrow;
1450 | x[i] = borrow & PyLong_MASK((digit)(((digit)1 << 30) - 1));
1451 | borrow >>= PyLong_SHIFT30;
1452 | borrow &= 1; /* keep only 1 sign bit */
1453 | }
1454 | for (; borrow && i < m; ++i) {
1455 | borrow = x[i] - borrow;
1456 | x[i] = borrow & PyLong_MASK((digit)(((digit)1 << 30) - 1));
1457 | borrow >>= PyLong_SHIFT30;
1458 | borrow &= 1;
1459 | }
1460 | return borrow;
1461 | }
1462 |
1463 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put
1464 | * result in z[0:m], and return the d bits shifted out of the top.
1465 | */
1466 | static digit
1467 | v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
1468 | {
1469 | Py_ssize_t i;
1470 | digit carry = 0;
1471 |
1472 | assert(0 <= d && d < PyLong_SHIFT)(__builtin_expect(!(0 <= d && d < 30), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1472, "0 <= d && d < PyLong_SHIFT" ) : (void)0);
1473 | for (i=0; i < m; i++) {
1474 | twodigits acc = (twodigits)a[i] << d | carry;
1475 | z[i] = (digit)acc & PyLong_MASK((digit)(((digit)1 << 30) - 1));
1476 | carry = (digit)(acc >> PyLong_SHIFT30);
1477 | }
1478 | return carry;
1479 | }
1480 |
1481 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put
1482 | * result in z[0:m], and return the d bits shifted out of the bottom.
1483 | */
1484 | static digit
1485 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
1486 | {
1487 | Py_ssize_t i;
1488 | digit carry = 0;
1489 | digit mask = ((digit)1 << d) - 1U;
1490 |
1491 | assert(0 <= d && d < PyLong_SHIFT)(__builtin_expect(!(0 <= d && d < 30), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1491, "0 <= d && d < PyLong_SHIFT" ) : (void)0);
1492 | for (i=m; i-- > 0;) {
1493 | twodigits acc = (twodigits)carry << PyLong_SHIFT30 | a[i];
1494 | carry = (digit)acc & mask;
1495 | z[i] = (digit)(acc >> d);
1496 | }
1497 | return carry;
1498 | }
1499 |
1500 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1501 | in pout, and returning the remainder. pin and pout point at the LSD.
1502 | It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1503 | _PyLong_Format, but that should be done with great care since longs are
1504 | immutable. */
1505 |
1506 | static digit
1507 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
1508 | {
1509 | twodigits rem = 0;
1510 |
1511 | assert(n > 0 && n <= PyLong_MASK)(__builtin_expect(!(n > 0 && n <= ((digit)(((digit )1 << 30) - 1))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1511, "n > 0 && n <= PyLong_MASK") : (void)0);
1512 | pin += size;
1513 | pout += size;
1514 | while (--size >= 0) {
1515 | digit hi;
1516 | rem = (rem << PyLong_SHIFT30) | *--pin;
1517 | *--pout = hi = (digit)(rem / n);
1518 | rem -= (twodigits)hi * n;
1519 | }
1520 | return (digit)rem;
1521 | }
1522 |
1523 | /* Divide a long integer by a digit, returning both the quotient
1524 | (as function result) and the remainder (through *prem).
1525 | The sign of a is ignored; n should not be zero. */
1526 |
1527 | static PyLongObject *
1528 | divrem1(PyLongObject *a, digit n, digit *prem)
1529 | {
1530 | const Py_ssize_t size = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
1531 | PyLongObject *z;
1532 |
1533 | assert(n > 0 && n <= PyLong_MASK)(__builtin_expect(!(n > 0 && n <= ((digit)(((digit )1 << 30) - 1))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1533, "n > 0 && n <= PyLong_MASK") : (void)0);
1534 | z = _PyLong_New(size);
1535 | if (z == NULL((void*)0))
1536 | return NULL((void*)0);
1537 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
1538 | return long_normalize(z);
1539 | }
1540 |
1541 | /* Convert a long integer to a base 10 string. Returns a new non-shared
1542 | string. (Return value is non-shared so that callers can modify the
1543 | returned value if necessary.) */
1544 |
1545 | static PyObject *
1546 | long_to_decimal_string(PyObject *aa)
1547 | {
1548 | PyLongObject *scratch, *a;
1549 | PyObject *str;
1550 | Py_ssize_t size, strlen, size_a, i, j;
1551 | digit *pout, *pin, rem, tenpow;
1552 | Py_UNICODE *p;
1553 | int negative;
1554 |
1555 | a = (PyLongObject *)aa;
1556 | if (a == NULL((void*)0) || !PyLong_Check(a)((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1557 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1557);
1558 | return NULL((void*)0);
1559 | }
1560 | size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
1561 | negative = Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0;
1562 |
1563 | /* quick and dirty upper bound for the number of digits
1564 | required to express a in base _PyLong_DECIMAL_BASE:
1565 |
1566 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE))
1567 |
1568 | But log2(a) < size_a * PyLong_SHIFT, and
1569 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
1570 | > 3 * _PyLong_DECIMAL_SHIFT
1571 | */
1572 | if (size_a > PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) / PyLong_SHIFT30) {
1573 | PyErr_SetString(PyExc_OverflowError,
1574 | "long is too large to format");
1575 | return NULL((void*)0);
1576 | }
1577 | /* the expression size_a * PyLong_SHIFT is now safe from overflow */
1578 | size = 1 + size_a * PyLong_SHIFT30 / (3 * _PyLong_DECIMAL_SHIFT9);
1579 | scratch = _PyLong_New(size);
1580 | if (scratch == NULL((void*)0))
1581 | return NULL((void*)0);
1582 |
1583 | /* convert array of base _PyLong_BASE digits in pin to an array of
1584 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP,
1585 | Volume 2 (3rd edn), section 4.4, Method 1b). */
1586 | pin = a->ob_digit;
1587 | pout = scratch->ob_digit;
1588 | size = 0;
1589 | for (i = size_a; --i >= 0; ) {
1590 | digit hi = pin[i];
1591 | for (j = 0; j < size; j++) {
1592 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT30 | hi;
1593 | hi = (digit)(z / _PyLong_DECIMAL_BASE((digit)1000000000));
1594 | pout[j] = (digit)(z - (twodigits)hi *
1595 | _PyLong_DECIMAL_BASE((digit)1000000000));
1596 | }
1597 | while (hi) {
1598 | pout[size++] = hi % _PyLong_DECIMAL_BASE((digit)1000000000);
1599 | hi /= _PyLong_DECIMAL_BASE((digit)1000000000);
1600 | }
1601 | /* check for keyboard interrupt */
1602 | SIGCHECK({do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*) scratch)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 1603, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject * )(scratch)); } while (0); return ((void*)0); } } while(0)
1603 | Py_DECREF(scratch);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*) scratch)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 1603, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject * )(scratch)); } while (0); return ((void*)0); } } while(0)
1604 | return NULL;do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*) scratch)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 1603, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject * )(scratch)); } while (0); return ((void*)0); } } while(0)
1605 | })do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*) scratch)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 1603, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject * )(scratch)); } while (0); return ((void*)0); } } while(0);
1606 | }
1607 | /* pout should have at least one digit, so that the case when a = 0
1608 | works correctly */
1609 | if (size == 0)
1610 | pout[size++] = 0;
1611 |
1612 | /* calculate exact length of output string, and allocate */
1613 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT9;
1614 | tenpow = 10;
1615 | rem = pout[size-1];
1616 | while (rem >= tenpow) {
1617 | tenpow *= 10;
1618 | strlen++;
1619 | }
1620 | str = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), strlen);
1621 | if (str == NULL((void*)0)) {
1622 | Py_DECREF(scratch)do { if (_Py_RefTotal-- , --((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*)scratch)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1622, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject *)(scratch)); } while (0);
1623 | return NULL((void*)0);
1624 | }
1625 |
1626 | /* fill the string right-to-left */
1627 | p = PyUnicode_AS_UNICODE(str)((__builtin_expect(!(((((((PyObject*)(str))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 1627, "PyUnicode_Check(str)") : (void )0),(((PyUnicodeObject *)(str))->str)) + strlen;
1628 | *p = '\0';
1629 | /* pout[0] through pout[size-2] contribute exactly
1630 | _PyLong_DECIMAL_SHIFT digits each */
1631 | for (i=0; i < size - 1; i++) {
1632 | rem = pout[i];
1633 | for (j = 0; j < _PyLong_DECIMAL_SHIFT9; j++) {
1634 | *--p = '0' + rem % 10;
1635 | rem /= 10;
1636 | }
1637 | }
1638 | /* pout[size-1]: always produce at least one decimal digit */
1639 | rem = pout[i];
1640 | do {
1641 | *--p = '0' + rem % 10;
1642 | rem /= 10;
1643 | } while (rem != 0);
1644 |
1645 | /* and sign */
1646 | if (negative)
1647 | *--p = '-';
1648 |
1649 | /* check we've counted correctly */
1650 | assert(p == PyUnicode_AS_UNICODE(str))(__builtin_expect(!(p == ((__builtin_expect(!(((((((PyObject* )(str))->ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 1650 , "PyUnicode_Check(str)") : (void)0),(((PyUnicodeObject *)(str ))->str))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1650, "p == PyUnicode_AS_UNICODE(str)") : (void)0);
1651 | Py_DECREF(scratch)do { if (_Py_RefTotal-- , --((PyObject*)(scratch))->ob_refcnt != 0) { if (((PyObject*)scratch)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1651, (PyObject *)(scratch)); } else _Py_Dealloc((PyObject *)(scratch)); } while (0);
1652 | return (PyObject *)str;
1653 | }
1654 |
1655 | /* Convert a long int object to a string, using a given conversion base,
1656 | which should be one of 2, 8, 10 or 16. Return a string object.
1657 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */
1658 |
1659 | PyObject *
1660 | _PyLong_Format(PyObject *aa, int base)
1661 | {
1662 | register PyLongObject *a = (PyLongObject *)aa;
1663 | PyObject *str;
1664 | Py_ssize_t i, sz;
1665 | Py_ssize_t size_a;
1666 | Py_UNICODE *p, sign = '\0';
1667 | int bits;
1668 |
1669 | assert(base == 2 || base == 8 || base == 10 || base == 16)(__builtin_expect(!(base == 2 || base == 8 || base == 10 || base == 16), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 1669 , "base == 2 || base == 8 || base == 10 || base == 16") : (void )0);
1670 | if (base == 10)
1671 | return long_to_decimal_string((PyObject *)a);
1672 |
1673 | if (a == NULL((void*)0) || !PyLong_Check(a)((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
1674 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 1674);
1675 | return NULL((void*)0);
1676 | }
1677 | size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
1678 |
1679 | /* Compute a rough upper bound for the length of the string */
1680 | switch (base) {
1681 | case 16:
1682 | bits = 4;
1683 | break;
1684 | case 8:
1685 | bits = 3;
1686 | break;
1687 | case 2:
1688 | bits = 1;
1689 | break;
1690 | default:
1691 | assert(0)(__builtin_expect(!(0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1691, "0") : (void)0); /* shouldn't ever get here */
1692 | bits = 0; /* to silence gcc warning */
1693 | }
1694 | /* compute length of output string: allow 2 characters for prefix and
1695 | 1 for possible '-' sign. */
1696 | if (size_a > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 3) / PyLong_SHIFT30) {
1697 | PyErr_SetString(PyExc_OverflowError,
1698 | "int is too large to format");
1699 | return NULL((void*)0);
1700 | }
1701 | /* now size_a * PyLong_SHIFT + 3 <= PY_SSIZE_T_MAX, so the RHS below
1702 | is safe from overflow */
1703 | sz = 3 + (size_a * PyLong_SHIFT30 + (bits - 1)) / bits;
1704 | assert(sz >= 0)(__builtin_expect(!(sz >= 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1704, "sz >= 0") : (void)0);
1705 | str = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), sz);
1706 | if (str == NULL((void*)0))
1707 | return NULL((void*)0);
1708 | p = PyUnicode_AS_UNICODE(str)((__builtin_expect(!(((((((PyObject*)(str))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 1708, "PyUnicode_Check(str)") : (void )0),(((PyUnicodeObject *)(str))->str)) + sz;
1709 | *p = '\0';
1710 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0)
1711 | sign = '-';
1712 |
1713 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) == 0) {
1714 | *--p = '0';
1715 | }
1716 | else {
1717 | /* JRH: special case for power-of-2 bases */
1718 | twodigits accum = 0;
1719 | int accumbits = 0; /* # of bits in accum */
1720 | for (i = 0; i < size_a; ++i) {
1721 | accum |= (twodigits)a->ob_digit[i] << accumbits;
1722 | accumbits += PyLong_SHIFT30;
1723 | assert(accumbits >= bits)(__builtin_expect(!(accumbits >= bits), 0) ? __assert_rtn( __func__, "Objects/longobject.c", 1723, "accumbits >= bits" ) : (void)0);
1724 | do {
1725 | Py_UNICODE cdigit;
1726 | cdigit = (Py_UNICODE)(accum & (base - 1));
1727 | cdigit += (cdigit < 10) ? '0' : 'a'-10;
1728 | assert(p > PyUnicode_AS_UNICODE(str))(__builtin_expect(!(p > ((__builtin_expect(!(((((((PyObject *)(str))->ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 1728 , "PyUnicode_Check(str)") : (void)0),(((PyUnicodeObject *)(str ))->str))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1728, "p > PyUnicode_AS_UNICODE(str)") : (void)0);
1729 | *--p = cdigit;
1730 | accumbits -= bits;
1731 | accum >>= bits;
1732 | } while (i < size_a-1 ? accumbits >= bits : accum > 0);
1733 | }
1734 | }
1735 |
1736 | if (base == 16)
1737 | *--p = 'x';
1738 | else if (base == 8)
1739 | *--p = 'o';
1740 | else /* (base == 2) */
1741 | *--p = 'b';
1742 | *--p = '0';
1743 | if (sign)
1744 | *--p = sign;
1745 | if (p != PyUnicode_AS_UNICODE(str)((__builtin_expect(!(((((((PyObject*)(str))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 1745, "PyUnicode_Check(str)") : (void )0),(((PyUnicodeObject *)(str))->str))) {
1746 | Py_UNICODE *q = PyUnicode_AS_UNICODE(str)((__builtin_expect(!(((((((PyObject*)(str))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 1746, "PyUnicode_Check(str)") : (void )0),(((PyUnicodeObject *)(str))->str));
1747 | assert(p > q)(__builtin_expect(!(p > q), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1747, "p > q") : (void)0);
1748 | do {
1749 | } while ((*q++ = *p++) != '\0');
1750 | q--;
1751 | if (PyUnicode_ResizePyUnicodeUCS2_Resize(&str,(Py_ssize_t) (q -
1752 | PyUnicode_AS_UNICODE(str)((__builtin_expect(!(((((((PyObject*)(str))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 1752, "PyUnicode_Check(str)") : (void )0),(((PyUnicodeObject *)(str))->str))))) {
1753 | Py_DECREF(str)do { if (_Py_RefTotal-- , --((PyObject*)(str))->ob_refcnt != 0) { if (((PyObject*)str)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 1753, (PyObject *)(str)); } else _Py_Dealloc ((PyObject *)(str)); } while (0);
1754 | return NULL((void*)0);
1755 | }
1756 | }
1757 | return (PyObject *)str;
1758 | }
1759 |
1760 | /* Table of digit values for 8-bit string -> integer conversion.
1761 | * '0' maps to 0, ..., '9' maps to 9.
1762 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1763 | * All other indices map to 37.
1764 | * Note that when converting a base B string, a char c is a legitimate
1765 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
1766 | */
1767 | unsigned char _PyLong_DigitValue[256] = {
1768 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1769 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1770 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1771 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1772 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1773 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1774 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1775 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1776 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1777 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1778 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1779 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1780 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1781 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1782 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1783 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1784 | };
1785 |
1786 | /* *str points to the first digit in a string of base `base` digits. base
1787 | * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1788 | * non-digit (which may be *str!). A normalized long is returned.
1789 | * The point to this routine is that it takes time linear in the number of
1790 | * string characters.
1791 | */
1792 | static PyLongObject *
1793 | long_from_binary_base(char **str, int base)
1794 | {
1795 | char *p = *str;
1796 | char *start = p;
1797 | int bits_per_char;
1798 | Py_ssize_t n;
1799 | PyLongObject *z;
1800 | twodigits accum;
1801 | int bits_in_accum;
1802 | digit *pdigit;
1803 |
1804 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0)(__builtin_expect(!(base >= 2 && base <= 32 && (base & (base - 1)) == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 1804, "base >= 2 && base <= 32 && (base & (base - 1)) == 0" ) : (void)0);
1805 | n = base;
1806 | for (bits_per_char = -1; n; ++bits_per_char)
1 | Loop condition is true. Entering loop body
2 | Loop condition is false. Execution continues on line 1809
1807 | n >>= 1;
1808 | /* n <- total # of bits needed, while setting p to end-of-string */
1809 | while (_PyLong_DigitValue[Py_CHARMASK(*p)((unsigned char)((*p) & 0xff))] < base)
3 | Loop condition is false. Execution continues on line 1811
1810 | ++p;
1811 | *str = p;
1812 | /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1813 | n = (p - start) * bits_per_char + PyLong_SHIFT30 - 1;
1814 | if (n / bits_per_char < p - start) {
4 | Division by zero
1815 | PyErr_SetString(PyExc_ValueError,
1816 | "int string too large to convert");
1817 | return NULL((void*)0);
1818 | }
1819 | n = n / PyLong_SHIFT30;
1820 | z = _PyLong_New(n);
1821 | if (z == NULL((void*)0))
1822 | return NULL((void*)0);
1823 | /* Read string from right, and fill in long from left; i.e.,
1824 | * from least to most significant in both.
1825 | */
1826 | accum = 0;
1827 | bits_in_accum = 0;
1828 | pdigit = z->ob_digit;
1829 | while (--p >= start) {
1830 | int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)((unsigned char)((*p) & 0xff))];
1831 | assert(k >= 0 && k < base)(__builtin_expect(!(k >= 0 && k < base), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1831, "k >= 0 && k < base" ) : (void)0);
1832 | accum |= (twodigits)k << bits_in_accum;
1833 | bits_in_accum += bits_per_char;
1834 | if (bits_in_accum >= PyLong_SHIFT30) {
1835 | *pdigit++ = (digit)(accum & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
1836 | assert(pdigit - z->ob_digit <= n)(__builtin_expect(!(pdigit - z->ob_digit <= n), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1836, "pdigit - z->ob_digit <= n" ) : (void)0);
1837 | accum >>= PyLong_SHIFT30;
1838 | bits_in_accum -= PyLong_SHIFT30;
1839 | assert(bits_in_accum < PyLong_SHIFT)(__builtin_expect(!(bits_in_accum < 30), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1839, "bits_in_accum < PyLong_SHIFT" ) : (void)0);
1840 | }
1841 | }
1842 | if (bits_in_accum) {
1843 | assert(bits_in_accum <= PyLong_SHIFT)(__builtin_expect(!(bits_in_accum <= 30), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1843, "bits_in_accum <= PyLong_SHIFT" ) : (void)0);
1844 | *pdigit++ = (digit)accum;
1845 | assert(pdigit - z->ob_digit <= n)(__builtin_expect(!(pdigit - z->ob_digit <= n), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 1845, "pdigit - z->ob_digit <= n" ) : (void)0);
1846 | }
1847 | while (pdigit - z->ob_digit < n)
1848 | *pdigit++ = 0;
1849 | return long_normalize(z);
1850 | }
1851 |
1852 | PyObject *
1853 | PyLong_FromString(char *str, char **pend, int base)
1854 | {
1855 | int sign = 1, error_if_nonzero = 0;
1856 | char *start, *orig_str = str;
1857 | PyLongObject *z = NULL((void*)0);
1858 | PyObject *strobj;
1859 | Py_ssize_t slen;
1860 |
1861 | if ((base != 0 && base < 2) || base > 36) {
1862 | PyErr_SetString(PyExc_ValueError,
1863 | "int() arg 2 must be >= 2 and <= 36");
1864 | return NULL((void*)0);
1865 | }
1866 | while (*str != '\0' && isspace(Py_CHARMASK(*str))iswspace(btowc(((unsigned char)((*str) & 0xff)))))
1867 | str++;
1868 | if (*str == '+')
1869 | ++str;
1870 | else if (*str == '-') {
1871 | ++str;
1872 | sign = -1;
1873 | }
1874 | if (base == 0) {
1875 | if (str[0] != '0')
1876 | base = 10;
1877 | else if (str[1] == 'x' || str[1] == 'X')
1878 | base = 16;
1879 | else if (str[1] == 'o' || str[1] == 'O')
1880 | base = 8;
1881 | else if (str[1] == 'b' || str[1] == 'B')
1882 | base = 2;
1883 | else {
1884 | /* "old" (C-style) octal literal, now invalid.
1885 | it might still be zero though */
1886 | error_if_nonzero = 1;
1887 | base = 10;
1888 | }
1889 | }
1890 | if (str[0] == '0' &&
1891 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1892 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
1893 | (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
1894 | str += 2;
1895 |
1896 | start = str;
1897 | if ((base & (base - 1)) == 0)
1898 | z = long_from_binary_base(&str, base);
1899 | else {
1900 | /***
1901 | Binary bases can be converted in time linear in the number of digits, because
1902 | Python's representation base is binary. Other bases (including decimal!) use
1903 | the simple quadratic-time algorithm below, complicated by some speed tricks.
1904 |
1905 | First some math: the largest integer that can be expressed in N base-B digits
1906 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1907 | case number of Python digits needed to hold it is the smallest integer n s.t.
1908 |
1909 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1910 | BASE**n >= B**N [taking logs to base BASE]
1911 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1912 |
1913 | The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1914 | this quickly. A Python long with that much space is reserved near the start,
1915 | and the result is computed into it.
1916 |
1917 | The input string is actually treated as being in base base**i (i.e., i digits
1918 | are processed at a time), where two more static arrays hold:
1919 |
1920 | convwidth_base[base] = the largest integer i such that base**i <= BASE
1921 | convmultmax_base[base] = base ** convwidth_base[base]
1922 |
1923 | The first of these is the largest i such that i consecutive input digits
1924 | must fit in a single Python digit. The second is effectively the input
1925 | base we're really using.
1926 |
1927 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1928 | convmultmax_base[base], the result is "simply"
1929 |
1930 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1931 |
1932 | where B = convmultmax_base[base].
1933 |
1934 | Error analysis: as above, the number of Python digits `n` needed is worst-
1935 | case
1936 |
1937 | n >= N * log(B)/log(BASE)
1938 |
1939 | where `N` is the number of input digits in base `B`. This is computed via
1940 |
1941 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1942 |
1943 | below. Two numeric concerns are how much space this can waste, and whether
1944 | the computed result can be too small. To be concrete, assume BASE = 2**15,
1945 | which is the default (and it's unlikely anyone changes that).
1946 |
1947 | Waste isn't a problem: provided the first input digit isn't 0, the difference
1948 | between the worst-case input with N digits and the smallest input with N
1949 | digits is about a factor of B, but B is small compared to BASE so at most
1950 | one allocated Python digit can remain unused on that count. If
1951 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1952 | and adding 1 returns a result 1 larger than necessary. However, that can't
1953 | happen: whenever B is a power of 2, long_from_binary_base() is called
1954 | instead, and it's impossible for B**i to be an integer power of 2**15 when
1955 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1956 | an exact integer when B is not a power of 2, since B**i has a prime factor
1957 | other than 2 in that case, but (2**15)**j's only prime factor is 2).
1958 |
1959 | The computed result can be too small if the true value of N*log(B)/log(BASE)
1960 | is a little bit larger than an exact integer, but due to roundoff errors (in
1961 | computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1962 | yields a numeric result a little less than that integer. Unfortunately, "how
1963 | close can a transcendental function get to an integer over some range?"
1964 | questions are generally theoretically intractable. Computer analysis via
1965 | continued fractions is practical: expand log(B)/log(BASE) via continued
1966 | fractions, giving a sequence i/j of "the best" rational approximations. Then
1967 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1968 | we can get very close to being in trouble, but very rarely. For example,
1969 | 76573 is a denominator in one of the continued-fraction approximations to
1970 | log(10)/log(2**15), and indeed:
1971 |
1972 | >>> log(10)/log(2**15)*76573
1973 | 16958.000000654003
1974 |
1975 | is very close to an integer. If we were working with IEEE single-precision,
1976 | rounding errors could kill us. Finding worst cases in IEEE double-precision
1977 | requires better-than-double-precision log() functions, and Tim didn't bother.
1978 | Instead the code checks to see whether the allocated space is enough as each
1979 | new Python digit is added, and copies the whole thing to a larger long if not.
1980 | This should happen extremely rarely, and in fact I don't have a test case
1981 | that triggers it(!). Instead the code was tested by artificially allocating
1982 | just 1 digit at the start, so that the copying code was exercised for every
1983 | digit beyond the first.
1984 | ***/
1985 | register twodigits c; /* current input character */
1986 | Py_ssize_t size_z;
1987 | int i;
1988 | int convwidth;
1989 | twodigits convmultmax, convmult;
1990 | digit *pz, *pzstop;
1991 | char* scan;
1992 |
1993 | static double log_base_BASE[37] = {0.0e0,};
1994 | static int convwidth_base[37] = {0,};
1995 | static twodigits convmultmax_base[37] = {0,};
1996 |
1997 | if (log_base_BASE[base] == 0.0) {
1998 | twodigits convmax = base;
1999 | int i = 1;
2000 |
2001 | log_base_BASE[base] = (log((double)base) /
2002 | log((double)PyLong_BASE((digit)1 << 30)));
2003 | for (;;) {
2004 | twodigits next = convmax * base;
2005 | if (next > PyLong_BASE((digit)1 << 30))
2006 | break;
2007 | convmax = next;
2008 | ++i;
2009 | }
2010 | convmultmax_base[base] = convmax;
2011 | assert(i > 0)(__builtin_expect(!(i > 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2011, "i > 0") : (void)0);
2012 | convwidth_base[base] = i;
2013 | }
2014 |
2015 | /* Find length of the string of numeric characters. */
2016 | scan = str;
2017 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)((unsigned char)((*scan) & 0xff))] < base)
2018 | ++scan;
2019 |
2020 | /* Create a long object that can contain the largest possible
2021 | * integer with this base and length. Note that there's no
2022 | * need to initialize z->ob_digit -- no slot is read up before
2023 | * being stored into.
2024 | */
2025 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
2026 | /* Uncomment next line to test exceedingly rare copy code */
2027 | /* size_z = 1; */
2028 | assert(size_z > 0)(__builtin_expect(!(size_z > 0), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 2028, "size_z > 0") : (void)0);
2029 | z = _PyLong_New(size_z);
2030 | if (z == NULL((void*)0))
2031 | return NULL((void*)0);
2032 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = 0;
2033 |
2034 | /* `convwidth` consecutive input digits are treated as a single
2035 | * digit in base `convmultmax`.
2036 | */
2037 | convwidth = convwidth_base[base];
2038 | convmultmax = convmultmax_base[base];
2039 |
2040 | /* Work ;-) */
2041 | while (str < scan) {
2042 | /* grab up to convwidth digits from the input string */
2043 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)((unsigned char)((*str++) & 0xff))];
2044 | for (i = 1; i < convwidth && str != scan; ++i, ++str) {
2045 | c = (twodigits)(c * base +
2046 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)((unsigned char)((*str) & 0xff))]);
2047 | assert(c < PyLong_BASE)(__builtin_expect(!(c < ((digit)1 << 30)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2047, "c < PyLong_BASE" ) : (void)0);
2048 | }
2049 |
2050 | convmult = convmultmax;
2051 | /* Calculate the shift only if we couldn't get
2052 | * convwidth digits.
2053 | */
2054 | if (i != convwidth) {
2055 | convmult = base;
2056 | for ( ; i > 1; --i)
2057 | convmult *= base;
2058 | }
2059 |
2060 | /* Multiply z by convmult, and add c. */
2061 | pz = z->ob_digit;
2062 | pzstop = pz + Py_SIZE(z)(((PyVarObject*)(z))->ob_size);
2063 | for (; pz < pzstop; ++pz) {
2064 | c += (twodigits)*pz * convmult;
2065 | *pz = (digit)(c & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2066 | c >>= PyLong_SHIFT30;
2067 | }
2068 | /* carry off the current end? */
2069 | if (c) {
2070 | assert(c < PyLong_BASE)(__builtin_expect(!(c < ((digit)1 << 30)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2070, "c < PyLong_BASE" ) : (void)0);
2071 | if (Py_SIZE(z)(((PyVarObject*)(z))->ob_size) < size_z) {
2072 | *pz = (digit)c;
2073 | ++Py_SIZE(z)(((PyVarObject*)(z))->ob_size);
2074 | }
2075 | else {
2076 | PyLongObject *tmp;
2077 | /* Extremely rare. Get more space. */
2078 | assert(Py_SIZE(z) == size_z)(__builtin_expect(!((((PyVarObject*)(z))->ob_size) == size_z ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2078, "Py_SIZE(z) == size_z" ) : (void)0);
2079 | tmp = _PyLong_New(size_z + 1);
2080 | if (tmp == NULL((void*)0)) {
2081 | Py_DECREF(z)do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2081, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0);
2082 | return NULL((void*)0);
2083 | }
2084 | memcpy(tmp->ob_digit,((__builtin_object_size (tmp->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z, __builtin_object_size (tmp->ob_digit, 0) ) : __inline_memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z))
2085 | z->ob_digit,((__builtin_object_size (tmp->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z, __builtin_object_size (tmp->ob_digit, 0) ) : __inline_memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z))
2086 | sizeof(digit) * size_z)((__builtin_object_size (tmp->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z, __builtin_object_size (tmp->ob_digit, 0) ) : __inline_memcpy_chk (tmp->ob_digit, z->ob_digit, sizeof (digit) * size_z));
2087 | Py_DECREF(z)do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2087, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0);
2088 | z = tmp;
2089 | z->ob_digit[size_z] = (digit)c;
2090 | ++size_z;
2091 | }
2092 | }
2093 | }
2094 | }
2095 | if (z == NULL((void*)0))
2096 | return NULL((void*)0);
2097 | if (error_if_nonzero) {
2098 | /* reset the base to 0, else the exception message
2099 | doesn't make too much sense */
2100 | base = 0;
2101 | if (Py_SIZE(z)(((PyVarObject*)(z))->ob_size) != 0)
2102 | goto onError;
2103 | /* there might still be other problems, therefore base
2104 | remains zero here for the same reason */
2105 | }
2106 | if (str == start)
2107 | goto onError;
2108 | if (sign < 0)
2109 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(z)(((PyVarObject*)(z))->ob_size));
2110 | while (*str && isspace(Py_CHARMASK(*str))iswspace(btowc(((unsigned char)((*str) & 0xff)))))
2111 | str++;
2112 | if (*str != '\0')
2113 | goto onError;
2114 | if (pend)
2115 | *pend = str;
2116 | long_normalize(z);
2117 | return (PyObject *) maybe_small_long(z);
2118 |
2119 | onError:
2120 | Py_XDECREF(z)do { if ((z) == ((void*)0)) ; else do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2120, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); } while (0);
2121 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
2122 | strobj = PyUnicode_FromStringAndSizePyUnicodeUCS2_FromStringAndSize(orig_str, slen);
2123 | if (strobj == NULL((void*)0))
2124 | return NULL((void*)0);
2125 | PyErr_Format(PyExc_ValueError,
2126 | "invalid literal for int() with base %d: %R",
2127 | base, strobj);
2128 | Py_DECREF(strobj)do { if (_Py_RefTotal-- , --((PyObject*)(strobj))->ob_refcnt != 0) { if (((PyObject*)strobj)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2128, (PyObject *)(strobj)); } else _Py_Dealloc ((PyObject *)(strobj)); } while (0);
2129 | return NULL((void*)0);
2130 | }
2131 |
2132 | PyObject *
2133 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
2134 | {
2135 | PyObject *result;
2136 | PyObject *asciidig;
2137 | char *buffer, *end;
2138 | Py_ssize_t i, buflen;
2139 | Py_UNICODE *ptr;
2140 |
2141 | asciidig = PyUnicode_TransformDecimalToASCII(u, length);
2142 | if (asciidig == NULL((void*)0))
2143 | return NULL((void*)0);
2144 | /* Replace non-ASCII whitespace with ' ' */
2145 | ptr = PyUnicode_AS_UNICODE(asciidig)((__builtin_expect(!(((((((PyObject*)(asciidig))->ob_type) )->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2145, "PyUnicode_Check(asciidig)" ) : (void)0),(((PyUnicodeObject *)(asciidig))->str));
2146 | for (i = 0; i < length; i++) {
2147 | Py_UNICODE ch = ptr[i];
2148 | if (ch > 127 && Py_UNICODE_ISSPACE(ch)((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace (ch)))
2149 | ptr[i] = ' ';
2150 | }
2151 | buffer = _PyUnicode_AsStringAndSize(asciidig, &buflen);
2152 | if (buffer == NULL((void*)0)) {
2153 | Py_DECREF(asciidig)do { if (_Py_RefTotal-- , --((PyObject*)(asciidig))->ob_refcnt != 0) { if (((PyObject*)asciidig)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2153, (PyObject *)(asciidig)); } else _Py_Dealloc((PyObject *)(asciidig)); } while (0);
2154 | return NULL((void*)0);
2155 | }
2156 | result = PyLong_FromString(buffer, &end, base);
2157 | if (result != NULL((void*)0) && end != buffer + buflen) {
2158 | PyErr_SetString(PyExc_ValueError,
2159 | "null byte in argument for int()");
2160 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2160, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
2161 | result = NULL((void*)0);
2162 | }
2163 | Py_DECREF(asciidig)do { if (_Py_RefTotal-- , --((PyObject*)(asciidig))->ob_refcnt != 0) { if (((PyObject*)asciidig)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2163, (PyObject *)(asciidig)); } else _Py_Dealloc((PyObject *)(asciidig)); } while (0);
2164 | return result;
2165 | }
2166 |
2167 | /* forward */
2168 | static PyLongObject *x_divrem
2169 | (PyLongObject *, PyLongObject *, PyLongObject **);
2170 | static PyObject *long_long(PyObject *v);
2171 |
2172 | /* Long division with remainder, top-level routine */
2173 |
2174 | static int
2175 | long_divrem(PyLongObject *a, PyLongObject *b,
2176 | PyLongObject **pdiv, PyLongObject **prem)
2177 | {
2178 | Py_ssize_t size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))), size_b = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
2179 | PyLongObject *z;
2180 |
2181 | if (size_b == 0) {
2182 | PyErr_SetString(PyExc_ZeroDivisionError,
2183 | "integer division or modulo by zero");
2184 | return -1;
2185 | }
2186 | if (size_a < size_b ||
2187 | (size_a == size_b &&
2188 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
2189 | /* |a| < |b|. */
2190 | *pdiv = (PyLongObject*)PyLong_FromLong(0);
2191 | if (*pdiv == NULL((void*)0))
2192 | return -1;
2193 | Py_INCREF(a)( _Py_RefTotal++ , ((PyObject*)(a))->ob_refcnt++);
2194 | *prem = (PyLongObject *) a;
2195 | return 0;
2196 | }
2197 | if (size_b == 1) {
2198 | digit rem = 0;
2199 | z = divrem1(a, b->ob_digit[0], &rem);
2200 | if (z == NULL((void*)0))
2201 | return -1;
2202 | *prem = (PyLongObject *) PyLong_FromLong((long)rem);
2203 | if (*prem == NULL((void*)0)) {
2204 | Py_DECREF(z)do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2204, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0);
2205 | return -1;
2206 | }
2207 | }
2208 | else {
2209 | z = x_divrem(a, b, prem);
2210 | if (z == NULL((void*)0))
2211 | return -1;
2212 | }
2213 | /* Set the signs.
2214 | The quotient z has the sign of a*b;
2215 | the remainder r has the sign of a,
2216 | so a = b*z + r. */
2217 | if ((Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) != (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0))
2218 | NEGATE(z)do if ((((PyObject*)(z))->ob_refcnt) == 1) (((PyVarObject* )(z))->ob_size) = -(((PyVarObject*)(z))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(z))->ob_size ) < 0 ? -(sdigit)(z)->ob_digit[0] : ((((PyVarObject*)(z ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(z)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2218, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); (z) = (PyLongObject*)tmp; } while (0);
2219 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0 && Py_SIZE(*prem)(((PyVarObject*)(*prem))->ob_size) != 0)
2220 | NEGATE(*prem)do if ((((PyObject*)(*prem))->ob_refcnt) == 1) (((PyVarObject *)(*prem))->ob_size) = -(((PyVarObject*)(*prem))->ob_size ); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(*prem ))->ob_size) < 0 ? -(sdigit)(*prem)->ob_digit[0] : ( (((PyVarObject*)(*prem))->ob_size) == 0 ? (sdigit)0 : (sdigit )(*prem)->ob_digit[0]))); do { if (_Py_RefTotal-- , --((PyObject *)(*prem))->ob_refcnt != 0) { if (((PyObject*)*prem)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c", 2220, ( PyObject *)(*prem)); } else _Py_Dealloc((PyObject *)(*prem)); } while (0); (*prem) = (PyLongObject*)tmp; } while(0);
2221 | *pdiv = maybe_small_long(z);
2222 | return 0;
2223 | }
2224 |
2225 | /* Unsigned long division with remainder -- the algorithm. The arguments v1
2226 | and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */
2227 |
2228 | static PyLongObject *
2229 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
2230 | {
2231 | PyLongObject *v, *w, *a;
2232 | Py_ssize_t i, k, size_v, size_w;
2233 | int d;
2234 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
2235 | twodigits vv;
2236 | sdigit zhi;
2237 | stwodigits z;
2238 |
2239 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
2240 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly
2241 | handle the special case when the initial estimate q for a quotient
2242 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
2243 | that won't overflow a digit. */
2244 |
2245 | /* allocate space; w will also be used to hold the final remainder */
2246 | size_v = ABS(Py_SIZE(v1))(((((PyVarObject*)(v1))->ob_size)) < 0 ? -((((PyVarObject *)(v1))->ob_size)) : ((((PyVarObject*)(v1))->ob_size)));
2247 | size_w = ABS(Py_SIZE(w1))(((((PyVarObject*)(w1))->ob_size)) < 0 ? -((((PyVarObject *)(w1))->ob_size)) : ((((PyVarObject*)(w1))->ob_size)));
2248 | assert(size_v >= size_w && size_w >= 2)(__builtin_expect(!(size_v >= size_w && size_w >= 2), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2248 , "size_v >= size_w && size_w >= 2") : (void)0); /* Assert checks by div() */
2249 | v = _PyLong_New(size_v+1);
2250 | if (v == NULL((void*)0)) {
2251 | *prem = NULL((void*)0);
2252 | return NULL((void*)0);
2253 | }
2254 | w = _PyLong_New(size_w);
2255 | if (w == NULL((void*)0)) {
2256 | Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2256, (PyObject *)(v)); } else _Py_Dealloc ((PyObject *)(v)); } while (0);
2257 | *prem = NULL((void*)0);
2258 | return NULL((void*)0);
2259 | }
2260 |
2261 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
2262 | shift v1 left by the same amount. Results go into w and v. */
2263 | d = PyLong_SHIFT30 - bits_in_digit(w1->ob_digit[size_w-1]);
2264 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
2265 | assert(carry == 0)(__builtin_expect(!(carry == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2265, "carry == 0") : (void)0);
2266 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
2267 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
2268 | v->ob_digit[size_v] = carry;
2269 | size_v++;
2270 | }
2271 |
2272 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
2273 | at most (and usually exactly) k = size_v - size_w digits. */
2274 | k = size_v - size_w;
2275 | assert(k >= 0)(__builtin_expect(!(k >= 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2275, "k >= 0") : (void)0);
2276 | a = _PyLong_New(k);
2277 | if (a == NULL((void*)0)) {
2278 | Py_DECREF(w)do { if (_Py_RefTotal-- , --((PyObject*)(w))->ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2278, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0);
2279 | Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2279, (PyObject *)(v)); } else _Py_Dealloc ((PyObject *)(v)); } while (0);
2280 | *prem = NULL((void*)0);
2281 | return NULL((void*)0);
2282 | }
2283 | v0 = v->ob_digit;
2284 | w0 = w->ob_digit;
2285 | wm1 = w0[size_w-1];
2286 | wm2 = w0[size_w-2];
2287 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
2288 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
2289 | single-digit quotient q, remainder in vk[0:size_w]. */
2290 |
2291 | SIGCHECK({do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2292 | Py_DECREF(a);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2293 | Py_DECREF(w);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2294 | Py_DECREF(v);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2295 | *prem = NULL;do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2296 | return NULL;do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0)
2297 | })do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2292, (PyObject *)(a)); } else _Py_Dealloc((PyObject *)(a)) ; } while (0); do { if (_Py_RefTotal-- , --((PyObject*)(w))-> ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2293, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0); do { if (_Py_RefTotal-- , --( (PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2294, (PyObject *)(v)); } else _Py_Dealloc((PyObject *)(v)) ; } while (0); *prem = ((void*)0); return ((void*)0); } } while (0);
2298 |
2299 | /* estimate quotient digit q; may overestimate by 1 (rare) */
2300 | vtop = vk[size_w];
2301 | assert(vtop <= wm1)(__builtin_expect(!(vtop <= wm1), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 2301, "vtop <= wm1") : (void)0);
2302 | vv = ((twodigits)vtop << PyLong_SHIFT30) | vk[size_w-1];
2303 | q = (digit)(vv / wm1);
2304 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
2305 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT30)
2306 | | vk[size_w-2])) {
2307 | --q;
2308 | r += wm1;
2309 | if (r >= PyLong_BASE((digit)1 << 30))
2310 | break;
2311 | }
2312 | assert(q <= PyLong_BASE)(__builtin_expect(!(q <= ((digit)1 << 30)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2312, "q <= PyLong_BASE" ) : (void)0);
2313 |
2314 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
2315 | zhi = 0;
2316 | for (i = 0; i < size_w; ++i) {
2317 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
2318 | -PyLong_BASE * q <= z < PyLong_BASE */
2319 | z = (sdigit)vk[i] + zhi -
2320 | (stwodigits)q * (stwodigits)w0[i];
2321 | vk[i] = (digit)z & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2322 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,((z) >> (30))
2323 | z, PyLong_SHIFT)((z) >> (30));
2324 | }
2325 |
2326 | /* add w back if q was too large (this branch taken rarely) */
2327 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0)(__builtin_expect(!((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2327, "(sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0" ) : (void)0);
2328 | if ((sdigit)vtop + zhi < 0) {
2329 | carry = 0;
2330 | for (i = 0; i < size_w; ++i) {
2331 | carry += vk[i] + w0[i];
2332 | vk[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2333 | carry >>= PyLong_SHIFT30;
2334 | }
2335 | --q;
2336 | }
2337 |
2338 | /* store quotient digit */
2339 | assert(q < PyLong_BASE)(__builtin_expect(!(q < ((digit)1 << 30)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2339, "q < PyLong_BASE" ) : (void)0);
2340 | *--ak = q;
2341 | }
2342 |
2343 | /* unshift remainder; we reuse w to store the result */
2344 | carry = v_rshift(w0, v0, size_w, d);
2345 | assert(carry==0)(__builtin_expect(!(carry==0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2345, "carry==0") : (void)0);
2346 | Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt != 0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2346, (PyObject *)(v)); } else _Py_Dealloc ((PyObject *)(v)); } while (0);
2347 |
2348 | *prem = long_normalize(w);
2349 | return long_normalize(a);
2350 | }
2351 |
2352 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <=
2353 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is
2354 | rounded to DBL_MANT_DIG significant bits using round-half-to-even.
2355 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent
2356 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return
2357 | -1.0. */
2358 |
2359 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */
2360 | #if DBL_MANT_DIG53 == 53
2361 | #define EXP2_DBL_MANT_DIG9007199254740992.0 9007199254740992.0
2362 | #else
2363 | #define EXP2_DBL_MANT_DIG9007199254740992.0 (ldexp(1.0, DBL_MANT_DIG53))
2364 | #endif
2365 |
2366 | double
2367 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
2368 | {
2369 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size;
2370 | /* See below for why x_digits is always large enough. */
2371 | digit rem, x_digits[2 + (DBL_MANT_DIG53 + 1) / PyLong_SHIFT30];
2372 | double dx;
2373 | /* Correction term for round-half-to-even rounding. For a digit x,
2374 | "x + half_even_correction[x & 7]" gives x rounded to the nearest
2375 | multiple of 4, rounding ties to a multiple of 8. */
2376 | static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1};
2377 |
2378 | a_size = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
2379 | if (a_size == 0) {
2380 | /* Special case for 0: significand 0.0, exponent 0. */
2381 | *e = 0;
2382 | return 0.0;
2383 | }
2384 | a_bits = bits_in_digit(a->ob_digit[a_size-1]);
2385 | /* The following is an overflow-free version of the check
2386 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */
2387 | if (a_size >= (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 1) / PyLong_SHIFT30 + 1 &&
2388 | (a_size > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 1) / PyLong_SHIFT30 + 1 ||
2389 | a_bits > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 1) % PyLong_SHIFT30 + 1))
2390 | goto overflow;
2391 | a_bits = (a_size - 1) * PyLong_SHIFT30 + a_bits;
2392 |
2393 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size]
2394 | (shifting left if a_bits <= DBL_MANT_DIG + 2).
2395 |
2396 | Number of digits needed for result: write // for floor division.
2397 | Then if shifting left, we end up using
2398 |
2399 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT
2400 |
2401 | digits. If shifting right, we use
2402 |
2403 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT
2404 |
2405 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with
2406 | the inequalities
2407 |
2408 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT
2409 | m // PyLong_SHIFT - n // PyLong_SHIFT <=
2410 | 1 + (m - n - 1) // PyLong_SHIFT,
2411 |
2412 | valid for any integers m and n, we find that x_size satisfies
2413 |
2414 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT
2415 |
2416 | in both cases.
2417 | */
2418 | if (a_bits <= DBL_MANT_DIG53 + 2) {
2419 | shift_digits = (DBL_MANT_DIG53 + 2 - a_bits) / PyLong_SHIFT30;
2420 | shift_bits = (DBL_MANT_DIG53 + 2 - a_bits) % PyLong_SHIFT30;
2421 | x_size = 0;
2422 | while (x_size < shift_digits)
2423 | x_digits[x_size++] = 0;
2424 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size,
2425 | (int)shift_bits);
2426 | x_size += a_size;
2427 | x_digits[x_size++] = rem;
2428 | }
2429 | else {
2430 | shift_digits = (a_bits - DBL_MANT_DIG53 - 2) / PyLong_SHIFT30;
2431 | shift_bits = (a_bits - DBL_MANT_DIG53 - 2) % PyLong_SHIFT30;
2432 | rem = v_rshift(x_digits, a->ob_digit + shift_digits,
2433 | a_size - shift_digits, (int)shift_bits);
2434 | x_size = a_size - shift_digits;
2435 | /* For correct rounding below, we need the least significant
2436 | bit of x to be 'sticky' for this shift: if any of the bits
2437 | shifted out was nonzero, we set the least significant bit
2438 | of x. */
2439 | if (rem)
2440 | x_digits[0] |= 1;
2441 | else
2442 | while (shift_digits > 0)
2443 | if (a->ob_digit[--shift_digits]) {
2444 | x_digits[0] |= 1;
2445 | break;
2446 | }
2447 | }
2448 | assert(1 <= x_size &&(__builtin_expect(!(1 <= x_size && x_size <= (Py_ssize_t )(sizeof(x_digits)/sizeof(digit))), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 2449, "1 <= x_size && x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit))" ) : (void)0)
2449 | x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit)))(__builtin_expect(!(1 <= x_size && x_size <= (Py_ssize_t )(sizeof(x_digits)/sizeof(digit))), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 2449, "1 <= x_size && x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit))" ) : (void)0);
2450 |
2451 | /* Round, and convert to double. */
2452 | x_digits[0] += half_even_correction[x_digits[0] & 7];
2453 | dx = x_digits[--x_size];
2454 | while (x_size > 0)
2455 | dx = dx * PyLong_BASE((digit)1 << 30) + x_digits[--x_size];
2456 |
2457 | /* Rescale; make correction if result is 1.0. */
2458 | dx /= 4.0 * EXP2_DBL_MANT_DIG9007199254740992.0;
2459 | if (dx == 1.0) {
2460 | if (a_bits == PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)))
2461 | goto overflow;
2462 | dx = 0.5;
2463 | a_bits += 1;
2464 | }
2465 |
2466 | *e = a_bits;
2467 | return Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0 ? -dx : dx;
2468 |
2469 | overflow:
2470 | /* exponent > PY_SSIZE_T_MAX */
2471 | PyErr_SetString(PyExc_OverflowError,
2472 | "huge integer: number of bits overflows a Py_ssize_t");
2473 | *e = 0;
2474 | return -1.0;
2475 | }
2476 |
2477 | /* Get a C double from a long int object. Rounds to the nearest double,
2478 | using the round-half-to-even rule in the case of a tie. */
2479 |
2480 | double
2481 | PyLong_AsDouble(PyObject *v)
2482 | {
2483 | Py_ssize_t exponent;
2484 | double x;
2485 |
2486 | if (v == NULL((void*)0) || !PyLong_Check(v)((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
2487 | PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/longobject.c", 2487);
2488 | return -1.0;
2489 | }
2490 | x = _PyLong_Frexp((PyLongObject *)v, &exponent);
2491 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP1024) {
2492 | PyErr_SetString(PyExc_OverflowError,
2493 | "long int too large to convert to float");
2494 | return -1.0;
2495 | }
2496 | return ldexp(x, (int)exponent);
2497 | }
2498 |
2499 | /* Methods */
2500 |
2501 | static void
2502 | long_dealloc(PyObject *v)
2503 | {
2504 | Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_free(v);
2505 | }
2506 |
2507 | static int
2508 | long_compare(PyLongObject *a, PyLongObject *b)
2509 | {
2510 | Py_ssize_t sign;
2511 |
2512 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) != Py_SIZE(b)(((PyVarObject*)(b))->ob_size)) {
2513 | sign = Py_SIZE(a)(((PyVarObject*)(a))->ob_size) - Py_SIZE(b)(((PyVarObject*)(b))->ob_size);
2514 | }
2515 | else {
2516 | Py_ssize_t i = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
2517 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2518 | ;
2519 | if (i < 0)
2520 | sign = 0;
2521 | else {
2522 | sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
2523 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0)
2524 | sign = -sign;
2525 | }
2526 | }
2527 | return sign < 0 ? -1 : sign > 0 ? 1 : 0;
2528 | }
2529 |
2530 | #define TEST_COND(cond)((cond) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) & _Py_FalseStruct)) \
2531 | ((cond) ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct))
2532 |
2533 | static PyObject *
2534 | long_richcompare(PyObject *self, PyObject *other, int op)
2535 | {
2536 | int result;
2537 | PyObject *v;
2538 | CHECK_BINOP(self, other)do { if (!((((((PyObject*)(self))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(other))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
2539 | if (self == other)
2540 | result = 0;
2541 | else
2542 | result = long_compare((PyLongObject*)self, (PyLongObject*)other);
2543 | /* Convert the return value to a Boolean */
2544 | switch (op) {
2545 | case Py_EQ2:
2546 | v = TEST_COND(result == 0)((result == 0) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2547 | break;
2548 | case Py_NE3:
2549 | v = TEST_COND(result != 0)((result != 0) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2550 | break;
2551 | case Py_LE1:
2552 | v = TEST_COND(result <= 0)((result <= 0) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2553 | break;
2554 | case Py_GE5:
2555 | v = TEST_COND(result >= 0)((result >= 0) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2556 | break;
2557 | case Py_LT0:
2558 | v = TEST_COND(result == -1)((result == -1) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2559 | break;
2560 | case Py_GT4:
2561 | v = TEST_COND(result == 1)((result == 1) ? ((PyObject *) &_Py_TrueStruct) : ((PyObject *) &_Py_FalseStruct));
2562 | break;
2563 | default:
2564 | PyErr_BadArgument();
2565 | return NULL((void*)0);
2566 | }
2567 | Py_INCREF(v)( _Py_RefTotal++ , ((PyObject*)(v))->ob_refcnt++);
2568 | return v;
2569 | }
2570 |
2571 | static Py_hash_t
2572 | long_hash(PyLongObject *v)
2573 | {
2574 | Py_uhash_t x;
2575 | Py_ssize_t i;
2576 | int sign;
2577 |
2578 | i = Py_SIZE(v)(((PyVarObject*)(v))->ob_size);
2579 | switch(i) {
2580 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
2581 | case 0: return 0;
2582 | case 1: return v->ob_digit[0];
2583 | }
2584 | sign = 1;
2585 | x = 0;
2586 | if (i < 0) {
2587 | sign = -1;
2588 | i = -(i);
2589 | }
2590 | while (--i >= 0) {
2591 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
2592 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo
2593 | _PyHASH_MODULUS.
2594 |
2595 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS
2596 | amounts to a rotation of the bits of x. To see this, write
2597 |
2598 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z
2599 |
2600 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top
2601 | PyLong_SHIFT bits of x (those that are shifted out of the
2602 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
2603 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT
2604 | bits of x, shifted up. Then since 2**_PyHASH_BITS is
2605 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is
2606 | congruent to y modulo _PyHASH_MODULUS. So
2607 |
2608 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS).
2609 |
2610 | The right-hand side is just the result of rotating the
2611 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since
2612 | not all _PyHASH_BITS bits of x are 1s, the same is true
2613 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is
2614 | the reduction of x*2**PyLong_SHIFT modulo
2615 | _PyHASH_MODULUS. */
2616 | x = ((x << PyLong_SHIFT30) & _PyHASH_MODULUS(((size_t)1 << 61) - 1)) |
2617 | (x >> (_PyHASH_BITS61 - PyLong_SHIFT30));
2618 | x += v->ob_digit[i];
2619 | if (x >= _PyHASH_MODULUS(((size_t)1 << 61) - 1))
2620 | x -= _PyHASH_MODULUS(((size_t)1 << 61) - 1);
2621 | }
2622 | x = x * sign;
2623 | if (x == (Py_uhash_t)-1)
2624 | x = (Py_uhash_t)-2;
2625 | return (Py_hash_t)x;
2626 | }
2627 |
2628 |
2629 | /* Add the absolute values of two long integers. */
2630 |
2631 | static PyLongObject *
2632 | x_add(PyLongObject *a, PyLongObject *b)
2633 | {
2634 | Py_ssize_t size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))), size_b = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
2635 | PyLongObject *z;
2636 | Py_ssize_t i;
2637 | digit carry = 0;
2638 |
2639 | /* Ensure a is the larger of the two: */
2640 | if (size_a < size_b) {
2641 | { PyLongObject *temp = a; a = b; b = temp; }
2642 | { Py_ssize_t size_temp = size_a;
2643 | size_a = size_b;
2644 | size_b = size_temp; }
2645 | }
2646 | z = _PyLong_New(size_a+1);
2647 | if (z == NULL((void*)0))
2648 | return NULL((void*)0);
2649 | for (i = 0; i < size_b; ++i) {
2650 | carry += a->ob_digit[i] + b->ob_digit[i];
2651 | z->ob_digit[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2652 | carry >>= PyLong_SHIFT30;
2653 | }
2654 | for (; i < size_a; ++i) {
2655 | carry += a->ob_digit[i];
2656 | z->ob_digit[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2657 | carry >>= PyLong_SHIFT30;
2658 | }
2659 | z->ob_digit[i] = carry;
2660 | return long_normalize(z);
2661 | }
2662 |
2663 | /* Subtract the absolute values of two integers. */
2664 |
2665 | static PyLongObject *
2666 | x_sub(PyLongObject *a, PyLongObject *b)
2667 | {
2668 | Py_ssize_t size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))), size_b = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
2669 | PyLongObject *z;
2670 | Py_ssize_t i;
2671 | int sign = 1;
2672 | digit borrow = 0;
2673 |
2674 | /* Ensure a is the larger of the two: */
2675 | if (size_a < size_b) {
2676 | sign = -1;
2677 | { PyLongObject *temp = a; a = b; b = temp; }
2678 | { Py_ssize_t size_temp = size_a;
2679 | size_a = size_b;
2680 | size_b = size_temp; }
2681 | }
2682 | else if (size_a == size_b) {
2683 | /* Find highest digit where a and b differ: */
2684 | i = size_a;
2685 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2686 | ;
2687 | if (i < 0)
2688 | return (PyLongObject *)PyLong_FromLong(0);
2689 | if (a->ob_digit[i] < b->ob_digit[i]) {
2690 | sign = -1;
2691 | { PyLongObject *temp = a; a = b; b = temp; }
2692 | }
2693 | size_a = size_b = i+1;
2694 | }
2695 | z = _PyLong_New(size_a);
2696 | if (z == NULL((void*)0))
2697 | return NULL((void*)0);
2698 | for (i = 0; i < size_b; ++i) {
2699 | /* The following assumes unsigned arithmetic
2700 | works module 2**N for some N>PyLong_SHIFT. */
2701 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
2702 | z->ob_digit[i] = borrow & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2703 | borrow >>= PyLong_SHIFT30;
2704 | borrow &= 1; /* Keep only one sign bit */
2705 | }
2706 | for (; i < size_a; ++i) {
2707 | borrow = a->ob_digit[i] - borrow;
2708 | z->ob_digit[i] = borrow & PyLong_MASK((digit)(((digit)1 << 30) - 1));
2709 | borrow >>= PyLong_SHIFT30;
2710 | borrow &= 1; /* Keep only one sign bit */
2711 | }
2712 | assert(borrow == 0)(__builtin_expect(!(borrow == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2712, "borrow == 0") : (void)0);
2713 | if (sign < 0)
2714 | NEGATE(z)do if ((((PyObject*)(z))->ob_refcnt) == 1) (((PyVarObject* )(z))->ob_size) = -(((PyVarObject*)(z))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(z))->ob_size ) < 0 ? -(sdigit)(z)->ob_digit[0] : ((((PyVarObject*)(z ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(z)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2714, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); (z) = (PyLongObject*)tmp; } while (0);
2715 | return long_normalize(z);
2716 | }
2717 |
2718 | static PyObject *
2719 | long_add(PyLongObject *a, PyLongObject *b)
2720 | {
2721 | PyLongObject *z;
2722 |
2723 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
2724 |
2725 | if (ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))) <= 1 && ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size))) <= 1) {
2726 | PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a)((((PyVarObject*)(a))->ob_size) < 0 ? -(sdigit)(a)-> ob_digit[0] : ((((PyVarObject*)(a))->ob_size) == 0 ? (sdigit )0 : (sdigit)(a)->ob_digit[0])) +
2727 | MEDIUM_VALUE(b)((((PyVarObject*)(b))->ob_size) < 0 ? -(sdigit)(b)-> ob_digit[0] : ((((PyVarObject*)(b))->ob_size) == 0 ? (sdigit )0 : (sdigit)(b)->ob_digit[0])));
2728 | return result;
2729 | }
2730 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) {
2731 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0) {
2732 | z = x_add(a, b);
2733 | if (z != NULL((void*)0) && Py_SIZE(z)(((PyVarObject*)(z))->ob_size) != 0)
2734 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(z)(((PyVarObject*)(z))->ob_size));
2735 | }
2736 | else
2737 | z = x_sub(b, a);
2738 | }
2739 | else {
2740 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0)
2741 | z = x_sub(a, b);
2742 | else
2743 | z = x_add(a, b);
2744 | }
2745 | return (PyObject *)z;
2746 | }
2747 |
2748 | static PyObject *
2749 | long_sub(PyLongObject *a, PyLongObject *b)
2750 | {
2751 | PyLongObject *z;
2752 |
2753 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
2754 |
2755 | if (ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))) <= 1 && ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size))) <= 1) {
2756 | PyObject* r;
2757 | r = PyLong_FromLong(MEDIUM_VALUE(a)((((PyVarObject*)(a))->ob_size) < 0 ? -(sdigit)(a)-> ob_digit[0] : ((((PyVarObject*)(a))->ob_size) == 0 ? (sdigit )0 : (sdigit)(a)->ob_digit[0]))-MEDIUM_VALUE(b)((((PyVarObject*)(b))->ob_size) < 0 ? -(sdigit)(b)-> ob_digit[0] : ((((PyVarObject*)(b))->ob_size) == 0 ? (sdigit )0 : (sdigit)(b)->ob_digit[0])));
2758 | return r;
2759 | }
2760 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) {
2761 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0)
2762 | z = x_sub(a, b);
2763 | else
2764 | z = x_add(a, b);
2765 | if (z != NULL((void*)0) && Py_SIZE(z)(((PyVarObject*)(z))->ob_size) != 0)
2766 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(z)(((PyVarObject*)(z))->ob_size));
2767 | }
2768 | else {
2769 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0)
2770 | z = x_add(a, b);
2771 | else
2772 | z = x_sub(a, b);
2773 | }
2774 | return (PyObject *)z;
2775 | }
2776 |
2777 | /* Grade school multiplication, ignoring the signs.
2778 | * Returns the absolute value of the product, or NULL if error.
2779 | */
2780 | static PyLongObject *
2781 | x_mul(PyLongObject *a, PyLongObject *b)
2782 | {
2783 | PyLongObject *z;
2784 | Py_ssize_t size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
2785 | Py_ssize_t size_b = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
2786 | Py_ssize_t i;
2787 |
2788 | z = _PyLong_New(size_a + size_b);
2789 | if (z == NULL((void*)0))
2790 | return NULL((void*)0);
2791 |
2792 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit))((__builtin_object_size (z->ob_digit, 0) != (size_t) -1) ? __builtin___memset_chk (z->ob_digit, 0, (((PyVarObject*)( z))->ob_size) * sizeof(digit), __builtin_object_size (z-> ob_digit, 0)) : __inline_memset_chk (z->ob_digit, 0, (((PyVarObject *)(z))->ob_size) * sizeof(digit)));
2793 | if (a == b) {
2794 | /* Efficient squaring per HAC, Algorithm 14.16:
2795 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2796 | * Gives slightly less than a 2x speedup when a == b,
2797 | * via exploiting that each entry in the multiplication
2798 | * pyramid appears twice (except for the size_a squares).
2799 | */
2800 | for (i = 0; i < size_a; ++i) {
2801 | twodigits carry;
2802 | twodigits f = a->ob_digit[i];
2803 | digit *pz = z->ob_digit + (i << 1);
2804 | digit *pa = a->ob_digit + i + 1;
2805 | digit *paend = a->ob_digit + size_a;
2806 |
2807 | SIGCHECK({do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2808, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2808 | Py_DECREF(z);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2808, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2809 | return NULL;do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2808, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2810 | })do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2808, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0);
2811 |
2812 | carry = *pz + f * f;
2813 | *pz++ = (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2814 | carry >>= PyLong_SHIFT30;
2815 | assert(carry <= PyLong_MASK)(__builtin_expect(!(carry <= ((digit)(((digit)1 << 30 ) - 1))), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2815, "carry <= PyLong_MASK") : (void)0);
2816 |
2817 | /* Now f is added in twice in each column of the
2818 | * pyramid it appears. Same as adding f<<1 once.
2819 | */
2820 | f <<= 1;
2821 | while (pa < paend) {
2822 | carry += *pz + *pa++ * f;
2823 | *pz++ = (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2824 | carry >>= PyLong_SHIFT30;
2825 | assert(carry <= (PyLong_MASK << 1))(__builtin_expect(!(carry <= (((digit)(((digit)1 << 30 ) - 1)) << 1)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 2825, "carry <= (PyLong_MASK << 1)") : (void)0);
2826 | }
2827 | if (carry) {
2828 | carry += *pz;
2829 | *pz++ = (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2830 | carry >>= PyLong_SHIFT30;
2831 | }
2832 | if (carry)
2833 | *pz += (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2834 | assert((carry >> PyLong_SHIFT) == 0)(__builtin_expect(!((carry >> 30) == 0), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2834, "(carry >> PyLong_SHIFT) == 0" ) : (void)0);
2835 | }
2836 | }
2837 | else { /* a is not the same as b -- gradeschool long mult */
2838 | for (i = 0; i < size_a; ++i) {
2839 | twodigits carry = 0;
2840 | twodigits f = a->ob_digit[i];
2841 | digit *pz = z->ob_digit + i;
2842 | digit *pb = b->ob_digit;
2843 | digit *pbend = b->ob_digit + size_b;
2844 |
2845 | SIGCHECK({do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2846, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2846 | Py_DECREF(z);do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2846, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2847 | return NULL;do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2846, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0)
2848 | })do { if (PyErr_CheckSignals()) { do { if (_Py_RefTotal-- , -- ((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 2846, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); return ((void*)0); } } while(0);
2849 |
2850 | while (pb < pbend) {
2851 | carry += *pz + *pb++ * f;
2852 | *pz++ = (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2853 | carry >>= PyLong_SHIFT30;
2854 | assert(carry <= PyLong_MASK)(__builtin_expect(!(carry <= ((digit)(((digit)1 << 30 ) - 1))), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2854, "carry <= PyLong_MASK") : (void)0);
2855 | }
2856 | if (carry)
2857 | *pz += (digit)(carry & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
2858 | assert((carry >> PyLong_SHIFT) == 0)(__builtin_expect(!((carry >> 30) == 0), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2858, "(carry >> PyLong_SHIFT) == 0" ) : (void)0);
2859 | }
2860 | }
2861 | return long_normalize(z);
2862 | }
2863 |
2864 | /* A helper for Karatsuba multiplication (k_mul).
2865 | Takes a long "n" and an integer "size" representing the place to
2866 | split, and sets low and high such that abs(n) == (high << size) + low,
2867 | viewing the shift as being by digits. The sign bit is ignored, and
2868 | the return values are >= 0.
2869 | Returns 0 on success, -1 on failure.
2870 | */
2871 | static int
2872 | kmul_split(PyLongObject *n,
2873 | Py_ssize_t size,
2874 | PyLongObject **high,
2875 | PyLongObject **low)
2876 | {
2877 | PyLongObject *hi, *lo;
2878 | Py_ssize_t size_lo, size_hi;
2879 | const Py_ssize_t size_n = ABS(Py_SIZE(n))(((((PyVarObject*)(n))->ob_size)) < 0 ? -((((PyVarObject *)(n))->ob_size)) : ((((PyVarObject*)(n))->ob_size)));
2880 |
2881 | size_lo = MIN(size_n, size)((size_n) > (size) ? (size) : (size_n));
2882 | size_hi = size_n - size_lo;
2883 |
2884 | if ((hi = _PyLong_New(size_hi)) == NULL((void*)0))
2885 | return -1;
2886 | if ((lo = _PyLong_New(size_lo)) == NULL((void*)0)) {
2887 | Py_DECREF(hi)do { if (_Py_RefTotal-- , --((PyObject*)(hi))->ob_refcnt != 0) { if (((PyObject*)hi)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 2887, (PyObject *)(hi)); } else _Py_Dealloc ((PyObject *)(hi)); } while (0);
2888 | return -1;
2889 | }
2890 |
2891 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit))((__builtin_object_size (lo->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (lo->ob_digit, n->ob_digit, size_lo * sizeof(digit), __builtin_object_size (lo->ob_digit, 0)) : __inline_memcpy_chk (lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)));
2892 | memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit))((__builtin_object_size (hi->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (hi->ob_digit, n->ob_digit + size_lo , size_hi * sizeof(digit), __builtin_object_size (hi->ob_digit , 0)) : __inline_memcpy_chk (hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)));
2893 |
2894 | *high = long_normalize(hi);
2895 | *low = long_normalize(lo);
2896 | return 0;
2897 | }
2898 |
2899 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2900 |
2901 | /* Karatsuba multiplication. Ignores the input signs, and returns the
2902 | * absolute value of the product (or NULL if error).
2903 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2904 | */
2905 | static PyLongObject *
2906 | k_mul(PyLongObject *a, PyLongObject *b)
2907 | {
2908 | Py_ssize_t asize = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
2909 | Py_ssize_t bsize = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
2910 | PyLongObject *ah = NULL((void*)0);
2911 | PyLongObject *al = NULL((void*)0);
2912 | PyLongObject *bh = NULL((void*)0);
2913 | PyLongObject *bl = NULL((void*)0);
2914 | PyLongObject *ret = NULL((void*)0);
2915 | PyLongObject *t1, *t2, *t3;
2916 | Py_ssize_t shift; /* the number of digits we split off */
2917 | Py_ssize_t i;
2918 |
2919 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2920 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2921 | * Then the original product is
2922 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
2923 | * By picking X to be a power of 2, "*X" is just shifting, and it's
2924 | * been reduced to 3 multiplies on numbers half the size.
2925 | */
2926 |
2927 | /* We want to split based on the larger number; fiddle so that b
2928 | * is largest.
2929 | */
2930 | if (asize > bsize) {
2931 | t1 = a;
2932 | a = b;
2933 | b = t1;
2934 |
2935 | i = asize;
2936 | asize = bsize;
2937 | bsize = i;
2938 | }
2939 |
2940 | /* Use gradeschool math when either number is too small. */
2941 | i = a == b ? KARATSUBA_SQUARE_CUTOFF(2 * 70) : KARATSUBA_CUTOFF70;
2942 | if (asize <= i) {
2943 | if (asize == 0)
2944 | return (PyLongObject *)PyLong_FromLong(0);
2945 | else
2946 | return x_mul(a, b);
2947 | }
2948 |
2949 | /* If a is small compared to b, splitting on b gives a degenerate
2950 | * case with ah==0, and Karatsuba may be (even much) less efficient
2951 | * than "grade school" then. However, we can still win, by viewing
2952 | * b as a string of "big digits", each of width a->ob_size. That
2953 | * leads to a sequence of balanced calls to k_mul.
2954 | */
2955 | if (2 * asize <= bsize)
2956 | return k_lopsided_mul(a, b);
2957 |
2958 | /* Split a & b into hi & lo pieces. */
2959 | shift = bsize >> 1;
2960 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
2961 | assert(Py_SIZE(ah) > 0)(__builtin_expect(!((((PyVarObject*)(ah))->ob_size) > 0 ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2961, "Py_SIZE(ah) > 0" ) : (void)0); /* the split isn't degenerate */
2962 |
2963 | if (a == b) {
2964 | bh = ah;
2965 | bl = al;
2966 | Py_INCREF(bh)( _Py_RefTotal++ , ((PyObject*)(bh))->ob_refcnt++);
2967 | Py_INCREF(bl)( _Py_RefTotal++ , ((PyObject*)(bl))->ob_refcnt++);
2968 | }
2969 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
2970 |
2971 | /* The plan:
2972 | * 1. Allocate result space (asize + bsize digits: that's always
2973 | * enough).
2974 | * 2. Compute ah*bh, and copy into result at 2*shift.
2975 | * 3. Compute al*bl, and copy into result at 0. Note that this
2976 | * can't overlap with #2.
2977 | * 4. Subtract al*bl from the result, starting at shift. This may
2978 | * underflow (borrow out of the high digit), but we don't care:
2979 | * we're effectively doing unsigned arithmetic mod
2980 | * BASE**(sizea + sizeb), and so long as the *final* result fits,
2981 | * borrows and carries out of the high digit can be ignored.
2982 | * 5. Subtract ah*bh from the result, starting at shift.
2983 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2984 | * at shift.
2985 | */
2986 |
2987 | /* 1. Allocate result space. */
2988 | ret = _PyLong_New(asize + bsize);
2989 | if (ret == NULL((void*)0)) goto fail;
2990 | #ifdef Py_DEBUG1
2991 | /* Fill with trash, to catch reference to uninitialized digits. */
2992 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit))((__builtin_object_size (ret->ob_digit, 0) != (size_t) -1) ? __builtin___memset_chk (ret->ob_digit, 0xDF, (((PyVarObject *)(ret))->ob_size) * sizeof(digit), __builtin_object_size ( ret->ob_digit, 0)) : __inline_memset_chk (ret->ob_digit , 0xDF, (((PyVarObject*)(ret))->ob_size) * sizeof(digit)));
2993 | #endif
2994 |
2995 | /* 2. t1 <- ah*bh, and copy into high digits of result. */
2996 | if ((t1 = k_mul(ah, bh)) == NULL((void*)0)) goto fail;
2997 | assert(Py_SIZE(t1) >= 0)(__builtin_expect(!((((PyVarObject*)(t1))->ob_size) >= 0 ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 2997, "Py_SIZE(t1) >= 0" ) : (void)0);
2998 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret))(__builtin_expect(!(2*shift + (((PyVarObject*)(t1))->ob_size ) <= (((PyVarObject*)(ret))->ob_size)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 2998, "2*shift + Py_SIZE(t1) <= Py_SIZE(ret)" ) : (void)0);
2999 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit,((__builtin_object_size (ret->ob_digit + 2*shift, 0) != (size_t ) -1) ? __builtin___memcpy_chk (ret->ob_digit + 2*shift, t1 ->ob_digit, (((PyVarObject*)(t1))->ob_size) * sizeof(digit ), __builtin_object_size (ret->ob_digit + 2*shift, 0)) : __inline_memcpy_chk (ret->ob_digit + 2*shift, t1->ob_digit, (((PyVarObject *)(t1))->ob_size) * sizeof(digit)))
3000 | Py_SIZE(t1) * sizeof(digit))((__builtin_object_size (ret->ob_digit + 2*shift, 0) != (size_t ) -1) ? __builtin___memcpy_chk (ret->ob_digit + 2*shift, t1 ->ob_digit, (((PyVarObject*)(t1))->ob_size) * sizeof(digit ), __builtin_object_size (ret->ob_digit + 2*shift, 0)) : __inline_memcpy_chk (ret->ob_digit + 2*shift, t1->ob_digit, (((PyVarObject *)(t1))->ob_size) * sizeof(digit)));
3001 |
3002 | /* Zero-out the digits higher than the ah*bh copy. */
3003 | i = Py_SIZE(ret)(((PyVarObject*)(ret))->ob_size) - 2*shift - Py_SIZE(t1)(((PyVarObject*)(t1))->ob_size);
3004 | if (i)
3005 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,((__builtin_object_size (ret->ob_digit + 2*shift + (((PyVarObject *)(t1))->ob_size), 0) != (size_t) -1) ? __builtin___memset_chk (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size ), 0, i * sizeof(digit), __builtin_object_size (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size), 0)) : __inline_memset_chk (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size ), 0, i * sizeof(digit)))
3006 | i * sizeof(digit))((__builtin_object_size (ret->ob_digit + 2*shift + (((PyVarObject *)(t1))->ob_size), 0) != (size_t) -1) ? __builtin___memset_chk (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size ), 0, i * sizeof(digit), __builtin_object_size (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size), 0)) : __inline_memset_chk (ret->ob_digit + 2*shift + (((PyVarObject*)(t1))->ob_size ), 0, i * sizeof(digit)));
3007 |
3008 | /* 3. t2 <- al*bl, and copy into the low digits. */
3009 | if ((t2 = k_mul(al, bl)) == NULL((void*)0)) {
3010 | Py_DECREF(t1)do { if (_Py_RefTotal-- , --((PyObject*)(t1))->ob_refcnt != 0) { if (((PyObject*)t1)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3010, (PyObject *)(t1)); } else _Py_Dealloc ((PyObject *)(t1)); } while (0);
3011 | goto fail;
3012 | }
3013 | assert(Py_SIZE(t2) >= 0)(__builtin_expect(!((((PyVarObject*)(t2))->ob_size) >= 0 ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 3013, "Py_SIZE(t2) >= 0" ) : (void)0);
3014 | assert(Py_SIZE(t2) <= 2*shift)(__builtin_expect(!((((PyVarObject*)(t2))->ob_size) <= 2 *shift), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 3014 , "Py_SIZE(t2) <= 2*shift") : (void)0); /* no overlap with high digits */
3015 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit))((__builtin_object_size (ret->ob_digit, 0) != (size_t) -1) ? __builtin___memcpy_chk (ret->ob_digit, t2->ob_digit, (((PyVarObject*)(t2))->ob_size) * sizeof(digit), __builtin_object_size (ret->ob_digit, 0)) : __inline_memcpy_chk (ret->ob_digit , t2->ob_digit, (((PyVarObject*)(t2))->ob_size) * sizeof (digit)));
3016 |
3017 | /* Zero out remaining digits. */
3018 | i = 2*shift - Py_SIZE(t2)(((PyVarObject*)(t2))->ob_size); /* number of uninitialized digits */
3019 | if (i)
3020 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit))((__builtin_object_size (ret->ob_digit + (((PyVarObject*)( t2))->ob_size), 0) != (size_t) -1) ? __builtin___memset_chk (ret->ob_digit + (((PyVarObject*)(t2))->ob_size), 0, i * sizeof(digit), __builtin_object_size (ret->ob_digit + ( ((PyVarObject*)(t2))->ob_size), 0)) : __inline_memset_chk ( ret->ob_digit + (((PyVarObject*)(t2))->ob_size), 0, i * sizeof(digit)));
3021 |
3022 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
3023 | * because it's fresher in cache.
3024 | */
3025 | i = Py_SIZE(ret)(((PyVarObject*)(ret))->ob_size) - shift; /* # digits after shift */
3026 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)(((PyVarObject*)(t2))->ob_size));
3027 | Py_DECREF(t2)do { if (_Py_RefTotal-- , --((PyObject*)(t2))->ob_refcnt != 0) { if (((PyObject*)t2)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3027, (PyObject *)(t2)); } else _Py_Dealloc ((PyObject *)(t2)); } while (0);
3028 |
3029 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)(((PyVarObject*)(t1))->ob_size));
3030 | Py_DECREF(t1)do { if (_Py_RefTotal-- , --((PyObject*)(t1))->ob_refcnt != 0) { if (((PyObject*)t1)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3030, (PyObject *)(t1)); } else _Py_Dealloc ((PyObject *)(t1)); } while (0);
3031 |
3032 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
3033 | if ((t1 = x_add(ah, al)) == NULL((void*)0)) goto fail;
3034 | Py_DECREF(ah)do { if (_Py_RefTotal-- , --((PyObject*)(ah))->ob_refcnt != 0) { if (((PyObject*)ah)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3034, (PyObject *)(ah)); } else _Py_Dealloc ((PyObject *)(ah)); } while (0);
3035 | Py_DECREF(al)do { if (_Py_RefTotal-- , --((PyObject*)(al))->ob_refcnt != 0) { if (((PyObject*)al)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3035, (PyObject *)(al)); } else _Py_Dealloc ((PyObject *)(al)); } while (0);
3036 | ah = al = NULL((void*)0);
3037 |
3038 | if (a == b) {
3039 | t2 = t1;
3040 | Py_INCREF(t2)( _Py_RefTotal++ , ((PyObject*)(t2))->ob_refcnt++);
3041 | }
3042 | else if ((t2 = x_add(bh, bl)) == NULL((void*)0)) {
3043 | Py_DECREF(t1)do { if (_Py_RefTotal-- , --((PyObject*)(t1))->ob_refcnt != 0) { if (((PyObject*)t1)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3043, (PyObject *)(t1)); } else _Py_Dealloc ((PyObject *)(t1)); } while (0);
3044 | goto fail;
3045 | }
3046 | Py_DECREF(bh)do { if (_Py_RefTotal-- , --((PyObject*)(bh))->ob_refcnt != 0) { if (((PyObject*)bh)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3046, (PyObject *)(bh)); } else _Py_Dealloc ((PyObject *)(bh)); } while (0);
3047 | Py_DECREF(bl)do { if (_Py_RefTotal-- , --((PyObject*)(bl))->ob_refcnt != 0) { if (((PyObject*)bl)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3047, (PyObject *)(bl)); } else _Py_Dealloc ((PyObject *)(bl)); } while (0);
3048 | bh = bl = NULL((void*)0);
3049 |
3050 | t3 = k_mul(t1, t2);
3051 | Py_DECREF(t1)do { if (_Py_RefTotal-- , --((PyObject*)(t1))->ob_refcnt != 0) { if (((PyObject*)t1)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3051, (PyObject *)(t1)); } else _Py_Dealloc ((PyObject *)(t1)); } while (0);
3052 | Py_DECREF(t2)do { if (_Py_RefTotal-- , --((PyObject*)(t2))->ob_refcnt != 0) { if (((PyObject*)t2)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3052, (PyObject *)(t2)); } else _Py_Dealloc ((PyObject *)(t2)); } while (0);
3053 | if (t3 == NULL((void*)0)) goto fail;
3054 | assert(Py_SIZE(t3) >= 0)(__builtin_expect(!((((PyVarObject*)(t3))->ob_size) >= 0 ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 3054, "Py_SIZE(t3) >= 0" ) : (void)0);
3055 |
3056 | /* Add t3. It's not obvious why we can't run out of room here.
3057 | * See the (*) comment after this function.
3058 | */
3059 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)(((PyVarObject*)(t3))->ob_size));
3060 | Py_DECREF(t3)do { if (_Py_RefTotal-- , --((PyObject*)(t3))->ob_refcnt != 0) { if (((PyObject*)t3)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3060, (PyObject *)(t3)); } else _Py_Dealloc ((PyObject *)(t3)); } while (0);
3061 |
3062 | return long_normalize(ret);
3063 |
3064 | fail:
3065 | Py_XDECREF(ret)do { if ((ret) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(ret))->ob_refcnt != 0) { if (((PyObject*)ret )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3065, (PyObject *)(ret)); } else _Py_Dealloc((PyObject *)(ret )); } while (0); } while (0);
3066 | Py_XDECREF(ah)do { if ((ah) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(ah))->ob_refcnt != 0) { if (((PyObject*)ah )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3066, (PyObject *)(ah)); } else _Py_Dealloc((PyObject *)(ah )); } while (0); } while (0);
3067 | Py_XDECREF(al)do { if ((al) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(al))->ob_refcnt != 0) { if (((PyObject*)al )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3067, (PyObject *)(al)); } else _Py_Dealloc((PyObject *)(al )); } while (0); } while (0);
3068 | Py_XDECREF(bh)do { if ((bh) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(bh))->ob_refcnt != 0) { if (((PyObject*)bh )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3068, (PyObject *)(bh)); } else _Py_Dealloc((PyObject *)(bh )); } while (0); } while (0);
3069 | Py_XDECREF(bl)do { if ((bl) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(bl))->ob_refcnt != 0) { if (((PyObject*)bl )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3069, (PyObject *)(bl)); } else _Py_Dealloc((PyObject *)(bl )); } while (0); } while (0);
3070 | return NULL((void*)0);
3071 | }
3072 |
3073 | /* (*) Why adding t3 can't "run out of room" above.
3074 |
3075 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
3076 | to start with:
3077 |
3078 | 1. For any integer i, i = c(i/2) + f(i/2). In particular,
3079 | bsize = c(bsize/2) + f(bsize/2).
3080 | 2. shift = f(bsize/2)
3081 | 3. asize <= bsize
3082 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
3083 | routine, so asize > bsize/2 >= f(bsize/2) in this routine.
3084 |
3085 | We allocated asize + bsize result digits, and add t3 into them at an offset
3086 | of shift. This leaves asize+bsize-shift allocated digit positions for t3
3087 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
3088 | asize + c(bsize/2) available digit positions.
3089 |
3090 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
3091 | at most c(bsize/2) digits + 1 bit.
3092 |
3093 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
3094 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
3095 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
3096 |
3097 | The product (ah+al)*(bh+bl) therefore has at most
3098 |
3099 | c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
3100 |
3101 | and we have asize + c(bsize/2) available digit positions. We need to show
3102 | this is always enough. An instance of c(bsize/2) cancels out in both, so
3103 | the question reduces to whether asize digits is enough to hold
3104 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
3105 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
3106 | asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
3107 | digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
3108 | asize == bsize, then we're asking whether bsize digits is enough to hold
3109 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
3110 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because
3111 | bsize >= KARATSUBA_CUTOFF >= 2.
3112 |
3113 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's
3114 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract
3115 | ah*bh and al*bl too.
3116 | */
3117 |
3118 | /* b has at least twice the digits of a, and a is big enough that Karatsuba
3119 | * would pay off *if* the inputs had balanced sizes. View b as a sequence
3120 | * of slices, each with a->ob_size digits, and multiply the slices by a,
3121 | * one at a time. This gives k_mul balanced inputs to work with, and is
3122 | * also cache-friendly (we compute one double-width slice of the result
3123 | * at a time, then move on, never bactracking except for the helpful
3124 | * single-width slice overlap between successive partial sums).
3125 | */
3126 | static PyLongObject *
3127 | k_lopsided_mul(PyLongObject *a, PyLongObject *b)
3128 | {
3129 | const Py_ssize_t asize = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
3130 | Py_ssize_t bsize = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
3131 | Py_ssize_t nbdone; /* # of b digits already multiplied */
3132 | PyLongObject *ret;
3133 | PyLongObject *bslice = NULL((void*)0);
3134 |
3135 | assert(asize > KARATSUBA_CUTOFF)(__builtin_expect(!(asize > 70), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 3135, "asize > KARATSUBA_CUTOFF" ) : (void)0);
3136 | assert(2 * asize <= bsize)(__builtin_expect(!(2 * asize <= bsize), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 3136, "2 * asize <= bsize" ) : (void)0);
3137 |
3138 | /* Allocate result space, and zero it out. */
3139 | ret = _PyLong_New(asize + bsize);
3140 | if (ret == NULL((void*)0))
3141 | return NULL((void*)0);
3142 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit))((__builtin_object_size (ret->ob_digit, 0) != (size_t) -1) ? __builtin___memset_chk (ret->ob_digit, 0, (((PyVarObject *)(ret))->ob_size) * sizeof(digit), __builtin_object_size ( ret->ob_digit, 0)) : __inline_memset_chk (ret->ob_digit , 0, (((PyVarObject*)(ret))->ob_size) * sizeof(digit)));
3143 |
3144 | /* Successive slices of b are copied into bslice. */
3145 | bslice = _PyLong_New(asize);
3146 | if (bslice == NULL((void*)0))
3147 | goto fail;
3148 |
3149 | nbdone = 0;
3150 | while (bsize > 0) {
3151 | PyLongObject *product;
3152 | const Py_ssize_t nbtouse = MIN(bsize, asize)((bsize) > (asize) ? (asize) : (bsize));
3153 |
3154 | /* Multiply the next slice of b by a. */
3155 | memcpy(bslice->ob_digit, b->ob_digit + nbdone,((__builtin_object_size (bslice->ob_digit, 0) != (size_t) - 1) ? __builtin___memcpy_chk (bslice->ob_digit, b->ob_digit + nbdone, nbtouse * sizeof(digit), __builtin_object_size (bslice ->ob_digit, 0)) : __inline_memcpy_chk (bslice->ob_digit , b->ob_digit + nbdone, nbtouse * sizeof(digit)))
3156 | nbtouse * sizeof(digit))((__builtin_object_size (bslice->ob_digit, 0) != (size_t) - 1) ? __builtin___memcpy_chk (bslice->ob_digit, b->ob_digit + nbdone, nbtouse * sizeof(digit), __builtin_object_size (bslice ->ob_digit, 0)) : __inline_memcpy_chk (bslice->ob_digit , b->ob_digit + nbdone, nbtouse * sizeof(digit)));
3157 | Py_SIZE(bslice)(((PyVarObject*)(bslice))->ob_size) = nbtouse;
3158 | product = k_mul(a, bslice);
3159 | if (product == NULL((void*)0))
3160 | goto fail;
3161 |
3162 | /* Add into result. */
3163 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret)(((PyVarObject*)(ret))->ob_size) - nbdone,
3164 | product->ob_digit, Py_SIZE(product)(((PyVarObject*)(product))->ob_size));
3165 | Py_DECREF(product)do { if (_Py_RefTotal-- , --((PyObject*)(product))->ob_refcnt != 0) { if (((PyObject*)product)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3165, (PyObject *)(product)); } else _Py_Dealloc((PyObject *)(product)); } while (0);
3166 |
3167 | bsize -= nbtouse;
3168 | nbdone += nbtouse;
3169 | }
3170 |
3171 | Py_DECREF(bslice)do { if (_Py_RefTotal-- , --((PyObject*)(bslice))->ob_refcnt != 0) { if (((PyObject*)bslice)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3171, (PyObject *)(bslice)); } else _Py_Dealloc ((PyObject *)(bslice)); } while (0);
3172 | return long_normalize(ret);
3173 |
3174 | fail:
3175 | Py_DECREF(ret)do { if (_Py_RefTotal-- , --((PyObject*)(ret))->ob_refcnt != 0) { if (((PyObject*)ret)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3175, (PyObject *)(ret)); } else _Py_Dealloc ((PyObject *)(ret)); } while (0);
3176 | Py_XDECREF(bslice)do { if ((bslice) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(bslice))->ob_refcnt != 0) { if (((PyObject *)bslice)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3176, (PyObject *)(bslice)); } else _Py_Dealloc((PyObject * )(bslice)); } while (0); } while (0);
3177 | return NULL((void*)0);
3178 | }
3179 |
3180 | static PyObject *
3181 | long_mul(PyLongObject *a, PyLongObject *b)
3182 | {
3183 | PyLongObject *z;
3184 |
3185 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3186 |
3187 | /* fast path for single-digit multiplication */
3188 | if (ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))) <= 1 && ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size))) <= 1) {
3189 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)((((PyVarObject*)(a))->ob_size) < 0 ? -(sdigit)(a)-> ob_digit[0] : ((((PyVarObject*)(a))->ob_size) == 0 ? (sdigit )0 : (sdigit)(a)->ob_digit[0]))) * MEDIUM_VALUE(b)((((PyVarObject*)(b))->ob_size) < 0 ? -(sdigit)(b)-> ob_digit[0] : ((((PyVarObject*)(b))->ob_size) == 0 ? (sdigit )0 : (sdigit)(b)->ob_digit[0]));
3190 | #ifdef HAVE_LONG_LONG1
3191 | return PyLong_FromLongLong((PY_LONG_LONGlong long)v);
3192 | #else
3193 | /* if we don't have long long then we're almost certainly
3194 | using 15-bit digits, so v will fit in a long. In the
3195 | unlikely event that we're using 30-bit digits on a platform
3196 | without long long, a large v will just cause us to fall
3197 | through to the general multiplication code below. */
3198 | if (v >= LONG_MIN(-9223372036854775807L -1L) && v <= LONG_MAX9223372036854775807L)
3199 | return PyLong_FromLong((long)v);
3200 | #endif
3201 | }
3202 |
3203 | z = k_mul(a, b);
3204 | /* Negate if exactly one of the inputs is negative. */
3205 | if (((Py_SIZE(a)(((PyVarObject*)(a))->ob_size) ^ Py_SIZE(b)(((PyVarObject*)(b))->ob_size)) < 0) && z)
3206 | NEGATE(z)do if ((((PyObject*)(z))->ob_refcnt) == 1) (((PyVarObject* )(z))->ob_size) = -(((PyVarObject*)(z))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(z))->ob_size ) < 0 ? -(sdigit)(z)->ob_digit[0] : ((((PyVarObject*)(z ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(z)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3206, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); (z) = (PyLongObject*)tmp; } while (0);
3207 | return (PyObject *)z;
3208 | }
3209 |
3210 | /* The / and % operators are now defined in terms of divmod().
3211 | The expression a mod b has the value a - b*floor(a/b).
3212 | The long_divrem function gives the remainder after division of
3213 | |a| by |b|, with the sign of a. This is also expressed
3214 | as a - b*trunc(a/b), if trunc truncates towards zero.
3215 | Some examples:
3216 | a b a rem b a mod b
3217 | 13 10 3 3
3218 | -13 10 -3 7
3219 | 13 -10 3 -7
3220 | -13 -10 -3 -3
3221 | So, to get from rem to mod, we have to add b if a and b
3222 | have different signs. We then subtract one from the 'div'
3223 | part of the outcome to keep the invariant intact. */
3224 |
3225 | /* Compute
3226 | * *pdiv, *pmod = divmod(v, w)
3227 | * NULL can be passed for pdiv or pmod, in which case that part of
3228 | * the result is simply thrown away. The caller owns a reference to
3229 | * each of these it requests (does not pass NULL for).
3230 | */
3231 | static int
3232 | l_divmod(PyLongObject *v, PyLongObject *w,
3233 | PyLongObject **pdiv, PyLongObject **pmod)
3234 | {
3235 | PyLongObject *div, *mod;
3236 |
3237 | if (long_divrem(v, w, &div, &mod) < 0)
3238 | return -1;
3239 | if ((Py_SIZE(mod)(((PyVarObject*)(mod))->ob_size) < 0 && Py_SIZE(w)(((PyVarObject*)(w))->ob_size) > 0) ||
3240 | (Py_SIZE(mod)(((PyVarObject*)(mod))->ob_size) > 0 && Py_SIZE(w)(((PyVarObject*)(w))->ob_size) < 0)) {
3241 | PyLongObject *temp;
3242 | PyLongObject *one;
3243 | temp = (PyLongObject *) long_add(mod, w);
3244 | Py_DECREF(mod)do { if (_Py_RefTotal-- , --((PyObject*)(mod))->ob_refcnt != 0) { if (((PyObject*)mod)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3244, (PyObject *)(mod)); } else _Py_Dealloc ((PyObject *)(mod)); } while (0);
3245 | mod = temp;
3246 | if (mod == NULL((void*)0)) {
3247 | Py_DECREF(div)do { if (_Py_RefTotal-- , --((PyObject*)(div))->ob_refcnt != 0) { if (((PyObject*)div)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3247, (PyObject *)(div)); } else _Py_Dealloc ((PyObject *)(div)); } while (0);
3248 | return -1;
3249 | }
3250 | one = (PyLongObject *) PyLong_FromLong(1L);
3251 | if (one == NULL((void*)0) ||
3252 | (temp = (PyLongObject *) long_sub(div, one)) == NULL((void*)0)) {
3253 | Py_DECREF(mod)do { if (_Py_RefTotal-- , --((PyObject*)(mod))->ob_refcnt != 0) { if (((PyObject*)mod)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3253, (PyObject *)(mod)); } else _Py_Dealloc ((PyObject *)(mod)); } while (0);
3254 | Py_DECREF(div)do { if (_Py_RefTotal-- , --((PyObject*)(div))->ob_refcnt != 0) { if (((PyObject*)div)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3254, (PyObject *)(div)); } else _Py_Dealloc ((PyObject *)(div)); } while (0);
3255 | Py_XDECREF(one)do { if ((one) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt != 0) { if (((PyObject*)one )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3255, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one )); } while (0); } while (0);
3256 | return -1;
3257 | }
3258 | Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt != 0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3258, (PyObject *)(one)); } else _Py_Dealloc ((PyObject *)(one)); } while (0);
3259 | Py_DECREF(div)do { if (_Py_RefTotal-- , --((PyObject*)(div))->ob_refcnt != 0) { if (((PyObject*)div)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3259, (PyObject *)(div)); } else _Py_Dealloc ((PyObject *)(div)); } while (0);
3260 | div = temp;
3261 | }
3262 | if (pdiv != NULL((void*)0))
3263 | *pdiv = div;
3264 | else
3265 | Py_DECREF(div)do { if (_Py_RefTotal-- , --((PyObject*)(div))->ob_refcnt != 0) { if (((PyObject*)div)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3265, (PyObject *)(div)); } else _Py_Dealloc ((PyObject *)(div)); } while (0);
3266 |
3267 | if (pmod != NULL((void*)0))
3268 | *pmod = mod;
3269 | else
3270 | Py_DECREF(mod)do { if (_Py_RefTotal-- , --((PyObject*)(mod))->ob_refcnt != 0) { if (((PyObject*)mod)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3270, (PyObject *)(mod)); } else _Py_Dealloc ((PyObject *)(mod)); } while (0);
3271 |
3272 | return 0;
3273 | }
3274 |
3275 | static PyObject *
3276 | long_div(PyObject *a, PyObject *b)
3277 | {
3278 | PyLongObject *div;
3279 |
3280 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3281 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL((void*)0)) < 0)
3282 | div = NULL((void*)0);
3283 | return (PyObject *)div;
3284 | }
3285 |
3286 | /* PyLong/PyLong -> float, with correctly rounded result. */
3287 |
3288 | #define MANT_DIG_DIGITS(53 / 30) (DBL_MANT_DIG53 / PyLong_SHIFT30)
3289 | #define MANT_DIG_BITS(53 % 30) (DBL_MANT_DIG53 % PyLong_SHIFT30)
3290 |
3291 | static PyObject *
3292 | long_true_divide(PyObject *v, PyObject *w)
3293 | {
3294 | PyLongObject *a, *b, *x;
3295 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits;
3296 | digit mask, low;
3297 | int inexact, negate, a_is_small, b_is_small;
3298 | double dx, result;
3299 |
3300 | CHECK_BINOP(v, w)do { if (!((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(w))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3301 | a = (PyLongObject *)v;
3302 | b = (PyLongObject *)w;
3303 |
3304 | /*
3305 | Method in a nutshell:
3306 |
3307 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow
3308 | 1. choose a suitable integer 'shift'
3309 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b)
3310 | 3. adjust x for correct rounding
3311 | 4. convert x to a double dx with the same value
3312 | 5. return ldexp(dx, shift).
3313 |
3314 | In more detail:
3315 |
3316 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b
3317 | returns either 0.0 or -0.0, depending on the sign of b. For a and
3318 | b both nonzero, ignore signs of a and b, and add the sign back in
3319 | at the end. Now write a_bits and b_bits for the bit lengths of a
3320 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise
3321 | for b). Then
3322 |
3323 | 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1).
3324 |
3325 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and
3326 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP -
3327 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of
3328 | the way, we can assume that
3329 |
3330 | DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP.
3331 |
3332 | 1. The integer 'shift' is chosen so that x has the right number of
3333 | bits for a double, plus two or three extra bits that will be used
3334 | in the rounding decisions. Writing a_bits and b_bits for the
3335 | number of significant bits in a and b respectively, a
3336 | straightforward formula for shift is:
3337 |
3338 | shift = a_bits - b_bits - DBL_MANT_DIG - 2
3339 |
3340 | This is fine in the usual case, but if a/b is smaller than the
3341 | smallest normal float then it can lead to double rounding on an
3342 | IEEE 754 platform, giving incorrectly rounded results. So we
3343 | adjust the formula slightly. The actual formula used is:
3344 |
3345 | shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2
3346 |
3347 | 2. The quantity x is computed by first shifting a (left -shift bits
3348 | if shift <= 0, right shift bits if shift > 0) and then dividing by
3349 | b. For both the shift and the division, we keep track of whether
3350 | the result is inexact, in a flag 'inexact'; this information is
3351 | needed at the rounding stage.
3352 |
3353 | With the choice of shift above, together with our assumption that
3354 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows
3355 | that x >= 1.
3356 |
3357 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace
3358 | this with an exactly representable float of the form
3359 |
3360 | round(x/2**extra_bits) * 2**(extra_bits+shift).
3361 |
3362 | For float representability, we need x/2**extra_bits <
3363 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP -
3364 | DBL_MANT_DIG. This translates to the condition:
3365 |
3366 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG
3367 |
3368 | To round, we just modify the bottom digit of x in-place; this can
3369 | end up giving a digit with value > PyLONG_MASK, but that's not a
3370 | problem since digits can hold values up to 2*PyLONG_MASK+1.
3371 |
3372 | With the original choices for shift above, extra_bits will always
3373 | be 2 or 3. Then rounding under the round-half-to-even rule, we
3374 | round up iff the most significant of the extra bits is 1, and
3375 | either: (a) the computation of x in step 2 had an inexact result,
3376 | or (b) at least one other of the extra bits is 1, or (c) the least
3377 | significant bit of x (above those to be rounded) is 1.
3378 |
3379 | 4. Conversion to a double is straightforward; all floating-point
3380 | operations involved in the conversion are exact, so there's no
3381 | danger of rounding errors.
3382 |
3383 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result.
3384 | The result will always be exactly representable as a double, except
3385 | in the case that it overflows. To avoid dependence on the exact
3386 | behaviour of ldexp on overflow, we check for overflow before
3387 | applying ldexp. The result of ldexp is adjusted for sign before
3388 | returning.
3389 | */
3390 |
3391 | /* Reduce to case where a and b are both positive. */
3392 | a_size = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
3393 | b_size = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
3394 | negate = (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) ^ (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0);
3395 | if (b_size == 0) {
3396 | PyErr_SetString(PyExc_ZeroDivisionError,
3397 | "division by zero");
3398 | goto error;
3399 | }
3400 | if (a_size == 0)
3401 | goto underflow_or_zero;
3402 |
3403 | /* Fast path for a and b small (exactly representable in a double).
3404 | Relies on floating-point division being correctly rounded; results
3405 | may be subject to double rounding on x86 machines that operate with
3406 | the x87 FPU set to 64-bit precision. */
3407 | a_is_small = a_size <= MANT_DIG_DIGITS(53 / 30) ||
3408 | (a_size == MANT_DIG_DIGITS(53 / 30)+1 &&
3409 | a->ob_digit[MANT_DIG_DIGITS(53 / 30)] >> MANT_DIG_BITS(53 % 30) == 0);
3410 | b_is_small = b_size <= MANT_DIG_DIGITS(53 / 30) ||
3411 | (b_size == MANT_DIG_DIGITS(53 / 30)+1 &&
3412 | b->ob_digit[MANT_DIG_DIGITS(53 / 30)] >> MANT_DIG_BITS(53 % 30) == 0);
3413 | if (a_is_small && b_is_small) {
3414 | double da, db;
3415 | da = a->ob_digit[--a_size];
3416 | while (a_size > 0)
3417 | da = da * PyLong_BASE((digit)1 << 30) + a->ob_digit[--a_size];
3418 | db = b->ob_digit[--b_size];
3419 | while (b_size > 0)
3420 | db = db * PyLong_BASE((digit)1 << 30) + b->ob_digit[--b_size];
3421 | result = da / db;
3422 | goto success;
3423 | }
3424 |
3425 | /* Catch obvious cases of underflow and overflow */
3426 | diff = a_size - b_size;
3427 | if (diff > PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))/PyLong_SHIFT30 - 1)
3428 | /* Extreme overflow */
3429 | goto overflow;
3430 | else if (diff < 1 - PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))/PyLong_SHIFT30)
3431 | /* Extreme underflow */
3432 | goto underflow_or_zero;
3433 | /* Next line is now safe from overflowing a Py_ssize_t */
3434 | diff = diff * PyLong_SHIFT30 + bits_in_digit(a->ob_digit[a_size - 1]) -
3435 | bits_in_digit(b->ob_digit[b_size - 1]);
3436 | /* Now diff = a_bits - b_bits. */
3437 | if (diff > DBL_MAX_EXP1024)
3438 | goto overflow;
3439 | else if (diff < DBL_MIN_EXP(-1021) - DBL_MANT_DIG53 - 1)
3440 | goto underflow_or_zero;
3441 |
3442 | /* Choose value for shift; see comments for step 1 above. */
3443 | shift = MAX(diff, DBL_MIN_EXP)((diff) < ((-1021)) ? ((-1021)) : (diff)) - DBL_MANT_DIG53 - 2;
3444 |
3445 | inexact = 0;
3446 |
3447 | /* x = abs(a * 2**-shift) */
3448 | if (shift <= 0) {
3449 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT30;
3450 | digit rem;
3451 | /* x = a << -shift */
3452 | if (a_size >= PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 1 - shift_digits) {
3453 | /* In practice, it's probably impossible to end up
3454 | here. Both a and b would have to be enormous,
3455 | using close to SIZE_T_MAX bytes of memory each. */
3456 | PyErr_SetString(PyExc_OverflowError,
3457 | "intermediate overflow during division");
3458 | goto error;
3459 | }
3460 | x = _PyLong_New(a_size + shift_digits + 1);
3461 | if (x == NULL((void*)0))
3462 | goto error;
3463 | for (i = 0; i < shift_digits; i++)
3464 | x->ob_digit[i] = 0;
3465 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit,
3466 | a_size, -shift % PyLong_SHIFT30);
3467 | x->ob_digit[a_size + shift_digits] = rem;
3468 | }
3469 | else {
3470 | Py_ssize_t shift_digits = shift / PyLong_SHIFT30;
3471 | digit rem;
3472 | /* x = a >> shift */
3473 | assert(a_size >= shift_digits)(__builtin_expect(!(a_size >= shift_digits), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 3473, "a_size >= shift_digits" ) : (void)0);
3474 | x = _PyLong_New(a_size - shift_digits);
3475 | if (x == NULL((void*)0))
3476 | goto error;
3477 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits,
3478 | a_size - shift_digits, shift % PyLong_SHIFT30);
3479 | /* set inexact if any of the bits shifted out is nonzero */
3480 | if (rem)
3481 | inexact = 1;
3482 | while (!inexact && shift_digits > 0)
3483 | if (a->ob_digit[--shift_digits])
3484 | inexact = 1;
3485 | }
3486 | long_normalize(x);
3487 | x_size = Py_SIZE(x)(((PyVarObject*)(x))->ob_size);
3488 |
3489 | /* x //= b. If the remainder is nonzero, set inexact. We own the only
3490 | reference to x, so it's safe to modify it in-place. */
3491 | if (b_size == 1) {
3492 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size,
3493 | b->ob_digit[0]);
3494 | long_normalize(x);
3495 | if (rem)
3496 | inexact = 1;
3497 | }
3498 | else {
3499 | PyLongObject *div, *rem;
3500 | div = x_divrem(x, b, &rem);
3501 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3501, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0);
3502 | x = div;
3503 | if (x == NULL((void*)0))
3504 | goto error;
3505 | if (Py_SIZE(rem)(((PyVarObject*)(rem))->ob_size))
3506 | inexact = 1;
3507 | Py_DECREF(rem)do { if (_Py_RefTotal-- , --((PyObject*)(rem))->ob_refcnt != 0) { if (((PyObject*)rem)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3507, (PyObject *)(rem)); } else _Py_Dealloc ((PyObject *)(rem)); } while (0);
3508 | }
3509 | x_size = ABS(Py_SIZE(x))(((((PyVarObject*)(x))->ob_size)) < 0 ? -((((PyVarObject *)(x))->ob_size)) : ((((PyVarObject*)(x))->ob_size)));
3510 | assert(x_size > 0)(__builtin_expect(!(x_size > 0), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 3510, "x_size > 0") : (void)0); /* result of division is never zero */
3511 | x_bits = (x_size-1)*PyLong_SHIFT30+bits_in_digit(x->ob_digit[x_size-1]);
3512 |
3513 | /* The number of extra bits that have to be rounded away. */
3514 | extra_bits = MAX(x_bits, DBL_MIN_EXP - shift)((x_bits) < ((-1021) - shift) ? ((-1021) - shift) : (x_bits )) - DBL_MANT_DIG53;
3515 | assert(extra_bits == 2 || extra_bits == 3)(__builtin_expect(!(extra_bits == 2 || extra_bits == 3), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 3515, "extra_bits == 2 || extra_bits == 3" ) : (void)0);
3516 |
3517 | /* Round by directly modifying the low digit of x. */
3518 | mask = (digit)1 << (extra_bits - 1);
3519 | low = x->ob_digit[0] | inexact;
3520 | if (low & mask && low & (3*mask-1))
3521 | low += mask;
3522 | x->ob_digit[0] = low & ~(mask-1U);
3523 |
3524 | /* Convert x to a double dx; the conversion is exact. */
3525 | dx = x->ob_digit[--x_size];
3526 | while (x_size > 0)
3527 | dx = dx * PyLong_BASE((digit)1 << 30) + x->ob_digit[--x_size];
3528 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3528, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0);
3529 |
3530 | /* Check whether ldexp result will overflow a double. */
3531 | if (shift + x_bits >= DBL_MAX_EXP1024 &&
3532 | (shift + x_bits > DBL_MAX_EXP1024 || dx == ldexp(1.0, (int)x_bits)))
3533 | goto overflow;
3534 | result = ldexp(dx, (int)shift);
3535 |
3536 | success:
3537 | return PyFloat_FromDouble(negate ? -result : result);
3538 |
3539 | underflow_or_zero:
3540 | return PyFloat_FromDouble(negate ? -0.0 : 0.0);
3541 |
3542 | overflow:
3543 | PyErr_SetString(PyExc_OverflowError,
3544 | "integer division result too large for a float");
3545 | error:
3546 | return NULL((void*)0);
3547 | }
3548 |
3549 | static PyObject *
3550 | long_mod(PyObject *a, PyObject *b)
3551 | {
3552 | PyLongObject *mod;
3553 |
3554 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3555 |
3556 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL((void*)0), &mod) < 0)
3557 | mod = NULL((void*)0);
3558 | return (PyObject *)mod;
3559 | }
3560 |
3561 | static PyObject *
3562 | long_divmod(PyObject *a, PyObject *b)
3563 | {
3564 | PyLongObject *div, *mod;
3565 | PyObject *z;
3566 |
3567 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3568 |
3569 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
3570 | return NULL((void*)0);
3571 | }
3572 | z = PyTuple_New(2);
3573 | if (z != NULL((void*)0)) {
3574 | PyTuple_SetItem(z, 0, (PyObject *) div);
3575 | PyTuple_SetItem(z, 1, (PyObject *) mod);
3576 | }
3577 | else {
3578 | Py_DECREF(div)do { if (_Py_RefTotal-- , --((PyObject*)(div))->ob_refcnt != 0) { if (((PyObject*)div)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3578, (PyObject *)(div)); } else _Py_Dealloc ((PyObject *)(div)); } while (0);
3579 | Py_DECREF(mod)do { if (_Py_RefTotal-- , --((PyObject*)(mod))->ob_refcnt != 0) { if (((PyObject*)mod)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3579, (PyObject *)(mod)); } else _Py_Dealloc ((PyObject *)(mod)); } while (0);
3580 | }
3581 | return z;
3582 | }
3583 |
3584 | /* pow(v, w, x) */
3585 | static PyObject *
3586 | long_pow(PyObject *v, PyObject *w, PyObject *x)
3587 | {
3588 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
3589 | int negativeOutput = 0; /* if x<0 return negative output */
3590 |
3591 | PyLongObject *z = NULL((void*)0); /* accumulated result */
3592 | Py_ssize_t i, j, k; /* counters */
3593 | PyLongObject *temp = NULL((void*)0);
3594 |
3595 | /* 5-ary values. If the exponent is large enough, table is
3596 | * precomputed so that table[i] == a**i % c for i in range(32).
3597 | */
3598 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3599 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
3600 |
3601 | /* a, b, c = v, w, x */
3602 | CHECK_BINOP(v, w)do { if (!((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(w))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3603 | a = (PyLongObject*)v; Py_INCREF(a)( _Py_RefTotal++ , ((PyObject*)(a))->ob_refcnt++);
3604 | b = (PyLongObject*)w; Py_INCREF(b)( _Py_RefTotal++ , ((PyObject*)(b))->ob_refcnt++);
3605 | if (PyLong_Check(x)((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
3606 | c = (PyLongObject *)x;
3607 | Py_INCREF(x)( _Py_RefTotal++ , ((PyObject*)(x))->ob_refcnt++);
3608 | }
3609 | else if (x == Py_None(&_Py_NoneStruct))
3610 | c = NULL((void*)0);
3611 | else {
3612 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3612, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
3613 | Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt != 0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3613, (PyObject *)(b)); } else _Py_Dealloc ((PyObject *)(b)); } while (0);
3614 | Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct )))->ob_refcnt++);
3615 | return Py_NotImplemented(&_Py_NotImplementedStruct);
3616 | }
3617 |
3618 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0) { /* if exponent is negative */
3619 | if (c) {
3620 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
3621 | "cannot be negative when 3rd argument specified");
3622 | goto Error;
3623 | }
3624 | else {
3625 | /* else return a float. This works because we know
3626 | that this calls float_pow() which converts its
3627 | arguments to double. */
3628 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3628, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
3629 | Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt != 0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3629, (PyObject *)(b)); } else _Py_Dealloc ((PyObject *)(b)); } while (0);
3630 | return PyFloat_Type.tp_as_number->nb_power(v, w, x);
3631 | }
3632 | }
3633 |
3634 | if (c) {
3635 | /* if modulus == 0:
3636 | raise ValueError() */
3637 | if (Py_SIZE(c)(((PyVarObject*)(c))->ob_size) == 0) {
3638 | PyErr_SetString(PyExc_ValueError,
3639 | "pow() 3rd argument cannot be 0");
3640 | goto Error;
3641 | }
3642 |
3643 | /* if modulus < 0:
3644 | negativeOutput = True
3645 | modulus = -modulus */
3646 | if (Py_SIZE(c)(((PyVarObject*)(c))->ob_size) < 0) {
3647 | negativeOutput = 1;
3648 | temp = (PyLongObject *)_PyLong_Copy(c);
3649 | if (temp == NULL((void*)0))
3650 | goto Error;
3651 | Py_DECREF(c)do { if (_Py_RefTotal-- , --((PyObject*)(c))->ob_refcnt != 0) { if (((PyObject*)c)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3651, (PyObject *)(c)); } else _Py_Dealloc ((PyObject *)(c)); } while (0);
3652 | c = temp;
3653 | temp = NULL((void*)0);
3654 | NEGATE(c)do if ((((PyObject*)(c))->ob_refcnt) == 1) (((PyVarObject* )(c))->ob_size) = -(((PyVarObject*)(c))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(c))->ob_size ) < 0 ? -(sdigit)(c)->ob_digit[0] : ((((PyVarObject*)(c ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(c)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(c))->ob_refcnt != 0) { if (((PyObject*)c)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3654, (PyObject *)(c)); } else _Py_Dealloc ((PyObject *)(c)); } while (0); (c) = (PyLongObject*)tmp; } while (0);
3655 | }
3656 |
3657 | /* if modulus == 1:
3658 | return 0 */
3659 | if ((Py_SIZE(c)(((PyVarObject*)(c))->ob_size) == 1) && (c->ob_digit[0] == 1)) {
3660 | z = (PyLongObject *)PyLong_FromLong(0L);
3661 | goto Done;
3662 | }
3663 |
3664 | /* if base < 0:
3665 | base = base % modulus
3666 | Having the base positive just makes things easier. */
3667 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) {
3668 | if (l_divmod(a, c, NULL((void*)0), &temp) < 0)
3669 | goto Error;
3670 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3670, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
3671 | a = temp;
3672 | temp = NULL((void*)0);
3673 | }
3674 | }
3675 |
3676 | /* At this point a, b, and c are guaranteed non-negative UNLESS
3677 | c is NULL, in which case a may be negative. */
3678 |
3679 | z = (PyLongObject *)PyLong_FromLong(1L);
3680 | if (z == NULL((void*)0))
3681 | goto Error;
3682 |
3683 | /* Perform a modular reduction, X = X % c, but leave X alone if c
3684 | * is NULL.
3685 | */
3686 | #define REDUCE(X)do { if (c != ((void*)0)) { if (l_divmod(X, c, ((void*)0), & temp) < 0) goto Error; do { if ((X) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(X))->ob_refcnt != 0 ) { if (((PyObject*)X)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3686, (PyObject *)(X)); } else _Py_Dealloc ((PyObject *)(X)); } while (0); } while (0); X = temp; temp = ((void*)0); } } while(0) \
3687 | do { \
3688 | if (c != NULL((void*)0)) { \
3689 | if (l_divmod(X, c, NULL((void*)0), &temp) < 0) \
3690 | goto Error; \
3691 | Py_XDECREF(X)do { if ((X) == ((void*)0)) ; else do { if (_Py_RefTotal-- , -- ((PyObject*)(X))->ob_refcnt != 0) { if (((PyObject*)X)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3691, (PyObject *)(X)); } else _Py_Dealloc((PyObject *)(X)) ; } while (0); } while (0); \
3692 | X = temp; \
3693 | temp = NULL((void*)0); \
3694 | } \
3695 | } while(0)
3696 |
3697 | /* Multiply two values, then reduce the result:
3698 | result = X*Y % c. If c is NULL, skip the mod. */
3699 | #define MULT(X, Y, result)do { temp = (PyLongObject *)long_mul(X, Y); if (temp == ((void *)0)) goto Error; do { if ((result) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3699, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0); } while (0); result = temp ; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod (result, c, ((void*)0), &temp) < 0) goto Error; do { if ((result) == ((void*)0)) ; else do { if (_Py_RefTotal-- , -- ((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3699, (PyObject *)(result)); } else _Py_Dealloc((PyObject * )(result)); } while (0); } while (0); result = temp; temp = ( (void*)0); } } while(0); } while(0) \
3700 | do { \
3701 | temp = (PyLongObject *)long_mul(X, Y); \
3702 | if (temp == NULL((void*)0)) \
3703 | goto Error; \
3704 | Py_XDECREF(result)do { if ((result) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject *)result)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3704, (PyObject *)(result)); } else _Py_Dealloc((PyObject * )(result)); } while (0); } while (0); \
3705 | result = temp; \
3706 | temp = NULL((void*)0); \
3707 | REDUCE(result)do { if (c != ((void*)0)) { if (l_divmod(result, c, ((void*)0 ), &temp) < 0) goto Error; do { if ((result) == ((void *)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(result) )->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c", 3707, ( PyObject *)(result)); } else _Py_Dealloc((PyObject *)(result) ); } while (0); } while (0); result = temp; temp = ((void*)0) ; } } while(0); \
3708 | } while(0)
3709 |
3710 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) <= FIVEARY_CUTOFF8) {
3711 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
3712 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
3713 | for (i = Py_SIZE(b)(((PyVarObject*)(b))->ob_size) - 1; i >= 0; --i) {
3714 | digit bi = b->ob_digit[i];
3715 |
3716 | for (j = (digit)1 << (PyLong_SHIFT30-1); j != 0; j >>= 1) {
3717 | MULT(z, z, z)do { temp = (PyLongObject *)long_mul(z, z); if (temp == ((void *)0)) goto Error; do { if ((z) == ((void*)0)) ; else do { if ( _Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if ( ((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3717, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); } while (0); z = temp; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod(z, c, ((void*)0), &temp ) < 0) goto Error; do { if ((z) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3717, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); } while (0); z = temp; temp = ((void*)0); } } while(0); } while(0);
3718 | if (bi & j)
3719 | MULT(z, a, z)do { temp = (PyLongObject *)long_mul(z, a); if (temp == ((void *)0)) goto Error; do { if ((z) == ((void*)0)) ; else do { if ( _Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if ( ((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3719, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); } while (0); z = temp; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod(z, c, ((void*)0), &temp ) < 0) goto Error; do { if ((z) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3719, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); } while (0); z = temp; temp = ((void*)0); } } while(0); } while(0);
3720 | }
3721 | }
3722 | }
3723 | else {
3724 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
3725 | Py_INCREF(z)( _Py_RefTotal++ , ((PyObject*)(z))->ob_refcnt++); /* still holds 1L */
3726 | table[0] = z;
3727 | for (i = 1; i < 32; ++i)
3728 | MULT(table[i-1], a, table[i])do { temp = (PyLongObject *)long_mul(table[i-1], a); if (temp == ((void*)0)) goto Error; do { if ((table[i]) == ((void*)0) ) ; else do { if (_Py_RefTotal-- , --((PyObject*)(table[i]))-> ob_refcnt != 0) { if (((PyObject*)table[i])->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c", 3728, (PyObject *)(table[i])); } else _Py_Dealloc((PyObject *)(table[i])); } while (0); } while (0); table[i] = temp; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod(table[i], c, ((void*)0 ), &temp) < 0) goto Error; do { if ((table[i]) == ((void *)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(table[i ]))->ob_refcnt != 0) { if (((PyObject*)table[i])->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c", 3728, ( PyObject *)(table[i])); } else _Py_Dealloc((PyObject *)(table [i])); } while (0); } while (0); table[i] = temp; temp = ((void *)0); } } while(0); } while(0);
3729 |
3730 | for (i = Py_SIZE(b)(((PyVarObject*)(b))->ob_size) - 1; i >= 0; --i) {
3731 | const digit bi = b->ob_digit[i];
3732 |
3733 | for (j = PyLong_SHIFT30 - 5; j >= 0; j -= 5) {
3734 | const int index = (bi >> j) & 0x1f;
3735 | for (k = 0; k < 5; ++k)
3736 | MULT(z, z, z)do { temp = (PyLongObject *)long_mul(z, z); if (temp == ((void *)0)) goto Error; do { if ((z) == ((void*)0)) ; else do { if ( _Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if ( ((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3736, (PyObject *)(z)); } else _Py_Dealloc((PyObject *)(z)) ; } while (0); } while (0); z = temp; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod(z, c, ((void*)0), &temp ) < 0) goto Error; do { if ((z) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3736, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); } while (0); z = temp; temp = ((void*)0); } } while(0); } while(0);
3737 | if (index)
3738 | MULT(z, table[index], z)do { temp = (PyLongObject *)long_mul(z, table[index]); if (temp == ((void*)0)) goto Error; do { if ((z) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3738, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); } while (0); z = temp; temp = ((void*)0); do { if (c != ((void*)0)) { if (l_divmod(z, c, ( (void*)0), &temp) < 0) goto Error; do { if ((z) == ((void *)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(z))-> ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3738, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); } while (0); z = temp; temp = ((void*)0); } } while(0); } while(0);
3739 | }
3740 | }
3741 | }
3742 |
3743 | if (negativeOutput && (Py_SIZE(z)(((PyVarObject*)(z))->ob_size) != 0)) {
3744 | temp = (PyLongObject *)long_sub(z, c);
3745 | if (temp == NULL((void*)0))
3746 | goto Error;
3747 | Py_DECREF(z)do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3747, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0);
3748 | z = temp;
3749 | temp = NULL((void*)0);
3750 | }
3751 | goto Done;
3752 |
3753 | Error:
3754 | if (z != NULL((void*)0)) {
3755 | Py_DECREF(z)do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3755, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0);
3756 | z = NULL((void*)0);
3757 | }
3758 | /* fall through */
3759 | Done:
3760 | if (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) > FIVEARY_CUTOFF8) {
3761 | for (i = 0; i < 32; ++i)
3762 | Py_XDECREF(table[i])do { if ((table[i]) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(table[i]))->ob_refcnt != 0) { if (((PyObject *)table[i])->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3762, (PyObject *)(table[i])); } else _Py_Dealloc((PyObject *)(table[i])); } while (0); } while (0);
3763 | }
3764 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3764, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
3765 | Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt != 0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3765, (PyObject *)(b)); } else _Py_Dealloc ((PyObject *)(b)); } while (0);
3766 | Py_XDECREF(c)do { if ((c) == ((void*)0)) ; else do { if (_Py_RefTotal-- , -- ((PyObject*)(c))->ob_refcnt != 0) { if (((PyObject*)c)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3766, (PyObject *)(c)); } else _Py_Dealloc((PyObject *)(c)) ; } while (0); } while (0);
3767 | Py_XDECREF(temp)do { if ((temp) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(temp))->ob_refcnt != 0) { if (((PyObject *)temp)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 3767, (PyObject *)(temp)); } else _Py_Dealloc((PyObject *)( temp)); } while (0); } while (0);
3768 | return (PyObject *)z;
3769 | }
3770 |
3771 | static PyObject *
3772 | long_invert(PyLongObject *v)
3773 | {
3774 | /* Implement ~x as -(x+1) */
3775 | PyLongObject *x;
3776 | PyLongObject *w;
3777 | if (ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size))) <=1)
3778 | return PyLong_FromLong(-(MEDIUM_VALUE(v)((((PyVarObject*)(v))->ob_size) < 0 ? -(sdigit)(v)-> ob_digit[0] : ((((PyVarObject*)(v))->ob_size) == 0 ? (sdigit )0 : (sdigit)(v)->ob_digit[0]))+1));
3779 | w = (PyLongObject *)PyLong_FromLong(1L);
3780 | if (w == NULL((void*)0))
3781 | return NULL((void*)0);
3782 | x = (PyLongObject *) long_add(v, w);
3783 | Py_DECREF(w)do { if (_Py_RefTotal-- , --((PyObject*)(w))->ob_refcnt != 0) { if (((PyObject*)w)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3783, (PyObject *)(w)); } else _Py_Dealloc ((PyObject *)(w)); } while (0);
3784 | if (x == NULL((void*)0))
3785 | return NULL((void*)0);
3786 | Py_SIZE(x)(((PyVarObject*)(x))->ob_size) = -(Py_SIZE(x)(((PyVarObject*)(x))->ob_size));
3787 | return (PyObject *)maybe_small_long(x);
3788 | }
3789 |
3790 | static PyObject *
3791 | long_neg(PyLongObject *v)
3792 | {
3793 | PyLongObject *z;
3794 | if (ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size))) <= 1)
3795 | return PyLong_FromLong(-MEDIUM_VALUE(v)((((PyVarObject*)(v))->ob_size) < 0 ? -(sdigit)(v)-> ob_digit[0] : ((((PyVarObject*)(v))->ob_size) == 0 ? (sdigit )0 : (sdigit)(v)->ob_digit[0])));
3796 | z = (PyLongObject *)_PyLong_Copy(v);
3797 | if (z != NULL((void*)0))
3798 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(v)(((PyVarObject*)(v))->ob_size));
3799 | return (PyObject *)z;
3800 | }
3801 |
3802 | static PyObject *
3803 | long_abs(PyLongObject *v)
3804 | {
3805 | if (Py_SIZE(v)(((PyVarObject*)(v))->ob_size) < 0)
3806 | return long_neg(v);
3807 | else
3808 | return long_long((PyObject *)v);
3809 | }
3810 |
3811 | static int
3812 | long_bool(PyLongObject *v)
3813 | {
3814 | return Py_SIZE(v)(((PyVarObject*)(v))->ob_size) != 0;
3815 | }
3816 |
3817 | static PyObject *
3818 | long_rshift(PyLongObject *a, PyLongObject *b)
3819 | {
3820 | PyLongObject *z = NULL((void*)0);
3821 | Py_ssize_t shiftby, newsize, wordshift, loshift, hishift, i, j;
3822 | digit lomask, himask;
3823 |
3824 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3825 |
3826 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) {
3827 | /* Right shifting negative numbers is harder */
3828 | PyLongObject *a1, *a2;
3829 | a1 = (PyLongObject *) long_invert(a);
3830 | if (a1 == NULL((void*)0))
3831 | goto rshift_error;
3832 | a2 = (PyLongObject *) long_rshift(a1, b);
3833 | Py_DECREF(a1)do { if (_Py_RefTotal-- , --((PyObject*)(a1))->ob_refcnt != 0) { if (((PyObject*)a1)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3833, (PyObject *)(a1)); } else _Py_Dealloc ((PyObject *)(a1)); } while (0);
3834 | if (a2 == NULL((void*)0))
3835 | goto rshift_error;
3836 | z = (PyLongObject *) long_invert(a2);
3837 | Py_DECREF(a2)do { if (_Py_RefTotal-- , --((PyObject*)(a2))->ob_refcnt != 0) { if (((PyObject*)a2)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3837, (PyObject *)(a2)); } else _Py_Dealloc ((PyObject *)(a2)); } while (0);
3838 | }
3839 | else {
3840 | shiftby = PyLong_AsSsize_t((PyObject *)b);
3841 | if (shiftby == -1L && PyErr_Occurred())
3842 | goto rshift_error;
3843 | if (shiftby < 0) {
3844 | PyErr_SetString(PyExc_ValueError,
3845 | "negative shift count");
3846 | goto rshift_error;
3847 | }
3848 | wordshift = shiftby / PyLong_SHIFT30;
3849 | newsize = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size))) - wordshift;
3850 | if (newsize <= 0)
3851 | return PyLong_FromLong(0);
3852 | loshift = shiftby % PyLong_SHIFT30;
3853 | hishift = PyLong_SHIFT30 - loshift;
3854 | lomask = ((digit)1 << hishift) - 1;
3855 | himask = PyLong_MASK((digit)(((digit)1 << 30) - 1)) ^ lomask;
3856 | z = _PyLong_New(newsize);
3857 | if (z == NULL((void*)0))
3858 | goto rshift_error;
3859 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0)
3860 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(z)(((PyVarObject*)(z))->ob_size));
3861 | for (i = 0, j = wordshift; i < newsize; i++, j++) {
3862 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3863 | if (i+1 < newsize)
3864 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask;
3865 | }
3866 | z = long_normalize(z);
3867 | }
3868 | rshift_error:
3869 | return (PyObject *) maybe_small_long(z);
3870 |
3871 | }
3872 |
3873 | static PyObject *
3874 | long_lshift(PyObject *v, PyObject *w)
3875 | {
3876 | /* This version due to Tim Peters */
3877 | PyLongObject *a = (PyLongObject*)v;
3878 | PyLongObject *b = (PyLongObject*)w;
3879 | PyLongObject *z = NULL((void*)0);
3880 | Py_ssize_t shiftby, oldsize, newsize, wordshift, remshift, i, j;
3881 | twodigits accum;
3882 |
3883 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
3884 |
3885 | shiftby = PyLong_AsSsize_t((PyObject *)b);
3886 | if (shiftby == -1L && PyErr_Occurred())
3887 | goto lshift_error;
3888 | if (shiftby < 0) {
3889 | PyErr_SetString(PyExc_ValueError, "negative shift count");
3890 | goto lshift_error;
3891 | }
3892 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3893 | wordshift = shiftby / PyLong_SHIFT30;
3894 | remshift = shiftby - wordshift * PyLong_SHIFT30;
3895 |
3896 | oldsize = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
3897 | newsize = oldsize + wordshift;
3898 | if (remshift)
3899 | ++newsize;
3900 | z = _PyLong_New(newsize);
3901 | if (z == NULL((void*)0))
3902 | goto lshift_error;
3903 | if (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0)
3904 | NEGATE(z)do if ((((PyObject*)(z))->ob_refcnt) == 1) (((PyVarObject* )(z))->ob_size) = -(((PyVarObject*)(z))->ob_size); else { PyObject* tmp=PyLong_FromLong(-((((PyVarObject*)(z))->ob_size ) < 0 ? -(sdigit)(z)->ob_digit[0] : ((((PyVarObject*)(z ))->ob_size) == 0 ? (sdigit)0 : (sdigit)(z)->ob_digit[0 ]))); do { if (_Py_RefTotal-- , --((PyObject*)(z))->ob_refcnt != 0) { if (((PyObject*)z)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3904, (PyObject *)(z)); } else _Py_Dealloc ((PyObject *)(z)); } while (0); (z) = (PyLongObject*)tmp; } while (0);
3905 | for (i = 0; i < wordshift; i++)
3906 | z->ob_digit[i] = 0;
3907 | accum = 0;
3908 | for (i = wordshift, j = 0; j < oldsize; i++, j++) {
3909 | accum |= (twodigits)a->ob_digit[j] << remshift;
3910 | z->ob_digit[i] = (digit)(accum & PyLong_MASK((digit)(((digit)1 << 30) - 1)));
3911 | accum >>= PyLong_SHIFT30;
3912 | }
3913 | if (remshift)
3914 | z->ob_digit[newsize-1] = (digit)accum;
3915 | else
3916 | assert(!accum)(__builtin_expect(!(!accum), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 3916, "!accum") : (void)0);
3917 | z = long_normalize(z);
3918 | lshift_error:
3919 | return (PyObject *) maybe_small_long(z);
3920 | }
3921 |
3922 | /* Compute two's complement of digit vector a[0:m], writing result to
3923 | z[0:m]. The digit vector a need not be normalized, but should not
3924 | be entirely zero. a and z may point to the same digit vector. */
3925 |
3926 | static void
3927 | v_complement(digit *z, digit *a, Py_ssize_t m)
3928 | {
3929 | Py_ssize_t i;
3930 | digit carry = 1;
3931 | for (i = 0; i < m; ++i) {
3932 | carry += a[i] ^ PyLong_MASK((digit)(((digit)1 << 30) - 1));
3933 | z[i] = carry & PyLong_MASK((digit)(((digit)1 << 30) - 1));
3934 | carry >>= PyLong_SHIFT30;
3935 | }
3936 | assert(carry == 0)(__builtin_expect(!(carry == 0), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 3936, "carry == 0") : (void)0);
3937 | }
3938 |
3939 | /* Bitwise and/xor/or operations */
3940 |
3941 | static PyObject *
3942 | long_bitwise(PyLongObject *a,
3943 | int op, /* '&', '|', '^' */
3944 | PyLongObject *b)
3945 | {
3946 | int nega, negb, negz;
3947 | Py_ssize_t size_a, size_b, size_z, i;
3948 | PyLongObject *z;
3949 |
3950 | /* Bitwise operations for negative numbers operate as though
3951 | on a two's complement representation. So convert arguments
3952 | from sign-magnitude to two's complement, and convert the
3953 | result back to sign-magnitude at the end. */
3954 |
3955 | /* If a is negative, replace it by its two's complement. */
3956 | size_a = ABS(Py_SIZE(a))(((((PyVarObject*)(a))->ob_size)) < 0 ? -((((PyVarObject *)(a))->ob_size)) : ((((PyVarObject*)(a))->ob_size)));
3957 | nega = Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0;
3958 | if (nega) {
3959 | z = _PyLong_New(size_a);
3960 | if (z == NULL((void*)0))
3961 | return NULL((void*)0);
3962 | v_complement(z->ob_digit, a->ob_digit, size_a);
3963 | a = z;
3964 | }
3965 | else
3966 | /* Keep reference count consistent. */
3967 | Py_INCREF(a)( _Py_RefTotal++ , ((PyObject*)(a))->ob_refcnt++);
3968 |
3969 | /* Same for b. */
3970 | size_b = ABS(Py_SIZE(b))(((((PyVarObject*)(b))->ob_size)) < 0 ? -((((PyVarObject *)(b))->ob_size)) : ((((PyVarObject*)(b))->ob_size)));
3971 | negb = Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0;
3972 | if (negb) {
3973 | z = _PyLong_New(size_b);
3974 | if (z == NULL((void*)0)) {
3975 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 3975, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
3976 | return NULL((void*)0);
3977 | }
3978 | v_complement(z->ob_digit, b->ob_digit, size_b);
3979 | b = z;
3980 | }
3981 | else
3982 | Py_INCREF(b)( _Py_RefTotal++ , ((PyObject*)(b))->ob_refcnt++);
3983 |
3984 | /* Swap a and b if necessary to ensure size_a >= size_b. */
3985 | if (size_a < size_b) {
3986 | z = a; a = b; b = z;
3987 | size_z = size_a; size_a = size_b; size_b = size_z;
3988 | negz = nega; nega = negb; negb = negz;
3989 | }
3990 |
3991 | /* JRH: The original logic here was to allocate the result value (z)
3992 | as the longer of the two operands. However, there are some cases
3993 | where the result is guaranteed to be shorter than that: AND of two
3994 | positives, OR of two negatives: use the shorter number. AND with
3995 | mixed signs: use the positive number. OR with mixed signs: use the
3996 | negative number.
3997 | */
3998 | switch (op) {
3999 | case '^':
4000 | negz = nega ^ negb;
4001 | size_z = size_a;
4002 | break;
4003 | case '&':
4004 | negz = nega & negb;
4005 | size_z = negb ? size_a : size_b;
4006 | break;
4007 | case '|':
4008 | negz = nega | negb;
4009 | size_z = negb ? size_b : size_a;
4010 | break;
4011 | default:
4012 | PyErr_BadArgument();
4013 | return NULL((void*)0);
4014 | }
4015 |
4016 | /* We allow an extra digit if z is negative, to make sure that
4017 | the final two's complement of z doesn't overflow. */
4018 | z = _PyLong_New(size_z + negz);
4019 | if (z == NULL((void*)0)) {
4020 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4020, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
4021 | Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt != 0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4021, (PyObject *)(b)); } else _Py_Dealloc ((PyObject *)(b)); } while (0);
4022 | return NULL((void*)0);
4023 | }
4024 |
4025 | /* Compute digits for overlap of a and b. */
4026 | switch(op) {
4027 | case '&':
4028 | for (i = 0; i < size_b; ++i)
4029 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i];
4030 | break;
4031 | case '|':
4032 | for (i = 0; i < size_b; ++i)
4033 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i];
4034 | break;
4035 | case '^':
4036 | for (i = 0; i < size_b; ++i)
4037 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i];
4038 | break;
4039 | default:
4040 | PyErr_BadArgument();
4041 | return NULL((void*)0);
4042 | }
4043 |
4044 | /* Copy any remaining digits of a, inverting if necessary. */
4045 | if (op == '^' && negb)
4046 | for (; i < size_z; ++i)
4047 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK((digit)(((digit)1 << 30) - 1));
4048 | else if (i < size_z)
4049 | memcpy(&z->ob_digit[i], &a->ob_digit[i],((__builtin_object_size (&z->ob_digit[i], 0) != (size_t ) -1) ? __builtin___memcpy_chk (&z->ob_digit[i], & a->ob_digit[i], (size_z-i)*sizeof(digit), __builtin_object_size (&z->ob_digit[i], 0)) : __inline_memcpy_chk (&z-> ob_digit[i], &a->ob_digit[i], (size_z-i)*sizeof(digit) ))
4050 | (size_z-i)*sizeof(digit))((__builtin_object_size (&z->ob_digit[i], 0) != (size_t ) -1) ? __builtin___memcpy_chk (&z->ob_digit[i], & a->ob_digit[i], (size_z-i)*sizeof(digit), __builtin_object_size (&z->ob_digit[i], 0)) : __inline_memcpy_chk (&z-> ob_digit[i], &a->ob_digit[i], (size_z-i)*sizeof(digit) ));
4051 |
4052 | /* Complement result if negative. */
4053 | if (negz) {
4054 | Py_SIZE(z)(((PyVarObject*)(z))->ob_size) = -(Py_SIZE(z)(((PyVarObject*)(z))->ob_size));
4055 | z->ob_digit[size_z] = PyLong_MASK((digit)(((digit)1 << 30) - 1));
4056 | v_complement(z->ob_digit, z->ob_digit, size_z+1);
4057 | }
4058 |
4059 | Py_DECREF(a)do { if (_Py_RefTotal-- , --((PyObject*)(a))->ob_refcnt != 0) { if (((PyObject*)a)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4059, (PyObject *)(a)); } else _Py_Dealloc ((PyObject *)(a)); } while (0);
4060 | Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt != 0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4060, (PyObject *)(b)); } else _Py_Dealloc ((PyObject *)(b)); } while (0);
4061 | return (PyObject *)maybe_small_long(long_normalize(z));
4062 | }
4063 |
4064 | static PyObject *
4065 | long_and(PyObject *a, PyObject *b)
4066 | {
4067 | PyObject *c;
4068 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
4069 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
4070 | return c;
4071 | }
4072 |
4073 | static PyObject *
4074 | long_xor(PyObject *a, PyObject *b)
4075 | {
4076 | PyObject *c;
4077 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
4078 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
4079 | return c;
4080 | }
4081 |
4082 | static PyObject *
4083 | long_or(PyObject *a, PyObject *b)
4084 | {
4085 | PyObject *c;
4086 | CHECK_BINOP(a, b)do { if (!((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<24))) != 0) || !((((((PyObject*)(b))->ob_type ))->tp_flags & ((1L<<24))) != 0)) { ( _Py_RefTotal ++ , ((PyObject*)((&_Py_NotImplementedStruct)))->ob_refcnt ++); return (&_Py_NotImplementedStruct); } } while(0);
4087 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
4088 | return c;
4089 | }
4090 |
4091 | static PyObject *
4092 | long_long(PyObject *v)
4093 | {
4094 | if (PyLong_CheckExact(v)((((PyObject*)(v))->ob_type) == &PyLong_Type))
4095 | Py_INCREF(v)( _Py_RefTotal++ , ((PyObject*)(v))->ob_refcnt++);
4096 | else
4097 | v = _PyLong_Copy((PyLongObject *)v);
4098 | return v;
4099 | }
4100 |
4101 | static PyObject *
4102 | long_float(PyObject *v)
4103 | {
4104 | double result;
4105 | result = PyLong_AsDouble(v);
4106 | if (result == -1.0 && PyErr_Occurred())
4107 | return NULL((void*)0);
4108 | return PyFloat_FromDouble(result);
4109 | }
4110 |
4111 | static PyObject *
4112 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
4113 |
4114 | static PyObject *
4115 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4116 | {
4117 | PyObject *obase = NULL((void*)0), *x = NULL((void*)0);
4118 | long base;
4119 | int overflow;
4120 | static char *kwlist[] = {"x", "base", 0};
4121 |
4122 | if (type != &PyLong_Type)
4123 | return long_subtype_new(type, args, kwds); /* Wimp out */
4124 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist,
4125 | &x, &obase))
4126 | return NULL((void*)0);
4127 | if (x == NULL((void*)0))
4128 | return PyLong_FromLong(0L);
4129 | if (obase == NULL((void*)0))
4130 | return PyNumber_Long(x);
4131 |
4132 | base = PyLong_AsLongAndOverflow(obase, &overflow);
4133 | if (base == -1 && PyErr_Occurred())
4134 | return NULL((void*)0);
4135 | if (overflow || (base != 0 && base < 2) || base > 36) {
4136 | PyErr_SetString(PyExc_ValueError,
4137 | "int() arg 2 must be >= 2 and <= 36");
4138 | return NULL((void*)0);
4139 | }
4140 |
4141 | if (PyUnicode_Check(x)((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<< 28))) != 0))
4142 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x)((__builtin_expect(!(((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4142, "PyUnicode_Check(x)") : (void)0),(((PyUnicodeObject * )(x))->str)),
4143 | PyUnicode_GET_SIZE(x)((__builtin_expect(!(((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4143, "PyUnicode_Check(x)") : (void)0),(((PyUnicodeObject * )(x))->length)),
4144 | (int)base);
4145 | else if (PyByteArray_Check(x)((((PyObject*)(x))->ob_type) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(x))->ob_type), (&PyByteArray_Type ))) || PyBytes_Check(x)((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<< 27))) != 0)) {
4146 | /* Since PyLong_FromString doesn't have a length parameter,
4147 | * check here for possible NULs in the string. */
4148 | char *string;
4149 | Py_ssize_t size = Py_SIZE(x)(((PyVarObject*)(x))->ob_size);
4150 | if (PyByteArray_Check(x)((((PyObject*)(x))->ob_type) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(x))->ob_type), (&PyByteArray_Type ))))
4151 | string = PyByteArray_AS_STRING(x)((__builtin_expect(!(((((PyObject*)(x))->ob_type) == (& PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(x))->ob_type ), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4151, "PyByteArray_Check(x)") : (void)0), (((PyVarObject*)( x))->ob_size) ? ((PyByteArrayObject *)(x))->ob_bytes : _PyByteArray_empty_string );
4152 | else
4153 | string = PyBytes_AS_STRING(x)((__builtin_expect(!(((((((PyObject*)(x))->ob_type))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4153, "PyBytes_Check(x)") : (void)0), (((PyBytesObject *)(x ))->ob_sval));
4154 | if (strlen(string) != (size_t)size) {
4155 | /* We only see this if there's a null byte in x,
4156 | x is a bytes or buffer, *and* a base is given. */
4157 | PyErr_Format(PyExc_ValueError,
4158 | "invalid literal for int() with base %d: %R",
4159 | (int)base, x);
4160 | return NULL((void*)0);
4161 | }
4162 | return PyLong_FromString(string, NULL((void*)0), (int)base);
4163 | }
4164 | else {
4165 | PyErr_SetString(PyExc_TypeError,
4166 | "int() can't convert non-string with explicit base");
4167 | return NULL((void*)0);
4168 | }
4169 | }
4170 |
4171 | /* Wimpy, slow approach to tp_new calls for subtypes of long:
4172 | first create a regular long from whatever arguments we got,
4173 | then allocate a subtype instance and initialize it from
4174 | the regular long. The regular long is then thrown away.
4175 | */
4176 | static PyObject *
4177 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4178 | {
4179 | PyLongObject *tmp, *newobj;
4180 | Py_ssize_t i, n;
4181 |
4182 | assert(PyType_IsSubtype(type, &PyLong_Type))(__builtin_expect(!(PyType_IsSubtype(type, &PyLong_Type)) , 0) ? __assert_rtn(__func__, "Objects/longobject.c", 4182, "PyType_IsSubtype(type, &PyLong_Type)" ) : (void)0);
4183 | tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
4184 | if (tmp == NULL((void*)0))
4185 | return NULL((void*)0);
4186 | assert(PyLong_CheckExact(tmp))(__builtin_expect(!(((((PyObject*)(tmp))->ob_type) == & PyLong_Type)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4186, "PyLong_CheckExact(tmp)") : (void)0);
4187 | n = Py_SIZE(tmp)(((PyVarObject*)(tmp))->ob_size);
4188 | if (n < 0)
4189 | n = -n;
4190 | newobj = (PyLongObject *)type->tp_alloc(type, n);
4191 | if (newobj == NULL((void*)0)) {
4192 | Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt != 0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4192, (PyObject *)(tmp)); } else _Py_Dealloc ((PyObject *)(tmp)); } while (0);
4193 | return NULL((void*)0);
4194 | }
4195 | assert(PyLong_Check(newobj))(__builtin_expect(!(((((((PyObject*)(newobj))->ob_type))-> tp_flags & ((1L<<24))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 4195, "PyLong_Check(newobj)") : (void )0);
4196 | Py_SIZE(newobj)(((PyVarObject*)(newobj))->ob_size) = Py_SIZE(tmp)(((PyVarObject*)(tmp))->ob_size);
4197 | for (i = 0; i < n; i++)
4198 | newobj->ob_digit[i] = tmp->ob_digit[i];
4199 | Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt != 0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4199, (PyObject *)(tmp)); } else _Py_Dealloc ((PyObject *)(tmp)); } while (0);
4200 | return (PyObject *)newobj;
4201 | }
4202 |
4203 | static PyObject *
4204 | long_getnewargs(PyLongObject *v)
4205 | {
4206 | return Py_BuildValue("(N)", _PyLong_Copy(v));
4207 | }
4208 |
4209 | static PyObject *
4210 | long_get0(PyLongObject *v, void *context) {
4211 | return PyLong_FromLong(0L);
4212 | }
4213 |
4214 | static PyObject *
4215 | long_get1(PyLongObject *v, void *context) {
4216 | return PyLong_FromLong(1L);
4217 | }
4218 |
4219 | static PyObject *
4220 | long__format__(PyObject *self, PyObject *args)
4221 | {
4222 | PyObject *format_spec;
4223 |
4224 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
4225 | return NULL((void*)0);
4226 | return _PyLong_FormatAdvanced(self,
4227 | PyUnicode_AS_UNICODE(format_spec)((__builtin_expect(!(((((((PyObject*)(format_spec))->ob_type ))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 4227, "PyUnicode_Check(format_spec)" ) : (void)0),(((PyUnicodeObject *)(format_spec))->str)),
4228 | PyUnicode_GET_SIZE(format_spec)((__builtin_expect(!(((((((PyObject*)(format_spec))->ob_type ))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 4228, "PyUnicode_Check(format_spec)" ) : (void)0),(((PyUnicodeObject *)(format_spec))->length)));
4229 | }
4230 |
4231 | /* Return a pair (q, r) such that a = b * q + r, and
4232 | abs(r) <= abs(b)/2, with equality possible only if q is even.
4233 | In other words, q == a / b, rounded to the nearest integer using
4234 | round-half-to-even. */
4235 |
4236 | PyObject *
4237 | _PyLong_DivmodNear(PyObject *a, PyObject *b)
4238 | {
4239 | PyLongObject *quo = NULL((void*)0), *rem = NULL((void*)0);
4240 | PyObject *one = NULL((void*)0), *twice_rem, *result, *temp;
4241 | int cmp, quo_is_odd, quo_is_neg;
4242 |
4243 | /* Equivalent Python code:
4244 |
4245 | def divmod_near(a, b):
4246 | q, r = divmod(a, b)
4247 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
4248 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
4249 | # positive, 2 * r < b if b negative.
4250 | greater_than_half = 2*r > b if b > 0 else 2*r < b
4251 | exactly_half = 2*r == b
4252 | if greater_than_half or exactly_half and q % 2 == 1:
4253 | q += 1
4254 | r -= b
4255 | return q, r
4256 |
4257 | */
4258 | if (!PyLong_Check(a)((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<< 24))) != 0) || !PyLong_Check(b)((((((PyObject*)(b))->ob_type))->tp_flags & ((1L<< 24))) != 0)) {
4259 | PyErr_SetString(PyExc_TypeError,
4260 | "non-integer arguments in division");
4261 | return NULL((void*)0);
4262 | }
4263 |
4264 | /* Do a and b have different signs? If so, quotient is negative. */
4265 | quo_is_neg = (Py_SIZE(a)(((PyVarObject*)(a))->ob_size) < 0) != (Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0);
4266 |
4267 | one = PyLong_FromLong(1L);
4268 | if (one == NULL((void*)0))
4269 | return NULL((void*)0);
4270 |
4271 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
4272 | goto error;
4273 |
4274 | /* compare twice the remainder with the divisor, to see
4275 | if we need to adjust the quotient and remainder */
4276 | twice_rem = long_lshift((PyObject *)rem, one);
4277 | if (twice_rem == NULL((void*)0))
4278 | goto error;
4279 | if (quo_is_neg) {
4280 | temp = long_neg((PyLongObject*)twice_rem);
4281 | Py_DECREF(twice_rem)do { if (_Py_RefTotal-- , --((PyObject*)(twice_rem))->ob_refcnt != 0) { if (((PyObject*)twice_rem)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4281, (PyObject *)(twice_rem)); } else _Py_Dealloc((PyObject *)(twice_rem)); } while (0);
4282 | twice_rem = temp;
4283 | if (twice_rem == NULL((void*)0))
4284 | goto error;
4285 | }
4286 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b);
4287 | Py_DECREF(twice_rem)do { if (_Py_RefTotal-- , --((PyObject*)(twice_rem))->ob_refcnt != 0) { if (((PyObject*)twice_rem)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4287, (PyObject *)(twice_rem)); } else _Py_Dealloc((PyObject *)(twice_rem)); } while (0);
4288 |
4289 | quo_is_odd = Py_SIZE(quo)(((PyVarObject*)(quo))->ob_size) != 0 && ((quo->ob_digit[0] & 1) != 0);
4290 | if ((Py_SIZE(b)(((PyVarObject*)(b))->ob_size) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
4291 | /* fix up quotient */
4292 | if (quo_is_neg)
4293 | temp = long_sub(quo, (PyLongObject *)one);
4294 | else
4295 | temp = long_add(quo, (PyLongObject *)one);
4296 | Py_DECREF(quo)do { if (_Py_RefTotal-- , --((PyObject*)(quo))->ob_refcnt != 0) { if (((PyObject*)quo)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4296, (PyObject *)(quo)); } else _Py_Dealloc ((PyObject *)(quo)); } while (0);
4297 | quo = (PyLongObject *)temp;
4298 | if (quo == NULL((void*)0))
4299 | goto error;
4300 | /* and remainder */
4301 | if (quo_is_neg)
4302 | temp = long_add(rem, (PyLongObject *)b);
4303 | else
4304 | temp = long_sub(rem, (PyLongObject *)b);
4305 | Py_DECREF(rem)do { if (_Py_RefTotal-- , --((PyObject*)(rem))->ob_refcnt != 0) { if (((PyObject*)rem)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4305, (PyObject *)(rem)); } else _Py_Dealloc ((PyObject *)(rem)); } while (0);
4306 | rem = (PyLongObject *)temp;
4307 | if (rem == NULL((void*)0))
4308 | goto error;
4309 | }
4310 |
4311 | result = PyTuple_New(2);
4312 | if (result == NULL((void*)0))
4313 | goto error;
4314 |
4315 | /* PyTuple_SET_ITEM steals references */
4316 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo)(((PyTupleObject *)(result))->ob_item[0] = (PyObject *)quo );
4317 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem)(((PyTupleObject *)(result))->ob_item[1] = (PyObject *)rem );
4318 | Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt != 0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4318, (PyObject *)(one)); } else _Py_Dealloc ((PyObject *)(one)); } while (0);
4319 | return result;
4320 |
4321 | error:
4322 | Py_XDECREF(quo)do { if ((quo) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(quo))->ob_refcnt != 0) { if (((PyObject*)quo )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 4322, (PyObject *)(quo)); } else _Py_Dealloc((PyObject *)(quo )); } while (0); } while (0);
4323 | Py_XDECREF(rem)do { if ((rem) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(rem))->ob_refcnt != 0) { if (((PyObject*)rem )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 4323, (PyObject *)(rem)); } else _Py_Dealloc((PyObject *)(rem )); } while (0); } while (0);
4324 | Py_XDECREF(one)do { if ((one) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt != 0) { if (((PyObject*)one )->ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 4324, (PyObject *)(one)); } else _Py_Dealloc((PyObject *)(one )); } while (0); } while (0);
4325 | return NULL((void*)0);
4326 | }
4327 |
4328 | static PyObject *
4329 | long_round(PyObject *self, PyObject *args)
4330 | {
4331 | PyObject *o_ndigits=NULL((void*)0), *temp, *result, *ndigits;
4332 |
4333 | /* To round an integer m to the nearest 10**n (n positive), we make use of
4334 | * the divmod_near operation, defined by:
4335 | *
4336 | * divmod_near(a, b) = (q, r)
4337 | *
4338 | * where q is the nearest integer to the quotient a / b (the
4339 | * nearest even integer in the case of a tie) and r == a - q * b.
4340 | * Hence q * b = a - r is the nearest multiple of b to a,
4341 | * preferring even multiples in the case of a tie.
4342 | *
4343 | * So the nearest multiple of 10**n to m is:
4344 | *
4345 | * m - divmod_near(m, 10**n)[1].
4346 | */
4347 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
4348 | return NULL((void*)0);
4349 | if (o_ndigits == NULL((void*)0))
4350 | return long_long(self);
4351 |
4352 | ndigits = PyNumber_Index(o_ndigits);
4353 | if (ndigits == NULL((void*)0))
4354 | return NULL((void*)0);
4355 |
4356 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */
4357 | if (Py_SIZE(ndigits)(((PyVarObject*)(ndigits))->ob_size) >= 0) {
4358 | Py_DECREF(ndigits)do { if (_Py_RefTotal-- , --((PyObject*)(ndigits))->ob_refcnt != 0) { if (((PyObject*)ndigits)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4358, (PyObject *)(ndigits)); } else _Py_Dealloc((PyObject *)(ndigits)); } while (0);
4359 | return long_long(self);
4360 | }
4361 |
4362 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */
4363 | temp = long_neg((PyLongObject*)ndigits);
4364 | Py_DECREF(ndigits)do { if (_Py_RefTotal-- , --((PyObject*)(ndigits))->ob_refcnt != 0) { if (((PyObject*)ndigits)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4364, (PyObject *)(ndigits)); } else _Py_Dealloc((PyObject *)(ndigits)); } while (0);
4365 | ndigits = temp;
4366 | if (ndigits == NULL((void*)0))
4367 | return NULL((void*)0);
4368 |
4369 | result = PyLong_FromLong(10L);
4370 | if (result == NULL((void*)0)) {
4371 | Py_DECREF(ndigits)do { if (_Py_RefTotal-- , --((PyObject*)(ndigits))->ob_refcnt != 0) { if (((PyObject*)ndigits)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4371, (PyObject *)(ndigits)); } else _Py_Dealloc((PyObject *)(ndigits)); } while (0);
4372 | return NULL((void*)0);
4373 | }
4374 |
4375 | temp = long_pow(result, ndigits, Py_None(&_Py_NoneStruct));
4376 | Py_DECREF(ndigits)do { if (_Py_RefTotal-- , --((PyObject*)(ndigits))->ob_refcnt != 0) { if (((PyObject*)ndigits)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4376, (PyObject *)(ndigits)); } else _Py_Dealloc((PyObject *)(ndigits)); } while (0);
4377 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4377, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4378 | result = temp;
4379 | if (result == NULL((void*)0))
4380 | return NULL((void*)0);
4381 |
4382 | temp = _PyLong_DivmodNear(self, result);
4383 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4383, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4384 | result = temp;
4385 | if (result == NULL((void*)0))
4386 | return NULL((void*)0);
4387 |
4388 | temp = long_sub((PyLongObject *)self,
4389 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)(((PyTupleObject *)(result))->ob_item[1]));
4390 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4390, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4391 | result = temp;
4392 |
4393 | return result;
4394 | }
4395 |
4396 | static PyObject *
4397 | long_sizeof(PyLongObject *v)
4398 | {
4399 | Py_ssize_t res;
4400 |
4401 | res = offsetof(PyLongObject, ob_digit)__builtin_offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size)))*sizeof(digit);
4402 | return PyLong_FromSsize_t(res);
4403 | }
4404 |
4405 | static PyObject *
4406 | long_bit_length(PyLongObject *v)
4407 | {
4408 | PyLongObject *result, *x, *y;
4409 | Py_ssize_t ndigits, msd_bits = 0;
4410 | digit msd;
4411 |
4412 | assert(v != NULL)(__builtin_expect(!(v != ((void*)0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 4412, "v != NULL") : (void)0);
4413 | assert(PyLong_Check(v))(__builtin_expect(!(((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<24))) != 0)), 0) ? __assert_rtn(__func__, "Objects/longobject.c" , 4413, "PyLong_Check(v)") : (void)0);
4414 |
4415 | ndigits = ABS(Py_SIZE(v))(((((PyVarObject*)(v))->ob_size)) < 0 ? -((((PyVarObject *)(v))->ob_size)) : ((((PyVarObject*)(v))->ob_size)));
4416 | if (ndigits == 0)
4417 | return PyLong_FromLong(0);
4418 |
4419 | msd = v->ob_digit[ndigits-1];
4420 | while (msd >= 32) {
4421 | msd_bits += 6;
4422 | msd >>= 6;
4423 | }
4424 | msd_bits += (long)(BitLengthTable[msd]);
4425 |
4426 | if (ndigits <= PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))/PyLong_SHIFT30)
4427 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT30 + msd_bits);
4428 |
4429 | /* expression above may overflow; use Python integers instead */
4430 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
4431 | if (result == NULL((void*)0))
4432 | return NULL((void*)0);
4433 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT30);
4434 | if (x == NULL((void*)0))
4435 | goto error;
4436 | y = (PyLongObject *)long_mul(result, x);
4437 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4437, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0);
4438 | if (y == NULL((void*)0))
4439 | goto error;
4440 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4440, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4441 | result = y;
4442 |
4443 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
4444 | if (x == NULL((void*)0))
4445 | goto error;
4446 | y = (PyLongObject *)long_add(result, x);
4447 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4447, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0);
4448 | if (y == NULL((void*)0))
4449 | goto error;
4450 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4450, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4451 | result = y;
4452 |
4453 | return (PyObject *)result;
4454 |
4455 | error:
4456 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4456, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0);
4457 | return NULL((void*)0);
4458 | }
4459 |
4460 | PyDoc_STRVAR(long_bit_length_doc,static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4461 | "int.bit_length() -> int\n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4462 | \n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4463 | Number of bits necessary to represent self in binary.\n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4464 | >>> bin(37)\n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4465 | '0b100101'\n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4466 | >>> (37).bit_length()\n\static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6"
4467 | 6")static char long_bit_length_doc[] = "int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6";
4468 |
4469 | #if 0
4470 | static PyObject *
4471 | long_is_finite(PyObject *v)
4472 | {
4473 | Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct )))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct);
4474 | }
4475 | #endif
4476 |
4477 |
4478 | static PyObject *
4479 | long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds)
4480 | {
4481 | PyObject *byteorder_str;
4482 | PyObject *is_signed_obj = NULL((void*)0);
4483 | Py_ssize_t length;
4484 | int little_endian;
4485 | int is_signed;
4486 | PyObject *bytes;
4487 | static char *kwlist[] = {"length", "byteorder", "signed", NULL((void*)0)};
4488 |
4489 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist,
4490 | &length, &byteorder_str,
4491 | &is_signed_obj))
4492 | return NULL((void*)0);
4493 |
4494 | if (args != NULL((void*)0) && Py_SIZE(args)(((PyVarObject*)(args))->ob_size) > 2) {
4495 | PyErr_SetString(PyExc_TypeError,
4496 | "'signed' is a keyword-only argument");
4497 | return NULL((void*)0);
4498 | }
4499 |
4500 | if (!PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(byteorder_str, "little"))
4501 | little_endian = 1;
4502 | else if (!PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(byteorder_str, "big"))
4503 | little_endian = 0;
4504 | else {
4505 | PyErr_SetString(PyExc_ValueError,
4506 | "byteorder must be either 'little' or 'big'");
4507 | return NULL((void*)0);
4508 | }
4509 |
4510 | if (is_signed_obj != NULL((void*)0)) {
4511 | int cmp = PyObject_IsTrue(is_signed_obj);
4512 | if (cmp < 0)
4513 | return NULL((void*)0);
4514 | is_signed = cmp ? 1 : 0;
4515 | }
4516 | else {
4517 | /* If the signed argument was omitted, use False as the
4518 | default. */
4519 | is_signed = 0;
4520 | }
4521 |
4522 | if (length < 0) {
4523 | PyErr_SetString(PyExc_ValueError,
4524 | "length argument must be non-negative");
4525 | return NULL((void*)0);
4526 | }
4527 |
4528 | bytes = PyBytes_FromStringAndSize(NULL((void*)0), length);
4529 | if (bytes == NULL((void*)0))
4530 | return NULL((void*)0);
4531 |
4532 | if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes)((__builtin_expect(!(((((((PyObject*)(bytes))->ob_type))-> tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 4532, "PyBytes_Check(bytes)") : (void )0), (((PyBytesObject *)(bytes))->ob_sval)),
4533 | length, little_endian, is_signed) < 0) {
4534 | Py_DECREF(bytes)do { if (_Py_RefTotal-- , --((PyObject*)(bytes))->ob_refcnt != 0) { if (((PyObject*)bytes)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4534, (PyObject *)(bytes)); } else _Py_Dealloc ((PyObject *)(bytes)); } while (0);
4535 | return NULL((void*)0);
4536 | }
4537 |
4538 | return bytes;
4539 | }
4540 |
4541 | PyDoc_STRVAR(long_to_bytes_doc,static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4542 | "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4543 | \n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4544 | Return an array of bytes representing an integer.\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4545 | \n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4546 | The integer is represented using length bytes. An OverflowError is\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4547 | raised if the integer is not representable with the given number of\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4548 | bytes.\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4549 | \n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4550 | The byteorder argument determines the byte order used to represent the\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4551 | integer. If byteorder is 'big', the most significant byte is at the\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4552 | beginning of the byte array. If byteorder is 'little', the most\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4553 | significant byte is at the end of the byte array. To request the native\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4554 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4555 | \n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4556 | The signed keyword-only argument determines whether two's complement is\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4557 | used to represent the integer. If signed is False and a negative integer\n\static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised."
4558 | is given, an OverflowError is raised.")static char long_to_bytes_doc[] = "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\nReturn an array of bytes representing an integer.\n\nThe integer is represented using length bytes. An OverflowError is\nraised if the integer is not representable with the given number of\nbytes.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument determines whether two's complement is\nused to represent the integer. If signed is False and a negative integer\nis given, an OverflowError is raised.";
4559 |
4560 | static PyObject *
4561 | long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds)
4562 | {
4563 | PyObject *byteorder_str;
4564 | PyObject *is_signed_obj = NULL((void*)0);
4565 | int little_endian;
4566 | int is_signed;
4567 | PyObject *obj;
4568 | PyObject *bytes;
4569 | PyObject *long_obj;
4570 | static char *kwlist[] = {"bytes", "byteorder", "signed", NULL((void*)0)};
4571 |
4572 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist,
4573 | &obj, &byteorder_str,
4574 | &is_signed_obj))
4575 | return NULL((void*)0);
4576 |
4577 | if (args != NULL((void*)0) && Py_SIZE(args)(((PyVarObject*)(args))->ob_size) > 2) {
4578 | PyErr_SetString(PyExc_TypeError,
4579 | "'signed' is a keyword-only argument");
4580 | return NULL((void*)0);
4581 | }
4582 |
4583 | if (!PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(byteorder_str, "little"))
4584 | little_endian = 1;
4585 | else if (!PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(byteorder_str, "big"))
4586 | little_endian = 0;
4587 | else {
4588 | PyErr_SetString(PyExc_ValueError,
4589 | "byteorder must be either 'little' or 'big'");
4590 | return NULL((void*)0);
4591 | }
4592 |
4593 | if (is_signed_obj != NULL((void*)0)) {
4594 | int cmp = PyObject_IsTrue(is_signed_obj);
4595 | if (cmp < 0)
4596 | return NULL((void*)0);
4597 | is_signed = cmp ? 1 : 0;
4598 | }
4599 | else {
4600 | /* If the signed argument was omitted, use False as the
4601 | default. */
4602 | is_signed = 0;
4603 | }
4604 |
4605 | bytes = PyObject_Bytes(obj);
4606 | if (bytes == NULL((void*)0))
4607 | return NULL((void*)0);
4608 |
4609 | long_obj = _PyLong_FromByteArray(
4610 | (unsigned char *)PyBytes_AS_STRING(bytes)((__builtin_expect(!(((((((PyObject*)(bytes))->ob_type))-> tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 4610, "PyBytes_Check(bytes)") : (void )0), (((PyBytesObject *)(bytes))->ob_sval)), Py_SIZE(bytes)(((PyVarObject*)(bytes))->ob_size),
4611 | little_endian, is_signed);
4612 | Py_DECREF(bytes)do { if (_Py_RefTotal-- , --((PyObject*)(bytes))->ob_refcnt != 0) { if (((PyObject*)bytes)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4612, (PyObject *)(bytes)); } else _Py_Dealloc ((PyObject *)(bytes)); } while (0);
4613 |
4614 | /* If from_bytes() was used on subclass, allocate new subclass
4615 | * instance, initialize it with decoded long value and return it.
4616 | */
4617 | if (type != &PyLong_Type && PyType_IsSubtype(type, &PyLong_Type)) {
4618 | PyLongObject *newobj;
4619 | int i;
4620 | Py_ssize_t n = ABS(Py_SIZE(long_obj))(((((PyVarObject*)(long_obj))->ob_size)) < 0 ? -((((PyVarObject *)(long_obj))->ob_size)) : ((((PyVarObject*)(long_obj))-> ob_size)));
4621 |
4622 | newobj = (PyLongObject *)type->tp_alloc(type, n);
4623 | if (newobj == NULL((void*)0)) {
4624 | Py_DECREF(long_obj)do { if (_Py_RefTotal-- , --((PyObject*)(long_obj))->ob_refcnt != 0) { if (((PyObject*)long_obj)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4624, (PyObject *)(long_obj)); } else _Py_Dealloc((PyObject *)(long_obj)); } while (0);
4625 | return NULL((void*)0);
4626 | }
4627 | assert(PyLong_Check(newobj))(__builtin_expect(!(((((((PyObject*)(newobj))->ob_type))-> tp_flags & ((1L<<24))) != 0)), 0) ? __assert_rtn(__func__ , "Objects/longobject.c", 4627, "PyLong_Check(newobj)") : (void )0);
4628 | Py_SIZE(newobj)(((PyVarObject*)(newobj))->ob_size) = Py_SIZE(long_obj)(((PyVarObject*)(long_obj))->ob_size);
4629 | for (i = 0; i < n; i++) {
4630 | newobj->ob_digit[i] =
4631 | ((PyLongObject *)long_obj)->ob_digit[i];
4632 | }
4633 | Py_DECREF(long_obj)do { if (_Py_RefTotal-- , --((PyObject*)(long_obj))->ob_refcnt != 0) { if (((PyObject*)long_obj)->ob_refcnt < 0) _Py_NegativeRefcount ("Objects/longobject.c", 4633, (PyObject *)(long_obj)); } else _Py_Dealloc((PyObject *)(long_obj)); } while (0);
4634 | return (PyObject *)newobj;
4635 | }
4636 |
4637 | return long_obj;
4638 | }
4639 |
4640 | PyDoc_STRVAR(long_from_bytes_doc,static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4641 | "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4642 | \n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4643 | Return the integer represented by the given array of bytes.\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4644 | \n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4645 | The bytes argument must either support the buffer protocol or be an\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4646 | iterable object producing bytes. Bytes and bytearray are examples of\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4647 | built-in objects that support the buffer protocol.\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4648 | \n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4649 | The byteorder argument determines the byte order used to represent the\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4650 | integer. If byteorder is 'big', the most significant byte is at the\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4651 | beginning of the byte array. If byteorder is 'little', the most\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4652 | significant byte is at the end of the byte array. To request the native\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4653 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4654 | \n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4655 | The signed keyword-only argument indicates whether two's complement is\n\static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer."
4656 | used to represent the integer.")static char long_from_bytes_doc[] = "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\nReturn the integer represented by the given array of bytes.\n\nThe bytes argument must either support the buffer protocol or be an\niterable object producing bytes. Bytes and bytearray are examples of\nbuilt-in objects that support the buffer protocol.\n\nThe byteorder argument determines the byte order used to represent the\ninteger. If byteorder is 'big', the most significant byte is at the\nbeginning of the byte array. If byteorder is 'little', the most\nsignificant byte is at the end of the byte array. To request the native\nbyte order of the host system, use `sys.byteorder' as the byte order value.\n\nThe signed keyword-only argument indicates whether two's complement is\nused to represent the integer.";
4657 |
4658 | static PyMethodDef long_methods[] = {
4659 | {"conjugate", (PyCFunction)long_long, METH_NOARGS0x0004,
4660 | "Returns self, the complex conjugate of any int."},
4661 | {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS0x0004,
4662 | long_bit_length_doc},
4663 | #if 0
4664 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS0x0004,
4665 | "Returns always True."},
4666 | #endif
4667 | {"to_bytes", (PyCFunction)long_to_bytes,
4668 | METH_VARARGS0x0001|METH_KEYWORDS0x0002, long_to_bytes_doc},
4669 | {"from_bytes", (PyCFunction)long_from_bytes,
4670 | METH_VARARGS0x0001|METH_KEYWORDS0x0002|METH_CLASS0x0010, long_from_bytes_doc},
4671 | {"__trunc__", (PyCFunction)long_long, METH_NOARGS0x0004,
4672 | "Truncating an Integral returns itself."},
4673 | {"__floor__", (PyCFunction)long_long, METH_NOARGS0x0004,
4674 | "Flooring an Integral returns itself."},
4675 | {"__ceil__", (PyCFunction)long_long, METH_NOARGS0x0004,
4676 | "Ceiling of an Integral returns itself."},
4677 | {"__round__", (PyCFunction)long_round, METH_VARARGS0x0001,
4678 | "Rounding an Integral returns itself.\n"
4679 | "Rounding with an ndigits argument also returns an integer."},
4680 | {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS0x0004},
4681 | {"__format__", (PyCFunction)long__format__, METH_VARARGS0x0001},
4682 | {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS0x0004,
4683 | "Returns size in memory, in bytes"},
4684 | {NULL((void*)0), NULL((void*)0)} /* sentinel */
4685 | };
4686 |
4687 | static PyGetSetDef long_getset[] = {
4688 | {"real",
4689 | (getter)long_long, (setter)NULL((void*)0),
4690 | "the real part of a complex number",
4691 | NULL((void*)0)},
4692 | {"imag",
4693 | (getter)long_get0, (setter)NULL((void*)0),
4694 | "the imaginary part of a complex number",
4695 | NULL((void*)0)},
4696 | {"numerator",
4697 | (getter)long_long, (setter)NULL((void*)0),
4698 | "the numerator of a rational number in lowest terms",
4699 | NULL((void*)0)},
4700 | {"denominator",
4701 | (getter)long_get1, (setter)NULL((void*)0),
4702 | "the denominator of a rational number in lowest terms",
4703 | NULL((void*)0)},
4704 | {NULL((void*)0)} /* Sentinel */
4705 | };
4706 |
4707 | PyDoc_STRVAR(long_doc,static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4708 | "int(x[, base]) -> integer\n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4709 | \n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4710 | Convert a string or number to an integer, if possible. A floating\n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4711 | point argument will be truncated towards zero (this does not include a\n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4712 | string representation of a floating point number!) When converting a\n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4713 | string, use the optional base. It is an error to supply a base when\n\static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string."
4714 | converting a non-string.")static char long_doc[] = "int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating\npoint argument will be truncated towards zero (this does not include a\nstring representation of a floating point number!) When converting a\nstring, use the optional base. It is an error to supply a base when\nconverting a non-string.";
4715 |
4716 | static PyNumberMethods long_as_number = {
4717 | (binaryfunc)long_add, /*nb_add*/
4718 | (binaryfunc)long_sub, /*nb_subtract*/
4719 | (binaryfunc)long_mul, /*nb_multiply*/
4720 | long_mod, /*nb_remainder*/
4721 | long_divmod, /*nb_divmod*/
4722 | long_pow, /*nb_power*/
4723 | (unaryfunc)long_neg, /*nb_negative*/
4724 | (unaryfunc)long_long, /*tp_positive*/
4725 | (unaryfunc)long_abs, /*tp_absolute*/
4726 | (inquiry)long_bool, /*tp_bool*/
4727 | (unaryfunc)long_invert, /*nb_invert*/
4728 | long_lshift, /*nb_lshift*/
4729 | (binaryfunc)long_rshift, /*nb_rshift*/
4730 | long_and, /*nb_and*/
4731 | long_xor, /*nb_xor*/
4732 | long_or, /*nb_or*/
4733 | long_long, /*nb_int*/
4734 | 0, /*nb_reserved*/
4735 | long_float, /*nb_float*/
4736 | 0, /* nb_inplace_add */
4737 | 0, /* nb_inplace_subtract */
4738 | 0, /* nb_inplace_multiply */
4739 | 0, /* nb_inplace_remainder */
4740 | 0, /* nb_inplace_power */
4741 | 0, /* nb_inplace_lshift */
4742 | 0, /* nb_inplace_rshift */
4743 | 0, /* nb_inplace_and */
4744 | 0, /* nb_inplace_xor */
4745 | 0, /* nb_inplace_or */
4746 | long_div, /* nb_floor_divide */
4747 | long_true_divide, /* nb_true_divide */
4748 | 0, /* nb_inplace_floor_divide */
4749 | 0, /* nb_inplace_true_divide */
4750 | long_long, /* nb_index */
4751 | };
4752 |
4753 | PyTypeObject PyLong_Type = {
4754 | PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
4755 | "int", /* tp_name */
4756 | offsetof(PyLongObject, ob_digit)__builtin_offsetof(PyLongObject, ob_digit), /* tp_basicsize */
4757 | sizeof(digit), /* tp_itemsize */
4758 | long_dealloc, /* tp_dealloc */
4759 | 0, /* tp_print */
4760 | 0, /* tp_getattr */
4761 | 0, /* tp_setattr */
4762 | 0, /* tp_reserved */
4763 | long_to_decimal_string, /* tp_repr */
4764 | &long_as_number, /* tp_as_number */
4765 | 0, /* tp_as_sequence */
4766 | 0, /* tp_as_mapping */
4767 | (hashfunc)long_hash, /* tp_hash */
4768 | 0, /* tp_call */
4769 | long_to_decimal_string, /* tp_str */
4770 | PyObject_GenericGetAttr, /* tp_getattro */
4771 | 0, /* tp_setattro */
4772 | 0, /* tp_as_buffer */
4773 | Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10) |
4774 | Py_TPFLAGS_LONG_SUBCLASS(1L<<24), /* tp_flags */
4775 | long_doc, /* tp_doc */
4776 | 0, /* tp_traverse */
4777 | 0, /* tp_clear */
4778 | long_richcompare, /* tp_richcompare */
4779 | 0, /* tp_weaklistoffset */
4780 | 0, /* tp_iter */
4781 | 0, /* tp_iternext */
4782 | long_methods, /* tp_methods */
4783 | 0, /* tp_members */
4784 | long_getset, /* tp_getset */
4785 | 0, /* tp_base */
4786 | 0, /* tp_dict */
4787 | 0, /* tp_descr_get */
4788 | 0, /* tp_descr_set */
4789 | 0, /* tp_dictoffset */
4790 | 0, /* tp_init */
4791 | 0, /* tp_alloc */
4792 | long_new, /* tp_new */
4793 | PyObject_Del_PyObject_DebugFree, /* tp_free */
4794 | };
4795 |
4796 | static PyTypeObject Int_InfoType;
4797 |
4798 | PyDoc_STRVAR(int_info__doc__,static char int_info__doc__[] = "sys.int_info\n\nA struct sequence that holds information about Python's\ninternal representation of integers. The attributes are read only."
4799 | "sys.int_info\n\static char int_info__doc__[] = "sys.int_info\n\nA struct sequence that holds information about Python's\ninternal representation of integers. The attributes are read only."
4800 | \n\static char int_info__doc__[] = "sys.int_info\n\nA struct sequence that holds information about Python's\ninternal representation of integers. The attributes are read only."
4801 | A struct sequence that holds information about Python's\n\static char int_info__doc__[] = "sys.int_info\n\nA struct sequence that holds information about Python's\ninternal representation of integers. The attributes are read only."
4802 | internal representation of integers. The attributes are read only.")static char int_info__doc__[] = "sys.int_info\n\nA struct sequence that holds information about Python's\ninternal representation of integers. The attributes are read only.";
4803 |
4804 | static PyStructSequence_Field int_info_fields[] = {
4805 | {"bits_per_digit", "size of a digit in bits"},
4806 | {"sizeof_digit", "size in bytes of the C type used to represent a digit"},
4807 | {NULL((void*)0), NULL((void*)0)}
4808 | };
4809 |
4810 | static PyStructSequence_Desc int_info_desc = {
4811 | "sys.int_info", /* name */
4812 | int_info__doc__, /* doc */
4813 | int_info_fields, /* fields */
4814 | 2 /* number of fields */
4815 | };
4816 |
4817 | PyObject *
4818 | PyLong_GetInfo(void)
4819 | {
4820 | PyObject* int_info;
4821 | int field = 0;
4822 | int_info = PyStructSequence_New(&Int_InfoType);
4823 | if (int_info == NULL((void*)0))
4824 | return NULL((void*)0);
4825 | PyStructSequence_SET_ITEM(int_info, field++,(((PyTupleObject *)(int_info))->ob_item[field++] = PyLong_FromLong (30))
4826 | PyLong_FromLong(PyLong_SHIFT))(((PyTupleObject *)(int_info))->ob_item[field++] = PyLong_FromLong (30));
4827 | PyStructSequence_SET_ITEM(int_info, field++,(((PyTupleObject *)(int_info))->ob_item[field++] = PyLong_FromLong (sizeof(digit)))
4828 | PyLong_FromLong(sizeof(digit)))(((PyTupleObject *)(int_info))->ob_item[field++] = PyLong_FromLong (sizeof(digit)));
4829 | if (PyErr_Occurred()) {
4830 | Py_CLEAR(int_info)do { if (int_info) { PyObject *_py_tmp = (PyObject *)(int_info ); (int_info) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject *)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)-> ob_refcnt < 0) _Py_NegativeRefcount("Objects/longobject.c" , 4830, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject * )(_py_tmp)); } while (0); } } while (0);
4831 | return NULL((void*)0);
4832 | }
4833 | return int_info;
4834 | }
4835 |
4836 | int
4837 | _PyLong_Init(void)
4838 | {
4839 | #if NSMALLNEGINTS5 + NSMALLPOSINTS257 > 0
4840 | int ival, size;
4841 | PyLongObject *v = small_ints;
4842 |
4843 | for (ival = -NSMALLNEGINTS5; ival < NSMALLPOSINTS257; ival++, v++) {
4844 | size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
4845 | if (Py_TYPE(v)(((PyObject*)(v))->ob_type) == &PyLong_Type) {
4846 | /* The element is already initialized, most likely
4847 | * the Python interpreter was initialized before.
4848 | */
4849 | Py_ssize_t refcnt;
4850 | PyObject* op = (PyObject*)v;
4851 |
4852 | refcnt = Py_REFCNT(op)(((PyObject*)(op))->ob_refcnt) < 0 ? 0 : Py_REFCNT(op)(((PyObject*)(op))->ob_refcnt);
4853 | _Py_NewReference(op);
4854 | /* _Py_NewReference sets the ref count to 1 but
4855 | * the ref count might be larger. Set the refcnt
4856 | * to the original refcnt + 1 */
4857 | Py_REFCNT(op)(((PyObject*)(op))->ob_refcnt) = refcnt + 1;
4858 | assert(Py_SIZE(op) == size)(__builtin_expect(!((((PyVarObject*)(op))->ob_size) == size ), 0) ? __assert_rtn(__func__, "Objects/longobject.c", 4858, "Py_SIZE(op) == size" ) : (void)0);
4859 | assert(v->ob_digit[0] == abs(ival))(__builtin_expect(!(v->ob_digit[0] == abs(ival)), 0) ? __assert_rtn (__func__, "Objects/longobject.c", 4859, "v->ob_digit[0] == abs(ival)" ) : (void)0);
4860 | }
4861 | else {
4862 | PyObject_INIT(v, &PyLong_Type)( (((PyObject*)(v))->ob_type) = (&PyLong_Type), _Py_NewReference ((PyObject *)(v)), (v) );
4863 | }
4864 | Py_SIZE(v)(((PyVarObject*)(v))->ob_size) = size;
4865 | v->ob_digit[0] = abs(ival);
4866 | }
4867 | #endif
4868 | /* initialize int_info */
4869 | if (Int_InfoType.tp_name == 0)
4870 | PyStructSequence_InitType(&Int_InfoType, &int_info_desc);
4871 |
4872 | return 1;
4873 | }
4874 |
4875 | void
4876 | PyLong_Fini(void)
4877 | {
4878 | /* Integers are currently statically allocated. Py_DECREF is not
4879 | needed, but Python must forget about the reference or multiple
4880 | reinitializations will fail. */
4881 | #if NSMALLNEGINTS5 + NSMALLPOSINTS257 > 0
4882 | int i;
4883 | PyLongObject *v = small_ints;
4884 | for (i = 0; i < NSMALLNEGINTS5 + NSMALLPOSINTS257; i++, v++) {
4885 | _Py_DEC_REFTOTAL_Py_RefTotal--;
4886 | _Py_ForgetReference((PyObject*)v);
4887 | }
4888 | #endif
4889 | }