blob: 2f6d103bfec5fc74797669f97e4af4c197cdec9f [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
2/* Long (arbitrary precision) integer object implementation */
3
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004/* XXX The functional organization of this file is terrible */
5
Guido van Rossumc0b618a1997-05-02 03:12:38 +00006#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00007#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00009#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000010
Tim Peters5af4e6c2002-08-12 02:31:19 +000011/* For long multiplication, use the O(N**2) school algorithm unless
12 * both operands contain more than KARATSUBA_CUTOFF digits (this
13 * being an internal Python long digit, in base BASE).
14 */
Tim Peters0973b992004-08-29 22:16:50 +000015#define KARATSUBA_CUTOFF 70
16#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000017
Guido van Rossume32e0141992-01-19 16:31:05 +000018#define ABS(x) ((x) < 0 ? -(x) : (x))
19
Tim Peters5af4e6c2002-08-12 02:31:19 +000020#undef MIN
21#undef MAX
22#define MAX(x, y) ((x) < (y) ? (y) : (x))
23#define MIN(x, y) ((x) > (y) ? (y) : (x))
24
Guido van Rossume32e0141992-01-19 16:31:05 +000025/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000026static PyLongObject *long_normalize(PyLongObject *);
27static PyLongObject *mul1(PyLongObject *, wdigit);
28static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000029static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Tim Peters9f688bf2000-07-07 15:53:28 +000030static PyObject *long_format(PyObject *aa, int base, int addL);
Guido van Rossume32e0141992-01-19 16:31:05 +000031
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000033 if (--_Py_Ticker < 0) { \
34 _Py_Ticker = _Py_CheckInterval; \
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 if (PyErr_CheckSignals()) { PyTryBlock; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000036 }
37
Guido van Rossumedcc38a1991-05-05 20:09:44 +000038/* Normalize (remove leading zeros from) a long int object.
39 Doesn't attempt to free the storage--in most cases, due to the nature
40 of the algorithms used, this could save at most be one word anyway. */
41
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000043long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000044{
Guido van Rossum4c260ff1992-01-14 18:36:43 +000045 int j = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046 register int i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000047
Guido van Rossumedcc38a1991-05-05 20:09:44 +000048 while (i > 0 && v->ob_digit[i-1] == 0)
49 --i;
50 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000051 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052 return v;
53}
54
55/* Allocate a new long int object with size digits.
56 Return NULL and set exception if we run out of memory. */
57
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000059_PyLong_New(int size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000062}
63
Tim Peters64b5ce32001-09-10 20:52:51 +000064PyObject *
65_PyLong_Copy(PyLongObject *src)
66{
67 PyLongObject *result;
68 int i;
69
70 assert(src != NULL);
71 i = src->ob_size;
72 if (i < 0)
73 i = -(i);
74 result = _PyLong_New(i);
75 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000076 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000077 while (--i >= 0)
78 result->ob_digit[i] = src->ob_digit[i];
79 }
80 return (PyObject *)result;
81}
82
Guido van Rossumedcc38a1991-05-05 20:09:44 +000083/* Create a new long int object from a C long int */
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000086PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000087{
Tim Petersce9de2f2001-06-14 04:56:19 +000088 PyLongObject *v;
89 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
90 int ndigits = 0;
91 int negative = 0;
92
93 if (ival < 0) {
94 ival = -ival;
95 negative = 1;
96 }
97
98 /* Count the number of Python digits.
99 We used to pick 5 ("big enough for anything"), but that's a
100 waste of time and space given that 5*15 = 75 bits are rarely
101 needed. */
102 t = (unsigned long)ival;
103 while (t) {
104 ++ndigits;
105 t >>= SHIFT;
106 }
107 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000108 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000109 digit *p = v->ob_digit;
110 v->ob_size = negative ? -ndigits : ndigits;
111 t = (unsigned long)ival;
112 while (t) {
113 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000114 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000115 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000116 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000118}
119
Guido van Rossum53756b11997-01-03 17:14:46 +0000120/* Create a new long int object from a C unsigned long int */
121
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000123PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000124{
Tim Petersce9de2f2001-06-14 04:56:19 +0000125 PyLongObject *v;
126 unsigned long t;
127 int ndigits = 0;
128
129 /* Count the number of Python digits. */
130 t = (unsigned long)ival;
131 while (t) {
132 ++ndigits;
133 t >>= SHIFT;
134 }
135 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000136 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000137 digit *p = v->ob_digit;
138 v->ob_size = ndigits;
139 while (ival) {
140 *p++ = (digit)(ival & MASK);
141 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000142 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000143 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000145}
146
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000147/* Create a new long int object from a C double */
148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000151{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000153 double frac;
154 int i, ndig, expo, neg;
155 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000156 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000157 PyErr_SetString(PyExc_OverflowError,
158 "cannot convert float infinity to long");
159 return NULL;
160 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000161 if (dval < 0.0) {
162 neg = 1;
163 dval = -dval;
164 }
165 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
166 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000168 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000170 if (v == NULL)
171 return NULL;
172 frac = ldexp(frac, (expo-1) % SHIFT + 1);
173 for (i = ndig; --i >= 0; ) {
174 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000175 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000176 frac = frac - (double)bits;
177 frac = ldexp(frac, SHIFT);
178 }
179 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000180 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000182}
183
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000184/* Get a C long int from a long int object.
185 Returns -1 and sets an error condition if overflow occurs. */
186
187long
Tim Peters9f688bf2000-07-07 15:53:28 +0000188PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000189{
Guido van Rossumf7531811998-05-26 14:33:37 +0000190 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000192 unsigned long x, prev;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000193 int i, sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000194
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000196 if (vv != NULL && PyInt_Check(vv))
197 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000199 return -1;
200 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000202 i = v->ob_size;
203 sign = 1;
204 x = 0;
205 if (i < 0) {
206 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000207 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000208 }
209 while (--i >= 0) {
210 prev = x;
211 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000212 if ((x >> SHIFT) != prev)
213 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000214 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000215 /* Haven't lost any bits, but if the sign bit is set we're in
216 * trouble *unless* this is the min negative number. So,
217 * trouble iff sign bit set && (positive || some bit set other
218 * than the sign bit).
219 */
220 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
221 goto overflow;
222 return (long)x * sign;
223
224 overflow:
225 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000226 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000227 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000228}
229
Guido van Rossumd8c80482002-08-13 00:24:58 +0000230/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000231 Returns -1 and sets an error condition if overflow occurs. */
232
233unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000234PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000235{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000237 unsigned long x, prev;
238 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000239
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 if (vv == NULL || !PyLong_Check(vv)) {
241 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000242 return (unsigned long) -1;
243 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000245 i = v->ob_size;
246 x = 0;
247 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000249 "can't convert negative value to unsigned long");
250 return (unsigned long) -1;
251 }
252 while (--i >= 0) {
253 prev = x;
254 x = (x << SHIFT) + v->ob_digit[i];
255 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000257 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000258 return (unsigned long) -1;
259 }
260 }
261 return x;
262}
263
Thomas Hellera4ea6032003-04-17 18:55:45 +0000264/* Get a C unsigned long int from a long int object, ignoring the high bits.
265 Returns -1 and sets an error condition if an error occurs. */
266
267unsigned long
268PyLong_AsUnsignedLongMask(PyObject *vv)
269{
270 register PyLongObject *v;
271 unsigned long x;
272 int i, sign;
273
274 if (vv == NULL || !PyLong_Check(vv)) {
275 PyErr_BadInternalCall();
276 return (unsigned long) -1;
277 }
278 v = (PyLongObject *)vv;
279 i = v->ob_size;
280 sign = 1;
281 x = 0;
282 if (i < 0) {
283 sign = -1;
284 i = -i;
285 }
286 while (--i >= 0) {
287 x = (x << SHIFT) + v->ob_digit[i];
288 }
289 return x * sign;
290}
291
Tim Peters5b8132f2003-01-31 15:52:05 +0000292int
293_PyLong_Sign(PyObject *vv)
294{
295 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000296
297 assert(v != NULL);
298 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000299
Tim Petersee1a53c2003-02-02 02:57:53 +0000300 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000301}
302
Tim Petersbaefd9e2003-01-28 20:37:45 +0000303size_t
304_PyLong_NumBits(PyObject *vv)
305{
306 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000307 size_t result = 0;
Guido van Rossum004a65c2003-02-03 15:28:19 +0000308 int ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000309
310 assert(v != NULL);
311 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000312 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000313 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
314 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000315 digit msd = v->ob_digit[ndigits - 1];
316
Tim Peters5b8132f2003-01-31 15:52:05 +0000317 result = (ndigits - 1) * SHIFT;
Tim Peters08a1d9c2003-01-31 21:45:13 +0000318 if (result / SHIFT != (size_t)ndigits - 1)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000319 goto Overflow;
320 do {
321 ++result;
322 if (result == 0)
323 goto Overflow;
324 msd >>= 1;
325 } while (msd);
326 }
327 return result;
328
329Overflow:
330 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
331 "to express in a platform size_t");
332 return (size_t)-1;
333}
334
Tim Peters2a9b3672001-06-11 21:23:58 +0000335PyObject *
336_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
337 int little_endian, int is_signed)
338{
339 const unsigned char* pstartbyte;/* LSB of bytes */
340 int incr; /* direction to move pstartbyte */
341 const unsigned char* pendbyte; /* MSB of bytes */
342 size_t numsignificantbytes; /* number of bytes that matter */
343 size_t ndigits; /* number of Python long digits */
344 PyLongObject* v; /* result */
345 int idigit = 0; /* next free index in v->ob_digit */
346
347 if (n == 0)
348 return PyLong_FromLong(0L);
349
350 if (little_endian) {
351 pstartbyte = bytes;
352 pendbyte = bytes + n - 1;
353 incr = 1;
354 }
355 else {
356 pstartbyte = bytes + n - 1;
357 pendbyte = bytes;
358 incr = -1;
359 }
360
361 if (is_signed)
362 is_signed = *pendbyte >= 0x80;
363
364 /* Compute numsignificantbytes. This consists of finding the most
365 significant byte. Leading 0 bytes are insignficant if the number
366 is positive, and leading 0xff bytes if negative. */
367 {
368 size_t i;
369 const unsigned char* p = pendbyte;
370 const int pincr = -incr; /* search MSB to LSB */
371 const unsigned char insignficant = is_signed ? 0xff : 0x00;
372
373 for (i = 0; i < n; ++i, p += pincr) {
374 if (*p != insignficant)
375 break;
376 }
377 numsignificantbytes = n - i;
378 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
379 actually has 2 significant bytes. OTOH, 0xff0001 ==
380 -0x00ffff, so we wouldn't *need* to bump it there; but we
381 do for 0xffff = -0x0001. To be safe without bothering to
382 check every case, bump it regardless. */
383 if (is_signed && numsignificantbytes < n)
384 ++numsignificantbytes;
385 }
386
387 /* How many Python long digits do we need? We have
388 8*numsignificantbytes bits, and each Python long digit has SHIFT
389 bits, so it's the ceiling of the quotient. */
390 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
391 if (ndigits > (size_t)INT_MAX)
392 return PyErr_NoMemory();
393 v = _PyLong_New((int)ndigits);
394 if (v == NULL)
395 return NULL;
396
397 /* Copy the bits over. The tricky parts are computing 2's-comp on
398 the fly for signed numbers, and dealing with the mismatch between
399 8-bit bytes and (probably) 15-bit Python digits.*/
400 {
401 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000402 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000403 twodigits accum = 0; /* sliding register */
404 unsigned int accumbits = 0; /* number of bits in accum */
405 const unsigned char* p = pstartbyte;
406
407 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000408 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000409 /* Compute correction for 2's comp, if needed. */
410 if (is_signed) {
411 thisbyte = (0xff ^ thisbyte) + carry;
412 carry = thisbyte >> 8;
413 thisbyte &= 0xff;
414 }
415 /* Because we're going LSB to MSB, thisbyte is
416 more significant than what's already in accum,
417 so needs to be prepended to accum. */
418 accum |= thisbyte << accumbits;
419 accumbits += 8;
420 if (accumbits >= SHIFT) {
421 /* There's enough to fill a Python digit. */
422 assert(idigit < (int)ndigits);
423 v->ob_digit[idigit] = (digit)(accum & MASK);
424 ++idigit;
425 accum >>= SHIFT;
426 accumbits -= SHIFT;
427 assert(accumbits < SHIFT);
428 }
429 }
430 assert(accumbits < SHIFT);
431 if (accumbits) {
432 assert(idigit < (int)ndigits);
433 v->ob_digit[idigit] = (digit)accum;
434 ++idigit;
435 }
436 }
437
438 v->ob_size = is_signed ? -idigit : idigit;
439 return (PyObject *)long_normalize(v);
440}
441
442int
443_PyLong_AsByteArray(PyLongObject* v,
444 unsigned char* bytes, size_t n,
445 int little_endian, int is_signed)
446{
447 int i; /* index into v->ob_digit */
448 int ndigits; /* |v->ob_size| */
449 twodigits accum; /* sliding register */
450 unsigned int accumbits; /* # bits in accum */
451 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
452 twodigits carry; /* for computing 2's-comp */
453 size_t j; /* # bytes filled */
454 unsigned char* p; /* pointer to next byte in bytes */
455 int pincr; /* direction to move p */
456
457 assert(v != NULL && PyLong_Check(v));
458
459 if (v->ob_size < 0) {
460 ndigits = -(v->ob_size);
461 if (!is_signed) {
462 PyErr_SetString(PyExc_TypeError,
463 "can't convert negative long to unsigned");
464 return -1;
465 }
466 do_twos_comp = 1;
467 }
468 else {
469 ndigits = v->ob_size;
470 do_twos_comp = 0;
471 }
472
473 if (little_endian) {
474 p = bytes;
475 pincr = 1;
476 }
477 else {
478 p = bytes + n - 1;
479 pincr = -1;
480 }
481
Tim Peters898cf852001-06-13 20:50:08 +0000482 /* Copy over all the Python digits.
483 It's crucial that every Python digit except for the MSD contribute
484 exactly SHIFT bits to the total, so first assert that the long is
485 normalized. */
486 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000487 j = 0;
488 accum = 0;
489 accumbits = 0;
490 carry = do_twos_comp ? 1 : 0;
491 for (i = 0; i < ndigits; ++i) {
492 twodigits thisdigit = v->ob_digit[i];
493 if (do_twos_comp) {
494 thisdigit = (thisdigit ^ MASK) + carry;
495 carry = thisdigit >> SHIFT;
496 thisdigit &= MASK;
497 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000498 /* Because we're going LSB to MSB, thisdigit is more
499 significant than what's already in accum, so needs to be
500 prepended to accum. */
501 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000502 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000503
Tim Petersede05092001-06-14 08:53:38 +0000504 /* The most-significant digit may be (probably is) at least
505 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000506 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000507 /* Count # of sign bits -- they needn't be stored,
508 * although for signed conversion we need later to
509 * make sure at least one sign bit gets stored.
510 * First shift conceptual sign bit to real sign bit.
511 */
512 stwodigits s = (stwodigits)(thisdigit <<
513 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000514 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000515 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000516 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000517 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000518 }
Tim Petersede05092001-06-14 08:53:38 +0000519 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000520 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000521
Tim Peters2a9b3672001-06-11 21:23:58 +0000522 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000523 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000524 if (j >= n)
525 goto Overflow;
526 ++j;
527 *p = (unsigned char)(accum & 0xff);
528 p += pincr;
529 accumbits -= 8;
530 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000531 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000532 }
533
534 /* Store the straggler (if any). */
535 assert(accumbits < 8);
536 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000537 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000538 if (j >= n)
539 goto Overflow;
540 ++j;
541 if (do_twos_comp) {
542 /* Fill leading bits of the byte with sign bits
543 (appropriately pretending that the long had an
544 infinite supply of sign bits). */
545 accum |= (~(twodigits)0) << accumbits;
546 }
547 *p = (unsigned char)(accum & 0xff);
548 p += pincr;
549 }
Tim Peters05607ad2001-06-13 21:01:27 +0000550 else if (j == n && n > 0 && is_signed) {
551 /* The main loop filled the byte array exactly, so the code
552 just above didn't get to ensure there's a sign bit, and the
553 loop below wouldn't add one either. Make sure a sign bit
554 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000555 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000556 int sign_bit_set = msb >= 0x80;
557 assert(accumbits == 0);
558 if (sign_bit_set == do_twos_comp)
559 return 0;
560 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000561 goto Overflow;
562 }
Tim Peters05607ad2001-06-13 21:01:27 +0000563
564 /* Fill remaining bytes with copies of the sign bit. */
565 {
566 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
567 for ( ; j < n; ++j, p += pincr)
568 *p = signbyte;
569 }
570
Tim Peters2a9b3672001-06-11 21:23:58 +0000571 return 0;
572
573Overflow:
574 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
575 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000576
Tim Peters2a9b3672001-06-11 21:23:58 +0000577}
578
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000579double
580_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
581{
582/* NBITS_WANTED should be > the number of bits in a double's precision,
583 but small enough so that 2**NBITS_WANTED is within the normal double
584 range. nbitsneeded is set to 1 less than that because the most-significant
585 Python digit contains at least 1 significant bit, but we don't want to
586 bother counting them (catering to the worst case cheaply).
587
588 57 is one more than VAX-D double precision; I (Tim) don't know of a double
589 format with more precision than that; it's 1 larger so that we add in at
590 least one round bit to stand in for the ignored least-significant bits.
591*/
592#define NBITS_WANTED 57
593 PyLongObject *v;
594 double x;
595 const double multiplier = (double)(1L << SHIFT);
596 int i, sign;
597 int nbitsneeded;
598
599 if (vv == NULL || !PyLong_Check(vv)) {
600 PyErr_BadInternalCall();
601 return -1;
602 }
603 v = (PyLongObject *)vv;
604 i = v->ob_size;
605 sign = 1;
606 if (i < 0) {
607 sign = -1;
608 i = -(i);
609 }
610 else if (i == 0) {
611 *exponent = 0;
612 return 0.0;
613 }
614 --i;
615 x = (double)v->ob_digit[i];
616 nbitsneeded = NBITS_WANTED - 1;
617 /* Invariant: i Python digits remain unaccounted for. */
618 while (i > 0 && nbitsneeded > 0) {
619 --i;
620 x = x * multiplier + (double)v->ob_digit[i];
621 nbitsneeded -= SHIFT;
622 }
623 /* There are i digits we didn't shift in. Pretending they're all
624 zeroes, the true value is x * 2**(i*SHIFT). */
625 *exponent = i;
626 assert(x > 0.0);
627 return x * sign;
628#undef NBITS_WANTED
629}
630
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000631/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000632
633double
Tim Peters9f688bf2000-07-07 15:53:28 +0000634PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000635{
Tim Peters9fffa3e2001-09-04 05:14:19 +0000636 int e;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000637 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 if (vv == NULL || !PyLong_Check(vv)) {
640 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000641 return -1;
642 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000643 x = _PyLong_AsScaledDouble(vv, &e);
644 if (x == -1.0 && PyErr_Occurred())
645 return -1.0;
646 if (e > INT_MAX / SHIFT)
647 goto overflow;
648 errno = 0;
649 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000650 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000651 goto overflow;
652 return x;
653
654overflow:
655 PyErr_SetString(PyExc_OverflowError,
656 "long int too large to convert to float");
657 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000658}
659
Guido van Rossum78694d91998-09-18 14:14:13 +0000660/* Create a new long (or int) object from a C pointer */
661
662PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000663PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000664{
Tim Peters70128a12001-06-16 08:48:40 +0000665#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000666 return PyInt_FromLong((long)p);
667#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000668
Tim Peters70128a12001-06-16 08:48:40 +0000669#ifndef HAVE_LONG_LONG
670# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
671#endif
672#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000673# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000674#endif
675 /* optimize null pointers */
676 if (p == NULL)
677 return PyInt_FromLong(0);
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000678 return PyLong_FromLongLong((PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000679
680#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000681}
682
683/* Get a C pointer from a long object (or an int object in some cases) */
684
685void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000686PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000687{
688 /* This function will allow int or long objects. If vv is neither,
689 then the PyLong_AsLong*() functions will raise the exception:
690 PyExc_SystemError, "bad argument to internal function"
691 */
Tim Peters70128a12001-06-16 08:48:40 +0000692#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000693 long x;
694
Tim Peters70128a12001-06-16 08:48:40 +0000695 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000696 x = PyInt_AS_LONG(vv);
697 else
698 x = PyLong_AsLong(vv);
699#else
Tim Peters70128a12001-06-16 08:48:40 +0000700
701#ifndef HAVE_LONG_LONG
702# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
703#endif
704#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000705# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000706#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000707 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000708
Tim Peters70128a12001-06-16 08:48:40 +0000709 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000710 x = PyInt_AS_LONG(vv);
711 else
712 x = PyLong_AsLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000713
714#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000715
716 if (x == -1 && PyErr_Occurred())
717 return NULL;
718 return (void *)x;
719}
720
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000721#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000722
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000723/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000724 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000725 */
726
Tim Peterscf37dfc2001-06-14 18:42:50 +0000727#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000728
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000729/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000730
731PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000732PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000733{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000734 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000735 int one = 1;
736 return _PyLong_FromByteArray(
737 (unsigned char *)&bytes,
738 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000739}
740
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000741/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000742
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000743PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000744PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000745{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000746 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000747 int one = 1;
748 return _PyLong_FromByteArray(
749 (unsigned char *)&bytes,
750 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000751}
752
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000753/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000754 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000755
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000756PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000757PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000758{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000759 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000760 int one = 1;
761 int res;
762
Tim Petersd38b1c72001-09-30 05:09:37 +0000763 if (vv == NULL) {
764 PyErr_BadInternalCall();
765 return -1;
766 }
767 if (!PyLong_Check(vv)) {
768 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000769 return (PY_LONG_LONG)PyInt_AsLong(vv);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000770 PyErr_BadInternalCall();
771 return -1;
772 }
773
Tim Petersd1a7da62001-06-13 00:35:57 +0000774 res = _PyLong_AsByteArray(
775 (PyLongObject *)vv, (unsigned char *)&bytes,
776 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000777
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000778 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000779 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000780 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000781 else
782 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000783}
784
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000785/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000786 Return -1 and set an error if overflow occurs. */
787
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000788unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000789PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000790{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000791 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000792 int one = 1;
793 int res;
794
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000795 if (vv == NULL || !PyLong_Check(vv)) {
796 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000797 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000798 }
799
Tim Petersd1a7da62001-06-13 00:35:57 +0000800 res = _PyLong_AsByteArray(
801 (PyLongObject *)vv, (unsigned char *)&bytes,
802 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000803
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000804 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000805 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000806 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000807 else
808 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000809}
Tim Petersd1a7da62001-06-13 00:35:57 +0000810
Thomas Hellera4ea6032003-04-17 18:55:45 +0000811/* Get a C unsigned long int from a long int object, ignoring the high bits.
812 Returns -1 and sets an error condition if an error occurs. */
813
814unsigned PY_LONG_LONG
815PyLong_AsUnsignedLongLongMask(PyObject *vv)
816{
817 register PyLongObject *v;
818 unsigned PY_LONG_LONG x;
819 int i, sign;
820
821 if (vv == NULL || !PyLong_Check(vv)) {
822 PyErr_BadInternalCall();
823 return (unsigned long) -1;
824 }
825 v = (PyLongObject *)vv;
826 i = v->ob_size;
827 sign = 1;
828 x = 0;
829 if (i < 0) {
830 sign = -1;
831 i = -i;
832 }
833 while (--i >= 0) {
834 x = (x << SHIFT) + v->ob_digit[i];
835 }
836 return x * sign;
837}
Tim Petersd1a7da62001-06-13 00:35:57 +0000838#undef IS_LITTLE_ENDIAN
839
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000840#endif /* HAVE_LONG_LONG */
841
Neil Schemenauerba872e22001-01-04 01:46:03 +0000842
843static int
844convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000845 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000846 *a = (PyLongObject *) v;
847 Py_INCREF(v);
848 }
849 else if (PyInt_Check(v)) {
850 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
851 }
852 else {
853 return 0;
854 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000855 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000856 *b = (PyLongObject *) w;
857 Py_INCREF(w);
858 }
859 else if (PyInt_Check(w)) {
860 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
861 }
862 else {
863 Py_DECREF(*a);
864 return 0;
865 }
866 return 1;
867}
868
869#define CONVERT_BINOP(v, w, a, b) \
870 if (!convert_binop(v, w, a, b)) { \
871 Py_INCREF(Py_NotImplemented); \
872 return Py_NotImplemented; \
873 }
874
Tim Peters877a2122002-08-12 05:09:36 +0000875/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
876 * is modified in place, by adding y to it. Carries are propagated as far as
877 * x[m-1], and the remaining carry (0 or 1) is returned.
878 */
879static digit
880v_iadd(digit *x, int m, digit *y, int n)
881{
882 int i;
883 digit carry = 0;
884
885 assert(m >= n);
886 for (i = 0; i < n; ++i) {
887 carry += x[i] + y[i];
888 x[i] = carry & MASK;
889 carry >>= SHIFT;
890 assert((carry & 1) == carry);
891 }
892 for (; carry && i < m; ++i) {
893 carry += x[i];
894 x[i] = carry & MASK;
895 carry >>= SHIFT;
896 assert((carry & 1) == carry);
897 }
898 return carry;
899}
900
901/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
902 * is modified in place, by subtracting y from it. Borrows are propagated as
903 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
904 */
905static digit
906v_isub(digit *x, int m, digit *y, int n)
907{
908 int i;
909 digit borrow = 0;
910
911 assert(m >= n);
912 for (i = 0; i < n; ++i) {
913 borrow = x[i] - y[i] - borrow;
914 x[i] = borrow & MASK;
915 borrow >>= SHIFT;
916 borrow &= 1; /* keep only 1 sign bit */
917 }
918 for (; borrow && i < m; ++i) {
919 borrow = x[i] - borrow;
920 x[i] = borrow & MASK;
921 borrow >>= SHIFT;
922 borrow &= 1;
923 }
924 return borrow;
925}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000926
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000927/* Multiply by a single digit, ignoring the sign. */
928
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000930mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000931{
932 return muladd1(a, n, (digit)0);
933}
934
935/* Multiply by a single digit and add a single digit, ignoring the sign. */
936
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000938muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000939{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000940 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000941 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000942 twodigits carry = extra;
943 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000944
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000945 if (z == NULL)
946 return NULL;
947 for (i = 0; i < size_a; ++i) {
948 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000949 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000950 carry >>= SHIFT;
951 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000952 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000953 return long_normalize(z);
954}
955
Tim Peters212e6142001-07-14 12:23:19 +0000956/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
957 in pout, and returning the remainder. pin and pout point at the LSD.
958 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
959 long_format, but that should be done with great care since longs are
960 immutable. */
961
962static digit
963inplace_divrem1(digit *pout, digit *pin, int size, digit n)
964{
965 twodigits rem = 0;
966
967 assert(n > 0 && n <= MASK);
968 pin += size;
969 pout += size;
970 while (--size >= 0) {
971 digit hi;
972 rem = (rem << SHIFT) + *--pin;
973 *--pout = hi = (digit)(rem / n);
974 rem -= hi * n;
975 }
976 return (digit)rem;
977}
978
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000979/* Divide a long integer by a digit, returning both the quotient
980 (as function result) and the remainder (through *prem).
981 The sign of a is ignored; n should not be zero. */
982
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +0000984divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000985{
Tim Peters212e6142001-07-14 12:23:19 +0000986 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000988
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000989 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000991 if (z == NULL)
992 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +0000993 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000994 return long_normalize(z);
995}
996
997/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000998 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +0000999 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001001static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001002long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001003{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001004 register PyLongObject *a = (PyLongObject *)aa;
1005 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001006 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001007 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001008 char *p;
1009 int bits;
1010 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012 if (a == NULL || !PyLong_Check(a)) {
1013 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001014 return NULL;
1015 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001016 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001017
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001018 /* Compute a rough upper bound for the length of the string */
1019 i = base;
1020 bits = 0;
1021 while (i > 1) {
1022 ++bits;
1023 i >>= 1;
1024 }
Fred Drake121ee271999-12-23 15:41:28 +00001025 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001027 if (str == NULL)
1028 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001029 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001030 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001031 if (addL)
1032 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001033 if (a->ob_size < 0)
1034 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001035
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001036 if (a->ob_size == 0) {
1037 *--p = '0';
1038 }
1039 else if ((base & (base - 1)) == 0) {
1040 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001041 twodigits accum = 0;
1042 int accumbits = 0; /* # of bits in accum */
1043 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001044 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001045 while ((i >>= 1) > 1)
1046 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001047
1048 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001049 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001050 accumbits += SHIFT;
1051 assert(accumbits >= basebits);
1052 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001053 char cdigit = (char)(accum & (base - 1));
1054 cdigit += (cdigit < 10) ? '0' : 'A'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001055 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001056 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001057 accumbits -= basebits;
1058 accum >>= basebits;
1059 } while (i < size_a-1 ? accumbits >= basebits :
1060 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001061 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001062 }
1063 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001064 /* Not 0, and base not a power of 2. Divide repeatedly by
1065 base, but for speed use the highest power of base that
1066 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001067 int size = size_a;
1068 digit *pin = a->ob_digit;
1069 PyLongObject *scratch;
1070 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001071 digit powbase = base; /* powbase == base ** power */
1072 int power = 1;
1073 for (;;) {
1074 unsigned long newpow = powbase * (unsigned long)base;
1075 if (newpow >> SHIFT) /* doesn't fit in a digit */
1076 break;
1077 powbase = (digit)newpow;
1078 ++power;
1079 }
Tim Peters212e6142001-07-14 12:23:19 +00001080
1081 /* Get a scratch area for repeated division. */
1082 scratch = _PyLong_New(size);
1083 if (scratch == NULL) {
1084 Py_DECREF(str);
1085 return NULL;
1086 }
1087
1088 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001089 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001090 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001091 digit rem = inplace_divrem1(scratch->ob_digit,
1092 pin, size, powbase);
1093 pin = scratch->ob_digit; /* no need to use a again */
1094 if (pin[size - 1] == 0)
1095 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001096 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001097 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001098 Py_DECREF(str);
1099 return NULL;
1100 })
Tim Peters212e6142001-07-14 12:23:19 +00001101
1102 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001103 assert(ntostore > 0);
1104 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001105 digit nextrem = (digit)(rem / base);
1106 char c = (char)(rem - nextrem * base);
1107 assert(p > PyString_AS_STRING(str));
1108 c += (c < 10) ? '0' : 'A'-10;
1109 *--p = c;
1110 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001111 --ntostore;
1112 /* Termination is a bit delicate: must not
1113 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001114 remaining quotient and rem are both 0. */
1115 } while (ntostore && (size || rem));
1116 } while (size != 0);
1117 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001118 }
1119
Guido van Rossum2c475421992-08-14 15:13:07 +00001120 if (base == 8) {
1121 if (size_a != 0)
1122 *--p = '0';
1123 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001124 else if (base == 16) {
1125 *--p = 'x';
1126 *--p = '0';
1127 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001128 else if (base != 10) {
1129 *--p = '#';
1130 *--p = '0' + base%10;
1131 if (base > 10)
1132 *--p = '0' + base/10;
1133 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001134 if (sign)
1135 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 if (p != PyString_AS_STRING(str)) {
1137 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001138 assert(p > q);
1139 do {
1140 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001141 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001142 _PyString_Resize((PyObject **)&str,
1143 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001144 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001146}
1147
Tim Petersbf2674b2003-02-02 07:51:32 +00001148/* *str points to the first digit in a string of base base digits. base
1149 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1150 * non-digit (which may be *str!). A normalized long is returned.
1151 * The point to this routine is that it takes time linear in the number of
1152 * string characters.
1153 */
1154static PyLongObject *
1155long_from_binary_base(char **str, int base)
1156{
1157 char *p = *str;
1158 char *start = p;
1159 int bits_per_char;
1160 int n;
1161 PyLongObject *z;
1162 twodigits accum;
1163 int bits_in_accum;
1164 digit *pdigit;
1165
1166 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1167 n = base;
1168 for (bits_per_char = -1; n; ++bits_per_char)
1169 n >>= 1;
1170 /* n <- total # of bits needed, while setting p to end-of-string */
1171 n = 0;
1172 for (;;) {
1173 int k = -1;
1174 char ch = *p;
1175
1176 if (ch <= '9')
1177 k = ch - '0';
1178 else if (ch >= 'a')
1179 k = ch - 'a' + 10;
1180 else if (ch >= 'A')
1181 k = ch - 'A' + 10;
1182 if (k < 0 || k >= base)
1183 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001184 ++p;
1185 }
1186 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001187 n = (p - start) * bits_per_char;
1188 if (n / bits_per_char != p - start) {
1189 PyErr_SetString(PyExc_ValueError,
1190 "long string too large to convert");
1191 return NULL;
1192 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001193 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1194 n = (n + SHIFT - 1) / SHIFT;
1195 z = _PyLong_New(n);
1196 if (z == NULL)
1197 return NULL;
1198 /* Read string from right, and fill in long from left; i.e.,
1199 * from least to most significant in both.
1200 */
1201 accum = 0;
1202 bits_in_accum = 0;
1203 pdigit = z->ob_digit;
1204 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001205 int k;
1206 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001207
1208 if (ch <= '9')
1209 k = ch - '0';
1210 else if (ch >= 'a')
1211 k = ch - 'a' + 10;
1212 else {
1213 assert(ch >= 'A');
1214 k = ch - 'A' + 10;
1215 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001216 assert(k >= 0 && k < base);
1217 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001218 bits_in_accum += bits_per_char;
1219 if (bits_in_accum >= SHIFT) {
1220 *pdigit++ = (digit)(accum & MASK);
1221 assert(pdigit - z->ob_digit <= n);
1222 accum >>= SHIFT;
1223 bits_in_accum -= SHIFT;
1224 assert(bits_in_accum < SHIFT);
1225 }
1226 }
1227 if (bits_in_accum) {
1228 assert(bits_in_accum <= SHIFT);
1229 *pdigit++ = (digit)accum;
1230 assert(pdigit - z->ob_digit <= n);
1231 }
1232 while (pdigit - z->ob_digit < n)
1233 *pdigit++ = 0;
1234 return long_normalize(z);
1235}
1236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001238PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001239{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001240 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001241 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001243
Guido van Rossum472c04f1996-12-05 21:57:21 +00001244 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001245 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001246 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001247 return NULL;
1248 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001249 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001250 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001251 if (*str == '+')
1252 ++str;
1253 else if (*str == '-') {
1254 ++str;
1255 sign = -1;
1256 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001257 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001258 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001259 if (base == 0) {
1260 if (str[0] != '0')
1261 base = 10;
1262 else if (str[1] == 'x' || str[1] == 'X')
1263 base = 16;
1264 else
1265 base = 8;
1266 }
1267 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1268 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001269 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001270 if ((base & (base - 1)) == 0)
1271 z = long_from_binary_base(&str, base);
1272 else {
1273 z = _PyLong_New(0);
1274 for ( ; z != NULL; ++str) {
1275 int k = -1;
1276 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001277
Tim Petersbf2674b2003-02-02 07:51:32 +00001278 if (*str <= '9')
1279 k = *str - '0';
1280 else if (*str >= 'a')
1281 k = *str - 'a' + 10;
1282 else if (*str >= 'A')
1283 k = *str - 'A' + 10;
1284 if (k < 0 || k >= base)
1285 break;
1286 temp = muladd1(z, (digit)base, (digit)k);
1287 Py_DECREF(z);
1288 z = temp;
1289 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001290 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001291 if (z == NULL)
1292 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001293 if (str == start)
1294 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001295 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001296 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001297 if (*str == 'L' || *str == 'l')
1298 str++;
1299 while (*str && isspace(Py_CHARMASK(*str)))
1300 str++;
1301 if (*str != '\0')
1302 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001303 if (pend)
1304 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001306
1307 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001308 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001309 "invalid literal for long(): %.200s", orig_str);
1310 Py_XDECREF(z);
1311 return NULL;
1312}
1313
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001314#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001315PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001316PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001317{
Walter Dörwald07e14762002-11-06 16:15:14 +00001318 PyObject *result;
1319 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001320
Walter Dörwald07e14762002-11-06 16:15:14 +00001321 if (buffer == NULL)
1322 return NULL;
1323
1324 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1325 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001326 return NULL;
1327 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001328 result = PyLong_FromString(buffer, NULL, base);
1329 PyMem_FREE(buffer);
1330 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001331}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001332#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001333
Tim Peters9f688bf2000-07-07 15:53:28 +00001334/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001336 (PyLongObject *, PyLongObject *, PyLongObject **);
1337static PyObject *long_pos(PyLongObject *);
1338static int long_divrem(PyLongObject *, PyLongObject *,
1339 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001340
1341/* Long division with remainder, top-level routine */
1342
Guido van Rossume32e0141992-01-19 16:31:05 +00001343static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001344long_divrem(PyLongObject *a, PyLongObject *b,
1345 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001346{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001347 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001349
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001350 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001351 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001352 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001353 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001354 }
1355 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001356 (size_a == size_b &&
1357 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001358 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 *pdiv = _PyLong_New(0);
1360 Py_INCREF(a);
1361 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001362 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001363 }
1364 if (size_b == 1) {
1365 digit rem = 0;
1366 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001367 if (z == NULL)
1368 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001370 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001371 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001372 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001373 if (z == NULL)
1374 return -1;
1375 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001376 /* Set the signs.
1377 The quotient z has the sign of a*b;
1378 the remainder r has the sign of a,
1379 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001380 if ((a->ob_size < 0) != (b->ob_size < 0))
1381 z->ob_size = -(z->ob_size);
1382 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1383 (*prem)->ob_size = -((*prem)->ob_size);
1384 *pdiv = z;
1385 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001386}
1387
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001388/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001389
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001391x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001392{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001393 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001394 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 PyLongObject *v = mul1(v1, d);
1396 PyLongObject *w = mul1(w1, d);
1397 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001398 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001399
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001400 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401 Py_XDECREF(v);
1402 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001403 return NULL;
1404 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001405
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001406 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001407 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001408 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001409
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001410 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001411 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001412
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001413 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1414 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1415 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001416 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001417 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001418
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001419 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001420 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001421 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001422 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001423 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001424 if (vj == w->ob_digit[size_w-1])
1425 q = MASK;
1426 else
1427 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1428 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001429
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001430 while (w->ob_digit[size_w-2]*q >
1431 ((
1432 ((twodigits)vj << SHIFT)
1433 + v->ob_digit[j-1]
1434 - q*w->ob_digit[size_w-1]
1435 ) << SHIFT)
1436 + v->ob_digit[j-2])
1437 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001438
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001439 for (i = 0; i < size_w && i+k < size_v; ++i) {
1440 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001441 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442 carry += v->ob_digit[i+k] - z
1443 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001444 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001445 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1446 carry, SHIFT);
1447 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001448 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001449
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001450 if (i+k < size_v) {
1451 carry += v->ob_digit[i+k];
1452 v->ob_digit[i+k] = 0;
1453 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001454
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001455 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001456 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001457 else {
1458 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001459 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001460 carry = 0;
1461 for (i = 0; i < size_w && i+k < size_v; ++i) {
1462 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001463 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001464 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1465 BASE_TWODIGITS_TYPE,
1466 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001467 }
1468 }
1469 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001470
Guido van Rossumc206c761995-01-10 15:23:19 +00001471 if (a == NULL)
1472 *prem = NULL;
1473 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001474 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001475 *prem = divrem1(v, d, &d);
1476 /* d receives the (unused) remainder */
1477 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001478 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001479 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001480 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001481 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 Py_DECREF(v);
1483 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001484 return a;
1485}
1486
1487/* Methods */
1488
1489static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001490long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001491{
Guido van Rossum9475a232001-10-05 20:51:39 +00001492 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001493}
1494
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001495static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001496long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001497{
Fred Drake121ee271999-12-23 15:41:28 +00001498 return long_format(v, 10, 1);
1499}
1500
1501static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001502long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001503{
1504 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001505}
1506
1507static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001508long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001509{
1510 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001511
Guido van Rossumc6913e71991-11-19 20:26:46 +00001512 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001513 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001514 sign = 0;
1515 else
1516 sign = a->ob_size - b->ob_size;
1517 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001518 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001519 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001520 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1521 ;
1522 if (i < 0)
1523 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001524 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001525 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001526 if (a->ob_size < 0)
1527 sign = -sign;
1528 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001529 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001530 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001531}
1532
Guido van Rossum9bfef441993-03-29 10:43:31 +00001533static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001534long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001535{
1536 long x;
1537 int i, sign;
1538
1539 /* This is designed so that Python ints and longs with the
1540 same value hash to the same value, otherwise comparisons
1541 of mapping keys will turn out weird */
1542 i = v->ob_size;
1543 sign = 1;
1544 x = 0;
1545 if (i < 0) {
1546 sign = -1;
1547 i = -(i);
1548 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001549#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001550 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001551 /* Force a native long #-bits (32 or 64) circular shift */
1552 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001553 x += v->ob_digit[i];
1554 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001555#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001556 x = x * sign;
1557 if (x == -1)
1558 x = -2;
1559 return x;
1560}
1561
1562
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001563/* Add the absolute values of two long integers. */
1564
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001566x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001567{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001568 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001569 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001570 int i;
1571 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001572
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001573 /* Ensure a is the larger of the two: */
1574 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001575 { PyLongObject *temp = a; a = b; b = temp; }
1576 { int size_temp = size_a;
1577 size_a = size_b;
1578 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001579 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001581 if (z == NULL)
1582 return NULL;
1583 for (i = 0; i < size_b; ++i) {
1584 carry += a->ob_digit[i] + b->ob_digit[i];
1585 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001586 carry >>= SHIFT;
1587 }
1588 for (; i < size_a; ++i) {
1589 carry += a->ob_digit[i];
1590 z->ob_digit[i] = carry & MASK;
1591 carry >>= SHIFT;
1592 }
1593 z->ob_digit[i] = carry;
1594 return long_normalize(z);
1595}
1596
1597/* Subtract the absolute values of two integers. */
1598
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001599static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001600x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001601{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001602 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001604 int i;
1605 int sign = 1;
1606 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001607
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001608 /* Ensure a is the larger of the two: */
1609 if (size_a < size_b) {
1610 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001611 { PyLongObject *temp = a; a = b; b = temp; }
1612 { int size_temp = size_a;
1613 size_a = size_b;
1614 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001615 }
1616 else if (size_a == size_b) {
1617 /* Find highest digit where a and b differ: */
1618 i = size_a;
1619 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1620 ;
1621 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001622 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001623 if (a->ob_digit[i] < b->ob_digit[i]) {
1624 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001626 }
1627 size_a = size_b = i+1;
1628 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001630 if (z == NULL)
1631 return NULL;
1632 for (i = 0; i < size_b; ++i) {
1633 /* The following assumes unsigned arithmetic
1634 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001635 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001636 z->ob_digit[i] = borrow & MASK;
1637 borrow >>= SHIFT;
1638 borrow &= 1; /* Keep only one sign bit */
1639 }
1640 for (; i < size_a; ++i) {
1641 borrow = a->ob_digit[i] - borrow;
1642 z->ob_digit[i] = borrow & MASK;
1643 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001644 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001645 }
1646 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001647 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001648 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001649 return long_normalize(z);
1650}
1651
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001652static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001653long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001654{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001655 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001656
Neil Schemenauerba872e22001-01-04 01:46:03 +00001657 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1658
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001659 if (a->ob_size < 0) {
1660 if (b->ob_size < 0) {
1661 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001662 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001663 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001664 }
1665 else
1666 z = x_sub(b, a);
1667 }
1668 else {
1669 if (b->ob_size < 0)
1670 z = x_sub(a, b);
1671 else
1672 z = x_add(a, b);
1673 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001674 Py_DECREF(a);
1675 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001676 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001677}
1678
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001680long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001681{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001682 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001683
Neil Schemenauerba872e22001-01-04 01:46:03 +00001684 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1685
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001686 if (a->ob_size < 0) {
1687 if (b->ob_size < 0)
1688 z = x_sub(a, b);
1689 else
1690 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001691 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001692 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001693 }
1694 else {
1695 if (b->ob_size < 0)
1696 z = x_add(a, b);
1697 else
1698 z = x_sub(a, b);
1699 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001700 Py_DECREF(a);
1701 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001702 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001703}
1704
Tim Peters5af4e6c2002-08-12 02:31:19 +00001705/* Grade school multiplication, ignoring the signs.
1706 * Returns the absolute value of the product, or NULL if error.
1707 */
1708static PyLongObject *
1709x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001710{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001711 PyLongObject *z;
1712 int size_a = ABS(a->ob_size);
1713 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001714 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001715
Tim Peters5af4e6c2002-08-12 02:31:19 +00001716 z = _PyLong_New(size_a + size_b);
1717 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001718 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001719
1720 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001721 if (a == b) {
1722 /* Efficient squaring per HAC, Algorithm 14.16:
1723 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1724 * Gives slightly less than a 2x speedup when a == b,
1725 * via exploiting that each entry in the multiplication
1726 * pyramid appears twice (except for the size_a squares).
1727 */
1728 for (i = 0; i < size_a; ++i) {
1729 twodigits carry;
1730 twodigits f = a->ob_digit[i];
1731 digit *pz = z->ob_digit + (i << 1);
1732 digit *pa = a->ob_digit + i + 1;
1733 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001734
Tim Peters0973b992004-08-29 22:16:50 +00001735 SIGCHECK({
1736 Py_DECREF(z);
1737 return NULL;
1738 })
1739
1740 carry = *pz + f * f;
1741 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001743 assert(carry <= MASK);
1744
1745 /* Now f is added in twice in each column of the
1746 * pyramid it appears. Same as adding f<<1 once.
1747 */
1748 f <<= 1;
1749 while (pa < paend) {
1750 carry += *pz + *pa++ * f;
1751 *pz++ = (digit)(carry & MASK);
1752 carry >>= SHIFT;
1753 assert(carry <= (MASK << 1));
1754 }
1755 if (carry) {
1756 carry += *pz;
1757 *pz++ = (digit)(carry & MASK);
1758 carry >>= SHIFT;
1759 }
1760 if (carry)
1761 *pz += (digit)(carry & MASK);
1762 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001763 }
Tim Peters0973b992004-08-29 22:16:50 +00001764 }
1765 else { /* a is not the same as b -- gradeschool long mult */
1766 for (i = 0; i < size_a; ++i) {
1767 twodigits carry = 0;
1768 twodigits f = a->ob_digit[i];
1769 digit *pz = z->ob_digit + i;
1770 digit *pb = b->ob_digit;
1771 digit *pbend = b->ob_digit + size_b;
1772
1773 SIGCHECK({
1774 Py_DECREF(z);
1775 return NULL;
1776 })
1777
1778 while (pb < pbend) {
1779 carry += *pz + *pb++ * f;
1780 *pz++ = (digit)(carry & MASK);
1781 carry >>= SHIFT;
1782 assert(carry <= MASK);
1783 }
1784 if (carry)
1785 *pz += (digit)(carry & MASK);
1786 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001787 }
1788 }
Tim Peters44121a62002-08-12 06:17:58 +00001789 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001790}
1791
1792/* A helper for Karatsuba multiplication (k_mul).
1793 Takes a long "n" and an integer "size" representing the place to
1794 split, and sets low and high such that abs(n) == (high << size) + low,
1795 viewing the shift as being by digits. The sign bit is ignored, and
1796 the return values are >= 0.
1797 Returns 0 on success, -1 on failure.
1798*/
1799static int
1800kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1801{
1802 PyLongObject *hi, *lo;
1803 int size_lo, size_hi;
1804 const int size_n = ABS(n->ob_size);
1805
1806 size_lo = MIN(size_n, size);
1807 size_hi = size_n - size_lo;
1808
1809 if ((hi = _PyLong_New(size_hi)) == NULL)
1810 return -1;
1811 if ((lo = _PyLong_New(size_lo)) == NULL) {
1812 Py_DECREF(hi);
1813 return -1;
1814 }
1815
1816 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1817 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1818
1819 *high = long_normalize(hi);
1820 *low = long_normalize(lo);
1821 return 0;
1822}
1823
Tim Peters60004642002-08-12 22:01:34 +00001824static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1825
Tim Peters5af4e6c2002-08-12 02:31:19 +00001826/* Karatsuba multiplication. Ignores the input signs, and returns the
1827 * absolute value of the product (or NULL if error).
1828 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1829 */
1830static PyLongObject *
1831k_mul(PyLongObject *a, PyLongObject *b)
1832{
Tim Peters738eda72002-08-12 15:08:20 +00001833 int asize = ABS(a->ob_size);
1834 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001835 PyLongObject *ah = NULL;
1836 PyLongObject *al = NULL;
1837 PyLongObject *bh = NULL;
1838 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001839 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001840 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001841 int shift; /* the number of digits we split off */
1842 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001843
Tim Peters5af4e6c2002-08-12 02:31:19 +00001844 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1845 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1846 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001847 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001848 * By picking X to be a power of 2, "*X" is just shifting, and it's
1849 * been reduced to 3 multiplies on numbers half the size.
1850 */
1851
Tim Petersfc07e562002-08-12 02:54:10 +00001852 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001853 * is largest.
1854 */
Tim Peters738eda72002-08-12 15:08:20 +00001855 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001856 t1 = a;
1857 a = b;
1858 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001859
1860 i = asize;
1861 asize = bsize;
1862 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001863 }
1864
1865 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001866 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1867 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001868 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001869 return _PyLong_New(0);
1870 else
1871 return x_mul(a, b);
1872 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001873
Tim Peters60004642002-08-12 22:01:34 +00001874 /* If a is small compared to b, splitting on b gives a degenerate
1875 * case with ah==0, and Karatsuba may be (even much) less efficient
1876 * than "grade school" then. However, we can still win, by viewing
1877 * b as a string of "big digits", each of width a->ob_size. That
1878 * leads to a sequence of balanced calls to k_mul.
1879 */
1880 if (2 * asize <= bsize)
1881 return k_lopsided_mul(a, b);
1882
Tim Petersd6974a52002-08-13 20:37:51 +00001883 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001884 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001885 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001886 assert(ah->ob_size > 0); /* the split isn't degenerate */
1887
Tim Peters0973b992004-08-29 22:16:50 +00001888 if (a == b) {
1889 bh = ah;
1890 bl = al;
1891 Py_INCREF(bh);
1892 Py_INCREF(bl);
1893 }
1894 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001895
Tim Petersd64c1de2002-08-12 17:36:03 +00001896 /* The plan:
1897 * 1. Allocate result space (asize + bsize digits: that's always
1898 * enough).
1899 * 2. Compute ah*bh, and copy into result at 2*shift.
1900 * 3. Compute al*bl, and copy into result at 0. Note that this
1901 * can't overlap with #2.
1902 * 4. Subtract al*bl from the result, starting at shift. This may
1903 * underflow (borrow out of the high digit), but we don't care:
1904 * we're effectively doing unsigned arithmetic mod
1905 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1906 * borrows and carries out of the high digit can be ignored.
1907 * 5. Subtract ah*bh from the result, starting at shift.
1908 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1909 * at shift.
1910 */
1911
1912 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001913 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001914 if (ret == NULL) goto fail;
1915#ifdef Py_DEBUG
1916 /* Fill with trash, to catch reference to uninitialized digits. */
1917 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1918#endif
Tim Peters44121a62002-08-12 06:17:58 +00001919
Tim Petersd64c1de2002-08-12 17:36:03 +00001920 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001921 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1922 assert(t1->ob_size >= 0);
1923 assert(2*shift + t1->ob_size <= ret->ob_size);
1924 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1925 t1->ob_size * sizeof(digit));
1926
1927 /* Zero-out the digits higher than the ah*bh copy. */
1928 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001929 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001930 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001931 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001932
Tim Petersd64c1de2002-08-12 17:36:03 +00001933 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001934 if ((t2 = k_mul(al, bl)) == NULL) {
1935 Py_DECREF(t1);
1936 goto fail;
1937 }
1938 assert(t2->ob_size >= 0);
1939 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1940 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001941
1942 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001943 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001944 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001945 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001946
Tim Petersd64c1de2002-08-12 17:36:03 +00001947 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1948 * because it's fresher in cache.
1949 */
Tim Peters738eda72002-08-12 15:08:20 +00001950 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001951 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001952 Py_DECREF(t2);
1953
Tim Petersd64c1de2002-08-12 17:36:03 +00001954 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001955 Py_DECREF(t1);
1956
Tim Petersd64c1de2002-08-12 17:36:03 +00001957 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001958 if ((t1 = x_add(ah, al)) == NULL) goto fail;
1959 Py_DECREF(ah);
1960 Py_DECREF(al);
1961 ah = al = NULL;
1962
Tim Peters0973b992004-08-29 22:16:50 +00001963 if (a == b) {
1964 t2 = t1;
1965 Py_INCREF(t2);
1966 }
1967 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001968 Py_DECREF(t1);
1969 goto fail;
1970 }
1971 Py_DECREF(bh);
1972 Py_DECREF(bl);
1973 bh = bl = NULL;
1974
Tim Peters738eda72002-08-12 15:08:20 +00001975 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001976 Py_DECREF(t1);
1977 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00001978 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00001979 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001980
Tim Petersd6974a52002-08-13 20:37:51 +00001981 /* Add t3. It's not obvious why we can't run out of room here.
1982 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00001983 */
Tim Petersd64c1de2002-08-12 17:36:03 +00001984 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001985 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001986
Tim Peters5af4e6c2002-08-12 02:31:19 +00001987 return long_normalize(ret);
1988
1989 fail:
1990 Py_XDECREF(ret);
1991 Py_XDECREF(ah);
1992 Py_XDECREF(al);
1993 Py_XDECREF(bh);
1994 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001995 return NULL;
1996}
1997
Tim Petersd6974a52002-08-13 20:37:51 +00001998/* (*) Why adding t3 can't "run out of room" above.
1999
Tim Petersab86c2b2002-08-15 20:06:00 +00002000Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2001to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002002
Tim Petersab86c2b2002-08-15 20:06:00 +000020031. For any integer i, i = c(i/2) + f(i/2). In particular,
2004 bsize = c(bsize/2) + f(bsize/2).
20052. shift = f(bsize/2)
20063. asize <= bsize
20074. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2008 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002009
Tim Petersab86c2b2002-08-15 20:06:00 +00002010We allocated asize + bsize result digits, and add t3 into them at an offset
2011of shift. This leaves asize+bsize-shift allocated digit positions for t3
2012to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2013asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002014
Tim Petersab86c2b2002-08-15 20:06:00 +00002015bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2016at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002017
Tim Petersab86c2b2002-08-15 20:06:00 +00002018If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2019digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2020most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002021
Tim Petersab86c2b2002-08-15 20:06:00 +00002022The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002023
Tim Petersab86c2b2002-08-15 20:06:00 +00002024 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002025
Tim Petersab86c2b2002-08-15 20:06:00 +00002026and we have asize + c(bsize/2) available digit positions. We need to show
2027this is always enough. An instance of c(bsize/2) cancels out in both, so
2028the question reduces to whether asize digits is enough to hold
2029(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2030then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2031asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2032digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2033asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002034c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2035is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2036bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002037
Tim Peters48d52c02002-08-14 17:07:32 +00002038Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2039clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2040ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002041*/
2042
Tim Peters60004642002-08-12 22:01:34 +00002043/* b has at least twice the digits of a, and a is big enough that Karatsuba
2044 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2045 * of slices, each with a->ob_size digits, and multiply the slices by a,
2046 * one at a time. This gives k_mul balanced inputs to work with, and is
2047 * also cache-friendly (we compute one double-width slice of the result
2048 * at a time, then move on, never bactracking except for the helpful
2049 * single-width slice overlap between successive partial sums).
2050 */
2051static PyLongObject *
2052k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2053{
2054 const int asize = ABS(a->ob_size);
2055 int bsize = ABS(b->ob_size);
2056 int nbdone; /* # of b digits already multiplied */
2057 PyLongObject *ret;
2058 PyLongObject *bslice = NULL;
2059
2060 assert(asize > KARATSUBA_CUTOFF);
2061 assert(2 * asize <= bsize);
2062
2063 /* Allocate result space, and zero it out. */
2064 ret = _PyLong_New(asize + bsize);
2065 if (ret == NULL)
2066 return NULL;
2067 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2068
2069 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002070 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002071 if (bslice == NULL)
2072 goto fail;
2073
2074 nbdone = 0;
2075 while (bsize > 0) {
2076 PyLongObject *product;
2077 const int nbtouse = MIN(bsize, asize);
2078
2079 /* Multiply the next slice of b by a. */
2080 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2081 nbtouse * sizeof(digit));
2082 bslice->ob_size = nbtouse;
2083 product = k_mul(a, bslice);
2084 if (product == NULL)
2085 goto fail;
2086
2087 /* Add into result. */
2088 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2089 product->ob_digit, product->ob_size);
2090 Py_DECREF(product);
2091
2092 bsize -= nbtouse;
2093 nbdone += nbtouse;
2094 }
2095
2096 Py_DECREF(bslice);
2097 return long_normalize(ret);
2098
2099 fail:
2100 Py_DECREF(ret);
2101 Py_XDECREF(bslice);
2102 return NULL;
2103}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002104
2105static PyObject *
2106long_mul(PyLongObject *v, PyLongObject *w)
2107{
2108 PyLongObject *a, *b, *z;
2109
2110 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002111 Py_INCREF(Py_NotImplemented);
2112 return Py_NotImplemented;
2113 }
2114
Tim Petersd64c1de2002-08-12 17:36:03 +00002115 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002116 /* Negate if exactly one of the inputs is negative. */
2117 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002118 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002119 Py_DECREF(a);
2120 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002121 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002122}
2123
Guido van Rossume32e0141992-01-19 16:31:05 +00002124/* The / and % operators are now defined in terms of divmod().
2125 The expression a mod b has the value a - b*floor(a/b).
2126 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002127 |a| by |b|, with the sign of a. This is also expressed
2128 as a - b*trunc(a/b), if trunc truncates towards zero.
2129 Some examples:
2130 a b a rem b a mod b
2131 13 10 3 3
2132 -13 10 -3 7
2133 13 -10 3 -7
2134 -13 -10 -3 -3
2135 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002136 have different signs. We then subtract one from the 'div'
2137 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002138
Guido van Rossume32e0141992-01-19 16:31:05 +00002139static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002140l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002141 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002142{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002143 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002144
Guido van Rossume32e0141992-01-19 16:31:05 +00002145 if (long_divrem(v, w, &div, &mod) < 0)
2146 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002147 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2148 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002149 PyLongObject *temp;
2150 PyLongObject *one;
2151 temp = (PyLongObject *) long_add(mod, w);
2152 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002153 mod = temp;
2154 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002155 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002156 return -1;
2157 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002158 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002159 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002160 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2161 Py_DECREF(mod);
2162 Py_DECREF(div);
2163 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002164 return -1;
2165 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002166 Py_DECREF(one);
2167 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002168 div = temp;
2169 }
2170 *pdiv = div;
2171 *pmod = mod;
2172 return 0;
2173}
2174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002175static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002176long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002177{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002178 PyLongObject *a, *b, *div, *mod;
2179
2180 CONVERT_BINOP(v, w, &a, &b);
2181
2182 if (l_divmod(a, b, &div, &mod) < 0) {
2183 Py_DECREF(a);
2184 Py_DECREF(b);
Guido van Rossume32e0141992-01-19 16:31:05 +00002185 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002186 }
2187 Py_DECREF(a);
2188 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002189 Py_DECREF(mod);
2190 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002191}
2192
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002193static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002194long_classic_div(PyObject *v, PyObject *w)
2195{
2196 PyLongObject *a, *b, *div, *mod;
2197
2198 CONVERT_BINOP(v, w, &a, &b);
2199
2200 if (Py_DivisionWarningFlag &&
2201 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2202 div = NULL;
2203 else if (l_divmod(a, b, &div, &mod) < 0)
2204 div = NULL;
2205 else
2206 Py_DECREF(mod);
2207
2208 Py_DECREF(a);
2209 Py_DECREF(b);
2210 return (PyObject *)div;
2211}
2212
2213static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002214long_true_divide(PyObject *v, PyObject *w)
2215{
Tim Peterse2a60002001-09-04 06:17:36 +00002216 PyLongObject *a, *b;
2217 double ad, bd;
Tim Peters6f97e492001-11-04 23:09:40 +00002218 int aexp, bexp, failed;
Tim Peterse2a60002001-09-04 06:17:36 +00002219
2220 CONVERT_BINOP(v, w, &a, &b);
2221 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2222 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002223 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2224 Py_DECREF(a);
2225 Py_DECREF(b);
2226 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002227 return NULL;
2228
2229 if (bd == 0.0) {
2230 PyErr_SetString(PyExc_ZeroDivisionError,
2231 "long division or modulo by zero");
2232 return NULL;
2233 }
2234
2235 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2236 ad /= bd; /* overflow/underflow impossible here */
2237 aexp -= bexp;
2238 if (aexp > INT_MAX / SHIFT)
2239 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002240 else if (aexp < -(INT_MAX / SHIFT))
2241 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002242 errno = 0;
2243 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002244 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002245 goto overflow;
2246 return PyFloat_FromDouble(ad);
2247
2248overflow:
2249 PyErr_SetString(PyExc_OverflowError,
2250 "long/long too large for a float");
2251 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002252
Tim Peters20dab9f2001-09-04 05:31:47 +00002253}
2254
2255static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002256long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002257{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002258 PyLongObject *a, *b, *div, *mod;
2259
2260 CONVERT_BINOP(v, w, &a, &b);
2261
2262 if (l_divmod(a, b, &div, &mod) < 0) {
2263 Py_DECREF(a);
2264 Py_DECREF(b);
Guido van Rossume32e0141992-01-19 16:31:05 +00002265 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002266 }
2267 Py_DECREF(a);
2268 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002269 Py_DECREF(div);
2270 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002271}
2272
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002273static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002274long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002275{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002276 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002277 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002278
2279 CONVERT_BINOP(v, w, &a, &b);
2280
2281 if (l_divmod(a, b, &div, &mod) < 0) {
2282 Py_DECREF(a);
2283 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002284 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002285 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002286 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002287 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002288 PyTuple_SetItem(z, 0, (PyObject *) div);
2289 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002290 }
2291 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002292 Py_DECREF(div);
2293 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002294 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002295 Py_DECREF(a);
2296 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002297 return z;
2298}
2299
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002300static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002301long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002302{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002303 PyLongObject *a, *b;
2304 PyObject *c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002305 PyLongObject *z, *div, *mod;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002306 int size_b, i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002307
2308 CONVERT_BINOP(v, w, &a, &b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002309 if (PyLong_Check(x) || Py_None == x) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002310 c = x;
2311 Py_INCREF(x);
2312 }
2313 else if (PyInt_Check(x)) {
2314 c = PyLong_FromLong(PyInt_AS_LONG(x));
2315 }
2316 else {
2317 Py_DECREF(a);
2318 Py_DECREF(b);
2319 Py_INCREF(Py_NotImplemented);
2320 return Py_NotImplemented;
2321 }
Tim Peters4c483c42001-09-05 06:24:58 +00002322
2323 if (c != Py_None && ((PyLongObject *)c)->ob_size == 0) {
2324 PyErr_SetString(PyExc_ValueError,
2325 "pow() 3rd argument cannot be 0");
2326 z = NULL;
2327 goto error;
2328 }
2329
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002330 size_b = b->ob_size;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002331 if (size_b < 0) {
Guido van Rossum0ec9aba2001-07-12 11:21:17 +00002332 Py_DECREF(a);
2333 Py_DECREF(b);
2334 Py_DECREF(c);
Tim Peters32f453e2001-09-03 08:35:41 +00002335 if (x != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +00002336 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
2337 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +00002338 return NULL;
2339 }
2340 /* Return a float. This works because we know that
2341 this calls float_pow() which converts its
2342 arguments to double. */
Guido van Rossum0ec9aba2001-07-12 11:21:17 +00002343 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002344 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002345 z = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002346 for (i = 0; i < size_b; ++i) {
2347 digit bi = b->ob_digit[i];
2348 int j;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002349
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002350 for (j = 0; j < SHIFT; ++j) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002351 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002352
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002353 if (bi & 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002354 temp = (PyLongObject *)long_mul(z, a);
2355 Py_DECREF(z);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002356 if (c!=Py_None && temp!=NULL) {
2357 if (l_divmod(temp,(PyLongObject *)c,
2358 &div,&mod) < 0) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002359 Py_DECREF(temp);
2360 z = NULL;
2361 goto error;
2362 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002363 Py_XDECREF(div);
2364 Py_DECREF(temp);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002365 temp = mod;
2366 }
2367 z = temp;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002368 if (z == NULL)
2369 break;
2370 }
2371 bi >>= 1;
2372 if (bi == 0 && i+1 == size_b)
2373 break;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002374 temp = (PyLongObject *)long_mul(a, a);
2375 Py_DECREF(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002376 if (c!=Py_None && temp!=NULL) {
2377 if (l_divmod(temp, (PyLongObject *)c, &div,
2378 &mod) < 0) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002379 Py_DECREF(temp);
2380 z = NULL;
2381 goto error;
2382 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002383 Py_XDECREF(div);
2384 Py_DECREF(temp);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002385 temp = mod;
2386 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002387 a = temp;
2388 if (a == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002389 Py_DECREF(z);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002390 z = NULL;
2391 break;
2392 }
2393 }
Guido van Rossumc206c761995-01-10 15:23:19 +00002394 if (a == NULL || z == NULL)
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00002395 break;
2396 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002397 if (c!=Py_None && z!=NULL) {
2398 if (l_divmod(z, (PyLongObject *)c, &div, &mod) < 0) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002399 Py_DECREF(z);
2400 z = NULL;
2401 }
2402 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002403 Py_XDECREF(div);
2404 Py_DECREF(z);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002405 z = mod;
2406 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002407 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002408 error:
Neil Schemenauerba872e22001-01-04 01:46:03 +00002409 Py_XDECREF(a);
2410 Py_DECREF(b);
2411 Py_DECREF(c);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002412 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002413}
2414
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002415static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002416long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002417{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002418 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002419 PyLongObject *x;
2420 PyLongObject *w;
2421 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002422 if (w == NULL)
2423 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002424 x = (PyLongObject *) long_add(v, w);
2425 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002426 if (x == NULL)
2427 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002428 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002429 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002430}
2431
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002432static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002433long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002434{
Tim Peters69c2de32001-09-11 22:31:33 +00002435 if (PyLong_CheckExact(v)) {
2436 Py_INCREF(v);
2437 return (PyObject *)v;
2438 }
2439 else
2440 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002441}
2442
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002443static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002444long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002445{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002446 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002447 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002448 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002449 Py_INCREF(v);
2450 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002451 }
Tim Peters69c2de32001-09-11 22:31:33 +00002452 z = (PyLongObject *)_PyLong_Copy(v);
2453 if (z != NULL)
2454 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002455 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002456}
2457
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002458static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002459long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002460{
2461 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002462 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002463 else
2464 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002465}
2466
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002467static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002468long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002469{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002470 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002471}
2472
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002473static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002474long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002475{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002476 PyLongObject *a, *b;
2477 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002478 long shiftby;
2479 int newsize, wordshift, loshift, hishift, i, j;
2480 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002481
Neil Schemenauerba872e22001-01-04 01:46:03 +00002482 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2483
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002484 if (a->ob_size < 0) {
2485 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002486 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002487 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002488 if (a1 == NULL)
2489 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002490 a2 = (PyLongObject *) long_rshift(a1, b);
2491 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002492 if (a2 == NULL)
2493 goto rshift_error;
2494 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002495 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002496 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002497 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002498
Neil Schemenauerba872e22001-01-04 01:46:03 +00002499 shiftby = PyLong_AsLong((PyObject *)b);
2500 if (shiftby == -1L && PyErr_Occurred())
2501 goto rshift_error;
2502 if (shiftby < 0) {
2503 PyErr_SetString(PyExc_ValueError,
2504 "negative shift count");
2505 goto rshift_error;
2506 }
2507 wordshift = shiftby / SHIFT;
2508 newsize = ABS(a->ob_size) - wordshift;
2509 if (newsize <= 0) {
2510 z = _PyLong_New(0);
2511 Py_DECREF(a);
2512 Py_DECREF(b);
2513 return (PyObject *)z;
2514 }
2515 loshift = shiftby % SHIFT;
2516 hishift = SHIFT - loshift;
2517 lomask = ((digit)1 << hishift) - 1;
2518 himask = MASK ^ lomask;
2519 z = _PyLong_New(newsize);
2520 if (z == NULL)
2521 goto rshift_error;
2522 if (a->ob_size < 0)
2523 z->ob_size = -(z->ob_size);
2524 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2525 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2526 if (i+1 < newsize)
2527 z->ob_digit[i] |=
2528 (a->ob_digit[j+1] << hishift) & himask;
2529 }
2530 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002531 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002532rshift_error:
2533 Py_DECREF(a);
2534 Py_DECREF(b);
2535 return (PyObject *) z;
2536
Guido van Rossumc6913e71991-11-19 20:26:46 +00002537}
2538
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002539static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002540long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002541{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002542 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002543 PyLongObject *a, *b;
2544 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002545 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002546 int oldsize, newsize, wordshift, remshift, i, j;
2547 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002548
Neil Schemenauerba872e22001-01-04 01:46:03 +00002549 CONVERT_BINOP(v, w, &a, &b);
2550
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002551 shiftby = PyLong_AsLong((PyObject *)b);
2552 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002553 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002554 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002555 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002556 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002557 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002558 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002559 PyErr_SetString(PyExc_ValueError,
2560 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002561 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002562 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002563 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2564 wordshift = (int)shiftby / SHIFT;
2565 remshift = (int)shiftby - wordshift * SHIFT;
2566
2567 oldsize = ABS(a->ob_size);
2568 newsize = oldsize + wordshift;
2569 if (remshift)
2570 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002571 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002572 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002573 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002574 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002575 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002576 for (i = 0; i < wordshift; i++)
2577 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002578 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002579 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002580 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002581 z->ob_digit[i] = (digit)(accum & MASK);
2582 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002583 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002584 if (remshift)
2585 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002586 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002587 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002588 z = long_normalize(z);
2589lshift_error:
2590 Py_DECREF(a);
2591 Py_DECREF(b);
2592 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002593}
2594
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002595
2596/* Bitwise and/xor/or operations */
2597
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002598static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002599long_bitwise(PyLongObject *a,
2600 int op, /* '&', '|', '^' */
2601 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002602{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002603 digit maska, maskb; /* 0 or MASK */
2604 int negz;
2605 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002606 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002607 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002608 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002609 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002610
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002611 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002612 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002613 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002614 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002615 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002616 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002617 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002618 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002619 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002620 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002621 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002622 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002623 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002624 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002625 maskb = 0;
2626 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002627
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002628 negz = 0;
2629 switch (op) {
2630 case '^':
2631 if (maska != maskb) {
2632 maska ^= MASK;
2633 negz = -1;
2634 }
2635 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002636 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002637 if (maska && maskb) {
2638 op = '|';
2639 maska ^= MASK;
2640 maskb ^= MASK;
2641 negz = -1;
2642 }
2643 break;
2644 case '|':
2645 if (maska || maskb) {
2646 op = '&';
2647 maska ^= MASK;
2648 maskb ^= MASK;
2649 negz = -1;
2650 }
2651 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002652 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002653
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002654 /* JRH: The original logic here was to allocate the result value (z)
2655 as the longer of the two operands. However, there are some cases
2656 where the result is guaranteed to be shorter than that: AND of two
2657 positives, OR of two negatives: use the shorter number. AND with
2658 mixed signs: use the positive number. OR with mixed signs: use the
2659 negative number. After the transformations above, op will be '&'
2660 iff one of these cases applies, and mask will be non-0 for operands
2661 whose length should be ignored.
2662 */
2663
2664 size_a = a->ob_size;
2665 size_b = b->ob_size;
2666 size_z = op == '&'
2667 ? (maska
2668 ? size_b
2669 : (maskb ? size_a : MIN(size_a, size_b)))
2670 : MAX(size_a, size_b);
2671 z = _PyLong_New(size_z);
2672 if (a == NULL || b == NULL || z == NULL) {
2673 Py_XDECREF(a);
2674 Py_XDECREF(b);
2675 Py_XDECREF(z);
2676 return NULL;
2677 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002678
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002679 for (i = 0; i < size_z; ++i) {
2680 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2681 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2682 switch (op) {
2683 case '&': z->ob_digit[i] = diga & digb; break;
2684 case '|': z->ob_digit[i] = diga | digb; break;
2685 case '^': z->ob_digit[i] = diga ^ digb; break;
2686 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002687 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002688
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002689 Py_DECREF(a);
2690 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002691 z = long_normalize(z);
2692 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002693 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002694 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002695 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002696 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002697}
2698
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002699static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002700long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002701{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002702 PyLongObject *a, *b;
2703 PyObject *c;
2704 CONVERT_BINOP(v, w, &a, &b);
2705 c = long_bitwise(a, '&', b);
2706 Py_DECREF(a);
2707 Py_DECREF(b);
2708 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002709}
2710
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002711static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002712long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002713{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002714 PyLongObject *a, *b;
2715 PyObject *c;
2716 CONVERT_BINOP(v, w, &a, &b);
2717 c = long_bitwise(a, '^', b);
2718 Py_DECREF(a);
2719 Py_DECREF(b);
2720 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002721}
2722
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002723static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002724long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002725{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002726 PyLongObject *a, *b;
2727 PyObject *c;
2728 CONVERT_BINOP(v, w, &a, &b);
2729 c = long_bitwise(a, '|', b);
2730 Py_DECREF(a);
2731 Py_DECREF(b);
2732 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002733}
2734
Guido van Rossum234f9421993-06-17 12:35:49 +00002735static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002736long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002737{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002738 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002739 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002740 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002741 return 0;
2742 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002743 else if (PyLong_Check(*pw)) {
2744 Py_INCREF(*pv);
2745 Py_INCREF(*pw);
2746 return 0;
2747 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002748 return 1; /* Can't do it */
2749}
2750
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002751static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002752long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002753{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002754 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002755 return v;
2756}
2757
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002758static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002759long_int(PyObject *v)
2760{
2761 long x;
2762 x = PyLong_AsLong(v);
2763 if (PyErr_Occurred()) {
2764 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2765 PyErr_Clear();
2766 if (PyLong_CheckExact(v)) {
2767 Py_INCREF(v);
2768 return v;
2769 }
2770 else
2771 return _PyLong_Copy((PyLongObject *)v);
2772 }
2773 else
2774 return NULL;
2775 }
2776 return PyInt_FromLong(x);
2777}
2778
2779static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002780long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002781{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002782 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002783 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002784 if (result == -1.0 && PyErr_Occurred())
2785 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002786 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002787}
2788
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002789static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002790long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002791{
Fred Drake121ee271999-12-23 15:41:28 +00002792 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002793}
2794
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002795static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002796long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002797{
Fred Drake121ee271999-12-23 15:41:28 +00002798 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002799}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002800
2801static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002802long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002803
Tim Peters6d6c1a32001-08-02 04:15:00 +00002804static PyObject *
2805long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2806{
2807 PyObject *x = NULL;
2808 int base = -909; /* unlikely! */
2809 static char *kwlist[] = {"x", "base", 0};
2810
Guido van Rossumbef14172001-08-29 15:47:46 +00002811 if (type != &PyLong_Type)
2812 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002813 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2814 &x, &base))
2815 return NULL;
2816 if (x == NULL)
2817 return PyLong_FromLong(0L);
2818 if (base == -909)
2819 return PyNumber_Long(x);
2820 else if (PyString_Check(x))
2821 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002822#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002823 else if (PyUnicode_Check(x))
2824 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2825 PyUnicode_GET_SIZE(x),
2826 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002827#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002828 else {
2829 PyErr_SetString(PyExc_TypeError,
2830 "long() can't convert non-string with explicit base");
2831 return NULL;
2832 }
2833}
2834
Guido van Rossumbef14172001-08-29 15:47:46 +00002835/* Wimpy, slow approach to tp_new calls for subtypes of long:
2836 first create a regular long from whatever arguments we got,
2837 then allocate a subtype instance and initialize it from
2838 the regular long. The regular long is then thrown away.
2839*/
2840static PyObject *
2841long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2842{
2843 PyLongObject *tmp, *new;
2844 int i, n;
2845
2846 assert(PyType_IsSubtype(type, &PyLong_Type));
2847 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2848 if (tmp == NULL)
2849 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002850 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002851 n = tmp->ob_size;
2852 if (n < 0)
2853 n = -n;
2854 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002855 if (new == NULL) {
2856 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002857 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002858 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002859 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002860 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002861 for (i = 0; i < n; i++)
2862 new->ob_digit[i] = tmp->ob_digit[i];
2863 Py_DECREF(tmp);
2864 return (PyObject *)new;
2865}
2866
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002867static PyObject *
2868long_getnewargs(PyLongObject *v)
2869{
2870 return Py_BuildValue("(N)", _PyLong_Copy(v));
2871}
2872
2873static PyMethodDef long_methods[] = {
2874 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2875 {NULL, NULL} /* sentinel */
2876};
2877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002878PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002879"long(x[, base]) -> integer\n\
2880\n\
2881Convert a string or number to a long integer, if possible. A floating\n\
2882point argument will be truncated towards zero (this does not include a\n\
2883string representation of a floating point number!) When converting a\n\
2884string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002885converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002886
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002887static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00002888 (binaryfunc) long_add, /*nb_add*/
2889 (binaryfunc) long_sub, /*nb_subtract*/
2890 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00002891 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002892 (binaryfunc) long_mod, /*nb_remainder*/
2893 (binaryfunc) long_divmod, /*nb_divmod*/
2894 (ternaryfunc) long_pow, /*nb_power*/
2895 (unaryfunc) long_neg, /*nb_negative*/
2896 (unaryfunc) long_pos, /*tp_positive*/
2897 (unaryfunc) long_abs, /*tp_absolute*/
2898 (inquiry) long_nonzero, /*tp_nonzero*/
2899 (unaryfunc) long_invert, /*nb_invert*/
2900 (binaryfunc) long_lshift, /*nb_lshift*/
2901 (binaryfunc) long_rshift, /*nb_rshift*/
2902 (binaryfunc) long_and, /*nb_and*/
2903 (binaryfunc) long_xor, /*nb_xor*/
2904 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00002905 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002906 (unaryfunc) long_int, /*nb_int*/
2907 (unaryfunc) long_long, /*nb_long*/
2908 (unaryfunc) long_float, /*nb_float*/
2909 (unaryfunc) long_oct, /*nb_oct*/
2910 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00002911 0, /* nb_inplace_add */
2912 0, /* nb_inplace_subtract */
2913 0, /* nb_inplace_multiply */
2914 0, /* nb_inplace_divide */
2915 0, /* nb_inplace_remainder */
2916 0, /* nb_inplace_power */
2917 0, /* nb_inplace_lshift */
2918 0, /* nb_inplace_rshift */
2919 0, /* nb_inplace_and */
2920 0, /* nb_inplace_xor */
2921 0, /* nb_inplace_or */
2922 (binaryfunc)long_div, /* nb_floor_divide */
2923 long_true_divide, /* nb_true_divide */
2924 0, /* nb_inplace_floor_divide */
2925 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002926};
2927
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002928PyTypeObject PyLong_Type = {
2929 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002930 0, /* ob_size */
2931 "long", /* tp_name */
2932 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
2933 sizeof(digit), /* tp_itemsize */
2934 (destructor)long_dealloc, /* tp_dealloc */
2935 0, /* tp_print */
2936 0, /* tp_getattr */
2937 0, /* tp_setattr */
2938 (cmpfunc)long_compare, /* tp_compare */
2939 (reprfunc)long_repr, /* tp_repr */
2940 &long_as_number, /* tp_as_number */
2941 0, /* tp_as_sequence */
2942 0, /* tp_as_mapping */
2943 (hashfunc)long_hash, /* tp_hash */
2944 0, /* tp_call */
2945 (reprfunc)long_str, /* tp_str */
2946 PyObject_GenericGetAttr, /* tp_getattro */
2947 0, /* tp_setattro */
2948 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00002949 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
2950 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002951 long_doc, /* tp_doc */
2952 0, /* tp_traverse */
2953 0, /* tp_clear */
2954 0, /* tp_richcompare */
2955 0, /* tp_weaklistoffset */
2956 0, /* tp_iter */
2957 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002958 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002959 0, /* tp_members */
2960 0, /* tp_getset */
2961 0, /* tp_base */
2962 0, /* tp_dict */
2963 0, /* tp_descr_get */
2964 0, /* tp_descr_set */
2965 0, /* tp_dictoffset */
2966 0, /* tp_init */
2967 0, /* tp_alloc */
2968 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00002969 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002970};