blob: 0103e5c079eaf98e46fe42a35bb0ee6047e751aa [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
Tim Peters47e52ee2004-08-30 02:44:38 +000018/* For exponentiation, use the binary left-to-right algorithm
19 * unless the exponent contains more than FIVEARY_CUTOFF digits.
20 * In that case, do 5 bits at a time. The potential drawback is that
21 * a table of 2**5 intermediate results is computed.
22 */
23#define FIVEARY_CUTOFF 8
24
Guido van Rossume32e0141992-01-19 16:31:05 +000025#define ABS(x) ((x) < 0 ? -(x) : (x))
26
Tim Peters5af4e6c2002-08-12 02:31:19 +000027#undef MIN
28#undef MAX
29#define MAX(x, y) ((x) < (y) ? (y) : (x))
30#define MIN(x, y) ((x) > (y) ? (y) : (x))
31
Guido van Rossume32e0141992-01-19 16:31:05 +000032/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000033static PyLongObject *long_normalize(PyLongObject *);
34static PyLongObject *mul1(PyLongObject *, wdigit);
35static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000036static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Tim Peters9f688bf2000-07-07 15:53:28 +000037static PyObject *long_format(PyObject *aa, int base, int addL);
Guido van Rossume32e0141992-01-19 16:31:05 +000038
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000040 if (--_Py_Ticker < 0) { \
41 _Py_Ticker = _Py_CheckInterval; \
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042 if (PyErr_CheckSignals()) { PyTryBlock; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000043 }
44
Guido van Rossumedcc38a1991-05-05 20:09:44 +000045/* Normalize (remove leading zeros from) a long int object.
46 Doesn't attempt to free the storage--in most cases, due to the nature
47 of the algorithms used, this could save at most be one word anyway. */
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000050long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000051{
Guido van Rossum4c260ff1992-01-14 18:36:43 +000052 int j = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000053 register int i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000054
Guido van Rossumedcc38a1991-05-05 20:09:44 +000055 while (i > 0 && v->ob_digit[i-1] == 0)
56 --i;
57 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000058 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000059 return v;
60}
61
62/* Allocate a new long int object with size digits.
63 Return NULL and set exception if we run out of memory. */
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000066_PyLong_New(int size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000067{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000069}
70
Tim Peters64b5ce32001-09-10 20:52:51 +000071PyObject *
72_PyLong_Copy(PyLongObject *src)
73{
74 PyLongObject *result;
75 int i;
76
77 assert(src != NULL);
78 i = src->ob_size;
79 if (i < 0)
80 i = -(i);
81 result = _PyLong_New(i);
82 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000083 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000084 while (--i >= 0)
85 result->ob_digit[i] = src->ob_digit[i];
86 }
87 return (PyObject *)result;
88}
89
Guido van Rossumedcc38a1991-05-05 20:09:44 +000090/* Create a new long int object from a C long int */
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000093PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000094{
Tim Petersce9de2f2001-06-14 04:56:19 +000095 PyLongObject *v;
96 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
97 int ndigits = 0;
98 int negative = 0;
99
100 if (ival < 0) {
101 ival = -ival;
102 negative = 1;
103 }
104
105 /* Count the number of Python digits.
106 We used to pick 5 ("big enough for anything"), but that's a
107 waste of time and space given that 5*15 = 75 bits are rarely
108 needed. */
109 t = (unsigned long)ival;
110 while (t) {
111 ++ndigits;
112 t >>= SHIFT;
113 }
114 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000115 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000116 digit *p = v->ob_digit;
117 v->ob_size = negative ? -ndigits : ndigits;
118 t = (unsigned long)ival;
119 while (t) {
120 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000121 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000122 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000123 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000125}
126
Guido van Rossum53756b11997-01-03 17:14:46 +0000127/* Create a new long int object from a C unsigned long int */
128
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000130PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000131{
Tim Petersce9de2f2001-06-14 04:56:19 +0000132 PyLongObject *v;
133 unsigned long t;
134 int ndigits = 0;
135
136 /* Count the number of Python digits. */
137 t = (unsigned long)ival;
138 while (t) {
139 ++ndigits;
140 t >>= SHIFT;
141 }
142 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000143 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000144 digit *p = v->ob_digit;
145 v->ob_size = ndigits;
146 while (ival) {
147 *p++ = (digit)(ival & MASK);
148 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000149 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000150 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000151 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000152}
153
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000154/* Create a new long int object from a C double */
155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000158{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000160 double frac;
161 int i, ndig, expo, neg;
162 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000163 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000164 PyErr_SetString(PyExc_OverflowError,
165 "cannot convert float infinity to long");
166 return NULL;
167 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000168 if (dval < 0.0) {
169 neg = 1;
170 dval = -dval;
171 }
172 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
173 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000175 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000176 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000177 if (v == NULL)
178 return NULL;
179 frac = ldexp(frac, (expo-1) % SHIFT + 1);
180 for (i = ndig; --i >= 0; ) {
181 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000182 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000183 frac = frac - (double)bits;
184 frac = ldexp(frac, SHIFT);
185 }
186 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000187 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000189}
190
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000191/* Get a C long int from a long int object.
192 Returns -1 and sets an error condition if overflow occurs. */
193
194long
Tim Peters9f688bf2000-07-07 15:53:28 +0000195PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000196{
Guido van Rossumf7531811998-05-26 14:33:37 +0000197 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000199 unsigned long x, prev;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000200 int i, sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000201
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000203 if (vv != NULL && PyInt_Check(vv))
204 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000206 return -1;
207 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000209 i = v->ob_size;
210 sign = 1;
211 x = 0;
212 if (i < 0) {
213 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000214 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000215 }
216 while (--i >= 0) {
217 prev = x;
218 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000219 if ((x >> SHIFT) != prev)
220 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000221 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000222 /* Haven't lost any bits, but if the sign bit is set we're in
223 * trouble *unless* this is the min negative number. So,
224 * trouble iff sign bit set && (positive || some bit set other
225 * than the sign bit).
226 */
227 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
228 goto overflow;
229 return (long)x * sign;
230
231 overflow:
232 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000233 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000234 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000235}
236
Guido van Rossumd8c80482002-08-13 00:24:58 +0000237/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000238 Returns -1 and sets an error condition if overflow occurs. */
239
240unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000241PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000242{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000244 unsigned long x, prev;
245 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000246
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 if (vv == NULL || !PyLong_Check(vv)) {
248 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000249 return (unsigned long) -1;
250 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000252 i = v->ob_size;
253 x = 0;
254 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000256 "can't convert negative value to unsigned long");
257 return (unsigned long) -1;
258 }
259 while (--i >= 0) {
260 prev = x;
261 x = (x << SHIFT) + v->ob_digit[i];
262 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000264 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000265 return (unsigned long) -1;
266 }
267 }
268 return x;
269}
270
Thomas Hellera4ea6032003-04-17 18:55:45 +0000271/* Get a C unsigned long int from a long int object, ignoring the high bits.
272 Returns -1 and sets an error condition if an error occurs. */
273
274unsigned long
275PyLong_AsUnsignedLongMask(PyObject *vv)
276{
277 register PyLongObject *v;
278 unsigned long x;
279 int i, sign;
280
281 if (vv == NULL || !PyLong_Check(vv)) {
282 PyErr_BadInternalCall();
283 return (unsigned long) -1;
284 }
285 v = (PyLongObject *)vv;
286 i = v->ob_size;
287 sign = 1;
288 x = 0;
289 if (i < 0) {
290 sign = -1;
291 i = -i;
292 }
293 while (--i >= 0) {
294 x = (x << SHIFT) + v->ob_digit[i];
295 }
296 return x * sign;
297}
298
Tim Peters5b8132f2003-01-31 15:52:05 +0000299int
300_PyLong_Sign(PyObject *vv)
301{
302 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000303
304 assert(v != NULL);
305 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000306
Tim Petersee1a53c2003-02-02 02:57:53 +0000307 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000308}
309
Tim Petersbaefd9e2003-01-28 20:37:45 +0000310size_t
311_PyLong_NumBits(PyObject *vv)
312{
313 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000314 size_t result = 0;
Guido van Rossum004a65c2003-02-03 15:28:19 +0000315 int ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000316
317 assert(v != NULL);
318 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000319 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000320 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
321 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000322 digit msd = v->ob_digit[ndigits - 1];
323
Tim Peters5b8132f2003-01-31 15:52:05 +0000324 result = (ndigits - 1) * SHIFT;
Tim Peters08a1d9c2003-01-31 21:45:13 +0000325 if (result / SHIFT != (size_t)ndigits - 1)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000326 goto Overflow;
327 do {
328 ++result;
329 if (result == 0)
330 goto Overflow;
331 msd >>= 1;
332 } while (msd);
333 }
334 return result;
335
336Overflow:
337 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
338 "to express in a platform size_t");
339 return (size_t)-1;
340}
341
Tim Peters2a9b3672001-06-11 21:23:58 +0000342PyObject *
343_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
344 int little_endian, int is_signed)
345{
346 const unsigned char* pstartbyte;/* LSB of bytes */
347 int incr; /* direction to move pstartbyte */
348 const unsigned char* pendbyte; /* MSB of bytes */
349 size_t numsignificantbytes; /* number of bytes that matter */
350 size_t ndigits; /* number of Python long digits */
351 PyLongObject* v; /* result */
352 int idigit = 0; /* next free index in v->ob_digit */
353
354 if (n == 0)
355 return PyLong_FromLong(0L);
356
357 if (little_endian) {
358 pstartbyte = bytes;
359 pendbyte = bytes + n - 1;
360 incr = 1;
361 }
362 else {
363 pstartbyte = bytes + n - 1;
364 pendbyte = bytes;
365 incr = -1;
366 }
367
368 if (is_signed)
369 is_signed = *pendbyte >= 0x80;
370
371 /* Compute numsignificantbytes. This consists of finding the most
372 significant byte. Leading 0 bytes are insignficant if the number
373 is positive, and leading 0xff bytes if negative. */
374 {
375 size_t i;
376 const unsigned char* p = pendbyte;
377 const int pincr = -incr; /* search MSB to LSB */
378 const unsigned char insignficant = is_signed ? 0xff : 0x00;
379
380 for (i = 0; i < n; ++i, p += pincr) {
381 if (*p != insignficant)
382 break;
383 }
384 numsignificantbytes = n - i;
385 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
386 actually has 2 significant bytes. OTOH, 0xff0001 ==
387 -0x00ffff, so we wouldn't *need* to bump it there; but we
388 do for 0xffff = -0x0001. To be safe without bothering to
389 check every case, bump it regardless. */
390 if (is_signed && numsignificantbytes < n)
391 ++numsignificantbytes;
392 }
393
394 /* How many Python long digits do we need? We have
395 8*numsignificantbytes bits, and each Python long digit has SHIFT
396 bits, so it's the ceiling of the quotient. */
397 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
398 if (ndigits > (size_t)INT_MAX)
399 return PyErr_NoMemory();
400 v = _PyLong_New((int)ndigits);
401 if (v == NULL)
402 return NULL;
403
404 /* Copy the bits over. The tricky parts are computing 2's-comp on
405 the fly for signed numbers, and dealing with the mismatch between
406 8-bit bytes and (probably) 15-bit Python digits.*/
407 {
408 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000409 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000410 twodigits accum = 0; /* sliding register */
411 unsigned int accumbits = 0; /* number of bits in accum */
412 const unsigned char* p = pstartbyte;
413
414 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000415 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000416 /* Compute correction for 2's comp, if needed. */
417 if (is_signed) {
418 thisbyte = (0xff ^ thisbyte) + carry;
419 carry = thisbyte >> 8;
420 thisbyte &= 0xff;
421 }
422 /* Because we're going LSB to MSB, thisbyte is
423 more significant than what's already in accum,
424 so needs to be prepended to accum. */
425 accum |= thisbyte << accumbits;
426 accumbits += 8;
427 if (accumbits >= SHIFT) {
428 /* There's enough to fill a Python digit. */
429 assert(idigit < (int)ndigits);
430 v->ob_digit[idigit] = (digit)(accum & MASK);
431 ++idigit;
432 accum >>= SHIFT;
433 accumbits -= SHIFT;
434 assert(accumbits < SHIFT);
435 }
436 }
437 assert(accumbits < SHIFT);
438 if (accumbits) {
439 assert(idigit < (int)ndigits);
440 v->ob_digit[idigit] = (digit)accum;
441 ++idigit;
442 }
443 }
444
445 v->ob_size = is_signed ? -idigit : idigit;
446 return (PyObject *)long_normalize(v);
447}
448
449int
450_PyLong_AsByteArray(PyLongObject* v,
451 unsigned char* bytes, size_t n,
452 int little_endian, int is_signed)
453{
454 int i; /* index into v->ob_digit */
455 int ndigits; /* |v->ob_size| */
456 twodigits accum; /* sliding register */
457 unsigned int accumbits; /* # bits in accum */
458 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
459 twodigits carry; /* for computing 2's-comp */
460 size_t j; /* # bytes filled */
461 unsigned char* p; /* pointer to next byte in bytes */
462 int pincr; /* direction to move p */
463
464 assert(v != NULL && PyLong_Check(v));
465
466 if (v->ob_size < 0) {
467 ndigits = -(v->ob_size);
468 if (!is_signed) {
469 PyErr_SetString(PyExc_TypeError,
470 "can't convert negative long to unsigned");
471 return -1;
472 }
473 do_twos_comp = 1;
474 }
475 else {
476 ndigits = v->ob_size;
477 do_twos_comp = 0;
478 }
479
480 if (little_endian) {
481 p = bytes;
482 pincr = 1;
483 }
484 else {
485 p = bytes + n - 1;
486 pincr = -1;
487 }
488
Tim Peters898cf852001-06-13 20:50:08 +0000489 /* Copy over all the Python digits.
490 It's crucial that every Python digit except for the MSD contribute
491 exactly SHIFT bits to the total, so first assert that the long is
492 normalized. */
493 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000494 j = 0;
495 accum = 0;
496 accumbits = 0;
497 carry = do_twos_comp ? 1 : 0;
498 for (i = 0; i < ndigits; ++i) {
499 twodigits thisdigit = v->ob_digit[i];
500 if (do_twos_comp) {
501 thisdigit = (thisdigit ^ MASK) + carry;
502 carry = thisdigit >> SHIFT;
503 thisdigit &= MASK;
504 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000505 /* Because we're going LSB to MSB, thisdigit is more
506 significant than what's already in accum, so needs to be
507 prepended to accum. */
508 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000509 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000510
Tim Petersede05092001-06-14 08:53:38 +0000511 /* The most-significant digit may be (probably is) at least
512 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000513 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000514 /* Count # of sign bits -- they needn't be stored,
515 * although for signed conversion we need later to
516 * make sure at least one sign bit gets stored.
517 * First shift conceptual sign bit to real sign bit.
518 */
519 stwodigits s = (stwodigits)(thisdigit <<
520 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000521 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000522 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000523 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000524 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000525 }
Tim Petersede05092001-06-14 08:53:38 +0000526 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000527 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000528
Tim Peters2a9b3672001-06-11 21:23:58 +0000529 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000530 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000531 if (j >= n)
532 goto Overflow;
533 ++j;
534 *p = (unsigned char)(accum & 0xff);
535 p += pincr;
536 accumbits -= 8;
537 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000538 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000539 }
540
541 /* Store the straggler (if any). */
542 assert(accumbits < 8);
543 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000544 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000545 if (j >= n)
546 goto Overflow;
547 ++j;
548 if (do_twos_comp) {
549 /* Fill leading bits of the byte with sign bits
550 (appropriately pretending that the long had an
551 infinite supply of sign bits). */
552 accum |= (~(twodigits)0) << accumbits;
553 }
554 *p = (unsigned char)(accum & 0xff);
555 p += pincr;
556 }
Tim Peters05607ad2001-06-13 21:01:27 +0000557 else if (j == n && n > 0 && is_signed) {
558 /* The main loop filled the byte array exactly, so the code
559 just above didn't get to ensure there's a sign bit, and the
560 loop below wouldn't add one either. Make sure a sign bit
561 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000562 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000563 int sign_bit_set = msb >= 0x80;
564 assert(accumbits == 0);
565 if (sign_bit_set == do_twos_comp)
566 return 0;
567 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000568 goto Overflow;
569 }
Tim Peters05607ad2001-06-13 21:01:27 +0000570
571 /* Fill remaining bytes with copies of the sign bit. */
572 {
573 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
574 for ( ; j < n; ++j, p += pincr)
575 *p = signbyte;
576 }
577
Tim Peters2a9b3672001-06-11 21:23:58 +0000578 return 0;
579
580Overflow:
581 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
582 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000583
Tim Peters2a9b3672001-06-11 21:23:58 +0000584}
585
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000586double
587_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
588{
589/* NBITS_WANTED should be > the number of bits in a double's precision,
590 but small enough so that 2**NBITS_WANTED is within the normal double
591 range. nbitsneeded is set to 1 less than that because the most-significant
592 Python digit contains at least 1 significant bit, but we don't want to
593 bother counting them (catering to the worst case cheaply).
594
595 57 is one more than VAX-D double precision; I (Tim) don't know of a double
596 format with more precision than that; it's 1 larger so that we add in at
597 least one round bit to stand in for the ignored least-significant bits.
598*/
599#define NBITS_WANTED 57
600 PyLongObject *v;
601 double x;
602 const double multiplier = (double)(1L << SHIFT);
603 int i, sign;
604 int nbitsneeded;
605
606 if (vv == NULL || !PyLong_Check(vv)) {
607 PyErr_BadInternalCall();
608 return -1;
609 }
610 v = (PyLongObject *)vv;
611 i = v->ob_size;
612 sign = 1;
613 if (i < 0) {
614 sign = -1;
615 i = -(i);
616 }
617 else if (i == 0) {
618 *exponent = 0;
619 return 0.0;
620 }
621 --i;
622 x = (double)v->ob_digit[i];
623 nbitsneeded = NBITS_WANTED - 1;
624 /* Invariant: i Python digits remain unaccounted for. */
625 while (i > 0 && nbitsneeded > 0) {
626 --i;
627 x = x * multiplier + (double)v->ob_digit[i];
628 nbitsneeded -= SHIFT;
629 }
630 /* There are i digits we didn't shift in. Pretending they're all
631 zeroes, the true value is x * 2**(i*SHIFT). */
632 *exponent = i;
633 assert(x > 0.0);
634 return x * sign;
635#undef NBITS_WANTED
636}
637
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000638/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000639
640double
Tim Peters9f688bf2000-07-07 15:53:28 +0000641PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000642{
Tim Peters9fffa3e2001-09-04 05:14:19 +0000643 int e;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000644 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000645
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 if (vv == NULL || !PyLong_Check(vv)) {
647 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000648 return -1;
649 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000650 x = _PyLong_AsScaledDouble(vv, &e);
651 if (x == -1.0 && PyErr_Occurred())
652 return -1.0;
653 if (e > INT_MAX / SHIFT)
654 goto overflow;
655 errno = 0;
656 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000657 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000658 goto overflow;
659 return x;
660
661overflow:
662 PyErr_SetString(PyExc_OverflowError,
663 "long int too large to convert to float");
664 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000665}
666
Guido van Rossum78694d91998-09-18 14:14:13 +0000667/* Create a new long (or int) object from a C pointer */
668
669PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000670PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000671{
Tim Peters70128a12001-06-16 08:48:40 +0000672#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000673 return PyInt_FromLong((long)p);
674#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000675
Tim Peters70128a12001-06-16 08:48:40 +0000676#ifndef HAVE_LONG_LONG
677# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
678#endif
679#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000680# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000681#endif
682 /* optimize null pointers */
683 if (p == NULL)
684 return PyInt_FromLong(0);
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000685 return PyLong_FromLongLong((PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000686
687#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000688}
689
690/* Get a C pointer from a long object (or an int object in some cases) */
691
692void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000693PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000694{
695 /* This function will allow int or long objects. If vv is neither,
696 then the PyLong_AsLong*() functions will raise the exception:
697 PyExc_SystemError, "bad argument to internal function"
698 */
Tim Peters70128a12001-06-16 08:48:40 +0000699#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000700 long x;
701
Tim Peters70128a12001-06-16 08:48:40 +0000702 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000703 x = PyInt_AS_LONG(vv);
704 else
705 x = PyLong_AsLong(vv);
706#else
Tim Peters70128a12001-06-16 08:48:40 +0000707
708#ifndef HAVE_LONG_LONG
709# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
710#endif
711#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000712# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000713#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000714 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000715
Tim Peters70128a12001-06-16 08:48:40 +0000716 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000717 x = PyInt_AS_LONG(vv);
718 else
719 x = PyLong_AsLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000720
721#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000722
723 if (x == -1 && PyErr_Occurred())
724 return NULL;
725 return (void *)x;
726}
727
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000728#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000729
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000730/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000731 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000732 */
733
Tim Peterscf37dfc2001-06-14 18:42:50 +0000734#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000735
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000736/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000737
738PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000739PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000740{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000741 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000742 int one = 1;
743 return _PyLong_FromByteArray(
744 (unsigned char *)&bytes,
745 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000746}
747
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000748/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000749
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000750PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000751PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000752{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000753 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000754 int one = 1;
755 return _PyLong_FromByteArray(
756 (unsigned char *)&bytes,
757 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000758}
759
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000760/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000761 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000762
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000763PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000764PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000765{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000766 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000767 int one = 1;
768 int res;
769
Tim Petersd38b1c72001-09-30 05:09:37 +0000770 if (vv == NULL) {
771 PyErr_BadInternalCall();
772 return -1;
773 }
774 if (!PyLong_Check(vv)) {
775 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000776 return (PY_LONG_LONG)PyInt_AsLong(vv);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000777 PyErr_BadInternalCall();
778 return -1;
779 }
780
Tim Petersd1a7da62001-06-13 00:35:57 +0000781 res = _PyLong_AsByteArray(
782 (PyLongObject *)vv, (unsigned char *)&bytes,
783 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000784
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000785 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000786 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000787 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000788 else
789 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000790}
791
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000792/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000793 Return -1 and set an error if overflow occurs. */
794
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000795unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000796PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000797{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000798 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000799 int one = 1;
800 int res;
801
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000802 if (vv == NULL || !PyLong_Check(vv)) {
803 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000804 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000805 }
806
Tim Petersd1a7da62001-06-13 00:35:57 +0000807 res = _PyLong_AsByteArray(
808 (PyLongObject *)vv, (unsigned char *)&bytes,
809 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000810
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000811 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000812 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000813 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000814 else
815 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000816}
Tim Petersd1a7da62001-06-13 00:35:57 +0000817
Thomas Hellera4ea6032003-04-17 18:55:45 +0000818/* Get a C unsigned long int from a long int object, ignoring the high bits.
819 Returns -1 and sets an error condition if an error occurs. */
820
821unsigned PY_LONG_LONG
822PyLong_AsUnsignedLongLongMask(PyObject *vv)
823{
824 register PyLongObject *v;
825 unsigned PY_LONG_LONG x;
826 int i, sign;
827
828 if (vv == NULL || !PyLong_Check(vv)) {
829 PyErr_BadInternalCall();
830 return (unsigned long) -1;
831 }
832 v = (PyLongObject *)vv;
833 i = v->ob_size;
834 sign = 1;
835 x = 0;
836 if (i < 0) {
837 sign = -1;
838 i = -i;
839 }
840 while (--i >= 0) {
841 x = (x << SHIFT) + v->ob_digit[i];
842 }
843 return x * sign;
844}
Tim Petersd1a7da62001-06-13 00:35:57 +0000845#undef IS_LITTLE_ENDIAN
846
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000847#endif /* HAVE_LONG_LONG */
848
Neil Schemenauerba872e22001-01-04 01:46:03 +0000849
850static int
851convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000852 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000853 *a = (PyLongObject *) v;
854 Py_INCREF(v);
855 }
856 else if (PyInt_Check(v)) {
857 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
858 }
859 else {
860 return 0;
861 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000862 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000863 *b = (PyLongObject *) w;
864 Py_INCREF(w);
865 }
866 else if (PyInt_Check(w)) {
867 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
868 }
869 else {
870 Py_DECREF(*a);
871 return 0;
872 }
873 return 1;
874}
875
876#define CONVERT_BINOP(v, w, a, b) \
877 if (!convert_binop(v, w, a, b)) { \
878 Py_INCREF(Py_NotImplemented); \
879 return Py_NotImplemented; \
880 }
881
Tim Peters877a2122002-08-12 05:09:36 +0000882/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
883 * is modified in place, by adding y to it. Carries are propagated as far as
884 * x[m-1], and the remaining carry (0 or 1) is returned.
885 */
886static digit
887v_iadd(digit *x, int m, digit *y, int n)
888{
889 int i;
890 digit carry = 0;
891
892 assert(m >= n);
893 for (i = 0; i < n; ++i) {
894 carry += x[i] + y[i];
895 x[i] = carry & MASK;
896 carry >>= SHIFT;
897 assert((carry & 1) == carry);
898 }
899 for (; carry && i < m; ++i) {
900 carry += x[i];
901 x[i] = carry & MASK;
902 carry >>= SHIFT;
903 assert((carry & 1) == carry);
904 }
905 return carry;
906}
907
908/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
909 * is modified in place, by subtracting y from it. Borrows are propagated as
910 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
911 */
912static digit
913v_isub(digit *x, int m, digit *y, int n)
914{
915 int i;
916 digit borrow = 0;
917
918 assert(m >= n);
919 for (i = 0; i < n; ++i) {
920 borrow = x[i] - y[i] - borrow;
921 x[i] = borrow & MASK;
922 borrow >>= SHIFT;
923 borrow &= 1; /* keep only 1 sign bit */
924 }
925 for (; borrow && i < m; ++i) {
926 borrow = x[i] - borrow;
927 x[i] = borrow & MASK;
928 borrow >>= SHIFT;
929 borrow &= 1;
930 }
931 return borrow;
932}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000933
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000934/* Multiply by a single digit, ignoring the sign. */
935
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000937mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000938{
939 return muladd1(a, n, (digit)0);
940}
941
942/* Multiply by a single digit and add a single digit, ignoring the sign. */
943
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000944static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000945muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000946{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000947 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000949 twodigits carry = extra;
950 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000951
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000952 if (z == NULL)
953 return NULL;
954 for (i = 0; i < size_a; ++i) {
955 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000956 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000957 carry >>= SHIFT;
958 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000959 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000960 return long_normalize(z);
961}
962
Tim Peters212e6142001-07-14 12:23:19 +0000963/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
964 in pout, and returning the remainder. pin and pout point at the LSD.
965 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
966 long_format, but that should be done with great care since longs are
967 immutable. */
968
969static digit
970inplace_divrem1(digit *pout, digit *pin, int size, digit n)
971{
972 twodigits rem = 0;
973
974 assert(n > 0 && n <= MASK);
975 pin += size;
976 pout += size;
977 while (--size >= 0) {
978 digit hi;
979 rem = (rem << SHIFT) + *--pin;
980 *--pout = hi = (digit)(rem / n);
981 rem -= hi * n;
982 }
983 return (digit)rem;
984}
985
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000986/* Divide a long integer by a digit, returning both the quotient
987 (as function result) and the remainder (through *prem).
988 The sign of a is ignored; n should not be zero. */
989
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +0000991divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000992{
Tim Peters212e6142001-07-14 12:23:19 +0000993 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000995
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000996 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000998 if (z == NULL)
999 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001000 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001001 return long_normalize(z);
1002}
1003
1004/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001005 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001006 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001008static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001009long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001010{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 register PyLongObject *a = (PyLongObject *)aa;
1012 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001013 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001014 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001015 char *p;
1016 int bits;
1017 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019 if (a == NULL || !PyLong_Check(a)) {
1020 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001021 return NULL;
1022 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001023 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001024
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001025 /* Compute a rough upper bound for the length of the string */
1026 i = base;
1027 bits = 0;
1028 while (i > 1) {
1029 ++bits;
1030 i >>= 1;
1031 }
Fred Drake121ee271999-12-23 15:41:28 +00001032 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001034 if (str == NULL)
1035 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001037 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001038 if (addL)
1039 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001040 if (a->ob_size < 0)
1041 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001042
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001043 if (a->ob_size == 0) {
1044 *--p = '0';
1045 }
1046 else if ((base & (base - 1)) == 0) {
1047 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001048 twodigits accum = 0;
1049 int accumbits = 0; /* # of bits in accum */
1050 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001051 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001052 while ((i >>= 1) > 1)
1053 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001054
1055 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001056 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001057 accumbits += SHIFT;
1058 assert(accumbits >= basebits);
1059 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001060 char cdigit = (char)(accum & (base - 1));
1061 cdigit += (cdigit < 10) ? '0' : 'A'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001062 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001063 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001064 accumbits -= basebits;
1065 accum >>= basebits;
1066 } while (i < size_a-1 ? accumbits >= basebits :
1067 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001068 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001069 }
1070 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001071 /* Not 0, and base not a power of 2. Divide repeatedly by
1072 base, but for speed use the highest power of base that
1073 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001074 int size = size_a;
1075 digit *pin = a->ob_digit;
1076 PyLongObject *scratch;
1077 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001078 digit powbase = base; /* powbase == base ** power */
1079 int power = 1;
1080 for (;;) {
1081 unsigned long newpow = powbase * (unsigned long)base;
1082 if (newpow >> SHIFT) /* doesn't fit in a digit */
1083 break;
1084 powbase = (digit)newpow;
1085 ++power;
1086 }
Tim Peters212e6142001-07-14 12:23:19 +00001087
1088 /* Get a scratch area for repeated division. */
1089 scratch = _PyLong_New(size);
1090 if (scratch == NULL) {
1091 Py_DECREF(str);
1092 return NULL;
1093 }
1094
1095 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001096 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001097 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001098 digit rem = inplace_divrem1(scratch->ob_digit,
1099 pin, size, powbase);
1100 pin = scratch->ob_digit; /* no need to use a again */
1101 if (pin[size - 1] == 0)
1102 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001103 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001104 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001105 Py_DECREF(str);
1106 return NULL;
1107 })
Tim Peters212e6142001-07-14 12:23:19 +00001108
1109 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001110 assert(ntostore > 0);
1111 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001112 digit nextrem = (digit)(rem / base);
1113 char c = (char)(rem - nextrem * base);
1114 assert(p > PyString_AS_STRING(str));
1115 c += (c < 10) ? '0' : 'A'-10;
1116 *--p = c;
1117 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001118 --ntostore;
1119 /* Termination is a bit delicate: must not
1120 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001121 remaining quotient and rem are both 0. */
1122 } while (ntostore && (size || rem));
1123 } while (size != 0);
1124 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001125 }
1126
Guido van Rossum2c475421992-08-14 15:13:07 +00001127 if (base == 8) {
1128 if (size_a != 0)
1129 *--p = '0';
1130 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001131 else if (base == 16) {
1132 *--p = 'x';
1133 *--p = '0';
1134 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001135 else if (base != 10) {
1136 *--p = '#';
1137 *--p = '0' + base%10;
1138 if (base > 10)
1139 *--p = '0' + base/10;
1140 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001141 if (sign)
1142 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001143 if (p != PyString_AS_STRING(str)) {
1144 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001145 assert(p > q);
1146 do {
1147 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001148 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001149 _PyString_Resize((PyObject **)&str,
1150 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001151 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001153}
1154
Tim Petersbf2674b2003-02-02 07:51:32 +00001155/* *str points to the first digit in a string of base base digits. base
1156 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1157 * non-digit (which may be *str!). A normalized long is returned.
1158 * The point to this routine is that it takes time linear in the number of
1159 * string characters.
1160 */
1161static PyLongObject *
1162long_from_binary_base(char **str, int base)
1163{
1164 char *p = *str;
1165 char *start = p;
1166 int bits_per_char;
1167 int n;
1168 PyLongObject *z;
1169 twodigits accum;
1170 int bits_in_accum;
1171 digit *pdigit;
1172
1173 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1174 n = base;
1175 for (bits_per_char = -1; n; ++bits_per_char)
1176 n >>= 1;
1177 /* n <- total # of bits needed, while setting p to end-of-string */
1178 n = 0;
1179 for (;;) {
1180 int k = -1;
1181 char ch = *p;
1182
1183 if (ch <= '9')
1184 k = ch - '0';
1185 else if (ch >= 'a')
1186 k = ch - 'a' + 10;
1187 else if (ch >= 'A')
1188 k = ch - 'A' + 10;
1189 if (k < 0 || k >= base)
1190 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001191 ++p;
1192 }
1193 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001194 n = (p - start) * bits_per_char;
1195 if (n / bits_per_char != p - start) {
1196 PyErr_SetString(PyExc_ValueError,
1197 "long string too large to convert");
1198 return NULL;
1199 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001200 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1201 n = (n + SHIFT - 1) / SHIFT;
1202 z = _PyLong_New(n);
1203 if (z == NULL)
1204 return NULL;
1205 /* Read string from right, and fill in long from left; i.e.,
1206 * from least to most significant in both.
1207 */
1208 accum = 0;
1209 bits_in_accum = 0;
1210 pdigit = z->ob_digit;
1211 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001212 int k;
1213 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001214
1215 if (ch <= '9')
1216 k = ch - '0';
1217 else if (ch >= 'a')
1218 k = ch - 'a' + 10;
1219 else {
1220 assert(ch >= 'A');
1221 k = ch - 'A' + 10;
1222 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001223 assert(k >= 0 && k < base);
1224 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001225 bits_in_accum += bits_per_char;
1226 if (bits_in_accum >= SHIFT) {
1227 *pdigit++ = (digit)(accum & MASK);
1228 assert(pdigit - z->ob_digit <= n);
1229 accum >>= SHIFT;
1230 bits_in_accum -= SHIFT;
1231 assert(bits_in_accum < SHIFT);
1232 }
1233 }
1234 if (bits_in_accum) {
1235 assert(bits_in_accum <= SHIFT);
1236 *pdigit++ = (digit)accum;
1237 assert(pdigit - z->ob_digit <= n);
1238 }
1239 while (pdigit - z->ob_digit < n)
1240 *pdigit++ = 0;
1241 return long_normalize(z);
1242}
1243
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001245PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001246{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001247 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001248 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001249 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001250
Guido van Rossum472c04f1996-12-05 21:57:21 +00001251 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001252 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001253 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001254 return NULL;
1255 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001256 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001257 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001258 if (*str == '+')
1259 ++str;
1260 else if (*str == '-') {
1261 ++str;
1262 sign = -1;
1263 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001264 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001265 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001266 if (base == 0) {
1267 if (str[0] != '0')
1268 base = 10;
1269 else if (str[1] == 'x' || str[1] == 'X')
1270 base = 16;
1271 else
1272 base = 8;
1273 }
1274 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1275 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001276 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001277 if ((base & (base - 1)) == 0)
1278 z = long_from_binary_base(&str, base);
1279 else {
1280 z = _PyLong_New(0);
1281 for ( ; z != NULL; ++str) {
1282 int k = -1;
1283 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001284
Tim Petersbf2674b2003-02-02 07:51:32 +00001285 if (*str <= '9')
1286 k = *str - '0';
1287 else if (*str >= 'a')
1288 k = *str - 'a' + 10;
1289 else if (*str >= 'A')
1290 k = *str - 'A' + 10;
1291 if (k < 0 || k >= base)
1292 break;
1293 temp = muladd1(z, (digit)base, (digit)k);
1294 Py_DECREF(z);
1295 z = temp;
1296 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001297 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001298 if (z == NULL)
1299 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001300 if (str == start)
1301 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001302 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001303 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001304 if (*str == 'L' || *str == 'l')
1305 str++;
1306 while (*str && isspace(Py_CHARMASK(*str)))
1307 str++;
1308 if (*str != '\0')
1309 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001310 if (pend)
1311 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001313
1314 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001315 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001316 "invalid literal for long(): %.200s", orig_str);
1317 Py_XDECREF(z);
1318 return NULL;
1319}
1320
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001321#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001322PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001323PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001324{
Walter Dörwald07e14762002-11-06 16:15:14 +00001325 PyObject *result;
1326 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001327
Walter Dörwald07e14762002-11-06 16:15:14 +00001328 if (buffer == NULL)
1329 return NULL;
1330
1331 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1332 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001333 return NULL;
1334 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001335 result = PyLong_FromString(buffer, NULL, base);
1336 PyMem_FREE(buffer);
1337 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001338}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001339#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001340
Tim Peters9f688bf2000-07-07 15:53:28 +00001341/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001343 (PyLongObject *, PyLongObject *, PyLongObject **);
1344static PyObject *long_pos(PyLongObject *);
1345static int long_divrem(PyLongObject *, PyLongObject *,
1346 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001347
1348/* Long division with remainder, top-level routine */
1349
Guido van Rossume32e0141992-01-19 16:31:05 +00001350static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001351long_divrem(PyLongObject *a, PyLongObject *b,
1352 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001353{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001354 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001356
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001357 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001358 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001359 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001360 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001361 }
1362 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001363 (size_a == size_b &&
1364 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001365 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366 *pdiv = _PyLong_New(0);
1367 Py_INCREF(a);
1368 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001369 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001370 }
1371 if (size_b == 1) {
1372 digit rem = 0;
1373 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001374 if (z == NULL)
1375 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001377 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001378 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001379 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001380 if (z == NULL)
1381 return -1;
1382 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001383 /* Set the signs.
1384 The quotient z has the sign of a*b;
1385 the remainder r has the sign of a,
1386 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001387 if ((a->ob_size < 0) != (b->ob_size < 0))
1388 z->ob_size = -(z->ob_size);
1389 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1390 (*prem)->ob_size = -((*prem)->ob_size);
1391 *pdiv = z;
1392 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001393}
1394
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001395/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001396
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001398x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001399{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001400 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001401 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 PyLongObject *v = mul1(v1, d);
1403 PyLongObject *w = mul1(w1, d);
1404 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001405 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001406
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001407 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 Py_XDECREF(v);
1409 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001410 return NULL;
1411 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001412
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001413 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001414 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001415 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001416
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001417 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001418 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001419
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001420 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1421 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1422 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001423 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001424 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001425
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001426 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001428 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001429 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001430 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001431 if (vj == w->ob_digit[size_w-1])
1432 q = MASK;
1433 else
1434 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1435 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001436
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001437 while (w->ob_digit[size_w-2]*q >
1438 ((
1439 ((twodigits)vj << SHIFT)
1440 + v->ob_digit[j-1]
1441 - q*w->ob_digit[size_w-1]
1442 ) << SHIFT)
1443 + v->ob_digit[j-2])
1444 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001445
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001446 for (i = 0; i < size_w && i+k < size_v; ++i) {
1447 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001448 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001449 carry += v->ob_digit[i+k] - z
1450 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001451 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001452 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1453 carry, SHIFT);
1454 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001455 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001456
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001457 if (i+k < size_v) {
1458 carry += v->ob_digit[i+k];
1459 v->ob_digit[i+k] = 0;
1460 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001461
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001462 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001463 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001464 else {
1465 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001466 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001467 carry = 0;
1468 for (i = 0; i < size_w && i+k < size_v; ++i) {
1469 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001470 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001471 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1472 BASE_TWODIGITS_TYPE,
1473 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001474 }
1475 }
1476 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001477
Guido van Rossumc206c761995-01-10 15:23:19 +00001478 if (a == NULL)
1479 *prem = NULL;
1480 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001481 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001482 *prem = divrem1(v, d, &d);
1483 /* d receives the (unused) remainder */
1484 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001485 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001486 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001487 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001488 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489 Py_DECREF(v);
1490 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001491 return a;
1492}
1493
1494/* Methods */
1495
1496static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001497long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001498{
Guido van Rossum9475a232001-10-05 20:51:39 +00001499 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001500}
1501
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001502static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001503long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001504{
Fred Drake121ee271999-12-23 15:41:28 +00001505 return long_format(v, 10, 1);
1506}
1507
1508static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001509long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001510{
1511 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001512}
1513
1514static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001515long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001516{
1517 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001518
Guido van Rossumc6913e71991-11-19 20:26:46 +00001519 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001520 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001521 sign = 0;
1522 else
1523 sign = a->ob_size - b->ob_size;
1524 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001525 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001526 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001527 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1528 ;
1529 if (i < 0)
1530 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001531 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001532 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001533 if (a->ob_size < 0)
1534 sign = -sign;
1535 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001536 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001537 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001538}
1539
Guido van Rossum9bfef441993-03-29 10:43:31 +00001540static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001541long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001542{
1543 long x;
1544 int i, sign;
1545
1546 /* This is designed so that Python ints and longs with the
1547 same value hash to the same value, otherwise comparisons
1548 of mapping keys will turn out weird */
1549 i = v->ob_size;
1550 sign = 1;
1551 x = 0;
1552 if (i < 0) {
1553 sign = -1;
1554 i = -(i);
1555 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001556#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001557 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001558 /* Force a native long #-bits (32 or 64) circular shift */
1559 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001560 x += v->ob_digit[i];
1561 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001562#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001563 x = x * sign;
1564 if (x == -1)
1565 x = -2;
1566 return x;
1567}
1568
1569
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001570/* Add the absolute values of two long integers. */
1571
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001573x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001574{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001575 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001576 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001577 int i;
1578 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001579
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001580 /* Ensure a is the larger of the two: */
1581 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001582 { PyLongObject *temp = a; a = b; b = temp; }
1583 { int size_temp = size_a;
1584 size_a = size_b;
1585 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001586 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001587 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001588 if (z == NULL)
1589 return NULL;
1590 for (i = 0; i < size_b; ++i) {
1591 carry += a->ob_digit[i] + b->ob_digit[i];
1592 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001593 carry >>= SHIFT;
1594 }
1595 for (; i < size_a; ++i) {
1596 carry += a->ob_digit[i];
1597 z->ob_digit[i] = carry & MASK;
1598 carry >>= SHIFT;
1599 }
1600 z->ob_digit[i] = carry;
1601 return long_normalize(z);
1602}
1603
1604/* Subtract the absolute values of two integers. */
1605
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001606static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001607x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001608{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001609 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001610 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001611 int i;
1612 int sign = 1;
1613 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001614
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001615 /* Ensure a is the larger of the two: */
1616 if (size_a < size_b) {
1617 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001618 { PyLongObject *temp = a; a = b; b = temp; }
1619 { int size_temp = size_a;
1620 size_a = size_b;
1621 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001622 }
1623 else if (size_a == size_b) {
1624 /* Find highest digit where a and b differ: */
1625 i = size_a;
1626 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1627 ;
1628 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001630 if (a->ob_digit[i] < b->ob_digit[i]) {
1631 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001632 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001633 }
1634 size_a = size_b = i+1;
1635 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001636 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001637 if (z == NULL)
1638 return NULL;
1639 for (i = 0; i < size_b; ++i) {
1640 /* The following assumes unsigned arithmetic
1641 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001642 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001643 z->ob_digit[i] = borrow & MASK;
1644 borrow >>= SHIFT;
1645 borrow &= 1; /* Keep only one sign bit */
1646 }
1647 for (; i < size_a; ++i) {
1648 borrow = a->ob_digit[i] - borrow;
1649 z->ob_digit[i] = borrow & MASK;
1650 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001651 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001652 }
1653 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001654 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001655 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001656 return long_normalize(z);
1657}
1658
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001659static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001660long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001661{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001662 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001663
Neil Schemenauerba872e22001-01-04 01:46:03 +00001664 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1665
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001666 if (a->ob_size < 0) {
1667 if (b->ob_size < 0) {
1668 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001669 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001670 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001671 }
1672 else
1673 z = x_sub(b, a);
1674 }
1675 else {
1676 if (b->ob_size < 0)
1677 z = x_sub(a, b);
1678 else
1679 z = x_add(a, b);
1680 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001681 Py_DECREF(a);
1682 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001683 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001684}
1685
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001686static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001687long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001688{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001689 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001690
Neil Schemenauerba872e22001-01-04 01:46:03 +00001691 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1692
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001693 if (a->ob_size < 0) {
1694 if (b->ob_size < 0)
1695 z = x_sub(a, b);
1696 else
1697 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001698 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001699 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001700 }
1701 else {
1702 if (b->ob_size < 0)
1703 z = x_add(a, b);
1704 else
1705 z = x_sub(a, b);
1706 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001707 Py_DECREF(a);
1708 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001709 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001710}
1711
Tim Peters5af4e6c2002-08-12 02:31:19 +00001712/* Grade school multiplication, ignoring the signs.
1713 * Returns the absolute value of the product, or NULL if error.
1714 */
1715static PyLongObject *
1716x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001717{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001718 PyLongObject *z;
1719 int size_a = ABS(a->ob_size);
1720 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001721 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001722
Tim Peters5af4e6c2002-08-12 02:31:19 +00001723 z = _PyLong_New(size_a + size_b);
1724 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001725 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001726
1727 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001728 if (a == b) {
1729 /* Efficient squaring per HAC, Algorithm 14.16:
1730 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1731 * Gives slightly less than a 2x speedup when a == b,
1732 * via exploiting that each entry in the multiplication
1733 * pyramid appears twice (except for the size_a squares).
1734 */
1735 for (i = 0; i < size_a; ++i) {
1736 twodigits carry;
1737 twodigits f = a->ob_digit[i];
1738 digit *pz = z->ob_digit + (i << 1);
1739 digit *pa = a->ob_digit + i + 1;
1740 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001741
Tim Peters0973b992004-08-29 22:16:50 +00001742 SIGCHECK({
1743 Py_DECREF(z);
1744 return NULL;
1745 })
1746
1747 carry = *pz + f * f;
1748 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001749 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001750 assert(carry <= MASK);
1751
1752 /* Now f is added in twice in each column of the
1753 * pyramid it appears. Same as adding f<<1 once.
1754 */
1755 f <<= 1;
1756 while (pa < paend) {
1757 carry += *pz + *pa++ * f;
1758 *pz++ = (digit)(carry & MASK);
1759 carry >>= SHIFT;
1760 assert(carry <= (MASK << 1));
1761 }
1762 if (carry) {
1763 carry += *pz;
1764 *pz++ = (digit)(carry & MASK);
1765 carry >>= SHIFT;
1766 }
1767 if (carry)
1768 *pz += (digit)(carry & MASK);
1769 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001770 }
Tim Peters0973b992004-08-29 22:16:50 +00001771 }
1772 else { /* a is not the same as b -- gradeschool long mult */
1773 for (i = 0; i < size_a; ++i) {
1774 twodigits carry = 0;
1775 twodigits f = a->ob_digit[i];
1776 digit *pz = z->ob_digit + i;
1777 digit *pb = b->ob_digit;
1778 digit *pbend = b->ob_digit + size_b;
1779
1780 SIGCHECK({
1781 Py_DECREF(z);
1782 return NULL;
1783 })
1784
1785 while (pb < pbend) {
1786 carry += *pz + *pb++ * f;
1787 *pz++ = (digit)(carry & MASK);
1788 carry >>= SHIFT;
1789 assert(carry <= MASK);
1790 }
1791 if (carry)
1792 *pz += (digit)(carry & MASK);
1793 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001794 }
1795 }
Tim Peters44121a62002-08-12 06:17:58 +00001796 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001797}
1798
1799/* A helper for Karatsuba multiplication (k_mul).
1800 Takes a long "n" and an integer "size" representing the place to
1801 split, and sets low and high such that abs(n) == (high << size) + low,
1802 viewing the shift as being by digits. The sign bit is ignored, and
1803 the return values are >= 0.
1804 Returns 0 on success, -1 on failure.
1805*/
1806static int
1807kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1808{
1809 PyLongObject *hi, *lo;
1810 int size_lo, size_hi;
1811 const int size_n = ABS(n->ob_size);
1812
1813 size_lo = MIN(size_n, size);
1814 size_hi = size_n - size_lo;
1815
1816 if ((hi = _PyLong_New(size_hi)) == NULL)
1817 return -1;
1818 if ((lo = _PyLong_New(size_lo)) == NULL) {
1819 Py_DECREF(hi);
1820 return -1;
1821 }
1822
1823 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1824 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1825
1826 *high = long_normalize(hi);
1827 *low = long_normalize(lo);
1828 return 0;
1829}
1830
Tim Peters60004642002-08-12 22:01:34 +00001831static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1832
Tim Peters5af4e6c2002-08-12 02:31:19 +00001833/* Karatsuba multiplication. Ignores the input signs, and returns the
1834 * absolute value of the product (or NULL if error).
1835 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1836 */
1837static PyLongObject *
1838k_mul(PyLongObject *a, PyLongObject *b)
1839{
Tim Peters738eda72002-08-12 15:08:20 +00001840 int asize = ABS(a->ob_size);
1841 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001842 PyLongObject *ah = NULL;
1843 PyLongObject *al = NULL;
1844 PyLongObject *bh = NULL;
1845 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001846 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001847 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001848 int shift; /* the number of digits we split off */
1849 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001850
Tim Peters5af4e6c2002-08-12 02:31:19 +00001851 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1852 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1853 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001854 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001855 * By picking X to be a power of 2, "*X" is just shifting, and it's
1856 * been reduced to 3 multiplies on numbers half the size.
1857 */
1858
Tim Petersfc07e562002-08-12 02:54:10 +00001859 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001860 * is largest.
1861 */
Tim Peters738eda72002-08-12 15:08:20 +00001862 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001863 t1 = a;
1864 a = b;
1865 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001866
1867 i = asize;
1868 asize = bsize;
1869 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001870 }
1871
1872 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001873 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1874 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001875 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001876 return _PyLong_New(0);
1877 else
1878 return x_mul(a, b);
1879 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001880
Tim Peters60004642002-08-12 22:01:34 +00001881 /* If a is small compared to b, splitting on b gives a degenerate
1882 * case with ah==0, and Karatsuba may be (even much) less efficient
1883 * than "grade school" then. However, we can still win, by viewing
1884 * b as a string of "big digits", each of width a->ob_size. That
1885 * leads to a sequence of balanced calls to k_mul.
1886 */
1887 if (2 * asize <= bsize)
1888 return k_lopsided_mul(a, b);
1889
Tim Petersd6974a52002-08-13 20:37:51 +00001890 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001891 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001892 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001893 assert(ah->ob_size > 0); /* the split isn't degenerate */
1894
Tim Peters0973b992004-08-29 22:16:50 +00001895 if (a == b) {
1896 bh = ah;
1897 bl = al;
1898 Py_INCREF(bh);
1899 Py_INCREF(bl);
1900 }
1901 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001902
Tim Petersd64c1de2002-08-12 17:36:03 +00001903 /* The plan:
1904 * 1. Allocate result space (asize + bsize digits: that's always
1905 * enough).
1906 * 2. Compute ah*bh, and copy into result at 2*shift.
1907 * 3. Compute al*bl, and copy into result at 0. Note that this
1908 * can't overlap with #2.
1909 * 4. Subtract al*bl from the result, starting at shift. This may
1910 * underflow (borrow out of the high digit), but we don't care:
1911 * we're effectively doing unsigned arithmetic mod
1912 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1913 * borrows and carries out of the high digit can be ignored.
1914 * 5. Subtract ah*bh from the result, starting at shift.
1915 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1916 * at shift.
1917 */
1918
1919 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001920 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001921 if (ret == NULL) goto fail;
1922#ifdef Py_DEBUG
1923 /* Fill with trash, to catch reference to uninitialized digits. */
1924 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1925#endif
Tim Peters44121a62002-08-12 06:17:58 +00001926
Tim Petersd64c1de2002-08-12 17:36:03 +00001927 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001928 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1929 assert(t1->ob_size >= 0);
1930 assert(2*shift + t1->ob_size <= ret->ob_size);
1931 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1932 t1->ob_size * sizeof(digit));
1933
1934 /* Zero-out the digits higher than the ah*bh copy. */
1935 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001936 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001937 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001938 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001939
Tim Petersd64c1de2002-08-12 17:36:03 +00001940 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001941 if ((t2 = k_mul(al, bl)) == NULL) {
1942 Py_DECREF(t1);
1943 goto fail;
1944 }
1945 assert(t2->ob_size >= 0);
1946 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1947 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001948
1949 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001950 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001951 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001952 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001953
Tim Petersd64c1de2002-08-12 17:36:03 +00001954 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1955 * because it's fresher in cache.
1956 */
Tim Peters738eda72002-08-12 15:08:20 +00001957 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001958 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001959 Py_DECREF(t2);
1960
Tim Petersd64c1de2002-08-12 17:36:03 +00001961 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001962 Py_DECREF(t1);
1963
Tim Petersd64c1de2002-08-12 17:36:03 +00001964 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001965 if ((t1 = x_add(ah, al)) == NULL) goto fail;
1966 Py_DECREF(ah);
1967 Py_DECREF(al);
1968 ah = al = NULL;
1969
Tim Peters0973b992004-08-29 22:16:50 +00001970 if (a == b) {
1971 t2 = t1;
1972 Py_INCREF(t2);
1973 }
1974 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001975 Py_DECREF(t1);
1976 goto fail;
1977 }
1978 Py_DECREF(bh);
1979 Py_DECREF(bl);
1980 bh = bl = NULL;
1981
Tim Peters738eda72002-08-12 15:08:20 +00001982 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001983 Py_DECREF(t1);
1984 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00001985 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00001986 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001987
Tim Petersd6974a52002-08-13 20:37:51 +00001988 /* Add t3. It's not obvious why we can't run out of room here.
1989 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00001990 */
Tim Petersd64c1de2002-08-12 17:36:03 +00001991 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001992 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001993
Tim Peters5af4e6c2002-08-12 02:31:19 +00001994 return long_normalize(ret);
1995
1996 fail:
1997 Py_XDECREF(ret);
1998 Py_XDECREF(ah);
1999 Py_XDECREF(al);
2000 Py_XDECREF(bh);
2001 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002002 return NULL;
2003}
2004
Tim Petersd6974a52002-08-13 20:37:51 +00002005/* (*) Why adding t3 can't "run out of room" above.
2006
Tim Petersab86c2b2002-08-15 20:06:00 +00002007Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2008to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002009
Tim Petersab86c2b2002-08-15 20:06:00 +000020101. For any integer i, i = c(i/2) + f(i/2). In particular,
2011 bsize = c(bsize/2) + f(bsize/2).
20122. shift = f(bsize/2)
20133. asize <= bsize
20144. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2015 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002016
Tim Petersab86c2b2002-08-15 20:06:00 +00002017We allocated asize + bsize result digits, and add t3 into them at an offset
2018of shift. This leaves asize+bsize-shift allocated digit positions for t3
2019to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2020asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002021
Tim Petersab86c2b2002-08-15 20:06:00 +00002022bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2023at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002024
Tim Petersab86c2b2002-08-15 20:06:00 +00002025If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2026digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2027most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002028
Tim Petersab86c2b2002-08-15 20:06:00 +00002029The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002030
Tim Petersab86c2b2002-08-15 20:06:00 +00002031 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002032
Tim Petersab86c2b2002-08-15 20:06:00 +00002033and we have asize + c(bsize/2) available digit positions. We need to show
2034this is always enough. An instance of c(bsize/2) cancels out in both, so
2035the question reduces to whether asize digits is enough to hold
2036(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2037then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2038asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2039digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2040asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002041c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2042is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2043bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002044
Tim Peters48d52c02002-08-14 17:07:32 +00002045Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2046clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2047ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002048*/
2049
Tim Peters60004642002-08-12 22:01:34 +00002050/* b has at least twice the digits of a, and a is big enough that Karatsuba
2051 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2052 * of slices, each with a->ob_size digits, and multiply the slices by a,
2053 * one at a time. This gives k_mul balanced inputs to work with, and is
2054 * also cache-friendly (we compute one double-width slice of the result
2055 * at a time, then move on, never bactracking except for the helpful
2056 * single-width slice overlap between successive partial sums).
2057 */
2058static PyLongObject *
2059k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2060{
2061 const int asize = ABS(a->ob_size);
2062 int bsize = ABS(b->ob_size);
2063 int nbdone; /* # of b digits already multiplied */
2064 PyLongObject *ret;
2065 PyLongObject *bslice = NULL;
2066
2067 assert(asize > KARATSUBA_CUTOFF);
2068 assert(2 * asize <= bsize);
2069
2070 /* Allocate result space, and zero it out. */
2071 ret = _PyLong_New(asize + bsize);
2072 if (ret == NULL)
2073 return NULL;
2074 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2075
2076 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002077 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002078 if (bslice == NULL)
2079 goto fail;
2080
2081 nbdone = 0;
2082 while (bsize > 0) {
2083 PyLongObject *product;
2084 const int nbtouse = MIN(bsize, asize);
2085
2086 /* Multiply the next slice of b by a. */
2087 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2088 nbtouse * sizeof(digit));
2089 bslice->ob_size = nbtouse;
2090 product = k_mul(a, bslice);
2091 if (product == NULL)
2092 goto fail;
2093
2094 /* Add into result. */
2095 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2096 product->ob_digit, product->ob_size);
2097 Py_DECREF(product);
2098
2099 bsize -= nbtouse;
2100 nbdone += nbtouse;
2101 }
2102
2103 Py_DECREF(bslice);
2104 return long_normalize(ret);
2105
2106 fail:
2107 Py_DECREF(ret);
2108 Py_XDECREF(bslice);
2109 return NULL;
2110}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002111
2112static PyObject *
2113long_mul(PyLongObject *v, PyLongObject *w)
2114{
2115 PyLongObject *a, *b, *z;
2116
2117 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002118 Py_INCREF(Py_NotImplemented);
2119 return Py_NotImplemented;
2120 }
2121
Tim Petersd64c1de2002-08-12 17:36:03 +00002122 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002123 /* Negate if exactly one of the inputs is negative. */
2124 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002125 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002126 Py_DECREF(a);
2127 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002128 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002129}
2130
Guido van Rossume32e0141992-01-19 16:31:05 +00002131/* The / and % operators are now defined in terms of divmod().
2132 The expression a mod b has the value a - b*floor(a/b).
2133 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002134 |a| by |b|, with the sign of a. This is also expressed
2135 as a - b*trunc(a/b), if trunc truncates towards zero.
2136 Some examples:
2137 a b a rem b a mod b
2138 13 10 3 3
2139 -13 10 -3 7
2140 13 -10 3 -7
2141 -13 -10 -3 -3
2142 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002143 have different signs. We then subtract one from the 'div'
2144 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002145
Tim Peters47e52ee2004-08-30 02:44:38 +00002146/* Compute
2147 * *pdiv, *pmod = divmod(v, w)
2148 * NULL can be passed for pdiv or pmod, in which case that part of
2149 * the result is simply thrown away. The caller owns a reference to
2150 * each of these it requests (does not pass NULL for).
2151 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002152static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002153l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002154 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002155{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002156 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002157
Guido van Rossume32e0141992-01-19 16:31:05 +00002158 if (long_divrem(v, w, &div, &mod) < 0)
2159 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002160 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2161 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002162 PyLongObject *temp;
2163 PyLongObject *one;
2164 temp = (PyLongObject *) long_add(mod, w);
2165 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002166 mod = temp;
2167 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002168 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002169 return -1;
2170 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002171 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002172 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002173 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2174 Py_DECREF(mod);
2175 Py_DECREF(div);
2176 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002177 return -1;
2178 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002179 Py_DECREF(one);
2180 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002181 div = temp;
2182 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002183 if (pdiv != NULL)
2184 *pdiv = div;
2185 else
2186 Py_DECREF(div);
2187
2188 if (pmod != NULL)
2189 *pmod = mod;
2190 else
2191 Py_DECREF(mod);
2192
Guido van Rossume32e0141992-01-19 16:31:05 +00002193 return 0;
2194}
2195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002196static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002197long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002198{
Tim Peters47e52ee2004-08-30 02:44:38 +00002199 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002200
2201 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002202 if (l_divmod(a, b, &div, NULL) < 0)
2203 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002204 Py_DECREF(a);
2205 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002206 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002207}
2208
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002210long_classic_div(PyObject *v, PyObject *w)
2211{
Tim Peters47e52ee2004-08-30 02:44:38 +00002212 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002213
2214 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002215 if (Py_DivisionWarningFlag &&
2216 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2217 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002218 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002219 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002220 Py_DECREF(a);
2221 Py_DECREF(b);
2222 return (PyObject *)div;
2223}
2224
2225static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002226long_true_divide(PyObject *v, PyObject *w)
2227{
Tim Peterse2a60002001-09-04 06:17:36 +00002228 PyLongObject *a, *b;
2229 double ad, bd;
Tim Peters6f97e492001-11-04 23:09:40 +00002230 int aexp, bexp, failed;
Tim Peterse2a60002001-09-04 06:17:36 +00002231
2232 CONVERT_BINOP(v, w, &a, &b);
2233 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2234 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002235 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2236 Py_DECREF(a);
2237 Py_DECREF(b);
2238 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002239 return NULL;
2240
2241 if (bd == 0.0) {
2242 PyErr_SetString(PyExc_ZeroDivisionError,
2243 "long division or modulo by zero");
2244 return NULL;
2245 }
2246
2247 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2248 ad /= bd; /* overflow/underflow impossible here */
2249 aexp -= bexp;
2250 if (aexp > INT_MAX / SHIFT)
2251 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002252 else if (aexp < -(INT_MAX / SHIFT))
2253 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002254 errno = 0;
2255 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002256 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002257 goto overflow;
2258 return PyFloat_FromDouble(ad);
2259
2260overflow:
2261 PyErr_SetString(PyExc_OverflowError,
2262 "long/long too large for a float");
2263 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002264
Tim Peters20dab9f2001-09-04 05:31:47 +00002265}
2266
2267static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002268long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002269{
Tim Peters47e52ee2004-08-30 02:44:38 +00002270 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002271
2272 CONVERT_BINOP(v, w, &a, &b);
2273
Tim Peters47e52ee2004-08-30 02:44:38 +00002274 if (l_divmod(a, b, NULL, &mod) < 0)
2275 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002276 Py_DECREF(a);
2277 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002278 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002279}
2280
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002281static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002282long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002283{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002284 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002285 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002286
2287 CONVERT_BINOP(v, w, &a, &b);
2288
2289 if (l_divmod(a, b, &div, &mod) < 0) {
2290 Py_DECREF(a);
2291 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002292 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002293 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002294 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002295 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002296 PyTuple_SetItem(z, 0, (PyObject *) div);
2297 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002298 }
2299 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002300 Py_DECREF(div);
2301 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002302 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002303 Py_DECREF(a);
2304 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002305 return z;
2306}
2307
Tim Peters47e52ee2004-08-30 02:44:38 +00002308/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002309static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002310long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002311{
Tim Peters47e52ee2004-08-30 02:44:38 +00002312 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2313 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002314
Tim Peters47e52ee2004-08-30 02:44:38 +00002315 PyLongObject *z = NULL; /* accumulated result */
2316 int i, j, k; /* counters */
2317 PyLongObject *temp = NULL;
2318
2319 /* 5-ary values. If the exponent is large enough, table is
2320 * precomputed so that table[i] == a**i % c for i in range(32).
2321 */
2322 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2323 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2324
2325 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002326 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002327 if (PyLong_Check(x)) {
2328 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002329 Py_INCREF(x);
2330 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002331 else if (PyInt_Check(x))
2332 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
2333 else if (x == Py_None)
2334 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002335 else {
2336 Py_DECREF(a);
2337 Py_DECREF(b);
2338 Py_INCREF(Py_NotImplemented);
2339 return Py_NotImplemented;
2340 }
Tim Peters4c483c42001-09-05 06:24:58 +00002341
Tim Peters47e52ee2004-08-30 02:44:38 +00002342 if (b->ob_size < 0) { /* if exponent is negative */
2343 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002344 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002345 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002346 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002347 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002348 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002349 /* else return a float. This works because we know
2350 that this calls float_pow() which converts its
2351 arguments to double. */
2352 Py_DECREF(a);
2353 Py_DECREF(b);
2354 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002355 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002356 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002357
2358 if (c) {
2359 /* if modulus == 0:
2360 raise ValueError() */
2361 if (c->ob_size == 0) {
2362 PyErr_SetString(PyExc_ValueError,
2363 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002364 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002365 }
2366
2367 /* if modulus < 0:
2368 negativeOutput = True
2369 modulus = -modulus */
2370 if (c->ob_size < 0) {
2371 negativeOutput = 1;
2372 temp = (PyLongObject *)_PyLong_Copy(c);
2373 if (temp == NULL)
2374 goto Error;
2375 Py_DECREF(c);
2376 c = temp;
2377 temp = NULL;
2378 c->ob_size = - c->ob_size;
2379 }
2380
2381 /* if modulus == 1:
2382 return 0 */
2383 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2384 z = (PyLongObject *)PyLong_FromLong(0L);
2385 goto Done;
2386 }
2387
2388 /* if base < 0:
2389 base = base % modulus
2390 Having the base positive just makes things easier. */
2391 if (a->ob_size < 0) {
2392 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002393 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002394 Py_DECREF(a);
2395 a = temp;
2396 temp = NULL;
2397 }
2398 }
2399
2400 /* At this point a, b, and c are guaranteed non-negative UNLESS
2401 c is NULL, in which case a may be negative. */
2402
2403 z = (PyLongObject *)PyLong_FromLong(1L);
2404 if (z == NULL)
2405 goto Error;
2406
2407 /* Perform a modular reduction, X = X % c, but leave X alone if c
2408 * is NULL.
2409 */
2410#define REDUCE(X) \
2411 if (c != NULL) { \
2412 if (l_divmod(X, c, NULL, &temp) < 0) \
2413 goto Error; \
2414 Py_XDECREF(X); \
2415 X = temp; \
2416 temp = NULL; \
2417 }
2418
2419 /* Multiply two values, then reduce the result:
2420 result = X*Y % c. If c is NULL, skip the mod. */
2421#define MULT(X, Y, result) \
2422{ \
2423 temp = (PyLongObject *)long_mul(X, Y); \
2424 if (temp == NULL) \
2425 goto Error; \
2426 Py_XDECREF(result); \
2427 result = temp; \
2428 temp = NULL; \
2429 REDUCE(result) \
2430}
2431
2432 if (b->ob_size <= FIVEARY_CUTOFF) {
2433 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2434 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2435 for (i = b->ob_size - 1; i >= 0; --i) {
2436 digit bi = b->ob_digit[i];
2437
2438 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2439 MULT(z, z, z)
2440 if (bi & j)
2441 MULT(z, a, z)
2442 }
2443 }
2444 }
2445 else {
2446 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2447 Py_INCREF(z); /* still holds 1L */
2448 table[0] = z;
2449 for (i = 1; i < 32; ++i)
2450 MULT(table[i-1], a, table[i])
2451
2452 for (i = b->ob_size - 1; i >= 0; --i) {
2453 const digit bi = b->ob_digit[i];
2454
2455 for (j = SHIFT - 5; j >= 0; j -= 5) {
2456 const int index = (bi >> j) & 0x1f;
2457 for (k = 0; k < 5; ++k)
2458 MULT(z, z, z)
2459 if (index)
2460 MULT(z, table[index], z)
2461 }
2462 }
2463 }
2464
2465 if (negativeOutput && (z->ob_size != 0)) {
2466 temp = (PyLongObject *)long_sub(z, c);
2467 if (temp == NULL)
2468 goto Error;
2469 Py_DECREF(z);
2470 z = temp;
2471 temp = NULL;
2472 }
2473 goto Done;
2474
2475 Error:
2476 if (z != NULL) {
2477 Py_DECREF(z);
2478 z = NULL;
2479 }
2480 /* fall through */
2481 Done:
Neil Schemenauerba872e22001-01-04 01:46:03 +00002482 Py_XDECREF(a);
Tim Peters47e52ee2004-08-30 02:44:38 +00002483 Py_XDECREF(b);
2484 Py_XDECREF(c);
2485 Py_XDECREF(temp);
2486 if (b->ob_size > FIVEARY_CUTOFF) {
2487 for (i = 0; i < 32; ++i)
2488 Py_XDECREF(table[i]);
2489 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002490 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002491}
2492
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002493static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002494long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002495{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002496 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002497 PyLongObject *x;
2498 PyLongObject *w;
2499 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002500 if (w == NULL)
2501 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002502 x = (PyLongObject *) long_add(v, w);
2503 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002504 if (x == NULL)
2505 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002506 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002507 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002508}
2509
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002510static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002511long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002512{
Tim Peters69c2de32001-09-11 22:31:33 +00002513 if (PyLong_CheckExact(v)) {
2514 Py_INCREF(v);
2515 return (PyObject *)v;
2516 }
2517 else
2518 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002519}
2520
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002521static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002522long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002523{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002524 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002525 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002526 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002527 Py_INCREF(v);
2528 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002529 }
Tim Peters69c2de32001-09-11 22:31:33 +00002530 z = (PyLongObject *)_PyLong_Copy(v);
2531 if (z != NULL)
2532 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002533 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002534}
2535
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002536static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002537long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002538{
2539 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002540 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002541 else
2542 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002543}
2544
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002545static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002546long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002547{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002548 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002549}
2550
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002551static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002552long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002553{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002554 PyLongObject *a, *b;
2555 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002556 long shiftby;
2557 int newsize, wordshift, loshift, hishift, i, j;
2558 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002559
Neil Schemenauerba872e22001-01-04 01:46:03 +00002560 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2561
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002562 if (a->ob_size < 0) {
2563 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002564 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002565 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002566 if (a1 == NULL)
2567 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002568 a2 = (PyLongObject *) long_rshift(a1, b);
2569 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002570 if (a2 == NULL)
2571 goto rshift_error;
2572 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002573 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002574 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002575 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002576
Neil Schemenauerba872e22001-01-04 01:46:03 +00002577 shiftby = PyLong_AsLong((PyObject *)b);
2578 if (shiftby == -1L && PyErr_Occurred())
2579 goto rshift_error;
2580 if (shiftby < 0) {
2581 PyErr_SetString(PyExc_ValueError,
2582 "negative shift count");
2583 goto rshift_error;
2584 }
2585 wordshift = shiftby / SHIFT;
2586 newsize = ABS(a->ob_size) - wordshift;
2587 if (newsize <= 0) {
2588 z = _PyLong_New(0);
2589 Py_DECREF(a);
2590 Py_DECREF(b);
2591 return (PyObject *)z;
2592 }
2593 loshift = shiftby % SHIFT;
2594 hishift = SHIFT - loshift;
2595 lomask = ((digit)1 << hishift) - 1;
2596 himask = MASK ^ lomask;
2597 z = _PyLong_New(newsize);
2598 if (z == NULL)
2599 goto rshift_error;
2600 if (a->ob_size < 0)
2601 z->ob_size = -(z->ob_size);
2602 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2603 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2604 if (i+1 < newsize)
2605 z->ob_digit[i] |=
2606 (a->ob_digit[j+1] << hishift) & himask;
2607 }
2608 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002609 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002610rshift_error:
2611 Py_DECREF(a);
2612 Py_DECREF(b);
2613 return (PyObject *) z;
2614
Guido van Rossumc6913e71991-11-19 20:26:46 +00002615}
2616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002617static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002618long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002619{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002620 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002621 PyLongObject *a, *b;
2622 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002623 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002624 int oldsize, newsize, wordshift, remshift, i, j;
2625 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002626
Neil Schemenauerba872e22001-01-04 01:46:03 +00002627 CONVERT_BINOP(v, w, &a, &b);
2628
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002629 shiftby = PyLong_AsLong((PyObject *)b);
2630 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002631 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002632 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002633 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002634 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002635 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002636 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002637 PyErr_SetString(PyExc_ValueError,
2638 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002639 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002640 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002641 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2642 wordshift = (int)shiftby / SHIFT;
2643 remshift = (int)shiftby - wordshift * SHIFT;
2644
2645 oldsize = ABS(a->ob_size);
2646 newsize = oldsize + wordshift;
2647 if (remshift)
2648 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002649 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002650 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002651 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002652 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002653 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002654 for (i = 0; i < wordshift; i++)
2655 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002656 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002657 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002658 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002659 z->ob_digit[i] = (digit)(accum & MASK);
2660 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002661 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002662 if (remshift)
2663 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002664 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002665 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002666 z = long_normalize(z);
2667lshift_error:
2668 Py_DECREF(a);
2669 Py_DECREF(b);
2670 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002671}
2672
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002673
2674/* Bitwise and/xor/or operations */
2675
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002676static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002677long_bitwise(PyLongObject *a,
2678 int op, /* '&', '|', '^' */
2679 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002680{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002681 digit maska, maskb; /* 0 or MASK */
2682 int negz;
2683 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002684 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002685 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002686 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002687 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002688
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002689 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002690 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002691 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002692 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002693 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002694 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002695 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002696 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002697 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002698 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002699 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002700 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002701 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002702 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002703 maskb = 0;
2704 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002705
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002706 negz = 0;
2707 switch (op) {
2708 case '^':
2709 if (maska != maskb) {
2710 maska ^= MASK;
2711 negz = -1;
2712 }
2713 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002714 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002715 if (maska && maskb) {
2716 op = '|';
2717 maska ^= MASK;
2718 maskb ^= MASK;
2719 negz = -1;
2720 }
2721 break;
2722 case '|':
2723 if (maska || maskb) {
2724 op = '&';
2725 maska ^= MASK;
2726 maskb ^= MASK;
2727 negz = -1;
2728 }
2729 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002730 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002731
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002732 /* JRH: The original logic here was to allocate the result value (z)
2733 as the longer of the two operands. However, there are some cases
2734 where the result is guaranteed to be shorter than that: AND of two
2735 positives, OR of two negatives: use the shorter number. AND with
2736 mixed signs: use the positive number. OR with mixed signs: use the
2737 negative number. After the transformations above, op will be '&'
2738 iff one of these cases applies, and mask will be non-0 for operands
2739 whose length should be ignored.
2740 */
2741
2742 size_a = a->ob_size;
2743 size_b = b->ob_size;
2744 size_z = op == '&'
2745 ? (maska
2746 ? size_b
2747 : (maskb ? size_a : MIN(size_a, size_b)))
2748 : MAX(size_a, size_b);
2749 z = _PyLong_New(size_z);
2750 if (a == NULL || b == NULL || z == NULL) {
2751 Py_XDECREF(a);
2752 Py_XDECREF(b);
2753 Py_XDECREF(z);
2754 return NULL;
2755 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002756
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002757 for (i = 0; i < size_z; ++i) {
2758 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2759 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2760 switch (op) {
2761 case '&': z->ob_digit[i] = diga & digb; break;
2762 case '|': z->ob_digit[i] = diga | digb; break;
2763 case '^': z->ob_digit[i] = diga ^ digb; break;
2764 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002765 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002766
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002767 Py_DECREF(a);
2768 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002769 z = long_normalize(z);
2770 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002771 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002772 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002773 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002774 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002775}
2776
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002777static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002778long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002779{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002780 PyLongObject *a, *b;
2781 PyObject *c;
2782 CONVERT_BINOP(v, w, &a, &b);
2783 c = long_bitwise(a, '&', b);
2784 Py_DECREF(a);
2785 Py_DECREF(b);
2786 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002787}
2788
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002789static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002790long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002791{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002792 PyLongObject *a, *b;
2793 PyObject *c;
2794 CONVERT_BINOP(v, w, &a, &b);
2795 c = long_bitwise(a, '^', b);
2796 Py_DECREF(a);
2797 Py_DECREF(b);
2798 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002799}
2800
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002801static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002802long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002803{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002804 PyLongObject *a, *b;
2805 PyObject *c;
2806 CONVERT_BINOP(v, w, &a, &b);
2807 c = long_bitwise(a, '|', b);
2808 Py_DECREF(a);
2809 Py_DECREF(b);
2810 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002811}
2812
Guido van Rossum234f9421993-06-17 12:35:49 +00002813static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002814long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002815{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002816 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002817 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002818 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002819 return 0;
2820 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002821 else if (PyLong_Check(*pw)) {
2822 Py_INCREF(*pv);
2823 Py_INCREF(*pw);
2824 return 0;
2825 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002826 return 1; /* Can't do it */
2827}
2828
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002829static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002830long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002831{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002832 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002833 return v;
2834}
2835
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002836static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002837long_int(PyObject *v)
2838{
2839 long x;
2840 x = PyLong_AsLong(v);
2841 if (PyErr_Occurred()) {
2842 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2843 PyErr_Clear();
2844 if (PyLong_CheckExact(v)) {
2845 Py_INCREF(v);
2846 return v;
2847 }
2848 else
2849 return _PyLong_Copy((PyLongObject *)v);
2850 }
2851 else
2852 return NULL;
2853 }
2854 return PyInt_FromLong(x);
2855}
2856
2857static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002858long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002859{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002860 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002861 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002862 if (result == -1.0 && PyErr_Occurred())
2863 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002864 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002865}
2866
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002867static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002868long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002869{
Fred Drake121ee271999-12-23 15:41:28 +00002870 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002871}
2872
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002873static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002874long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002875{
Fred Drake121ee271999-12-23 15:41:28 +00002876 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002877}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002878
2879static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002880long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002881
Tim Peters6d6c1a32001-08-02 04:15:00 +00002882static PyObject *
2883long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2884{
2885 PyObject *x = NULL;
2886 int base = -909; /* unlikely! */
2887 static char *kwlist[] = {"x", "base", 0};
2888
Guido van Rossumbef14172001-08-29 15:47:46 +00002889 if (type != &PyLong_Type)
2890 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002891 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2892 &x, &base))
2893 return NULL;
2894 if (x == NULL)
2895 return PyLong_FromLong(0L);
2896 if (base == -909)
2897 return PyNumber_Long(x);
2898 else if (PyString_Check(x))
2899 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002900#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002901 else if (PyUnicode_Check(x))
2902 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2903 PyUnicode_GET_SIZE(x),
2904 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002905#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002906 else {
2907 PyErr_SetString(PyExc_TypeError,
2908 "long() can't convert non-string with explicit base");
2909 return NULL;
2910 }
2911}
2912
Guido van Rossumbef14172001-08-29 15:47:46 +00002913/* Wimpy, slow approach to tp_new calls for subtypes of long:
2914 first create a regular long from whatever arguments we got,
2915 then allocate a subtype instance and initialize it from
2916 the regular long. The regular long is then thrown away.
2917*/
2918static PyObject *
2919long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2920{
2921 PyLongObject *tmp, *new;
2922 int i, n;
2923
2924 assert(PyType_IsSubtype(type, &PyLong_Type));
2925 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2926 if (tmp == NULL)
2927 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002928 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002929 n = tmp->ob_size;
2930 if (n < 0)
2931 n = -n;
2932 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002933 if (new == NULL) {
2934 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002935 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002936 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002937 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002938 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002939 for (i = 0; i < n; i++)
2940 new->ob_digit[i] = tmp->ob_digit[i];
2941 Py_DECREF(tmp);
2942 return (PyObject *)new;
2943}
2944
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002945static PyObject *
2946long_getnewargs(PyLongObject *v)
2947{
2948 return Py_BuildValue("(N)", _PyLong_Copy(v));
2949}
2950
2951static PyMethodDef long_methods[] = {
2952 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2953 {NULL, NULL} /* sentinel */
2954};
2955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002956PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002957"long(x[, base]) -> integer\n\
2958\n\
2959Convert a string or number to a long integer, if possible. A floating\n\
2960point argument will be truncated towards zero (this does not include a\n\
2961string representation of a floating point number!) When converting a\n\
2962string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002964
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002965static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00002966 (binaryfunc) long_add, /*nb_add*/
2967 (binaryfunc) long_sub, /*nb_subtract*/
2968 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00002969 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002970 (binaryfunc) long_mod, /*nb_remainder*/
2971 (binaryfunc) long_divmod, /*nb_divmod*/
2972 (ternaryfunc) long_pow, /*nb_power*/
2973 (unaryfunc) long_neg, /*nb_negative*/
2974 (unaryfunc) long_pos, /*tp_positive*/
2975 (unaryfunc) long_abs, /*tp_absolute*/
2976 (inquiry) long_nonzero, /*tp_nonzero*/
2977 (unaryfunc) long_invert, /*nb_invert*/
2978 (binaryfunc) long_lshift, /*nb_lshift*/
2979 (binaryfunc) long_rshift, /*nb_rshift*/
2980 (binaryfunc) long_and, /*nb_and*/
2981 (binaryfunc) long_xor, /*nb_xor*/
2982 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00002983 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002984 (unaryfunc) long_int, /*nb_int*/
2985 (unaryfunc) long_long, /*nb_long*/
2986 (unaryfunc) long_float, /*nb_float*/
2987 (unaryfunc) long_oct, /*nb_oct*/
2988 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00002989 0, /* nb_inplace_add */
2990 0, /* nb_inplace_subtract */
2991 0, /* nb_inplace_multiply */
2992 0, /* nb_inplace_divide */
2993 0, /* nb_inplace_remainder */
2994 0, /* nb_inplace_power */
2995 0, /* nb_inplace_lshift */
2996 0, /* nb_inplace_rshift */
2997 0, /* nb_inplace_and */
2998 0, /* nb_inplace_xor */
2999 0, /* nb_inplace_or */
3000 (binaryfunc)long_div, /* nb_floor_divide */
3001 long_true_divide, /* nb_true_divide */
3002 0, /* nb_inplace_floor_divide */
3003 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003004};
3005
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003006PyTypeObject PyLong_Type = {
3007 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003008 0, /* ob_size */
3009 "long", /* tp_name */
3010 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3011 sizeof(digit), /* tp_itemsize */
3012 (destructor)long_dealloc, /* tp_dealloc */
3013 0, /* tp_print */
3014 0, /* tp_getattr */
3015 0, /* tp_setattr */
3016 (cmpfunc)long_compare, /* tp_compare */
3017 (reprfunc)long_repr, /* tp_repr */
3018 &long_as_number, /* tp_as_number */
3019 0, /* tp_as_sequence */
3020 0, /* tp_as_mapping */
3021 (hashfunc)long_hash, /* tp_hash */
3022 0, /* tp_call */
3023 (reprfunc)long_str, /* tp_str */
3024 PyObject_GenericGetAttr, /* tp_getattro */
3025 0, /* tp_setattro */
3026 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3028 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003029 long_doc, /* tp_doc */
3030 0, /* tp_traverse */
3031 0, /* tp_clear */
3032 0, /* tp_richcompare */
3033 0, /* tp_weaklistoffset */
3034 0, /* tp_iter */
3035 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003036 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003037 0, /* tp_members */
3038 0, /* tp_getset */
3039 0, /* tp_base */
3040 0, /* tp_dict */
3041 0, /* tp_descr_get */
3042 0, /* tp_descr_set */
3043 0, /* tp_dictoffset */
3044 0, /* tp_init */
3045 0, /* tp_alloc */
3046 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003047 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003048};