blob: 6cea51a2841cc85a5fbfdfa6b5ed21820d4732af [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/* Long (arbitrary precision) integer object implementation */
2
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003/* XXX The functional organization of this file is terrible */
4
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00006#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00008#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +00009
Guido van Rossumddefaf32007-01-14 03:31:43 +000010#ifndef NSMALLPOSINTS
11#define NSMALLPOSINTS 257
12#endif
13#ifndef NSMALLNEGINTS
14#define NSMALLNEGINTS 5
15#endif
16#if NSMALLNEGINTS + NSMALLPOSINTS > 0
17/* Small integers are preallocated in this array so that they
18 can be shared.
19 The integers that are preallocated are those in the range
20 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
21*/
22static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
23#ifdef COUNT_ALLOCS
24int quick_int_allocs, quick_neg_int_allocs;
25#endif
26
Guido van Rossum7eaf8222007-06-18 17:58:50 +000027static PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +000028get_small_int(int ival)
29{
30 PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
31 Py_INCREF(v);
32#ifdef COUNT_ALLOCS
33 if (ival >= 0)
34 quick_int_allocs++;
35 else
36 quick_neg_int_allocs++;
37#endif
38 return v;
39}
40#define CHECK_SMALL_INT(ival) \
41 do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
42 return get_small_int(ival); \
43 } while(0)
44
45#else
46#define CHECK_SMALL_INT(ival)
47#endif
48
Christian Heimes90aa7642007-12-19 02:45:37 +000049#define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(x)->ob_digit[0] : (Py_SIZE(x) == 0 ? 0 : (x)->ob_digit[0]))
Guido van Rossumddefaf32007-01-14 03:31:43 +000050/* If a freshly-allocated long is already shared, it must
51 be a small integer, so negating it must go to PyLong_FromLong */
52#define NEGATE(x) \
Christian Heimes90aa7642007-12-19 02:45:37 +000053 do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \
Christian Heimes217cfd12007-12-02 14:31:20 +000054 else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \
Guido van Rossumddefaf32007-01-14 03:31:43 +000055 Py_DECREF(x); (x) = (PyLongObject*)tmp; } \
56 while(0)
Tim Peters5af4e6c2002-08-12 02:31:19 +000057/* For long multiplication, use the O(N**2) school algorithm unless
58 * both operands contain more than KARATSUBA_CUTOFF digits (this
59 * being an internal Python long digit, in base BASE).
60 */
Tim Peters0973b992004-08-29 22:16:50 +000061#define KARATSUBA_CUTOFF 70
62#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000063
Tim Peters47e52ee2004-08-30 02:44:38 +000064/* For exponentiation, use the binary left-to-right algorithm
65 * unless the exponent contains more than FIVEARY_CUTOFF digits.
66 * In that case, do 5 bits at a time. The potential drawback is that
67 * a table of 2**5 intermediate results is computed.
68 */
69#define FIVEARY_CUTOFF 8
70
Guido van Rossume32e0141992-01-19 16:31:05 +000071#define ABS(x) ((x) < 0 ? -(x) : (x))
72
Tim Peters5af4e6c2002-08-12 02:31:19 +000073#undef MIN
74#undef MAX
75#define MAX(x, y) ((x) < (y) ? (y) : (x))
76#define MIN(x, y) ((x) > (y) ? (y) : (x))
77
Guido van Rossume32e0141992-01-19 16:31:05 +000078/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000079static PyLongObject *long_normalize(PyLongObject *);
80static PyLongObject *mul1(PyLongObject *, wdigit);
81static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000082static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossume32e0141992-01-19 16:31:05 +000083
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000085 if (--_Py_Ticker < 0) { \
86 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000087 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000088 }
89
Guido van Rossumedcc38a1991-05-05 20:09:44 +000090/* Normalize (remove leading zeros from) a long int object.
91 Doesn't attempt to free the storage--in most cases, due to the nature
92 of the algorithms used, this could save at most be one word anyway. */
93
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000095long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000096{
Christian Heimes90aa7642007-12-19 02:45:37 +000097 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +000098 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000099
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000100 while (i > 0 && v->ob_digit[i-1] == 0)
101 --i;
102 if (i != j)
Christian Heimes90aa7642007-12-19 02:45:37 +0000103 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000104 return v;
105}
106
107/* Allocate a new long int object with size digits.
108 Return NULL and set exception if we run out of memory. */
109
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000112{
Guido van Rossumddefaf32007-01-14 03:31:43 +0000113 PyLongObject *result;
114 /* Can't use sizeof(PyLongObject) here, since the
115 compiler takes padding at the end into account.
116 As the consequence, this would waste 2 bytes on
117 a 32-bit system, and 6 bytes on a 64-bit system.
118 This computation would be incorrect on systems
119 which have padding before the digits; with 16-bit
120 digits this should not happen. */
121 result = PyObject_MALLOC(sizeof(PyVarObject) +
122 size*sizeof(digit));
123 if (!result) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124 PyErr_NoMemory();
125 return NULL;
126 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000127 return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128}
129
Tim Peters64b5ce32001-09-10 20:52:51 +0000130PyObject *
131_PyLong_Copy(PyLongObject *src)
132{
133 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +0000135
136 assert(src != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000137 i = Py_SIZE(src);
Tim Peters64b5ce32001-09-10 20:52:51 +0000138 if (i < 0)
139 i = -(i);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000140 if (i < 2) {
141 int ival = src->ob_digit[0];
Christian Heimes90aa7642007-12-19 02:45:37 +0000142 if (Py_SIZE(src) < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000143 ival = -ival;
144 CHECK_SMALL_INT(ival);
145 }
Tim Peters64b5ce32001-09-10 20:52:51 +0000146 result = _PyLong_New(i);
147 if (result != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000148 Py_SIZE(result) = Py_SIZE(src);
Tim Peters64b5ce32001-09-10 20:52:51 +0000149 while (--i >= 0)
150 result->ob_digit[i] = src->ob_digit[i];
151 }
152 return (PyObject *)result;
153}
154
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000155/* Create a new long int object from a C long int */
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000158PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000159{
Tim Petersce9de2f2001-06-14 04:56:19 +0000160 PyLongObject *v;
Christian Heimesdae2a892008-04-19 00:55:37 +0000161 unsigned long abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000162 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
163 int ndigits = 0;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000164 int sign = 1;
165
166 CHECK_SMALL_INT(ival);
Tim Petersce9de2f2001-06-14 04:56:19 +0000167
168 if (ival < 0) {
Christian Heimesdae2a892008-04-19 00:55:37 +0000169 /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
170 ANSI C says that the result of -ival is undefined when ival
171 == LONG_MIN. Hence the following workaround. */
172 abs_ival = (unsigned long)(-1-ival) + 1;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000173 sign = -1;
Tim Petersce9de2f2001-06-14 04:56:19 +0000174 }
Christian Heimesdae2a892008-04-19 00:55:37 +0000175 else {
176 abs_ival = (unsigned long)ival;
177 }
Tim Petersce9de2f2001-06-14 04:56:19 +0000178
Guido van Rossumddefaf32007-01-14 03:31:43 +0000179 /* Fast path for single-digits ints */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000180 if (!(ival>>PyLong_SHIFT)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000181 v = _PyLong_New(1);
182 if (v) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000183 Py_SIZE(v) = sign;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000184 v->ob_digit[0] = ival;
185 }
186 return (PyObject*)v;
187 }
188
189 /* 2 digits */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000190 if (!(ival >> 2*PyLong_SHIFT)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000191 v = _PyLong_New(2);
192 if (v) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000193 Py_SIZE(v) = 2*sign;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000194 v->ob_digit[0] = (digit)ival & PyLong_MASK;
195 v->ob_digit[1] = ival >> PyLong_SHIFT;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000196 }
197 return (PyObject*)v;
198 }
199
200 /* Larger numbers: loop to determine number of digits */
Christian Heimesdae2a892008-04-19 00:55:37 +0000201 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000202 while (t) {
203 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000204 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000205 }
206 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000207 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000208 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +0000209 Py_SIZE(v) = ndigits*sign;
Christian Heimesdae2a892008-04-19 00:55:37 +0000210 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000211 while (t) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000212 *p++ = (digit)(t & PyLong_MASK);
213 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000214 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000215 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000217}
218
Guido van Rossum53756b11997-01-03 17:14:46 +0000219/* Create a new long int object from a C unsigned long int */
220
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000222PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000223{
Tim Petersce9de2f2001-06-14 04:56:19 +0000224 PyLongObject *v;
225 unsigned long t;
226 int ndigits = 0;
227
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000228 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000229 return PyLong_FromLong(ival);
Tim Petersce9de2f2001-06-14 04:56:19 +0000230 /* Count the number of Python digits. */
231 t = (unsigned long)ival;
232 while (t) {
233 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000234 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000235 }
236 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000237 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000238 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +0000239 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000240 while (ival) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000241 *p++ = (digit)(ival & PyLong_MASK);
242 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000243 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000244 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000246}
247
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000248/* Create a new long int object from a C double */
249
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000252{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000254 double frac;
255 int i, ndig, expo, neg;
256 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000257 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000258 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000259 "cannot convert float infinity to int");
Guido van Rossum1a23c241999-09-27 17:11:52 +0000260 return NULL;
261 }
Christian Heimesa34706f2008-01-04 03:06:10 +0000262 if (Py_IS_NAN(dval)) {
Christian Heimes386cd1e2008-01-15 02:01:20 +0000263 PyErr_SetString(PyExc_OverflowError,
264 "cannot convert float NaN to int");
265 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +0000266 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000267 if (dval < 0.0) {
268 neg = 1;
269 dval = -dval;
270 }
271 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
272 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 return PyLong_FromLong(0L);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000274 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000276 if (v == NULL)
277 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000278 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000279 for (i = ndig; --i >= 0; ) {
280 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000281 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000282 frac = frac - (double)bits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000283 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000284 }
285 if (neg)
Christian Heimes90aa7642007-12-19 02:45:37 +0000286 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000288}
289
Thomas Wouters89f507f2006-12-13 04:49:30 +0000290/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
291 * anything about what happens when a signed integer operation overflows,
292 * and some compilers think they're doing you a favor by being "clever"
293 * then. The bit pattern for the largest postive signed long is
294 * (unsigned long)LONG_MAX, and for the smallest negative signed long
295 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
296 * However, some other compilers warn about applying unary minus to an
297 * unsigned operand. Hence the weird "0-".
298 */
299#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
300#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
301
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000302/* Get a C long int from a long int object.
303 Returns -1 and sets an error condition if overflow occurs. */
304
305long
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000306PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000307{
Guido van Rossumf7531811998-05-26 14:33:37 +0000308 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000310 unsigned long x, prev;
Georg Brandl61c31b02007-02-26 14:46:30 +0000311 long res;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000312 Py_ssize_t i;
313 int sign;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000314 int do_decref = 0; /* if nb_int was called */
Guido van Rossumf7531811998-05-26 14:33:37 +0000315
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000316 *overflow = 0;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000317 if (vv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000319 return -1;
320 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000321
322 if (!PyLong_Check(vv)) {
323 PyNumberMethods *nb;
324 if ((nb = vv->ob_type->tp_as_number) == NULL ||
325 nb->nb_int == NULL) {
326 PyErr_SetString(PyExc_TypeError, "an integer is required");
327 return -1;
328 }
329 vv = (*nb->nb_int) (vv);
330 if (vv == NULL)
331 return -1;
332 do_decref = 1;
333 if (!PyLong_Check(vv)) {
334 Py_DECREF(vv);
335 PyErr_SetString(PyExc_TypeError,
336 "nb_int should return int object");
337 return -1;
338 }
339 }
340
Georg Brandl61c31b02007-02-26 14:46:30 +0000341 res = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000343 i = Py_SIZE(v);
Guido van Rossumf7531811998-05-26 14:33:37 +0000344
Georg Brandl61c31b02007-02-26 14:46:30 +0000345 switch (i) {
346 case -1:
347 res = -v->ob_digit[0];
348 break;
349 case 0:
350 res = 0;
351 break;
352 case 1:
353 res = v->ob_digit[0];
354 break;
355 default:
356 sign = 1;
357 x = 0;
358 if (i < 0) {
359 sign = -1;
360 i = -(i);
361 }
362 while (--i >= 0) {
363 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000364 x = (x << PyLong_SHIFT) + v->ob_digit[i];
365 if ((x >> PyLong_SHIFT) != prev) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000366 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
Georg Brandl61c31b02007-02-26 14:46:30 +0000367 goto exit;
368 }
369 }
370 /* Haven't lost any bits, but casting to long requires extra care
371 * (see comment above).
372 */
373 if (x <= (unsigned long)LONG_MAX) {
374 res = (long)x * sign;
375 }
376 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
377 res = LONG_MIN;
378 }
379 else {
Christian Heimes90aa7642007-12-19 02:45:37 +0000380 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000381 /* res is already set to -1 */
Georg Brandl61c31b02007-02-26 14:46:30 +0000382 }
383 }
384 exit:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000385 if (do_decref) {
386 Py_DECREF(vv);
387 }
Georg Brandl61c31b02007-02-26 14:46:30 +0000388 return res;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000389}
390
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000391long
392PyLong_AsLong(PyObject *obj)
393{
394 int overflow;
395 long result = PyLong_AsLongAndOverflow(obj, &overflow);
396 if (overflow) {
397 /* XXX: could be cute and give a different
398 message for overflow == -1 */
399 PyErr_SetString(PyExc_OverflowError,
400 "Python int too large to convert to C long");
401 }
402 return result;
403}
404
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000405/* Get a Py_ssize_t from a long int object.
406 Returns -1 and sets an error condition if overflow occurs. */
407
408Py_ssize_t
Guido van Rossumddefaf32007-01-14 03:31:43 +0000409PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000410 register PyLongObject *v;
411 size_t x, prev;
412 Py_ssize_t i;
413 int sign;
414
415 if (vv == NULL || !PyLong_Check(vv)) {
416 PyErr_BadInternalCall();
417 return -1;
418 }
419 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000420 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000421 switch (i) {
422 case -1: return -v->ob_digit[0];
423 case 0: return 0;
424 case 1: return v->ob_digit[0];
425 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426 sign = 1;
427 x = 0;
428 if (i < 0) {
429 sign = -1;
430 i = -(i);
431 }
432 while (--i >= 0) {
433 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000434 x = (x << PyLong_SHIFT) + v->ob_digit[i];
435 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000436 goto overflow;
437 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000438 /* Haven't lost any bits, but casting to a signed type requires
439 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000440 */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000441 if (x <= (size_t)PY_SSIZE_T_MAX) {
442 return (Py_ssize_t)x * sign;
443 }
444 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
445 return PY_SSIZE_T_MIN;
446 }
447 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448
449 overflow:
450 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000451 "Python int too large to convert to C ssize_t");
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000452 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453}
454
Guido van Rossumd8c80482002-08-13 00:24:58 +0000455/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000456 Returns -1 and sets an error condition if overflow occurs. */
457
458unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000459PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000460{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000462 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 if (vv == NULL || !PyLong_Check(vv)) {
466 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000467 return (unsigned long) -1;
468 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000470 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000471 x = 0;
472 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000474 "can't convert negative value to unsigned int");
Guido van Rossum53756b11997-01-03 17:14:46 +0000475 return (unsigned long) -1;
476 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000477 switch (i) {
478 case 0: return 0;
479 case 1: return v->ob_digit[0];
480 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000481 while (--i >= 0) {
482 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000483 x = (x << PyLong_SHIFT) + v->ob_digit[i];
484 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000486 "python int too large to convert to C unsigned long");
Guido van Rossumddefaf32007-01-14 03:31:43 +0000487 return (unsigned long) -1;
488 }
489 }
490 return x;
491}
492
493/* Get a C unsigned long int from a long int object.
494 Returns -1 and sets an error condition if overflow occurs. */
495
496size_t
497PyLong_AsSize_t(PyObject *vv)
498{
499 register PyLongObject *v;
500 size_t x, prev;
501 Py_ssize_t i;
502
503 if (vv == NULL || !PyLong_Check(vv)) {
504 PyErr_BadInternalCall();
505 return (unsigned long) -1;
506 }
507 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000508 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000509 x = 0;
510 if (i < 0) {
511 PyErr_SetString(PyExc_OverflowError,
512 "can't convert negative value to size_t");
513 return (size_t) -1;
514 }
515 switch (i) {
516 case 0: return 0;
517 case 1: return v->ob_digit[0];
518 }
519 while (--i >= 0) {
520 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000521 x = (x << PyLong_SHIFT) + v->ob_digit[i];
522 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000523 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000524 "Python int too large to convert to C size_t");
Guido van Rossum53756b11997-01-03 17:14:46 +0000525 return (unsigned long) -1;
526 }
527 }
528 return x;
529}
530
Thomas Hellera4ea6032003-04-17 18:55:45 +0000531/* Get a C unsigned long int from a long int object, ignoring the high bits.
532 Returns -1 and sets an error condition if an error occurs. */
533
Guido van Rossumddefaf32007-01-14 03:31:43 +0000534static unsigned long
535_PyLong_AsUnsignedLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000536{
537 register PyLongObject *v;
538 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000539 Py_ssize_t i;
540 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000541
542 if (vv == NULL || !PyLong_Check(vv)) {
543 PyErr_BadInternalCall();
544 return (unsigned long) -1;
545 }
546 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000547 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000548 switch (i) {
549 case 0: return 0;
550 case 1: return v->ob_digit[0];
551 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000552 sign = 1;
553 x = 0;
554 if (i < 0) {
555 sign = -1;
556 i = -i;
557 }
558 while (--i >= 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000559 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000560 }
561 return x * sign;
562}
563
Guido van Rossumddefaf32007-01-14 03:31:43 +0000564unsigned long
565PyLong_AsUnsignedLongMask(register PyObject *op)
566{
567 PyNumberMethods *nb;
568 PyLongObject *lo;
569 unsigned long val;
570
571 if (op && PyLong_Check(op))
572 return _PyLong_AsUnsignedLongMask(op);
573
574 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
575 nb->nb_int == NULL) {
576 PyErr_SetString(PyExc_TypeError, "an integer is required");
577 return (unsigned long)-1;
578 }
579
580 lo = (PyLongObject*) (*nb->nb_int) (op);
581 if (lo == NULL)
582 return (unsigned long)-1;
583 if (PyLong_Check(lo)) {
584 val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
585 Py_DECREF(lo);
586 if (PyErr_Occurred())
587 return (unsigned long)-1;
588 return val;
589 }
590 else
591 {
592 Py_DECREF(lo);
593 PyErr_SetString(PyExc_TypeError,
594 "nb_int should return int object");
595 return (unsigned long)-1;
596 }
597}
598
Tim Peters5b8132f2003-01-31 15:52:05 +0000599int
600_PyLong_Sign(PyObject *vv)
601{
602 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000603
604 assert(v != NULL);
605 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000606
Christian Heimes90aa7642007-12-19 02:45:37 +0000607 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000608}
609
Tim Petersbaefd9e2003-01-28 20:37:45 +0000610size_t
611_PyLong_NumBits(PyObject *vv)
612{
613 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000614 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000615 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000616
617 assert(v != NULL);
618 assert(PyLong_Check(v));
Christian Heimes90aa7642007-12-19 02:45:37 +0000619 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000620 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
621 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000622 digit msd = v->ob_digit[ndigits - 1];
623
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000624 result = (ndigits - 1) * PyLong_SHIFT;
625 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000626 goto Overflow;
627 do {
628 ++result;
629 if (result == 0)
630 goto Overflow;
631 msd >>= 1;
632 } while (msd);
633 }
634 return result;
635
636Overflow:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000637 PyErr_SetString(PyExc_OverflowError, "int has too many bits "
Tim Petersbaefd9e2003-01-28 20:37:45 +0000638 "to express in a platform size_t");
639 return (size_t)-1;
640}
641
Tim Peters2a9b3672001-06-11 21:23:58 +0000642PyObject *
643_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
644 int little_endian, int is_signed)
645{
646 const unsigned char* pstartbyte;/* LSB of bytes */
647 int incr; /* direction to move pstartbyte */
648 const unsigned char* pendbyte; /* MSB of bytes */
649 size_t numsignificantbytes; /* number of bytes that matter */
650 size_t ndigits; /* number of Python long digits */
651 PyLongObject* v; /* result */
652 int idigit = 0; /* next free index in v->ob_digit */
653
654 if (n == 0)
655 return PyLong_FromLong(0L);
656
657 if (little_endian) {
658 pstartbyte = bytes;
659 pendbyte = bytes + n - 1;
660 incr = 1;
661 }
662 else {
663 pstartbyte = bytes + n - 1;
664 pendbyte = bytes;
665 incr = -1;
666 }
667
668 if (is_signed)
669 is_signed = *pendbyte >= 0x80;
670
671 /* Compute numsignificantbytes. This consists of finding the most
672 significant byte. Leading 0 bytes are insignficant if the number
673 is positive, and leading 0xff bytes if negative. */
674 {
675 size_t i;
676 const unsigned char* p = pendbyte;
677 const int pincr = -incr; /* search MSB to LSB */
678 const unsigned char insignficant = is_signed ? 0xff : 0x00;
679
680 for (i = 0; i < n; ++i, p += pincr) {
681 if (*p != insignficant)
682 break;
683 }
684 numsignificantbytes = n - i;
685 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
686 actually has 2 significant bytes. OTOH, 0xff0001 ==
687 -0x00ffff, so we wouldn't *need* to bump it there; but we
688 do for 0xffff = -0x0001. To be safe without bothering to
689 check every case, bump it regardless. */
690 if (is_signed && numsignificantbytes < n)
691 ++numsignificantbytes;
692 }
693
694 /* How many Python long digits do we need? We have
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000695 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
Tim Peters2a9b3672001-06-11 21:23:58 +0000696 bits, so it's the ceiling of the quotient. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000697 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Tim Peters2a9b3672001-06-11 21:23:58 +0000698 if (ndigits > (size_t)INT_MAX)
699 return PyErr_NoMemory();
700 v = _PyLong_New((int)ndigits);
701 if (v == NULL)
702 return NULL;
703
704 /* Copy the bits over. The tricky parts are computing 2's-comp on
705 the fly for signed numbers, and dealing with the mismatch between
706 8-bit bytes and (probably) 15-bit Python digits.*/
707 {
708 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000709 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000710 twodigits accum = 0; /* sliding register */
711 unsigned int accumbits = 0; /* number of bits in accum */
712 const unsigned char* p = pstartbyte;
713
714 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000715 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000716 /* Compute correction for 2's comp, if needed. */
717 if (is_signed) {
718 thisbyte = (0xff ^ thisbyte) + carry;
719 carry = thisbyte >> 8;
720 thisbyte &= 0xff;
721 }
722 /* Because we're going LSB to MSB, thisbyte is
723 more significant than what's already in accum,
724 so needs to be prepended to accum. */
725 accum |= thisbyte << accumbits;
726 accumbits += 8;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000727 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000728 /* There's enough to fill a Python digit. */
729 assert(idigit < (int)ndigits);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000730 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000731 ++idigit;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000732 accum >>= PyLong_SHIFT;
733 accumbits -= PyLong_SHIFT;
734 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000735 }
736 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000737 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000738 if (accumbits) {
739 assert(idigit < (int)ndigits);
740 v->ob_digit[idigit] = (digit)accum;
741 ++idigit;
742 }
743 }
744
Christian Heimes90aa7642007-12-19 02:45:37 +0000745 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000746 return (PyObject *)long_normalize(v);
747}
748
749int
750_PyLong_AsByteArray(PyLongObject* v,
751 unsigned char* bytes, size_t n,
752 int little_endian, int is_signed)
753{
754 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000756 twodigits accum; /* sliding register */
757 unsigned int accumbits; /* # bits in accum */
758 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
759 twodigits carry; /* for computing 2's-comp */
760 size_t j; /* # bytes filled */
761 unsigned char* p; /* pointer to next byte in bytes */
762 int pincr; /* direction to move p */
763
764 assert(v != NULL && PyLong_Check(v));
765
Christian Heimes90aa7642007-12-19 02:45:37 +0000766 if (Py_SIZE(v) < 0) {
767 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000768 if (!is_signed) {
769 PyErr_SetString(PyExc_TypeError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000770 "can't convert negative int to unsigned");
Tim Peters2a9b3672001-06-11 21:23:58 +0000771 return -1;
772 }
773 do_twos_comp = 1;
774 }
775 else {
Christian Heimes90aa7642007-12-19 02:45:37 +0000776 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000777 do_twos_comp = 0;
778 }
779
780 if (little_endian) {
781 p = bytes;
782 pincr = 1;
783 }
784 else {
785 p = bytes + n - 1;
786 pincr = -1;
787 }
788
Tim Peters898cf852001-06-13 20:50:08 +0000789 /* Copy over all the Python digits.
790 It's crucial that every Python digit except for the MSD contribute
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000791 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000792 normalized. */
793 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000794 j = 0;
795 accum = 0;
796 accumbits = 0;
797 carry = do_twos_comp ? 1 : 0;
798 for (i = 0; i < ndigits; ++i) {
799 twodigits thisdigit = v->ob_digit[i];
800 if (do_twos_comp) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000801 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
802 carry = thisdigit >> PyLong_SHIFT;
803 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000804 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000805 /* Because we're going LSB to MSB, thisdigit is more
806 significant than what's already in accum, so needs to be
807 prepended to accum. */
808 accum |= thisdigit << accumbits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000809 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000810
Tim Petersede05092001-06-14 08:53:38 +0000811 /* The most-significant digit may be (probably is) at least
812 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000813 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000814 /* Count # of sign bits -- they needn't be stored,
815 * although for signed conversion we need later to
816 * make sure at least one sign bit gets stored.
817 * First shift conceptual sign bit to real sign bit.
818 */
819 stwodigits s = (stwodigits)(thisdigit <<
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000820 (8*sizeof(stwodigits) - PyLong_SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000821 unsigned int nsignbits = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000822 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000823 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000824 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000825 }
Tim Petersede05092001-06-14 08:53:38 +0000826 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000827 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000828
Tim Peters2a9b3672001-06-11 21:23:58 +0000829 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000830 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000831 if (j >= n)
832 goto Overflow;
833 ++j;
834 *p = (unsigned char)(accum & 0xff);
835 p += pincr;
836 accumbits -= 8;
837 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000838 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000839 }
840
841 /* Store the straggler (if any). */
842 assert(accumbits < 8);
843 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000844 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000845 if (j >= n)
846 goto Overflow;
847 ++j;
848 if (do_twos_comp) {
849 /* Fill leading bits of the byte with sign bits
850 (appropriately pretending that the long had an
851 infinite supply of sign bits). */
852 accum |= (~(twodigits)0) << accumbits;
853 }
854 *p = (unsigned char)(accum & 0xff);
855 p += pincr;
856 }
Tim Peters05607ad2001-06-13 21:01:27 +0000857 else if (j == n && n > 0 && is_signed) {
858 /* The main loop filled the byte array exactly, so the code
859 just above didn't get to ensure there's a sign bit, and the
860 loop below wouldn't add one either. Make sure a sign bit
861 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000862 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000863 int sign_bit_set = msb >= 0x80;
864 assert(accumbits == 0);
865 if (sign_bit_set == do_twos_comp)
866 return 0;
867 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000868 goto Overflow;
869 }
Tim Peters05607ad2001-06-13 21:01:27 +0000870
871 /* Fill remaining bytes with copies of the sign bit. */
872 {
873 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
874 for ( ; j < n; ++j, p += pincr)
875 *p = signbyte;
876 }
877
Tim Peters2a9b3672001-06-11 21:23:58 +0000878 return 0;
879
880Overflow:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000881 PyErr_SetString(PyExc_OverflowError, "int too big to convert");
Tim Peters2a9b3672001-06-11 21:23:58 +0000882 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000883
Tim Peters2a9b3672001-06-11 21:23:58 +0000884}
885
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000886double
887_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
888{
889/* NBITS_WANTED should be > the number of bits in a double's precision,
890 but small enough so that 2**NBITS_WANTED is within the normal double
891 range. nbitsneeded is set to 1 less than that because the most-significant
892 Python digit contains at least 1 significant bit, but we don't want to
893 bother counting them (catering to the worst case cheaply).
894
895 57 is one more than VAX-D double precision; I (Tim) don't know of a double
896 format with more precision than that; it's 1 larger so that we add in at
897 least one round bit to stand in for the ignored least-significant bits.
898*/
899#define NBITS_WANTED 57
900 PyLongObject *v;
901 double x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000902 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000903 Py_ssize_t i;
904 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000905 int nbitsneeded;
906
907 if (vv == NULL || !PyLong_Check(vv)) {
908 PyErr_BadInternalCall();
909 return -1;
910 }
911 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000912 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000913 sign = 1;
914 if (i < 0) {
915 sign = -1;
916 i = -(i);
917 }
918 else if (i == 0) {
919 *exponent = 0;
920 return 0.0;
921 }
922 --i;
923 x = (double)v->ob_digit[i];
924 nbitsneeded = NBITS_WANTED - 1;
925 /* Invariant: i Python digits remain unaccounted for. */
926 while (i > 0 && nbitsneeded > 0) {
927 --i;
928 x = x * multiplier + (double)v->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000929 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000930 }
931 /* There are i digits we didn't shift in. Pretending they're all
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000932 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000933 *exponent = i;
934 assert(x > 0.0);
935 return x * sign;
936#undef NBITS_WANTED
937}
938
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000939/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000940
941double
Tim Peters9f688bf2000-07-07 15:53:28 +0000942PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000943{
Thomas Wouters553489a2006-02-01 21:32:04 +0000944 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000945 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000946
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 if (vv == NULL || !PyLong_Check(vv)) {
948 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000949 return -1;
950 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000951 x = _PyLong_AsScaledDouble(vv, &e);
952 if (x == -1.0 && PyErr_Occurred())
953 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000954 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
955 set correctly after a successful _PyLong_AsScaledDouble() call */
956 assert(e >= 0);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000957 if (e > INT_MAX / PyLong_SHIFT)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000958 goto overflow;
959 errno = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000960 x = ldexp(x, e * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000961 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000962 goto overflow;
963 return x;
964
965overflow:
966 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000967 "Python int too large to convert to C double");
Tim Peters9fffa3e2001-09-04 05:14:19 +0000968 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000969}
970
Guido van Rossum78694d91998-09-18 14:14:13 +0000971/* Create a new long (or int) object from a C pointer */
972
973PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000974PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000975{
Tim Peters70128a12001-06-16 08:48:40 +0000976#ifndef HAVE_LONG_LONG
977# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
978#endif
979#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000980# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000981#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +0000982 /* special-case null pointer */
983 if (!p)
Christian Heimes217cfd12007-12-02 14:31:20 +0000984 return PyLong_FromLong(0);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000985 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
Tim Peters70128a12001-06-16 08:48:40 +0000986
Guido van Rossum78694d91998-09-18 14:14:13 +0000987}
988
989/* Get a C pointer from a long object (or an int object in some cases) */
990
991void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000992PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000993{
994 /* This function will allow int or long objects. If vv is neither,
995 then the PyLong_AsLong*() functions will raise the exception:
996 PyExc_SystemError, "bad argument to internal function"
997 */
Tim Peters70128a12001-06-16 08:48:40 +0000998#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000999 long x;
1000
Guido van Rossumddefaf32007-01-14 03:31:43 +00001001 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001002 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001003 else
1004 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +00001005#else
Tim Peters70128a12001-06-16 08:48:40 +00001006
1007#ifndef HAVE_LONG_LONG
1008# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
1009#endif
1010#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001011# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +00001012#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001013 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001014
Guido van Rossumddefaf32007-01-14 03:31:43 +00001015 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001016 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 else
1018 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +00001019
1020#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +00001021
1022 if (x == -1 && PyErr_Occurred())
1023 return NULL;
1024 return (void *)x;
1025}
1026
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001027#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +00001028
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001029/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +00001030 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001031 */
1032
Tim Peterscf37dfc2001-06-14 18:42:50 +00001033#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +00001034
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001035/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001036
1037PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001038PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001039{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 PyLongObject *v;
Christian Heimesdae2a892008-04-19 00:55:37 +00001041 unsigned PY_LONG_LONG abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001042 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
1043 int ndigits = 0;
1044 int negative = 0;
1045
Guido van Rossumddefaf32007-01-14 03:31:43 +00001046 CHECK_SMALL_INT(ival);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047 if (ival < 0) {
Christian Heimesdae2a892008-04-19 00:55:37 +00001048 /* avoid signed overflow on negation; see comments
1049 in PyLong_FromLong above. */
1050 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001051 negative = 1;
1052 }
Christian Heimesdae2a892008-04-19 00:55:37 +00001053 else {
1054 abs_ival = (unsigned PY_LONG_LONG)ival;
1055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056
1057 /* Count the number of Python digits.
1058 We used to pick 5 ("big enough for anything"), but that's a
1059 waste of time and space given that 5*15 = 75 bits are rarely
1060 needed. */
Christian Heimesdae2a892008-04-19 00:55:37 +00001061 t = abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062 while (t) {
1063 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001064 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065 }
1066 v = _PyLong_New(ndigits);
1067 if (v != NULL) {
1068 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001069 Py_SIZE(v) = negative ? -ndigits : ndigits;
Christian Heimesdae2a892008-04-19 00:55:37 +00001070 t = abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 while (t) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001072 *p++ = (digit)(t & PyLong_MASK);
1073 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074 }
1075 }
1076 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001077}
1078
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001079/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +00001080
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001081PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001082PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001083{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001084 PyLongObject *v;
1085 unsigned PY_LONG_LONG t;
1086 int ndigits = 0;
1087
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001088 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +00001089 return PyLong_FromLong(ival);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001090 /* Count the number of Python digits. */
1091 t = (unsigned PY_LONG_LONG)ival;
1092 while (t) {
1093 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001094 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001095 }
1096 v = _PyLong_New(ndigits);
1097 if (v != NULL) {
1098 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001099 Py_SIZE(v) = ndigits;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100 while (ival) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001101 *p++ = (digit)(ival & PyLong_MASK);
1102 ival >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001103 }
1104 }
1105 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001106}
1107
Martin v. Löwis18e16552006-02-15 17:27:45 +00001108/* Create a new long int object from a C Py_ssize_t. */
1109
1110PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001111PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001112{
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001113 PyLongObject *v;
1114 size_t abs_ival;
1115 size_t t; /* unsigned so >> doesn't propagate sign bit */
1116 int ndigits = 0;
1117 int negative = 0;
1118
1119 CHECK_SMALL_INT(ival);
1120 if (ival < 0) {
1121 /* avoid signed overflow when ival = SIZE_T_MIN */
1122 abs_ival = (size_t)(-1-ival)+1;
1123 negative = 1;
1124 }
1125 else {
1126 abs_ival = (size_t)ival;
1127 }
1128
1129 /* Count the number of Python digits. */
1130 t = abs_ival;
1131 while (t) {
1132 ++ndigits;
1133 t >>= PyLong_SHIFT;
1134 }
1135 v = _PyLong_New(ndigits);
1136 if (v != NULL) {
1137 digit *p = v->ob_digit;
1138 Py_SIZE(v) = negative ? -ndigits : ndigits;
1139 t = abs_ival;
1140 while (t) {
1141 *p++ = (digit)(t & PyLong_MASK);
1142 t >>= PyLong_SHIFT;
1143 }
1144 }
1145 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146}
1147
1148/* Create a new long int object from a C size_t. */
1149
1150PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001151PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001152{
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001153 PyLongObject *v;
1154 size_t t;
1155 int ndigits = 0;
1156
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001157 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +00001158 return PyLong_FromLong(ival);
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001159 /* Count the number of Python digits. */
1160 t = ival;
1161 while (t) {
1162 ++ndigits;
1163 t >>= PyLong_SHIFT;
1164 }
1165 v = _PyLong_New(ndigits);
1166 if (v != NULL) {
1167 digit *p = v->ob_digit;
1168 Py_SIZE(v) = ndigits;
1169 while (ival) {
1170 *p++ = (digit)(ival & PyLong_MASK);
1171 ival >>= PyLong_SHIFT;
1172 }
1173 }
1174 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001175}
1176
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001177/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001178 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001179
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001180PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001181PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001182{
Guido van Rossumddefaf32007-01-14 03:31:43 +00001183 PyLongObject *v;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001184 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001185 int one = 1;
1186 int res;
1187
Tim Petersd38b1c72001-09-30 05:09:37 +00001188 if (vv == NULL) {
1189 PyErr_BadInternalCall();
1190 return -1;
1191 }
1192 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001193 PyNumberMethods *nb;
1194 PyObject *io;
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001195 if ((nb = vv->ob_type->tp_as_number) == NULL ||
1196 nb->nb_int == NULL) {
1197 PyErr_SetString(PyExc_TypeError, "an integer is required");
1198 return -1;
1199 }
1200 io = (*nb->nb_int) (vv);
1201 if (io == NULL)
1202 return -1;
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001203 if (PyLong_Check(io)) {
1204 bytes = PyLong_AsLongLong(io);
1205 Py_DECREF(io);
1206 return bytes;
1207 }
1208 Py_DECREF(io);
1209 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001210 return -1;
1211 }
1212
Guido van Rossumddefaf32007-01-14 03:31:43 +00001213 v = (PyLongObject*)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001214 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001215 case -1: return -v->ob_digit[0];
1216 case 0: return 0;
1217 case 1: return v->ob_digit[0];
1218 }
Tim Petersd1a7da62001-06-13 00:35:57 +00001219 res = _PyLong_AsByteArray(
1220 (PyLongObject *)vv, (unsigned char *)&bytes,
1221 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001222
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001223 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001224 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001225 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001226 else
1227 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001228}
1229
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001230/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001231 Return -1 and set an error if overflow occurs. */
1232
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001233unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001234PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001235{
Guido van Rossumddefaf32007-01-14 03:31:43 +00001236 PyLongObject *v;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001237 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001238 int one = 1;
1239 int res;
1240
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001241 if (vv == NULL || !PyLong_Check(vv)) {
1242 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001244 }
1245
Guido van Rossumddefaf32007-01-14 03:31:43 +00001246 v = (PyLongObject*)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001247 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001248 case 0: return 0;
1249 case 1: return v->ob_digit[0];
1250 }
1251
Tim Petersd1a7da62001-06-13 00:35:57 +00001252 res = _PyLong_AsByteArray(
1253 (PyLongObject *)vv, (unsigned char *)&bytes,
1254 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001255
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001256 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001257 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001258 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001259 else
1260 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001261}
Tim Petersd1a7da62001-06-13 00:35:57 +00001262
Thomas Hellera4ea6032003-04-17 18:55:45 +00001263/* Get a C unsigned long int from a long int object, ignoring the high bits.
1264 Returns -1 and sets an error condition if an error occurs. */
1265
Guido van Rossumddefaf32007-01-14 03:31:43 +00001266static unsigned PY_LONG_LONG
1267_PyLong_AsUnsignedLongLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001268{
1269 register PyLongObject *v;
1270 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001271 Py_ssize_t i;
1272 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001273
1274 if (vv == NULL || !PyLong_Check(vv)) {
1275 PyErr_BadInternalCall();
1276 return (unsigned long) -1;
1277 }
1278 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001279 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001280 case 0: return 0;
1281 case 1: return v->ob_digit[0];
1282 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001283 i = Py_SIZE(v);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001284 sign = 1;
1285 x = 0;
1286 if (i < 0) {
1287 sign = -1;
1288 i = -i;
1289 }
1290 while (--i >= 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001291 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001292 }
1293 return x * sign;
1294}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001295
1296unsigned PY_LONG_LONG
1297PyLong_AsUnsignedLongLongMask(register PyObject *op)
1298{
1299 PyNumberMethods *nb;
1300 PyLongObject *lo;
1301 unsigned PY_LONG_LONG val;
1302
1303 if (op && PyLong_Check(op))
1304 return _PyLong_AsUnsignedLongLongMask(op);
1305
1306 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
1307 nb->nb_int == NULL) {
1308 PyErr_SetString(PyExc_TypeError, "an integer is required");
1309 return (unsigned PY_LONG_LONG)-1;
1310 }
1311
1312 lo = (PyLongObject*) (*nb->nb_int) (op);
1313 if (lo == NULL)
1314 return (unsigned PY_LONG_LONG)-1;
1315 if (PyLong_Check(lo)) {
1316 val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
1317 Py_DECREF(lo);
1318 if (PyErr_Occurred())
1319 return (unsigned PY_LONG_LONG)-1;
1320 return val;
1321 }
1322 else
1323 {
1324 Py_DECREF(lo);
1325 PyErr_SetString(PyExc_TypeError,
1326 "nb_int should return int object");
1327 return (unsigned PY_LONG_LONG)-1;
1328 }
1329}
Tim Petersd1a7da62001-06-13 00:35:57 +00001330#undef IS_LITTLE_ENDIAN
1331
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001332#endif /* HAVE_LONG_LONG */
1333
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00001334#define CHECK_BINOP(v,w) \
1335 if (!PyLong_Check(v) || !PyLong_Check(w)) { \
Neil Schemenauerba872e22001-01-04 01:46:03 +00001336 Py_INCREF(Py_NotImplemented); \
1337 return Py_NotImplemented; \
1338 }
1339
Tim Peters877a2122002-08-12 05:09:36 +00001340/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1341 * is modified in place, by adding y to it. Carries are propagated as far as
1342 * x[m-1], and the remaining carry (0 or 1) is returned.
1343 */
1344static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001345v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001346{
1347 int i;
1348 digit carry = 0;
1349
1350 assert(m >= n);
1351 for (i = 0; i < n; ++i) {
1352 carry += x[i] + y[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001353 x[i] = carry & PyLong_MASK;
1354 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001355 assert((carry & 1) == carry);
1356 }
1357 for (; carry && i < m; ++i) {
1358 carry += x[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001359 x[i] = carry & PyLong_MASK;
1360 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001361 assert((carry & 1) == carry);
1362 }
1363 return carry;
1364}
1365
1366/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1367 * is modified in place, by subtracting y from it. Borrows are propagated as
1368 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1369 */
1370static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001371v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001372{
1373 int i;
1374 digit borrow = 0;
1375
1376 assert(m >= n);
1377 for (i = 0; i < n; ++i) {
1378 borrow = x[i] - y[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001379 x[i] = borrow & PyLong_MASK;
1380 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001381 borrow &= 1; /* keep only 1 sign bit */
1382 }
1383 for (; borrow && i < m; ++i) {
1384 borrow = x[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001385 x[i] = borrow & PyLong_MASK;
1386 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001387 borrow &= 1;
1388 }
1389 return borrow;
1390}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001391
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001392/* Multiply by a single digit, ignoring the sign. */
1393
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001395mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001396{
1397 return muladd1(a, n, (digit)0);
1398}
1399
1400/* Multiply by a single digit and add a single digit, ignoring the sign. */
1401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001403muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001404{
Christian Heimes90aa7642007-12-19 02:45:37 +00001405 Py_ssize_t size_a = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001406 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001407 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001408 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001409
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001410 if (z == NULL)
1411 return NULL;
1412 for (i = 0; i < size_a; ++i) {
1413 carry += (twodigits)a->ob_digit[i] * n;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001414 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1415 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001416 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001417 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001418 return long_normalize(z);
1419}
1420
Tim Peters212e6142001-07-14 12:23:19 +00001421/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1422 in pout, and returning the remainder. pin and pout point at the LSD.
1423 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001424 _PyLong_Format, but that should be done with great care since longs are
Tim Peters212e6142001-07-14 12:23:19 +00001425 immutable. */
1426
1427static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001428inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001429{
1430 twodigits rem = 0;
1431
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001432 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001433 pin += size;
1434 pout += size;
1435 while (--size >= 0) {
1436 digit hi;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001437 rem = (rem << PyLong_SHIFT) + *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001438 *--pout = hi = (digit)(rem / n);
1439 rem -= hi * n;
1440 }
1441 return (digit)rem;
1442}
1443
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001444/* Divide a long integer by a digit, returning both the quotient
1445 (as function result) and the remainder (through *prem).
1446 The sign of a is ignored; n should not be zero. */
1447
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001448static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001449divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001450{
Christian Heimes90aa7642007-12-19 02:45:37 +00001451 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001452 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001453
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001454 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001455 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001456 if (z == NULL)
1457 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001458 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001459 return long_normalize(z);
1460}
1461
1462/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001463 Return a string object.
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001464 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001465
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001466PyObject *
1467_PyLong_Format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001468{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001469 register PyLongObject *a = (PyLongObject *)aa;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001470 PyObject *str;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001471 Py_ssize_t i, j, sz;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001472 Py_ssize_t size_a;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001473 Py_UNICODE *p;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001474 int bits;
1475 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001476
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477 if (a == NULL || !PyLong_Check(a)) {
1478 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001479 return NULL;
1480 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001481 assert(base >= 2 && base <= 36);
Christian Heimes90aa7642007-12-19 02:45:37 +00001482 size_a = ABS(Py_SIZE(a));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001483
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001484 /* Compute a rough upper bound for the length of the string */
1485 i = base;
1486 bits = 0;
1487 while (i > 1) {
1488 ++bits;
1489 i >>= 1;
1490 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001491 i = 5;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001492 j = size_a*PyLong_SHIFT + bits-1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001493 sz = i + j / bits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001494 if (j / PyLong_SHIFT < size_a || sz < i) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001495 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001496 "int is too large to format");
Thomas Wouters89f507f2006-12-13 04:49:30 +00001497 return NULL;
1498 }
Walter Dörwald1ab83302007-05-18 17:15:44 +00001499 str = PyUnicode_FromUnicode(NULL, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001500 if (str == NULL)
1501 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001502 p = PyUnicode_AS_UNICODE(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001503 *p = '\0';
Christian Heimes90aa7642007-12-19 02:45:37 +00001504 if (Py_SIZE(a) < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001505 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001506
Christian Heimes90aa7642007-12-19 02:45:37 +00001507 if (Py_SIZE(a) == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001508 *--p = '0';
1509 }
1510 else if ((base & (base - 1)) == 0) {
1511 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001512 twodigits accum = 0;
1513 int accumbits = 0; /* # of bits in accum */
1514 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001515 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001516 while ((i >>= 1) > 1)
1517 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001518
1519 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001520 accum |= (twodigits)a->ob_digit[i] << accumbits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001521 accumbits += PyLong_SHIFT;
Tim Peters586b2e32001-07-15 09:11:14 +00001522 assert(accumbits >= basebits);
1523 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001524 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001525 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001526 assert(p > PyUnicode_AS_UNICODE(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001527 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001528 accumbits -= basebits;
1529 accum >>= basebits;
1530 } while (i < size_a-1 ? accumbits >= basebits :
1531 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001532 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001533 }
1534 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001535 /* Not 0, and base not a power of 2. Divide repeatedly by
1536 base, but for speed use the highest power of base that
1537 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001538 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001539 digit *pin = a->ob_digit;
1540 PyLongObject *scratch;
1541 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001542 digit powbase = base; /* powbase == base ** power */
1543 int power = 1;
1544 for (;;) {
1545 unsigned long newpow = powbase * (unsigned long)base;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001546 if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */
Tim Petersfad225f2001-07-13 02:59:26 +00001547 break;
1548 powbase = (digit)newpow;
1549 ++power;
1550 }
Tim Peters212e6142001-07-14 12:23:19 +00001551
1552 /* Get a scratch area for repeated division. */
1553 scratch = _PyLong_New(size);
1554 if (scratch == NULL) {
1555 Py_DECREF(str);
1556 return NULL;
1557 }
1558
1559 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001560 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001561 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001562 digit rem = inplace_divrem1(scratch->ob_digit,
1563 pin, size, powbase);
1564 pin = scratch->ob_digit; /* no need to use a again */
1565 if (pin[size - 1] == 0)
1566 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001567 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001568 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001569 Py_DECREF(str);
1570 return NULL;
1571 })
Tim Peters212e6142001-07-14 12:23:19 +00001572
1573 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001574 assert(ntostore > 0);
1575 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001576 digit nextrem = (digit)(rem / base);
1577 char c = (char)(rem - nextrem * base);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001578 assert(p > PyUnicode_AS_UNICODE(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001579 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001580 *--p = c;
1581 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001582 --ntostore;
1583 /* Termination is a bit delicate: must not
1584 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001585 remaining quotient and rem are both 0. */
1586 } while (ntostore && (size || rem));
1587 } while (size != 0);
1588 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001589 }
1590
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001591 if (base == 16) {
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001592 *--p = 'x';
1593 *--p = '0';
1594 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001595 else if (base == 8) {
1596 *--p = 'o';
1597 *--p = '0';
1598 }
1599 else if (base == 2) {
1600 *--p = 'b';
1601 *--p = '0';
1602 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001603 else if (base != 10) {
1604 *--p = '#';
1605 *--p = '0' + base%10;
1606 if (base > 10)
1607 *--p = '0' + base/10;
1608 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001609 if (sign)
1610 *--p = sign;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001611 if (p != PyUnicode_AS_UNICODE(str)) {
1612 Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001613 assert(p > q);
1614 do {
1615 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001616 q--;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001617 if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) {
1618 Py_DECREF(str);
1619 return NULL;
1620 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001621 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001622 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001623}
1624
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625/* Table of digit values for 8-bit string -> integer conversion.
1626 * '0' maps to 0, ..., '9' maps to 9.
1627 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1628 * All other indices map to 37.
1629 * Note that when converting a base B string, a char c is a legitimate
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001630 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631 */
1632int _PyLong_DigitValue[256] = {
1633 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1634 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1635 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1636 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1637 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1638 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1639 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1640 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1641 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1642 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1643 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1644 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1645 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1646 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1647 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1648 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1649};
1650
1651/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001652 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1653 * non-digit (which may be *str!). A normalized long is returned.
1654 * The point to this routine is that it takes time linear in the number of
1655 * string characters.
1656 */
1657static PyLongObject *
1658long_from_binary_base(char **str, int base)
1659{
1660 char *p = *str;
1661 char *start = p;
1662 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001663 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001664 PyLongObject *z;
1665 twodigits accum;
1666 int bits_in_accum;
1667 digit *pdigit;
1668
1669 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1670 n = base;
1671 for (bits_per_char = -1; n; ++bits_per_char)
1672 n >>= 1;
1673 /* n <- total # of bits needed, while setting p to end-of-string */
1674 n = 0;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001675 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001676 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001677 *str = p;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001678 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1679 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001680 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001681 PyErr_SetString(PyExc_ValueError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001682 "int string too large to convert");
Tim Peters1a3b19a2003-02-02 17:33:53 +00001683 return NULL;
1684 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001685 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001686 z = _PyLong_New(n);
1687 if (z == NULL)
1688 return NULL;
1689 /* Read string from right, and fill in long from left; i.e.,
1690 * from least to most significant in both.
1691 */
1692 accum = 0;
1693 bits_in_accum = 0;
1694 pdigit = z->ob_digit;
1695 while (--p >= start) {
Christian Heimesbbe741d2008-03-28 10:53:29 +00001696 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001697 assert(k >= 0 && k < base);
1698 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001699 bits_in_accum += bits_per_char;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001700 if (bits_in_accum >= PyLong_SHIFT) {
1701 *pdigit++ = (digit)(accum & PyLong_MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001702 assert(pdigit - z->ob_digit <= (int)n);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001703 accum >>= PyLong_SHIFT;
1704 bits_in_accum -= PyLong_SHIFT;
1705 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001706 }
1707 }
1708 if (bits_in_accum) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001709 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001710 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001711 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001712 }
1713 while (pdigit - z->ob_digit < n)
1714 *pdigit++ = 0;
1715 return long_normalize(z);
1716}
1717
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001718PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001719PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001720{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001721 int sign = 1, error_if_nonzero = 0;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001722 char *start, *orig_str = str;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001723 PyLongObject *z = NULL;
Guido van Rossum25236212007-08-22 23:28:23 +00001724 PyObject *strobj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001726
Guido van Rossum472c04f1996-12-05 21:57:21 +00001727 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001728 PyErr_SetString(PyExc_ValueError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001729 "int() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001730 return NULL;
1731 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001732 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001733 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001734 if (*str == '+')
1735 ++str;
1736 else if (*str == '-') {
1737 ++str;
1738 sign = -1;
1739 }
1740 if (base == 0) {
1741 if (str[0] != '0')
1742 base = 10;
1743 else if (str[1] == 'x' || str[1] == 'X')
1744 base = 16;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001745 else if (str[1] == 'o' || str[1] == 'O')
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001746 base = 8;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001747 else if (str[1] == 'b' || str[1] == 'B')
1748 base = 2;
1749 else {
1750 /* "old" (C-style) octal literal, now invalid.
1751 it might still be zero though */
1752 error_if_nonzero = 1;
1753 base = 10;
1754 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001755 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001756 if (str[0] == '0' &&
1757 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1758 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
1759 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001760 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001761
Guido van Rossume6762971998-06-22 03:54:46 +00001762 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001763 if ((base & (base - 1)) == 0)
1764 z = long_from_binary_base(&str, base);
1765 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001766/***
1767Binary bases can be converted in time linear in the number of digits, because
1768Python's representation base is binary. Other bases (including decimal!) use
1769the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001770
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771First some math: the largest integer that can be expressed in N base-B digits
1772is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1773case number of Python digits needed to hold it is the smallest integer n s.t.
1774
1775 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1776 BASE**n >= B**N [taking logs to base BASE]
1777 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1778
1779The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1780this quickly. A Python long with that much space is reserved near the start,
1781and the result is computed into it.
1782
1783The input string is actually treated as being in base base**i (i.e., i digits
1784are processed at a time), where two more static arrays hold:
1785
1786 convwidth_base[base] = the largest integer i such that base**i <= BASE
1787 convmultmax_base[base] = base ** convwidth_base[base]
1788
1789The first of these is the largest i such that i consecutive input digits
1790must fit in a single Python digit. The second is effectively the input
1791base we're really using.
1792
1793Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1794convmultmax_base[base], the result is "simply"
1795
1796 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1797
1798where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001799
1800Error analysis: as above, the number of Python digits `n` needed is worst-
1801case
1802
1803 n >= N * log(B)/log(BASE)
1804
1805where `N` is the number of input digits in base `B`. This is computed via
1806
1807 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1808
1809below. Two numeric concerns are how much space this can waste, and whether
1810the computed result can be too small. To be concrete, assume BASE = 2**15,
1811which is the default (and it's unlikely anyone changes that).
1812
1813Waste isn't a problem: provided the first input digit isn't 0, the difference
1814between the worst-case input with N digits and the smallest input with N
1815digits is about a factor of B, but B is small compared to BASE so at most
1816one allocated Python digit can remain unused on that count. If
1817N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1818and adding 1 returns a result 1 larger than necessary. However, that can't
1819happen: whenever B is a power of 2, long_from_binary_base() is called
1820instead, and it's impossible for B**i to be an integer power of 2**15 when
1821B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1822an exact integer when B is not a power of 2, since B**i has a prime factor
1823other than 2 in that case, but (2**15)**j's only prime factor is 2).
1824
1825The computed result can be too small if the true value of N*log(B)/log(BASE)
1826is a little bit larger than an exact integer, but due to roundoff errors (in
1827computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1828yields a numeric result a little less than that integer. Unfortunately, "how
1829close can a transcendental function get to an integer over some range?"
1830questions are generally theoretically intractable. Computer analysis via
1831continued fractions is practical: expand log(B)/log(BASE) via continued
1832fractions, giving a sequence i/j of "the best" rational approximations. Then
1833j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1834we can get very close to being in trouble, but very rarely. For example,
183576573 is a denominator in one of the continued-fraction approximations to
1836log(10)/log(2**15), and indeed:
1837
1838 >>> log(10)/log(2**15)*76573
1839 16958.000000654003
1840
1841is very close to an integer. If we were working with IEEE single-precision,
1842rounding errors could kill us. Finding worst cases in IEEE double-precision
1843requires better-than-double-precision log() functions, and Tim didn't bother.
1844Instead the code checks to see whether the allocated space is enough as each
1845new Python digit is added, and copies the whole thing to a larger long if not.
1846This should happen extremely rarely, and in fact I don't have a test case
1847that triggers it(!). Instead the code was tested by artificially allocating
1848just 1 digit at the start, so that the copying code was exercised for every
1849digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001850***/
1851 register twodigits c; /* current input character */
1852 Py_ssize_t size_z;
1853 int i;
1854 int convwidth;
1855 twodigits convmultmax, convmult;
1856 digit *pz, *pzstop;
1857 char* scan;
1858
1859 static double log_base_BASE[37] = {0.0e0,};
1860 static int convwidth_base[37] = {0,};
1861 static twodigits convmultmax_base[37] = {0,};
1862
1863 if (log_base_BASE[base] == 0.0) {
1864 twodigits convmax = base;
1865 int i = 1;
1866
1867 log_base_BASE[base] = log((double)base) /
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001868 log((double)PyLong_BASE);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001869 for (;;) {
1870 twodigits next = convmax * base;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001871 if (next > PyLong_BASE)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001872 break;
1873 convmax = next;
1874 ++i;
1875 }
1876 convmultmax_base[base] = convmax;
1877 assert(i > 0);
1878 convwidth_base[base] = i;
1879 }
1880
1881 /* Find length of the string of numeric characters. */
1882 scan = str;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001883 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001884 ++scan;
1885
1886 /* Create a long object that can contain the largest possible
1887 * integer with this base and length. Note that there's no
1888 * need to initialize z->ob_digit -- no slot is read up before
1889 * being stored into.
1890 */
1891 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001892 /* Uncomment next line to test exceedingly rare copy code */
1893 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001894 assert(size_z > 0);
1895 z = _PyLong_New(size_z);
1896 if (z == NULL)
1897 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00001898 Py_SIZE(z) = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001899
1900 /* `convwidth` consecutive input digits are treated as a single
1901 * digit in base `convmultmax`.
1902 */
1903 convwidth = convwidth_base[base];
1904 convmultmax = convmultmax_base[base];
1905
1906 /* Work ;-) */
1907 while (str < scan) {
1908 /* grab up to convwidth digits from the input string */
Christian Heimesbbe741d2008-03-28 10:53:29 +00001909 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001910 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1911 c = (twodigits)(c * base +
Christian Heimesbbe741d2008-03-28 10:53:29 +00001912 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001913 assert(c < PyLong_BASE);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001914 }
1915
1916 convmult = convmultmax;
1917 /* Calculate the shift only if we couldn't get
1918 * convwidth digits.
1919 */
1920 if (i != convwidth) {
1921 convmult = base;
1922 for ( ; i > 1; --i)
1923 convmult *= base;
1924 }
1925
1926 /* Multiply z by convmult, and add c. */
1927 pz = z->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001928 pzstop = pz + Py_SIZE(z);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001929 for (; pz < pzstop; ++pz) {
1930 c += (twodigits)*pz * convmult;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001931 *pz = (digit)(c & PyLong_MASK);
1932 c >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001933 }
1934 /* carry off the current end? */
1935 if (c) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001936 assert(c < PyLong_BASE);
Christian Heimes90aa7642007-12-19 02:45:37 +00001937 if (Py_SIZE(z) < size_z) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001938 *pz = (digit)c;
Christian Heimes90aa7642007-12-19 02:45:37 +00001939 ++Py_SIZE(z);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001940 }
1941 else {
1942 PyLongObject *tmp;
1943 /* Extremely rare. Get more space. */
Christian Heimes90aa7642007-12-19 02:45:37 +00001944 assert(Py_SIZE(z) == size_z);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001945 tmp = _PyLong_New(size_z + 1);
1946 if (tmp == NULL) {
1947 Py_DECREF(z);
1948 return NULL;
1949 }
1950 memcpy(tmp->ob_digit,
1951 z->ob_digit,
1952 sizeof(digit) * size_z);
1953 Py_DECREF(z);
1954 z = tmp;
1955 z->ob_digit[size_z] = (digit)c;
1956 ++size_z;
1957 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001958 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001959 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001960 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001961 if (z == NULL)
1962 return NULL;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001963 if (error_if_nonzero) {
1964 /* reset the base to 0, else the exception message
1965 doesn't make too much sense */
1966 base = 0;
Christian Heimes90aa7642007-12-19 02:45:37 +00001967 if (Py_SIZE(z) != 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001968 goto onError;
1969 /* there might still be other problems, therefore base
1970 remains zero here for the same reason */
1971 }
Guido van Rossum9e896b32000-04-05 20:11:21 +00001972 if (str == start)
1973 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001974 if (sign < 0)
Christian Heimes90aa7642007-12-19 02:45:37 +00001975 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001976 if (*str == 'L' || *str == 'l')
1977 str++;
1978 while (*str && isspace(Py_CHARMASK(*str)))
1979 str++;
1980 if (*str != '\0')
1981 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001982 if (pend)
1983 *pend = str;
Martin v. Löwis029656f2008-06-30 04:06:08 +00001984 long_normalize(z);
1985 if (ABS(Py_SIZE(z)) <= 1) {
1986 long res = MEDIUM_VALUE(z);
1987 if (-NSMALLPOSINTS <= res && res <= NSMALLPOSINTS) {
1988 Py_DECREF(z);
1989 return PyLong_FromLong(res);
1990 }
1991 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001992 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001993
1994 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001995 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001996 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
Guido van Rossum25236212007-08-22 23:28:23 +00001997 strobj = PyUnicode_FromStringAndSize(orig_str, slen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001998 if (strobj == NULL)
1999 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002000 PyErr_Format(PyExc_ValueError,
Guido van Rossum25236212007-08-22 23:28:23 +00002001 "invalid literal for int() with base %d: %R",
2002 base, strobj);
2003 Py_DECREF(strobj);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002004 return NULL;
2005}
2006
2007PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002008PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00002009{
Walter Dörwald07e14762002-11-06 16:15:14 +00002010 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002011 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002012
Walter Dörwald07e14762002-11-06 16:15:14 +00002013 if (buffer == NULL)
2014 return NULL;
2015
2016 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
2017 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002018 return NULL;
2019 }
Walter Dörwald07e14762002-11-06 16:15:14 +00002020 result = PyLong_FromString(buffer, NULL, base);
2021 PyMem_FREE(buffer);
2022 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002023}
2024
Tim Peters9f688bf2000-07-07 15:53:28 +00002025/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002026static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00002027 (PyLongObject *, PyLongObject *, PyLongObject **);
Guido van Rossumb43daf72007-08-01 18:08:08 +00002028static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00002029static int long_divrem(PyLongObject *, PyLongObject *,
2030 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002031
2032/* Long division with remainder, top-level routine */
2033
Guido van Rossume32e0141992-01-19 16:31:05 +00002034static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002035long_divrem(PyLongObject *a, PyLongObject *b,
2036 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002037{
Christian Heimes90aa7642007-12-19 02:45:37 +00002038 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002039 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002040
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002041 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002042 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002043 "integer division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00002044 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002045 }
2046 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00002047 (size_a == size_b &&
2048 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002049 /* |a| < |b|. */
Guido van Rossumddefaf32007-01-14 03:31:43 +00002050 *pdiv = (PyLongObject*)PyLong_FromLong(0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002051 if (*pdiv == NULL)
2052 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002053 Py_INCREF(a);
2054 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00002055 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002056 }
2057 if (size_b == 1) {
2058 digit rem = 0;
2059 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002060 if (z == NULL)
2061 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002062 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002063 if (*prem == NULL) {
2064 Py_DECREF(z);
2065 return -1;
2066 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002067 }
Guido van Rossume32e0141992-01-19 16:31:05 +00002068 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002069 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002070 if (z == NULL)
2071 return -1;
2072 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002073 /* Set the signs.
2074 The quotient z has the sign of a*b;
2075 the remainder r has the sign of a,
2076 so a = b*z + r. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002077 if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
Guido van Rossumddefaf32007-01-14 03:31:43 +00002078 NEGATE(z);
Christian Heimes90aa7642007-12-19 02:45:37 +00002079 if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002080 NEGATE(*prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002081 *pdiv = z;
2082 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002083}
2084
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002085/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002086
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002087static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002088x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002089{
Christian Heimes90aa7642007-12-19 02:45:37 +00002090 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002091 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002092 PyLongObject *v = mul1(v1, d);
2093 PyLongObject *w = mul1(w1, d);
2094 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002095 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002096
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002097 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002098 Py_XDECREF(v);
2099 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002100 return NULL;
2101 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002102
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002103 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimes90aa7642007-12-19 02:45:37 +00002104 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
2105 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002106
Christian Heimes90aa7642007-12-19 02:45:37 +00002107 size_v = ABS(Py_SIZE(v));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002108 k = size_v - size_w;
2109 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002110
Thomas Wouters477c8d52006-05-27 19:21:47 +00002111 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002112 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
2113 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002114 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002115 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002116
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002117 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002118 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002119 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002120 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002121 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002122 if (vj == w->ob_digit[size_w-1])
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002123 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002124 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002125 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002126 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00002127
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002128 while (w->ob_digit[size_w-2]*q >
2129 ((
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002130 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002131 + v->ob_digit[j-1]
2132 - q*w->ob_digit[size_w-1]
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002133 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002134 + v->ob_digit[j-2])
2135 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002136
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002137 for (i = 0; i < size_w && i+k < size_v; ++i) {
2138 twodigits z = w->ob_digit[i] * q;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002139 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002140 carry += v->ob_digit[i+k] - z
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002141 + ((twodigits)zz << PyLong_SHIFT);
2142 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00002143 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002144 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00002145 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002146 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002147
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002148 if (i+k < size_v) {
2149 carry += v->ob_digit[i+k];
2150 v->ob_digit[i+k] = 0;
2151 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002152
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002153 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00002154 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002155 else {
2156 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00002157 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002158 carry = 0;
2159 for (i = 0; i < size_w && i+k < size_v; ++i) {
2160 carry += v->ob_digit[i+k] + w->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002161 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00002162 carry = Py_ARITHMETIC_RIGHT_SHIFT(
2163 BASE_TWODIGITS_TYPE,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002164 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002165 }
2166 }
2167 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002168
Guido van Rossumc206c761995-01-10 15:23:19 +00002169 if (a == NULL)
2170 *prem = NULL;
2171 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00002172 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00002173 *prem = divrem1(v, d, &d);
2174 /* d receives the (unused) remainder */
2175 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002176 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00002177 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002178 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002179 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002180 Py_DECREF(v);
2181 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002182 return a;
2183}
2184
2185/* Methods */
2186
2187static void
Tim Peters9f688bf2000-07-07 15:53:28 +00002188long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002189{
Christian Heimes90aa7642007-12-19 02:45:37 +00002190 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002191}
2192
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002193static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002194long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002195{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002196 return _PyLong_Format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002197}
2198
2199static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002200long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002201{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002202 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002203
Christian Heimes90aa7642007-12-19 02:45:37 +00002204 if (Py_SIZE(a) != Py_SIZE(b)) {
2205 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002206 sign = 0;
2207 else
Christian Heimes90aa7642007-12-19 02:45:37 +00002208 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002209 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002210 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002211 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002212 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2213 ;
2214 if (i < 0)
2215 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002216 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002217 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimes90aa7642007-12-19 02:45:37 +00002218 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002219 sign = -sign;
2220 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002221 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00002222 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002223}
2224
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002225static PyObject *
2226long_richcompare(PyObject *self, PyObject *other, int op)
2227{
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002228 PyObject *result;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002229 CHECK_BINOP(self, other);
2230 result = Py_CmpToRich(op, long_compare((PyLongObject*)self,
2231 (PyLongObject*)other));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002232 return result;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002233}
2234
Guido van Rossum9bfef441993-03-29 10:43:31 +00002235static long
Tim Peters9f688bf2000-07-07 15:53:28 +00002236long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002237{
2238 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002239 Py_ssize_t i;
2240 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002241
2242 /* This is designed so that Python ints and longs with the
2243 same value hash to the same value, otherwise comparisons
2244 of mapping keys will turn out weird */
Christian Heimes90aa7642007-12-19 02:45:37 +00002245 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002246 switch(i) {
2247 case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0];
2248 case 0: return 0;
2249 case 1: return v->ob_digit[0];
2250 }
Guido van Rossum9bfef441993-03-29 10:43:31 +00002251 sign = 1;
2252 x = 0;
2253 if (i < 0) {
2254 sign = -1;
2255 i = -(i);
2256 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002257#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Thomas Woutersce272b62007-09-19 21:19:28 +00002258 /* The following loop produces a C long x such that (unsigned long)x
2259 is congruent to the absolute value of v modulo ULONG_MAX. The
2260 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002261 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00002262 /* Force a native long #-bits (32 or 64) circular shift */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002263 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002264 x += v->ob_digit[i];
Thomas Woutersce272b62007-09-19 21:19:28 +00002265 /* If the addition above overflowed (thinking of x as
2266 unsigned), we compensate by incrementing. This preserves
2267 the value modulo ULONG_MAX. */
2268 if ((unsigned long)x < v->ob_digit[i])
2269 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002270 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002271#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00002272 x = x * sign;
2273 if (x == -1)
2274 x = -2;
2275 return x;
2276}
2277
2278
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002279/* Add the absolute values of two long integers. */
2280
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002281static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002282x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002283{
Christian Heimes90aa7642007-12-19 02:45:37 +00002284 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002285 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002286 int i;
2287 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002288
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002289 /* Ensure a is the larger of the two: */
2290 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002291 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002292 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002293 size_a = size_b;
2294 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002296 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002297 if (z == NULL)
2298 return NULL;
2299 for (i = 0; i < size_b; ++i) {
2300 carry += a->ob_digit[i] + b->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002301 z->ob_digit[i] = carry & PyLong_MASK;
2302 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002303 }
2304 for (; i < size_a; ++i) {
2305 carry += a->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002306 z->ob_digit[i] = carry & PyLong_MASK;
2307 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002308 }
2309 z->ob_digit[i] = carry;
2310 return long_normalize(z);
2311}
2312
2313/* Subtract the absolute values of two integers. */
2314
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002315static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002316x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002317{
Christian Heimes90aa7642007-12-19 02:45:37 +00002318 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002319 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002320 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002321 int sign = 1;
2322 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002323
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002324 /* Ensure a is the larger of the two: */
2325 if (size_a < size_b) {
2326 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002327 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002328 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002329 size_a = size_b;
2330 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002331 }
2332 else if (size_a == size_b) {
2333 /* Find highest digit where a and b differ: */
2334 i = size_a;
2335 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2336 ;
2337 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002338 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002339 if (a->ob_digit[i] < b->ob_digit[i]) {
2340 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002341 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002342 }
2343 size_a = size_b = i+1;
2344 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002345 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002346 if (z == NULL)
2347 return NULL;
2348 for (i = 0; i < size_b; ++i) {
2349 /* The following assumes unsigned arithmetic
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002350 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002351 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002352 z->ob_digit[i] = borrow & PyLong_MASK;
2353 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002354 borrow &= 1; /* Keep only one sign bit */
2355 }
2356 for (; i < size_a; ++i) {
2357 borrow = a->ob_digit[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002358 z->ob_digit[i] = borrow & PyLong_MASK;
2359 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002360 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002361 }
2362 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002363 if (sign < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002364 NEGATE(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002365 return long_normalize(z);
2366}
2367
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002368static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002369long_add(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002370{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002371 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002372
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002373 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002374
Christian Heimes90aa7642007-12-19 02:45:37 +00002375 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Christian Heimes217cfd12007-12-02 14:31:20 +00002376 PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) +
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002377 MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002378 return result;
2379 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002380 if (Py_SIZE(a) < 0) {
2381 if (Py_SIZE(b) < 0) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002382 z = x_add(a, b);
Christian Heimes90aa7642007-12-19 02:45:37 +00002383 if (z != NULL && Py_SIZE(z) != 0)
2384 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002385 }
2386 else
2387 z = x_sub(b, a);
2388 }
2389 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002390 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002391 z = x_sub(a, b);
2392 else
2393 z = x_add(a, b);
2394 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002395 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002396}
2397
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002398static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002399long_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002400{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002401 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002402
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002403 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002404
Christian Heimes90aa7642007-12-19 02:45:37 +00002405 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002406 PyObject* r;
2407 r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002408 return r;
2409 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002410 if (Py_SIZE(a) < 0) {
2411 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002412 z = x_sub(a, b);
2413 else
2414 z = x_add(a, b);
Christian Heimes90aa7642007-12-19 02:45:37 +00002415 if (z != NULL && Py_SIZE(z) != 0)
2416 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002417 }
2418 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002419 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002420 z = x_add(a, b);
2421 else
2422 z = x_sub(a, b);
2423 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002424 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002425}
2426
Tim Peters5af4e6c2002-08-12 02:31:19 +00002427/* Grade school multiplication, ignoring the signs.
2428 * Returns the absolute value of the product, or NULL if error.
2429 */
2430static PyLongObject *
2431x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002432{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002433 PyLongObject *z;
Christian Heimes90aa7642007-12-19 02:45:37 +00002434 Py_ssize_t size_a = ABS(Py_SIZE(a));
2435 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002436 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002437
Tim Peters5af4e6c2002-08-12 02:31:19 +00002438 z = _PyLong_New(size_a + size_b);
2439 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002440 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002441
Christian Heimes90aa7642007-12-19 02:45:37 +00002442 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002443 if (a == b) {
2444 /* Efficient squaring per HAC, Algorithm 14.16:
2445 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2446 * Gives slightly less than a 2x speedup when a == b,
2447 * via exploiting that each entry in the multiplication
2448 * pyramid appears twice (except for the size_a squares).
2449 */
2450 for (i = 0; i < size_a; ++i) {
2451 twodigits carry;
2452 twodigits f = a->ob_digit[i];
2453 digit *pz = z->ob_digit + (i << 1);
2454 digit *pa = a->ob_digit + i + 1;
2455 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002456
Tim Peters0973b992004-08-29 22:16:50 +00002457 SIGCHECK({
2458 Py_DECREF(z);
2459 return NULL;
2460 })
2461
2462 carry = *pz + f * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002463 *pz++ = (digit)(carry & PyLong_MASK);
2464 carry >>= PyLong_SHIFT;
2465 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002466
2467 /* Now f is added in twice in each column of the
2468 * pyramid it appears. Same as adding f<<1 once.
2469 */
2470 f <<= 1;
2471 while (pa < paend) {
2472 carry += *pz + *pa++ * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002473 *pz++ = (digit)(carry & PyLong_MASK);
2474 carry >>= PyLong_SHIFT;
2475 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002476 }
2477 if (carry) {
2478 carry += *pz;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002479 *pz++ = (digit)(carry & PyLong_MASK);
2480 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002481 }
2482 if (carry)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002483 *pz += (digit)(carry & PyLong_MASK);
2484 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002485 }
Tim Peters0973b992004-08-29 22:16:50 +00002486 }
2487 else { /* a is not the same as b -- gradeschool long mult */
2488 for (i = 0; i < size_a; ++i) {
2489 twodigits carry = 0;
2490 twodigits f = a->ob_digit[i];
2491 digit *pz = z->ob_digit + i;
2492 digit *pb = b->ob_digit;
2493 digit *pbend = b->ob_digit + size_b;
2494
2495 SIGCHECK({
2496 Py_DECREF(z);
2497 return NULL;
2498 })
2499
2500 while (pb < pbend) {
2501 carry += *pz + *pb++ * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002502 *pz++ = (digit)(carry & PyLong_MASK);
2503 carry >>= PyLong_SHIFT;
2504 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002505 }
2506 if (carry)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002507 *pz += (digit)(carry & PyLong_MASK);
2508 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002509 }
2510 }
Tim Peters44121a62002-08-12 06:17:58 +00002511 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002512}
2513
2514/* A helper for Karatsuba multiplication (k_mul).
2515 Takes a long "n" and an integer "size" representing the place to
2516 split, and sets low and high such that abs(n) == (high << size) + low,
2517 viewing the shift as being by digits. The sign bit is ignored, and
2518 the return values are >= 0.
2519 Returns 0 on success, -1 on failure.
2520*/
2521static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002522kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002523{
2524 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002525 Py_ssize_t size_lo, size_hi;
Christian Heimes90aa7642007-12-19 02:45:37 +00002526 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002527
2528 size_lo = MIN(size_n, size);
2529 size_hi = size_n - size_lo;
2530
2531 if ((hi = _PyLong_New(size_hi)) == NULL)
2532 return -1;
2533 if ((lo = _PyLong_New(size_lo)) == NULL) {
2534 Py_DECREF(hi);
2535 return -1;
2536 }
2537
2538 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2539 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2540
2541 *high = long_normalize(hi);
2542 *low = long_normalize(lo);
2543 return 0;
2544}
2545
Tim Peters60004642002-08-12 22:01:34 +00002546static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2547
Tim Peters5af4e6c2002-08-12 02:31:19 +00002548/* Karatsuba multiplication. Ignores the input signs, and returns the
2549 * absolute value of the product (or NULL if error).
2550 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2551 */
2552static PyLongObject *
2553k_mul(PyLongObject *a, PyLongObject *b)
2554{
Christian Heimes90aa7642007-12-19 02:45:37 +00002555 Py_ssize_t asize = ABS(Py_SIZE(a));
2556 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002557 PyLongObject *ah = NULL;
2558 PyLongObject *al = NULL;
2559 PyLongObject *bh = NULL;
2560 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002561 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002562 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002563 Py_ssize_t shift; /* the number of digits we split off */
2564 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002565
Tim Peters5af4e6c2002-08-12 02:31:19 +00002566 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2567 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2568 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002569 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002570 * By picking X to be a power of 2, "*X" is just shifting, and it's
2571 * been reduced to 3 multiplies on numbers half the size.
2572 */
2573
Tim Petersfc07e562002-08-12 02:54:10 +00002574 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002575 * is largest.
2576 */
Tim Peters738eda72002-08-12 15:08:20 +00002577 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002578 t1 = a;
2579 a = b;
2580 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002581
2582 i = asize;
2583 asize = bsize;
2584 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002585 }
2586
2587 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002588 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2589 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002590 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002591 return _PyLong_New(0);
2592 else
2593 return x_mul(a, b);
2594 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002595
Tim Peters60004642002-08-12 22:01:34 +00002596 /* If a is small compared to b, splitting on b gives a degenerate
2597 * case with ah==0, and Karatsuba may be (even much) less efficient
2598 * than "grade school" then. However, we can still win, by viewing
2599 * b as a string of "big digits", each of width a->ob_size. That
2600 * leads to a sequence of balanced calls to k_mul.
2601 */
2602 if (2 * asize <= bsize)
2603 return k_lopsided_mul(a, b);
2604
Tim Petersd6974a52002-08-13 20:37:51 +00002605 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002606 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002607 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002608 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002609
Tim Peters0973b992004-08-29 22:16:50 +00002610 if (a == b) {
2611 bh = ah;
2612 bl = al;
2613 Py_INCREF(bh);
2614 Py_INCREF(bl);
2615 }
2616 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002617
Tim Petersd64c1de2002-08-12 17:36:03 +00002618 /* The plan:
2619 * 1. Allocate result space (asize + bsize digits: that's always
2620 * enough).
2621 * 2. Compute ah*bh, and copy into result at 2*shift.
2622 * 3. Compute al*bl, and copy into result at 0. Note that this
2623 * can't overlap with #2.
2624 * 4. Subtract al*bl from the result, starting at shift. This may
2625 * underflow (borrow out of the high digit), but we don't care:
2626 * we're effectively doing unsigned arithmetic mod
2627 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2628 * borrows and carries out of the high digit can be ignored.
2629 * 5. Subtract ah*bh from the result, starting at shift.
2630 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2631 * at shift.
2632 */
2633
2634 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002635 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002636 if (ret == NULL) goto fail;
2637#ifdef Py_DEBUG
2638 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002639 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002640#endif
Tim Peters44121a62002-08-12 06:17:58 +00002641
Tim Petersd64c1de2002-08-12 17:36:03 +00002642 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002643 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002644 assert(Py_SIZE(t1) >= 0);
2645 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002646 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimes90aa7642007-12-19 02:45:37 +00002647 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002648
2649 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002650 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002651 if (i)
Christian Heimes90aa7642007-12-19 02:45:37 +00002652 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002653 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002654
Tim Petersd64c1de2002-08-12 17:36:03 +00002655 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002656 if ((t2 = k_mul(al, bl)) == NULL) {
2657 Py_DECREF(t1);
2658 goto fail;
2659 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002660 assert(Py_SIZE(t2) >= 0);
2661 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2662 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002663
2664 /* Zero out remaining digits. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002665 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002666 if (i)
Christian Heimes90aa7642007-12-19 02:45:37 +00002667 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002668
Tim Petersd64c1de2002-08-12 17:36:03 +00002669 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2670 * because it's fresher in cache.
2671 */
Christian Heimes90aa7642007-12-19 02:45:37 +00002672 i = Py_SIZE(ret) - shift; /* # digits after shift */
2673 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002674 Py_DECREF(t2);
2675
Christian Heimes90aa7642007-12-19 02:45:37 +00002676 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002677 Py_DECREF(t1);
2678
Tim Petersd64c1de2002-08-12 17:36:03 +00002679 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002680 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2681 Py_DECREF(ah);
2682 Py_DECREF(al);
2683 ah = al = NULL;
2684
Tim Peters0973b992004-08-29 22:16:50 +00002685 if (a == b) {
2686 t2 = t1;
2687 Py_INCREF(t2);
2688 }
2689 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002690 Py_DECREF(t1);
2691 goto fail;
2692 }
2693 Py_DECREF(bh);
2694 Py_DECREF(bl);
2695 bh = bl = NULL;
2696
Tim Peters738eda72002-08-12 15:08:20 +00002697 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002698 Py_DECREF(t1);
2699 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002700 if (t3 == NULL) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002701 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002702
Tim Petersd6974a52002-08-13 20:37:51 +00002703 /* Add t3. It's not obvious why we can't run out of room here.
2704 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002705 */
Christian Heimes90aa7642007-12-19 02:45:37 +00002706 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002707 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002708
Tim Peters5af4e6c2002-08-12 02:31:19 +00002709 return long_normalize(ret);
2710
2711 fail:
2712 Py_XDECREF(ret);
2713 Py_XDECREF(ah);
2714 Py_XDECREF(al);
2715 Py_XDECREF(bh);
2716 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002717 return NULL;
2718}
2719
Tim Petersd6974a52002-08-13 20:37:51 +00002720/* (*) Why adding t3 can't "run out of room" above.
2721
Tim Petersab86c2b2002-08-15 20:06:00 +00002722Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2723to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002724
Tim Petersab86c2b2002-08-15 20:06:00 +000027251. For any integer i, i = c(i/2) + f(i/2). In particular,
2726 bsize = c(bsize/2) + f(bsize/2).
27272. shift = f(bsize/2)
27283. asize <= bsize
27294. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2730 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002731
Tim Petersab86c2b2002-08-15 20:06:00 +00002732We allocated asize + bsize result digits, and add t3 into them at an offset
2733of shift. This leaves asize+bsize-shift allocated digit positions for t3
2734to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2735asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002736
Tim Petersab86c2b2002-08-15 20:06:00 +00002737bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2738at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002739
Tim Petersab86c2b2002-08-15 20:06:00 +00002740If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2741digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2742most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002743
Tim Petersab86c2b2002-08-15 20:06:00 +00002744The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002745
Tim Petersab86c2b2002-08-15 20:06:00 +00002746 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002747
Tim Petersab86c2b2002-08-15 20:06:00 +00002748and we have asize + c(bsize/2) available digit positions. We need to show
2749this is always enough. An instance of c(bsize/2) cancels out in both, so
2750the question reduces to whether asize digits is enough to hold
2751(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2752then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2753asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002754digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002755asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002756c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2757is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2758bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002759
Tim Peters48d52c02002-08-14 17:07:32 +00002760Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2761clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2762ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002763*/
2764
Tim Peters60004642002-08-12 22:01:34 +00002765/* b has at least twice the digits of a, and a is big enough that Karatsuba
2766 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2767 * of slices, each with a->ob_size digits, and multiply the slices by a,
2768 * one at a time. This gives k_mul balanced inputs to work with, and is
2769 * also cache-friendly (we compute one double-width slice of the result
2770 * at a time, then move on, never bactracking except for the helpful
2771 * single-width slice overlap between successive partial sums).
2772 */
2773static PyLongObject *
2774k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2775{
Christian Heimes90aa7642007-12-19 02:45:37 +00002776 const Py_ssize_t asize = ABS(Py_SIZE(a));
2777 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002778 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002779 PyLongObject *ret;
2780 PyLongObject *bslice = NULL;
2781
2782 assert(asize > KARATSUBA_CUTOFF);
2783 assert(2 * asize <= bsize);
2784
2785 /* Allocate result space, and zero it out. */
2786 ret = _PyLong_New(asize + bsize);
2787 if (ret == NULL)
2788 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00002789 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002790
2791 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002792 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002793 if (bslice == NULL)
2794 goto fail;
2795
2796 nbdone = 0;
2797 while (bsize > 0) {
2798 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002799 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002800
2801 /* Multiply the next slice of b by a. */
2802 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2803 nbtouse * sizeof(digit));
Christian Heimes90aa7642007-12-19 02:45:37 +00002804 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002805 product = k_mul(a, bslice);
2806 if (product == NULL)
2807 goto fail;
2808
2809 /* Add into result. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002810 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2811 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002812 Py_DECREF(product);
2813
2814 bsize -= nbtouse;
2815 nbdone += nbtouse;
2816 }
2817
2818 Py_DECREF(bslice);
2819 return long_normalize(ret);
2820
2821 fail:
2822 Py_DECREF(ret);
2823 Py_XDECREF(bslice);
2824 return NULL;
2825}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002826
2827static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002828long_mul(PyLongObject *a, PyLongObject *b)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002829{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002830 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002831
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002832 CHECK_BINOP(a, b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002833
Christian Heimes90aa7642007-12-19 02:45:37 +00002834 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002835 PyObject *r;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002836 r = PyLong_FromLong(MEDIUM_VALUE(a)*MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002837 return r;
2838 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00002839
Tim Petersd64c1de2002-08-12 17:36:03 +00002840 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002841 /* Negate if exactly one of the inputs is negative. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002842 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002843 NEGATE(z);
Tim Peters9973d742002-08-15 19:41:06 +00002844 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002845}
2846
Guido van Rossume32e0141992-01-19 16:31:05 +00002847/* The / and % operators are now defined in terms of divmod().
2848 The expression a mod b has the value a - b*floor(a/b).
2849 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002850 |a| by |b|, with the sign of a. This is also expressed
2851 as a - b*trunc(a/b), if trunc truncates towards zero.
2852 Some examples:
2853 a b a rem b a mod b
2854 13 10 3 3
2855 -13 10 -3 7
2856 13 -10 3 -7
2857 -13 -10 -3 -3
2858 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002859 have different signs. We then subtract one from the 'div'
2860 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002861
Tim Peters47e52ee2004-08-30 02:44:38 +00002862/* Compute
2863 * *pdiv, *pmod = divmod(v, w)
2864 * NULL can be passed for pdiv or pmod, in which case that part of
2865 * the result is simply thrown away. The caller owns a reference to
2866 * each of these it requests (does not pass NULL for).
2867 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002868static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002869l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002870 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002871{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002872 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002873
Guido van Rossume32e0141992-01-19 16:31:05 +00002874 if (long_divrem(v, w, &div, &mod) < 0)
2875 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00002876 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2877 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002878 PyLongObject *temp;
2879 PyLongObject *one;
2880 temp = (PyLongObject *) long_add(mod, w);
2881 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002882 mod = temp;
2883 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002884 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002885 return -1;
2886 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002887 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002888 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002889 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2890 Py_DECREF(mod);
2891 Py_DECREF(div);
2892 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002893 return -1;
2894 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002895 Py_DECREF(one);
2896 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002897 div = temp;
2898 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002899 if (pdiv != NULL)
2900 *pdiv = div;
2901 else
2902 Py_DECREF(div);
2903
2904 if (pmod != NULL)
2905 *pmod = mod;
2906 else
2907 Py_DECREF(mod);
2908
Guido van Rossume32e0141992-01-19 16:31:05 +00002909 return 0;
2910}
2911
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002912static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002913long_div(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00002914{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002915 PyLongObject *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002916
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002917 CHECK_BINOP(a, b);
2918 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
Tim Peters47e52ee2004-08-30 02:44:38 +00002919 div = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002920 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002921}
2922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002923static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002924long_true_divide(PyObject *a, PyObject *b)
Tim Peters20dab9f2001-09-04 05:31:47 +00002925{
Tim Peterse2a60002001-09-04 06:17:36 +00002926 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002927 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002928
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002929 CHECK_BINOP(a, b);
Tim Peterse2a60002001-09-04 06:17:36 +00002930 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2931 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002932 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
Tim Peters6f97e492001-11-04 23:09:40 +00002933 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002934 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002935 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2936 but should really be set correctly after sucessful calls to
2937 _PyLong_AsScaledDouble() */
2938 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002939
2940 if (bd == 0.0) {
2941 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002942 "int division or modulo by zero");
Tim Peterse2a60002001-09-04 06:17:36 +00002943 return NULL;
2944 }
2945
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002946 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002947 ad /= bd; /* overflow/underflow impossible here */
2948 aexp -= bexp;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002949 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002950 goto overflow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002951 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002952 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002953 errno = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002954 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002955 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002956 goto overflow;
2957 return PyFloat_FromDouble(ad);
2958
2959overflow:
2960 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002961 "int/int too large for a float");
Tim Peterse2a60002001-09-04 06:17:36 +00002962 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002963
Tim Peters20dab9f2001-09-04 05:31:47 +00002964}
2965
2966static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002967long_mod(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00002968{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002969 PyLongObject *mod;
2970
2971 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002972
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002973 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
Tim Peters47e52ee2004-08-30 02:44:38 +00002974 mod = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002975 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002976}
2977
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002978static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002979long_divmod(PyObject *a, PyObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002980{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002981 PyLongObject *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002982 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002983
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002984 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002985
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002986 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002987 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002988 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002989 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002990 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002991 PyTuple_SetItem(z, 0, (PyObject *) div);
2992 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002993 }
2994 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002995 Py_DECREF(div);
2996 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002997 }
2998 return z;
2999}
3000
Tim Peters47e52ee2004-08-30 02:44:38 +00003001/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003002static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003003long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003004{
Tim Peters47e52ee2004-08-30 02:44:38 +00003005 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
3006 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003007
Tim Peters47e52ee2004-08-30 02:44:38 +00003008 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00003009 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00003010 PyLongObject *temp = NULL;
3011
3012 /* 5-ary values. If the exponent is large enough, table is
3013 * precomputed so that table[i] == a**i % c for i in range(32).
3014 */
3015 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3016 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
3017
3018 /* a, b, c = v, w, x */
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003019 CHECK_BINOP(v, w);
3020 a = (PyLongObject*)v; Py_INCREF(a);
3021 b = (PyLongObject*)w; Py_INCREF(b);
Tim Peters47e52ee2004-08-30 02:44:38 +00003022 if (PyLong_Check(x)) {
3023 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003024 Py_INCREF(x);
3025 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003026 else if (x == Py_None)
3027 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003028 else {
3029 Py_DECREF(a);
3030 Py_DECREF(b);
3031 Py_INCREF(Py_NotImplemented);
3032 return Py_NotImplemented;
3033 }
Tim Peters4c483c42001-09-05 06:24:58 +00003034
Christian Heimes90aa7642007-12-19 02:45:37 +00003035 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00003036 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00003037 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00003038 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00003039 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00003040 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003041 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00003042 /* else return a float. This works because we know
3043 that this calls float_pow() which converts its
3044 arguments to double. */
3045 Py_DECREF(a);
3046 Py_DECREF(b);
3047 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003048 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003049 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003050
3051 if (c) {
3052 /* if modulus == 0:
3053 raise ValueError() */
Christian Heimes90aa7642007-12-19 02:45:37 +00003054 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003055 PyErr_SetString(PyExc_ValueError,
3056 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00003057 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003058 }
3059
3060 /* if modulus < 0:
3061 negativeOutput = True
3062 modulus = -modulus */
Christian Heimes90aa7642007-12-19 02:45:37 +00003063 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003064 negativeOutput = 1;
3065 temp = (PyLongObject *)_PyLong_Copy(c);
3066 if (temp == NULL)
3067 goto Error;
3068 Py_DECREF(c);
3069 c = temp;
3070 temp = NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003071 NEGATE(c);
Tim Peters47e52ee2004-08-30 02:44:38 +00003072 }
3073
3074 /* if modulus == 1:
3075 return 0 */
Christian Heimes90aa7642007-12-19 02:45:37 +00003076 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003077 z = (PyLongObject *)PyLong_FromLong(0L);
3078 goto Done;
3079 }
3080
3081 /* if base < 0:
3082 base = base % modulus
3083 Having the base positive just makes things easier. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003084 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003085 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00003086 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003087 Py_DECREF(a);
3088 a = temp;
3089 temp = NULL;
3090 }
3091 }
3092
3093 /* At this point a, b, and c are guaranteed non-negative UNLESS
3094 c is NULL, in which case a may be negative. */
3095
3096 z = (PyLongObject *)PyLong_FromLong(1L);
3097 if (z == NULL)
3098 goto Error;
3099
3100 /* Perform a modular reduction, X = X % c, but leave X alone if c
3101 * is NULL.
3102 */
3103#define REDUCE(X) \
3104 if (c != NULL) { \
3105 if (l_divmod(X, c, NULL, &temp) < 0) \
3106 goto Error; \
3107 Py_XDECREF(X); \
3108 X = temp; \
3109 temp = NULL; \
3110 }
3111
3112 /* Multiply two values, then reduce the result:
3113 result = X*Y % c. If c is NULL, skip the mod. */
3114#define MULT(X, Y, result) \
3115{ \
3116 temp = (PyLongObject *)long_mul(X, Y); \
3117 if (temp == NULL) \
3118 goto Error; \
3119 Py_XDECREF(result); \
3120 result = temp; \
3121 temp = NULL; \
3122 REDUCE(result) \
3123}
3124
Christian Heimes90aa7642007-12-19 02:45:37 +00003125 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003126 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
3127 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimes90aa7642007-12-19 02:45:37 +00003128 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003129 digit bi = b->ob_digit[i];
3130
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003131 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003132 MULT(z, z, z)
3133 if (bi & j)
3134 MULT(z, a, z)
3135 }
3136 }
3137 }
3138 else {
3139 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
3140 Py_INCREF(z); /* still holds 1L */
3141 table[0] = z;
3142 for (i = 1; i < 32; ++i)
3143 MULT(table[i-1], a, table[i])
3144
Christian Heimes90aa7642007-12-19 02:45:37 +00003145 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003146 const digit bi = b->ob_digit[i];
3147
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003148 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003149 const int index = (bi >> j) & 0x1f;
3150 for (k = 0; k < 5; ++k)
3151 MULT(z, z, z)
3152 if (index)
3153 MULT(z, table[index], z)
3154 }
3155 }
3156 }
3157
Christian Heimes90aa7642007-12-19 02:45:37 +00003158 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003159 temp = (PyLongObject *)long_sub(z, c);
3160 if (temp == NULL)
3161 goto Error;
3162 Py_DECREF(z);
3163 z = temp;
3164 temp = NULL;
3165 }
3166 goto Done;
3167
3168 Error:
3169 if (z != NULL) {
3170 Py_DECREF(z);
3171 z = NULL;
3172 }
3173 /* fall through */
3174 Done:
Christian Heimes90aa7642007-12-19 02:45:37 +00003175 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003176 for (i = 0; i < 32; ++i)
3177 Py_XDECREF(table[i]);
3178 }
Tim Petersde7990b2005-07-17 23:45:23 +00003179 Py_DECREF(a);
3180 Py_DECREF(b);
3181 Py_XDECREF(c);
3182 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003183 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003184}
3185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003186static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003187long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003188{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003189 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003190 PyLongObject *x;
3191 PyLongObject *w;
Christian Heimes90aa7642007-12-19 02:45:37 +00003192 if (ABS(Py_SIZE(v)) <=1)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003193 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003194 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003195 if (w == NULL)
3196 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003197 x = (PyLongObject *) long_add(v, w);
3198 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003199 if (x == NULL)
3200 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00003201 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003202 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003203}
3204
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003205static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003206long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003207{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003208 PyLongObject *z;
Christian Heimes90aa7642007-12-19 02:45:37 +00003209 if (ABS(Py_SIZE(v)) <= 1)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003210 return PyLong_FromLong(-MEDIUM_VALUE(v));
Tim Peters69c2de32001-09-11 22:31:33 +00003211 z = (PyLongObject *)_PyLong_Copy(v);
3212 if (z != NULL)
Christian Heimes90aa7642007-12-19 02:45:37 +00003213 Py_SIZE(z) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003214 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003215}
3216
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003217static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003218long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003219{
Christian Heimes90aa7642007-12-19 02:45:37 +00003220 if (Py_SIZE(v) < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003221 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00003222 else
Guido van Rossumb43daf72007-08-01 18:08:08 +00003223 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003224}
3225
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003226static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00003227long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003228{
Christian Heimes90aa7642007-12-19 02:45:37 +00003229 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003230}
3231
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003232static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003233long_rshift(PyLongObject *a, PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003234{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003235 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003236 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003237 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003238 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003239
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003240 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003241
Christian Heimes90aa7642007-12-19 02:45:37 +00003242 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003243 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003244 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003245 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003246 if (a1 == NULL)
3247 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003248 a2 = (PyLongObject *) long_rshift(a1, b);
3249 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003250 if (a2 == NULL)
3251 goto rshift_error;
3252 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003253 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003254 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003255 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00003256
Neil Schemenauerba872e22001-01-04 01:46:03 +00003257 shiftby = PyLong_AsLong((PyObject *)b);
3258 if (shiftby == -1L && PyErr_Occurred())
3259 goto rshift_error;
3260 if (shiftby < 0) {
3261 PyErr_SetString(PyExc_ValueError,
3262 "negative shift count");
3263 goto rshift_error;
3264 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003265 wordshift = shiftby / PyLong_SHIFT;
Christian Heimes90aa7642007-12-19 02:45:37 +00003266 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003267 if (newsize <= 0) {
3268 z = _PyLong_New(0);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003269 return (PyObject *)z;
3270 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003271 loshift = shiftby % PyLong_SHIFT;
3272 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003273 lomask = ((digit)1 << hishift) - 1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003274 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003275 z = _PyLong_New(newsize);
3276 if (z == NULL)
3277 goto rshift_error;
Christian Heimes90aa7642007-12-19 02:45:37 +00003278 if (Py_SIZE(a) < 0)
3279 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003280 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3281 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3282 if (i+1 < newsize)
3283 z->ob_digit[i] |=
3284 (a->ob_digit[j+1] << hishift) & himask;
3285 }
3286 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003287 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003288rshift_error:
Neil Schemenauerba872e22001-01-04 01:46:03 +00003289 return (PyObject *) z;
3290
Guido van Rossumc6913e71991-11-19 20:26:46 +00003291}
3292
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003293static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003294long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003295{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003296 /* This version due to Tim Peters */
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003297 PyLongObject *a = (PyLongObject*)v;
3298 PyLongObject *b = (PyLongObject*)w;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003299 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003300 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003301 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003302 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003303
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003304 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003305
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003306 shiftby = PyLong_AsLong((PyObject *)b);
3307 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003308 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003309 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003310 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003311 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003312 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003313 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003314 PyErr_SetString(PyExc_ValueError,
3315 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003316 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003317 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003318 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3319 wordshift = (int)shiftby / PyLong_SHIFT;
3320 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003321
Christian Heimes90aa7642007-12-19 02:45:37 +00003322 oldsize = ABS(Py_SIZE(a));
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003323 newsize = oldsize + wordshift;
3324 if (remshift)
3325 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003326 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003327 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003328 goto lshift_error;
Christian Heimes90aa7642007-12-19 02:45:37 +00003329 if (Py_SIZE(a) < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003330 NEGATE(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003331 for (i = 0; i < wordshift; i++)
3332 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003333 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003334 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003335 accum |= (twodigits)a->ob_digit[j] << remshift;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003336 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3337 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003338 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003339 if (remshift)
3340 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003341 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003342 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003343 z = long_normalize(z);
3344lshift_error:
Neil Schemenauerba872e22001-01-04 01:46:03 +00003345 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003346}
3347
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003348
3349/* Bitwise and/xor/or operations */
3350
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003351static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003352long_bitwise(PyLongObject *a,
3353 int op, /* '&', '|', '^' */
3354 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003355{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003356 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003357 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003358 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003359 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003360 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003361 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003362 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003363
Christian Heimes90aa7642007-12-19 02:45:37 +00003364 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003365 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003366 if (a == NULL)
3367 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003368 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003369 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003370 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003371 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003372 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003373 }
Christian Heimes90aa7642007-12-19 02:45:37 +00003374 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003375 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003376 if (b == NULL) {
3377 Py_DECREF(a);
3378 return NULL;
3379 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003380 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003381 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003382 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003383 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003384 maskb = 0;
3385 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003386
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003387 negz = 0;
3388 switch (op) {
3389 case '^':
3390 if (maska != maskb) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003391 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003392 negz = -1;
3393 }
3394 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003395 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003396 if (maska && maskb) {
3397 op = '|';
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003398 maska ^= PyLong_MASK;
3399 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003400 negz = -1;
3401 }
3402 break;
3403 case '|':
3404 if (maska || maskb) {
3405 op = '&';
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003406 maska ^= PyLong_MASK;
3407 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003408 negz = -1;
3409 }
3410 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003411 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003412
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003413 /* JRH: The original logic here was to allocate the result value (z)
3414 as the longer of the two operands. However, there are some cases
3415 where the result is guaranteed to be shorter than that: AND of two
3416 positives, OR of two negatives: use the shorter number. AND with
3417 mixed signs: use the positive number. OR with mixed signs: use the
3418 negative number. After the transformations above, op will be '&'
3419 iff one of these cases applies, and mask will be non-0 for operands
3420 whose length should be ignored.
3421 */
3422
Christian Heimes90aa7642007-12-19 02:45:37 +00003423 size_a = Py_SIZE(a);
3424 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003425 size_z = op == '&'
3426 ? (maska
3427 ? size_b
3428 : (maskb ? size_a : MIN(size_a, size_b)))
3429 : MAX(size_a, size_b);
3430 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003431 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003432 Py_DECREF(a);
3433 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003434 return NULL;
3435 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003436
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003437 for (i = 0; i < size_z; ++i) {
3438 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3439 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3440 switch (op) {
3441 case '&': z->ob_digit[i] = diga & digb; break;
3442 case '|': z->ob_digit[i] = diga | digb; break;
3443 case '^': z->ob_digit[i] = diga ^ digb; break;
3444 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003445 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003446
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003447 Py_DECREF(a);
3448 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003449 z = long_normalize(z);
3450 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003451 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003452 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003453 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003454 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003455}
3456
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003457static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003458long_and(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003459{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003460 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003461 CHECK_BINOP(a, b);
3462 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003463 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003464}
3465
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003466static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003467long_xor(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003468{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003469 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003470 CHECK_BINOP(a, b);
3471 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003472 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003473}
3474
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003475static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003476long_or(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003477{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003478 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003479 CHECK_BINOP(a, b);
3480 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003481 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003482}
3483
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003484static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003485long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003486{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003487 if (PyLong_CheckExact(v))
3488 Py_INCREF(v);
3489 else
3490 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003491 return v;
3492}
3493
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003494static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003495long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003496{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003497 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003498 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003499 if (result == -1.0 && PyErr_Occurred())
3500 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003501 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003502}
3503
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003504static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003505long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003506
Tim Peters6d6c1a32001-08-02 04:15:00 +00003507static PyObject *
3508long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3509{
3510 PyObject *x = NULL;
3511 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003512 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003513
Guido van Rossumbef14172001-08-29 15:47:46 +00003514 if (type != &PyLong_Type)
3515 return long_subtype_new(type, args, kwds); /* Wimp out */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003516 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003517 &x, &base))
3518 return NULL;
3519 if (x == NULL)
3520 return PyLong_FromLong(0L);
3521 if (base == -909)
3522 return PyNumber_Long(x);
Guido van Rossum98297ee2007-11-06 21:34:58 +00003523 else if (PyUnicode_Check(x))
3524 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3525 PyUnicode_GET_SIZE(x),
3526 base);
Christian Heimes72b710a2008-05-26 13:28:38 +00003527 else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003528 /* Since PyLong_FromString doesn't have a length parameter,
3529 * check here for possible NULs in the string. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003530 char *string;
Christian Heimes90aa7642007-12-19 02:45:37 +00003531 int size = Py_SIZE(x);
Christian Heimes9c4756e2008-05-26 13:22:05 +00003532 if (PyByteArray_Check(x))
3533 string = PyByteArray_AS_STRING(x);
Guido van Rossum98297ee2007-11-06 21:34:58 +00003534 else
Christian Heimes72b710a2008-05-26 13:28:38 +00003535 string = PyBytes_AS_STRING(x);
Guido van Rossum4581ae52007-05-22 21:56:47 +00003536 if (strlen(string) != size) {
Guido van Rossum25236212007-08-22 23:28:23 +00003537 /* We only see this if there's a null byte in x,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003538 x is a bytes or buffer, *and* a base is given. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003539 PyErr_Format(PyExc_ValueError,
Guido van Rossum25236212007-08-22 23:28:23 +00003540 "invalid literal for int() with base %d: %R",
3541 base, x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003542 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003543 }
Guido van Rossum4581ae52007-05-22 21:56:47 +00003544 return PyLong_FromString(string, NULL, base);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003545 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003546 else {
3547 PyErr_SetString(PyExc_TypeError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00003548 "int() can't convert non-string with explicit base");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003549 return NULL;
3550 }
3551}
3552
Guido van Rossumbef14172001-08-29 15:47:46 +00003553/* Wimpy, slow approach to tp_new calls for subtypes of long:
3554 first create a regular long from whatever arguments we got,
3555 then allocate a subtype instance and initialize it from
3556 the regular long. The regular long is then thrown away.
3557*/
3558static PyObject *
3559long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3560{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003561 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003562 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003563
3564 assert(PyType_IsSubtype(type, &PyLong_Type));
3565 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3566 if (tmp == NULL)
3567 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003568 assert(PyLong_CheckExact(tmp));
Christian Heimes90aa7642007-12-19 02:45:37 +00003569 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003570 if (n < 0)
3571 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003572 newobj = (PyLongObject *)type->tp_alloc(type, n);
3573 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003574 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003575 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003576 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003577 assert(PyLong_Check(newobj));
Christian Heimes90aa7642007-12-19 02:45:37 +00003578 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003579 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003580 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003581 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003582 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003583}
3584
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003585static PyObject *
3586long_getnewargs(PyLongObject *v)
3587{
3588 return Py_BuildValue("(N)", _PyLong_Copy(v));
3589}
3590
Guido van Rossumb43daf72007-08-01 18:08:08 +00003591static PyObject *
3592long_getN(PyLongObject *v, void *context) {
Christian Heimesc36625b2008-01-04 13:33:00 +00003593 return PyLong_FromLong((Py_intptr_t)context);
Guido van Rossumb43daf72007-08-01 18:08:08 +00003594}
3595
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003596static PyObject *
Eric Smith8c663262007-08-25 02:26:07 +00003597long__format__(PyObject *self, PyObject *args)
3598{
Eric Smith4a7d76d2008-05-30 18:10:19 +00003599 PyObject *format_spec;
3600
3601 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
3602 return NULL;
3603 return _PyLong_FormatAdvanced(self,
3604 PyUnicode_AS_UNICODE(format_spec),
3605 PyUnicode_GET_SIZE(format_spec));
Eric Smith8c663262007-08-25 02:26:07 +00003606}
3607
3608
3609static PyObject *
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003610long_round(PyObject *self, PyObject *args)
3611{
3612#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
3613 int ndigits = UNDEF_NDIGITS;
3614 double x;
3615 PyObject *res;
3616
3617 if (!PyArg_ParseTuple(args, "|i", &ndigits))
3618 return NULL;
3619
3620 if (ndigits == UNDEF_NDIGITS)
3621 return long_long(self);
3622
3623 /* If called with two args, defer to float.__round__(). */
3624 x = PyLong_AsDouble(self);
3625 if (x == -1.0 && PyErr_Occurred())
3626 return NULL;
3627 self = PyFloat_FromDouble(x);
3628 if (self == NULL)
3629 return NULL;
3630 res = PyObject_CallMethod(self, "__round__", "i", ndigits);
3631 Py_DECREF(self);
3632 return res;
3633#undef UNDEF_NDIGITS
3634}
3635
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003636static PyObject *
3637long_sizeof(PyLongObject *v)
3638{
3639 Py_ssize_t res;
3640
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00003641 res = sizeof(PyVarObject) + abs(Py_SIZE(v))*sizeof(digit);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003642 return PyLong_FromSsize_t(res);
3643}
3644
Christian Heimes53876d92008-04-19 00:31:39 +00003645#if 0
3646static PyObject *
3647long_is_finite(PyObject *v)
3648{
3649 Py_RETURN_TRUE;
3650}
3651#endif
3652
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003653static PyMethodDef long_methods[] = {
Guido van Rossumb43daf72007-08-01 18:08:08 +00003654 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3655 "Returns self, the complex conjugate of any int."},
Christian Heimes53876d92008-04-19 00:31:39 +00003656#if 0
3657 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
3658 "Returns always True."},
3659#endif
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003660 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3661 "Truncating an Integral returns itself."},
3662 {"__floor__", (PyCFunction)long_long, METH_NOARGS,
3663 "Flooring an Integral returns itself."},
3664 {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
3665 "Ceiling of an Integral returns itself."},
3666 {"__round__", (PyCFunction)long_round, METH_VARARGS,
3667 "Rounding an Integral returns itself.\n"
3668 "Rounding with an ndigits arguments defers to float.__round__."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003669 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
Eric Smith8c663262007-08-25 02:26:07 +00003670 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003671 {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
3672 "Returns size in memory, in bytes"},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003673 {NULL, NULL} /* sentinel */
3674};
3675
Guido van Rossumb43daf72007-08-01 18:08:08 +00003676static PyGetSetDef long_getset[] = {
3677 {"real",
3678 (getter)long_long, (setter)NULL,
3679 "the real part of a complex number",
3680 NULL},
3681 {"imag",
3682 (getter)long_getN, (setter)NULL,
3683 "the imaginary part of a complex number",
3684 (void*)0},
3685 {"numerator",
3686 (getter)long_long, (setter)NULL,
3687 "the numerator of a rational number in lowest terms",
3688 NULL},
3689 {"denominator",
3690 (getter)long_getN, (setter)NULL,
3691 "the denominator of a rational number in lowest terms",
3692 (void*)1},
3693 {NULL} /* Sentinel */
3694};
3695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003696PyDoc_STRVAR(long_doc,
Guido van Rossumddefaf32007-01-14 03:31:43 +00003697"int(x[, base]) -> integer\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00003698\n\
Guido van Rossumddefaf32007-01-14 03:31:43 +00003699Convert a string or number to an integer, if possible. A floating\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00003700point argument will be truncated towards zero (this does not include a\n\
3701string representation of a floating point number!) When converting a\n\
3702string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003703converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003704
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003705static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003706 (binaryfunc) long_add, /*nb_add*/
3707 (binaryfunc) long_sub, /*nb_subtract*/
3708 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003709 long_mod, /*nb_remainder*/
3710 long_divmod, /*nb_divmod*/
3711 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003712 (unaryfunc) long_neg, /*nb_negative*/
Guido van Rossumb43daf72007-08-01 18:08:08 +00003713 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003714 (unaryfunc) long_abs, /*tp_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00003715 (inquiry) long_bool, /*tp_bool*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003716 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003717 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003718 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003719 long_and, /*nb_and*/
3720 long_xor, /*nb_xor*/
3721 long_or, /*nb_or*/
Guido van Rossumb43daf72007-08-01 18:08:08 +00003722 long_long, /*nb_int*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003723 long_long, /*nb_long*/
3724 long_float, /*nb_float*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003725 0, /* nb_inplace_add */
3726 0, /* nb_inplace_subtract */
3727 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003728 0, /* nb_inplace_remainder */
3729 0, /* nb_inplace_power */
3730 0, /* nb_inplace_lshift */
3731 0, /* nb_inplace_rshift */
3732 0, /* nb_inplace_and */
3733 0, /* nb_inplace_xor */
3734 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003735 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003736 long_true_divide, /* nb_true_divide */
3737 0, /* nb_inplace_floor_divide */
3738 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003739 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003740};
3741
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003742PyTypeObject PyLong_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003743 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003744 "int", /* tp_name */
3745 /* See _PyLong_New for why this isn't
3746 sizeof(PyLongObject) - sizeof(digit) */
3747 sizeof(PyVarObject), /* tp_basicsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003748 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003749 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003750 0, /* tp_print */
3751 0, /* tp_getattr */
3752 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003753 0, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003754 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003755 &long_as_number, /* tp_as_number */
3756 0, /* tp_as_sequence */
3757 0, /* tp_as_mapping */
3758 (hashfunc)long_hash, /* tp_hash */
3759 0, /* tp_call */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003760 long_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003761 PyObject_GenericGetAttr, /* tp_getattro */
3762 0, /* tp_setattro */
3763 0, /* tp_as_buffer */
Thomas Wouters27d517b2007-02-25 20:39:11 +00003764 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3765 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003766 long_doc, /* tp_doc */
3767 0, /* tp_traverse */
3768 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003769 long_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003770 0, /* tp_weaklistoffset */
3771 0, /* tp_iter */
3772 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003773 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003774 0, /* tp_members */
Guido van Rossumb43daf72007-08-01 18:08:08 +00003775 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003776 0, /* tp_base */
3777 0, /* tp_dict */
3778 0, /* tp_descr_get */
3779 0, /* tp_descr_set */
3780 0, /* tp_dictoffset */
3781 0, /* tp_init */
3782 0, /* tp_alloc */
3783 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003784 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003785};
Guido van Rossumddefaf32007-01-14 03:31:43 +00003786
3787int
3788_PyLong_Init(void)
3789{
3790#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003791 int ival, size;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003792 PyLongObject *v = small_ints;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003793
3794 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
3795 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
3796 if (Py_TYPE(v) == &PyLong_Type) {
3797 /* The element is already initialized, most likely
3798 * the Python interpreter was initialized before.
3799 */
Christian Heimes48aa4b12008-02-01 16:56:30 +00003800 Py_ssize_t refcnt;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003801 PyObject* op = (PyObject*)v;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003802
Christian Heimes48aa4b12008-02-01 16:56:30 +00003803 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
3804 _Py_NewReference(op);
3805 /* _Py_NewReference sets the ref count to 1 but
3806 * the ref count might be larger. Set the refcnt
3807 * to the original refcnt + 1 */
3808 Py_REFCNT(op) = refcnt + 1;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003809 assert(Py_SIZE(op) == size);
3810 assert(v->ob_digit[0] == abs(ival));
3811 }
3812 else {
3813 PyObject_INIT(v, &PyLong_Type);
3814 }
3815 Py_SIZE(v) = size;
3816 v->ob_digit[0] = abs(ival);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003817 }
3818#endif
3819 return 1;
3820}
3821
3822void
3823PyLong_Fini(void)
3824{
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003825 /* Integers are currently statically allocated. Py_DECREF is not
3826 needed, but Python must forget about the reference or multiple
3827 reinitializations will fail. */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003828#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003829 int i;
3830 PyLongObject *v = small_ints;
3831 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
3832 _Py_DEC_REFTOTAL;
3833 _Py_ForgetReference((PyObject*)v);
3834 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003835#endif
3836}