blob: ff5ba6f2eba009f57f9541951d9b219375462aa9 [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)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000248 if (vv != NULL && PyInt_Check(vv)) {
249 long val = PyInt_AsLong(vv);
250 if (val < 0) {
251 PyErr_SetString(PyExc_OverflowError,
252 "can't convert negative value to unsigned long");
253 return (unsigned long) -1;
254 }
255 return val;
256 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000257 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000258 return (unsigned long) -1;
259 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000261 i = v->ob_size;
262 x = 0;
263 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000265 "can't convert negative value to unsigned long");
266 return (unsigned long) -1;
267 }
268 while (--i >= 0) {
269 prev = x;
270 x = (x << SHIFT) + v->ob_digit[i];
271 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000272 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000273 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000274 return (unsigned long) -1;
275 }
276 }
277 return x;
278}
279
Thomas Hellera4ea6032003-04-17 18:55:45 +0000280/* Get a C unsigned long int from a long int object, ignoring the high bits.
281 Returns -1 and sets an error condition if an error occurs. */
282
283unsigned long
284PyLong_AsUnsignedLongMask(PyObject *vv)
285{
286 register PyLongObject *v;
287 unsigned long x;
288 int i, sign;
289
290 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000291 if (vv != NULL && PyInt_Check(vv))
292 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000293 PyErr_BadInternalCall();
294 return (unsigned long) -1;
295 }
296 v = (PyLongObject *)vv;
297 i = v->ob_size;
298 sign = 1;
299 x = 0;
300 if (i < 0) {
301 sign = -1;
302 i = -i;
303 }
304 while (--i >= 0) {
305 x = (x << SHIFT) + v->ob_digit[i];
306 }
307 return x * sign;
308}
309
Tim Peters5b8132f2003-01-31 15:52:05 +0000310int
311_PyLong_Sign(PyObject *vv)
312{
313 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000314
315 assert(v != NULL);
316 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000317
Tim Petersee1a53c2003-02-02 02:57:53 +0000318 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000319}
320
Tim Petersbaefd9e2003-01-28 20:37:45 +0000321size_t
322_PyLong_NumBits(PyObject *vv)
323{
324 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000325 size_t result = 0;
Guido van Rossum004a65c2003-02-03 15:28:19 +0000326 int ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000327
328 assert(v != NULL);
329 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000330 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000331 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
332 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000333 digit msd = v->ob_digit[ndigits - 1];
334
Tim Peters5b8132f2003-01-31 15:52:05 +0000335 result = (ndigits - 1) * SHIFT;
Tim Peters08a1d9c2003-01-31 21:45:13 +0000336 if (result / SHIFT != (size_t)ndigits - 1)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000337 goto Overflow;
338 do {
339 ++result;
340 if (result == 0)
341 goto Overflow;
342 msd >>= 1;
343 } while (msd);
344 }
345 return result;
346
347Overflow:
348 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
349 "to express in a platform size_t");
350 return (size_t)-1;
351}
352
Tim Peters2a9b3672001-06-11 21:23:58 +0000353PyObject *
354_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
355 int little_endian, int is_signed)
356{
357 const unsigned char* pstartbyte;/* LSB of bytes */
358 int incr; /* direction to move pstartbyte */
359 const unsigned char* pendbyte; /* MSB of bytes */
360 size_t numsignificantbytes; /* number of bytes that matter */
361 size_t ndigits; /* number of Python long digits */
362 PyLongObject* v; /* result */
363 int idigit = 0; /* next free index in v->ob_digit */
364
365 if (n == 0)
366 return PyLong_FromLong(0L);
367
368 if (little_endian) {
369 pstartbyte = bytes;
370 pendbyte = bytes + n - 1;
371 incr = 1;
372 }
373 else {
374 pstartbyte = bytes + n - 1;
375 pendbyte = bytes;
376 incr = -1;
377 }
378
379 if (is_signed)
380 is_signed = *pendbyte >= 0x80;
381
382 /* Compute numsignificantbytes. This consists of finding the most
383 significant byte. Leading 0 bytes are insignficant if the number
384 is positive, and leading 0xff bytes if negative. */
385 {
386 size_t i;
387 const unsigned char* p = pendbyte;
388 const int pincr = -incr; /* search MSB to LSB */
389 const unsigned char insignficant = is_signed ? 0xff : 0x00;
390
391 for (i = 0; i < n; ++i, p += pincr) {
392 if (*p != insignficant)
393 break;
394 }
395 numsignificantbytes = n - i;
396 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
397 actually has 2 significant bytes. OTOH, 0xff0001 ==
398 -0x00ffff, so we wouldn't *need* to bump it there; but we
399 do for 0xffff = -0x0001. To be safe without bothering to
400 check every case, bump it regardless. */
401 if (is_signed && numsignificantbytes < n)
402 ++numsignificantbytes;
403 }
404
405 /* How many Python long digits do we need? We have
406 8*numsignificantbytes bits, and each Python long digit has SHIFT
407 bits, so it's the ceiling of the quotient. */
408 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
409 if (ndigits > (size_t)INT_MAX)
410 return PyErr_NoMemory();
411 v = _PyLong_New((int)ndigits);
412 if (v == NULL)
413 return NULL;
414
415 /* Copy the bits over. The tricky parts are computing 2's-comp on
416 the fly for signed numbers, and dealing with the mismatch between
417 8-bit bytes and (probably) 15-bit Python digits.*/
418 {
419 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000420 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000421 twodigits accum = 0; /* sliding register */
422 unsigned int accumbits = 0; /* number of bits in accum */
423 const unsigned char* p = pstartbyte;
424
425 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000426 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000427 /* Compute correction for 2's comp, if needed. */
428 if (is_signed) {
429 thisbyte = (0xff ^ thisbyte) + carry;
430 carry = thisbyte >> 8;
431 thisbyte &= 0xff;
432 }
433 /* Because we're going LSB to MSB, thisbyte is
434 more significant than what's already in accum,
435 so needs to be prepended to accum. */
436 accum |= thisbyte << accumbits;
437 accumbits += 8;
438 if (accumbits >= SHIFT) {
439 /* There's enough to fill a Python digit. */
440 assert(idigit < (int)ndigits);
441 v->ob_digit[idigit] = (digit)(accum & MASK);
442 ++idigit;
443 accum >>= SHIFT;
444 accumbits -= SHIFT;
445 assert(accumbits < SHIFT);
446 }
447 }
448 assert(accumbits < SHIFT);
449 if (accumbits) {
450 assert(idigit < (int)ndigits);
451 v->ob_digit[idigit] = (digit)accum;
452 ++idigit;
453 }
454 }
455
456 v->ob_size = is_signed ? -idigit : idigit;
457 return (PyObject *)long_normalize(v);
458}
459
460int
461_PyLong_AsByteArray(PyLongObject* v,
462 unsigned char* bytes, size_t n,
463 int little_endian, int is_signed)
464{
465 int i; /* index into v->ob_digit */
466 int ndigits; /* |v->ob_size| */
467 twodigits accum; /* sliding register */
468 unsigned int accumbits; /* # bits in accum */
469 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
470 twodigits carry; /* for computing 2's-comp */
471 size_t j; /* # bytes filled */
472 unsigned char* p; /* pointer to next byte in bytes */
473 int pincr; /* direction to move p */
474
475 assert(v != NULL && PyLong_Check(v));
476
477 if (v->ob_size < 0) {
478 ndigits = -(v->ob_size);
479 if (!is_signed) {
480 PyErr_SetString(PyExc_TypeError,
481 "can't convert negative long to unsigned");
482 return -1;
483 }
484 do_twos_comp = 1;
485 }
486 else {
487 ndigits = v->ob_size;
488 do_twos_comp = 0;
489 }
490
491 if (little_endian) {
492 p = bytes;
493 pincr = 1;
494 }
495 else {
496 p = bytes + n - 1;
497 pincr = -1;
498 }
499
Tim Peters898cf852001-06-13 20:50:08 +0000500 /* Copy over all the Python digits.
501 It's crucial that every Python digit except for the MSD contribute
502 exactly SHIFT bits to the total, so first assert that the long is
503 normalized. */
504 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000505 j = 0;
506 accum = 0;
507 accumbits = 0;
508 carry = do_twos_comp ? 1 : 0;
509 for (i = 0; i < ndigits; ++i) {
510 twodigits thisdigit = v->ob_digit[i];
511 if (do_twos_comp) {
512 thisdigit = (thisdigit ^ MASK) + carry;
513 carry = thisdigit >> SHIFT;
514 thisdigit &= MASK;
515 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000516 /* Because we're going LSB to MSB, thisdigit is more
517 significant than what's already in accum, so needs to be
518 prepended to accum. */
519 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000520 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000521
Tim Petersede05092001-06-14 08:53:38 +0000522 /* The most-significant digit may be (probably is) at least
523 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000524 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000525 /* Count # of sign bits -- they needn't be stored,
526 * although for signed conversion we need later to
527 * make sure at least one sign bit gets stored.
528 * First shift conceptual sign bit to real sign bit.
529 */
530 stwodigits s = (stwodigits)(thisdigit <<
531 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000532 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000533 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000534 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000535 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000536 }
Tim Petersede05092001-06-14 08:53:38 +0000537 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000538 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000539
Tim Peters2a9b3672001-06-11 21:23:58 +0000540 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000541 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000542 if (j >= n)
543 goto Overflow;
544 ++j;
545 *p = (unsigned char)(accum & 0xff);
546 p += pincr;
547 accumbits -= 8;
548 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000549 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000550 }
551
552 /* Store the straggler (if any). */
553 assert(accumbits < 8);
554 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000555 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000556 if (j >= n)
557 goto Overflow;
558 ++j;
559 if (do_twos_comp) {
560 /* Fill leading bits of the byte with sign bits
561 (appropriately pretending that the long had an
562 infinite supply of sign bits). */
563 accum |= (~(twodigits)0) << accumbits;
564 }
565 *p = (unsigned char)(accum & 0xff);
566 p += pincr;
567 }
Tim Peters05607ad2001-06-13 21:01:27 +0000568 else if (j == n && n > 0 && is_signed) {
569 /* The main loop filled the byte array exactly, so the code
570 just above didn't get to ensure there's a sign bit, and the
571 loop below wouldn't add one either. Make sure a sign bit
572 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000573 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000574 int sign_bit_set = msb >= 0x80;
575 assert(accumbits == 0);
576 if (sign_bit_set == do_twos_comp)
577 return 0;
578 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000579 goto Overflow;
580 }
Tim Peters05607ad2001-06-13 21:01:27 +0000581
582 /* Fill remaining bytes with copies of the sign bit. */
583 {
584 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
585 for ( ; j < n; ++j, p += pincr)
586 *p = signbyte;
587 }
588
Tim Peters2a9b3672001-06-11 21:23:58 +0000589 return 0;
590
591Overflow:
592 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
593 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000594
Tim Peters2a9b3672001-06-11 21:23:58 +0000595}
596
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000597double
598_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
599{
600/* NBITS_WANTED should be > the number of bits in a double's precision,
601 but small enough so that 2**NBITS_WANTED is within the normal double
602 range. nbitsneeded is set to 1 less than that because the most-significant
603 Python digit contains at least 1 significant bit, but we don't want to
604 bother counting them (catering to the worst case cheaply).
605
606 57 is one more than VAX-D double precision; I (Tim) don't know of a double
607 format with more precision than that; it's 1 larger so that we add in at
608 least one round bit to stand in for the ignored least-significant bits.
609*/
610#define NBITS_WANTED 57
611 PyLongObject *v;
612 double x;
613 const double multiplier = (double)(1L << SHIFT);
614 int i, sign;
615 int nbitsneeded;
616
617 if (vv == NULL || !PyLong_Check(vv)) {
618 PyErr_BadInternalCall();
619 return -1;
620 }
621 v = (PyLongObject *)vv;
622 i = v->ob_size;
623 sign = 1;
624 if (i < 0) {
625 sign = -1;
626 i = -(i);
627 }
628 else if (i == 0) {
629 *exponent = 0;
630 return 0.0;
631 }
632 --i;
633 x = (double)v->ob_digit[i];
634 nbitsneeded = NBITS_WANTED - 1;
635 /* Invariant: i Python digits remain unaccounted for. */
636 while (i > 0 && nbitsneeded > 0) {
637 --i;
638 x = x * multiplier + (double)v->ob_digit[i];
639 nbitsneeded -= SHIFT;
640 }
641 /* There are i digits we didn't shift in. Pretending they're all
642 zeroes, the true value is x * 2**(i*SHIFT). */
643 *exponent = i;
644 assert(x > 0.0);
645 return x * sign;
646#undef NBITS_WANTED
647}
648
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000649/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000650
651double
Tim Peters9f688bf2000-07-07 15:53:28 +0000652PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000653{
Tim Peters9fffa3e2001-09-04 05:14:19 +0000654 int e;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000655 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 if (vv == NULL || !PyLong_Check(vv)) {
658 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000659 return -1;
660 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000661 x = _PyLong_AsScaledDouble(vv, &e);
662 if (x == -1.0 && PyErr_Occurred())
663 return -1.0;
664 if (e > INT_MAX / SHIFT)
665 goto overflow;
666 errno = 0;
667 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000668 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000669 goto overflow;
670 return x;
671
672overflow:
673 PyErr_SetString(PyExc_OverflowError,
674 "long int too large to convert to float");
675 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000676}
677
Guido van Rossum78694d91998-09-18 14:14:13 +0000678/* Create a new long (or int) object from a C pointer */
679
680PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000681PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000682{
Tim Peters70128a12001-06-16 08:48:40 +0000683#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000684 return PyInt_FromLong((long)p);
685#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000686
Tim Peters70128a12001-06-16 08:48:40 +0000687#ifndef HAVE_LONG_LONG
688# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
689#endif
690#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000691# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000692#endif
693 /* optimize null pointers */
694 if (p == NULL)
695 return PyInt_FromLong(0);
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000696 return PyLong_FromLongLong((PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000697
698#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000699}
700
701/* Get a C pointer from a long object (or an int object in some cases) */
702
703void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000704PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000705{
706 /* This function will allow int or long objects. If vv is neither,
707 then the PyLong_AsLong*() functions will raise the exception:
708 PyExc_SystemError, "bad argument to internal function"
709 */
Tim Peters70128a12001-06-16 08:48:40 +0000710#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000711 long x;
712
Tim Peters70128a12001-06-16 08:48:40 +0000713 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000714 x = PyInt_AS_LONG(vv);
715 else
716 x = PyLong_AsLong(vv);
717#else
Tim Peters70128a12001-06-16 08:48:40 +0000718
719#ifndef HAVE_LONG_LONG
720# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
721#endif
722#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000723# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000724#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000725 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000726
Tim Peters70128a12001-06-16 08:48:40 +0000727 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000728 x = PyInt_AS_LONG(vv);
729 else
730 x = PyLong_AsLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000731
732#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000733
734 if (x == -1 && PyErr_Occurred())
735 return NULL;
736 return (void *)x;
737}
738
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000739#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000740
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000741/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000742 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000743 */
744
Tim Peterscf37dfc2001-06-14 18:42:50 +0000745#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000746
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000747/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000748
749PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000750PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000751{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000752 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000753 int one = 1;
754 return _PyLong_FromByteArray(
755 (unsigned char *)&bytes,
756 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000757}
758
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000759/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000760
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000761PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000762PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000763{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000764 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000765 int one = 1;
766 return _PyLong_FromByteArray(
767 (unsigned char *)&bytes,
768 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000769}
770
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000771/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000772 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000773
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000774PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000775PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000776{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000777 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000778 int one = 1;
779 int res;
780
Tim Petersd38b1c72001-09-30 05:09:37 +0000781 if (vv == NULL) {
782 PyErr_BadInternalCall();
783 return -1;
784 }
785 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000786 PyNumberMethods *nb;
787 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000788 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000789 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000790 if ((nb = vv->ob_type->tp_as_number) == NULL ||
791 nb->nb_int == NULL) {
792 PyErr_SetString(PyExc_TypeError, "an integer is required");
793 return -1;
794 }
795 io = (*nb->nb_int) (vv);
796 if (io == NULL)
797 return -1;
798 if (PyInt_Check(io)) {
799 bytes = PyInt_AsLong(io);
800 Py_DECREF(io);
801 return bytes;
802 }
803 if (PyLong_Check(io)) {
804 bytes = PyLong_AsLongLong(io);
805 Py_DECREF(io);
806 return bytes;
807 }
808 Py_DECREF(io);
809 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000810 return -1;
811 }
812
Tim Petersd1a7da62001-06-13 00:35:57 +0000813 res = _PyLong_AsByteArray(
814 (PyLongObject *)vv, (unsigned char *)&bytes,
815 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000816
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000817 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000818 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000819 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000820 else
821 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000822}
823
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000824/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000825 Return -1 and set an error if overflow occurs. */
826
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000827unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000828PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000829{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000830 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000831 int one = 1;
832 int res;
833
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000834 if (vv == NULL || !PyLong_Check(vv)) {
835 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000836 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000837 }
838
Tim Petersd1a7da62001-06-13 00:35:57 +0000839 res = _PyLong_AsByteArray(
840 (PyLongObject *)vv, (unsigned char *)&bytes,
841 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000842
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000843 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000844 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000845 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000846 else
847 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000848}
Tim Petersd1a7da62001-06-13 00:35:57 +0000849
Thomas Hellera4ea6032003-04-17 18:55:45 +0000850/* Get a C unsigned long int from a long int object, ignoring the high bits.
851 Returns -1 and sets an error condition if an error occurs. */
852
853unsigned PY_LONG_LONG
854PyLong_AsUnsignedLongLongMask(PyObject *vv)
855{
856 register PyLongObject *v;
857 unsigned PY_LONG_LONG x;
858 int i, sign;
859
860 if (vv == NULL || !PyLong_Check(vv)) {
861 PyErr_BadInternalCall();
862 return (unsigned long) -1;
863 }
864 v = (PyLongObject *)vv;
865 i = v->ob_size;
866 sign = 1;
867 x = 0;
868 if (i < 0) {
869 sign = -1;
870 i = -i;
871 }
872 while (--i >= 0) {
873 x = (x << SHIFT) + v->ob_digit[i];
874 }
875 return x * sign;
876}
Tim Petersd1a7da62001-06-13 00:35:57 +0000877#undef IS_LITTLE_ENDIAN
878
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000879#endif /* HAVE_LONG_LONG */
880
Neil Schemenauerba872e22001-01-04 01:46:03 +0000881
882static int
883convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000884 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000885 *a = (PyLongObject *) v;
886 Py_INCREF(v);
887 }
888 else if (PyInt_Check(v)) {
889 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
890 }
891 else {
892 return 0;
893 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000894 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000895 *b = (PyLongObject *) w;
896 Py_INCREF(w);
897 }
898 else if (PyInt_Check(w)) {
899 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
900 }
901 else {
902 Py_DECREF(*a);
903 return 0;
904 }
905 return 1;
906}
907
908#define CONVERT_BINOP(v, w, a, b) \
909 if (!convert_binop(v, w, a, b)) { \
910 Py_INCREF(Py_NotImplemented); \
911 return Py_NotImplemented; \
912 }
913
Tim Peters877a2122002-08-12 05:09:36 +0000914/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
915 * is modified in place, by adding y to it. Carries are propagated as far as
916 * x[m-1], and the remaining carry (0 or 1) is returned.
917 */
918static digit
919v_iadd(digit *x, int m, digit *y, int n)
920{
921 int i;
922 digit carry = 0;
923
924 assert(m >= n);
925 for (i = 0; i < n; ++i) {
926 carry += x[i] + y[i];
927 x[i] = carry & MASK;
928 carry >>= SHIFT;
929 assert((carry & 1) == carry);
930 }
931 for (; carry && i < m; ++i) {
932 carry += x[i];
933 x[i] = carry & MASK;
934 carry >>= SHIFT;
935 assert((carry & 1) == carry);
936 }
937 return carry;
938}
939
940/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
941 * is modified in place, by subtracting y from it. Borrows are propagated as
942 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
943 */
944static digit
945v_isub(digit *x, int m, digit *y, int n)
946{
947 int i;
948 digit borrow = 0;
949
950 assert(m >= n);
951 for (i = 0; i < n; ++i) {
952 borrow = x[i] - y[i] - borrow;
953 x[i] = borrow & MASK;
954 borrow >>= SHIFT;
955 borrow &= 1; /* keep only 1 sign bit */
956 }
957 for (; borrow && i < m; ++i) {
958 borrow = x[i] - borrow;
959 x[i] = borrow & MASK;
960 borrow >>= SHIFT;
961 borrow &= 1;
962 }
963 return borrow;
964}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000965
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000966/* Multiply by a single digit, ignoring the sign. */
967
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000968static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000969mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000970{
971 return muladd1(a, n, (digit)0);
972}
973
974/* Multiply by a single digit and add a single digit, ignoring the sign. */
975
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000977muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000978{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000979 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000981 twodigits carry = extra;
982 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000983
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000984 if (z == NULL)
985 return NULL;
986 for (i = 0; i < size_a; ++i) {
987 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000988 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000989 carry >>= SHIFT;
990 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000991 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000992 return long_normalize(z);
993}
994
Tim Peters212e6142001-07-14 12:23:19 +0000995/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
996 in pout, and returning the remainder. pin and pout point at the LSD.
997 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
998 long_format, but that should be done with great care since longs are
999 immutable. */
1000
1001static digit
1002inplace_divrem1(digit *pout, digit *pin, int size, digit n)
1003{
1004 twodigits rem = 0;
1005
1006 assert(n > 0 && n <= MASK);
1007 pin += size;
1008 pout += size;
1009 while (--size >= 0) {
1010 digit hi;
1011 rem = (rem << SHIFT) + *--pin;
1012 *--pout = hi = (digit)(rem / n);
1013 rem -= hi * n;
1014 }
1015 return (digit)rem;
1016}
1017
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001018/* Divide a long integer by a digit, returning both the quotient
1019 (as function result) and the remainder (through *prem).
1020 The sign of a is ignored; n should not be zero. */
1021
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001023divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001024{
Tim Peters212e6142001-07-14 12:23:19 +00001025 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001027
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001028 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001029 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001030 if (z == NULL)
1031 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001032 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001033 return long_normalize(z);
1034}
1035
1036/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001037 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001038 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001039
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001041long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001042{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001043 register PyLongObject *a = (PyLongObject *)aa;
1044 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001045 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001046 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001047 char *p;
1048 int bits;
1049 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051 if (a == NULL || !PyLong_Check(a)) {
1052 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001053 return NULL;
1054 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001055 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001056
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001057 /* Compute a rough upper bound for the length of the string */
1058 i = base;
1059 bits = 0;
1060 while (i > 1) {
1061 ++bits;
1062 i >>= 1;
1063 }
Fred Drake121ee271999-12-23 15:41:28 +00001064 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001066 if (str == NULL)
1067 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001068 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001069 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001070 if (addL)
1071 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001072 if (a->ob_size < 0)
1073 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001074
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001075 if (a->ob_size == 0) {
1076 *--p = '0';
1077 }
1078 else if ((base & (base - 1)) == 0) {
1079 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001080 twodigits accum = 0;
1081 int accumbits = 0; /* # of bits in accum */
1082 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001083 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001084 while ((i >>= 1) > 1)
1085 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001086
1087 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001088 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001089 accumbits += SHIFT;
1090 assert(accumbits >= basebits);
1091 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001092 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001093 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001094 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001095 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001096 accumbits -= basebits;
1097 accum >>= basebits;
1098 } while (i < size_a-1 ? accumbits >= basebits :
1099 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001100 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001101 }
1102 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001103 /* Not 0, and base not a power of 2. Divide repeatedly by
1104 base, but for speed use the highest power of base that
1105 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001106 int size = size_a;
1107 digit *pin = a->ob_digit;
1108 PyLongObject *scratch;
1109 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001110 digit powbase = base; /* powbase == base ** power */
1111 int power = 1;
1112 for (;;) {
1113 unsigned long newpow = powbase * (unsigned long)base;
1114 if (newpow >> SHIFT) /* doesn't fit in a digit */
1115 break;
1116 powbase = (digit)newpow;
1117 ++power;
1118 }
Tim Peters212e6142001-07-14 12:23:19 +00001119
1120 /* Get a scratch area for repeated division. */
1121 scratch = _PyLong_New(size);
1122 if (scratch == NULL) {
1123 Py_DECREF(str);
1124 return NULL;
1125 }
1126
1127 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001128 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001129 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001130 digit rem = inplace_divrem1(scratch->ob_digit,
1131 pin, size, powbase);
1132 pin = scratch->ob_digit; /* no need to use a again */
1133 if (pin[size - 1] == 0)
1134 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001135 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001136 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001137 Py_DECREF(str);
1138 return NULL;
1139 })
Tim Peters212e6142001-07-14 12:23:19 +00001140
1141 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001142 assert(ntostore > 0);
1143 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001144 digit nextrem = (digit)(rem / base);
1145 char c = (char)(rem - nextrem * base);
1146 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001147 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001148 *--p = c;
1149 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001150 --ntostore;
1151 /* Termination is a bit delicate: must not
1152 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001153 remaining quotient and rem are both 0. */
1154 } while (ntostore && (size || rem));
1155 } while (size != 0);
1156 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001157 }
1158
Guido van Rossum2c475421992-08-14 15:13:07 +00001159 if (base == 8) {
1160 if (size_a != 0)
1161 *--p = '0';
1162 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001163 else if (base == 16) {
1164 *--p = 'x';
1165 *--p = '0';
1166 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001167 else if (base != 10) {
1168 *--p = '#';
1169 *--p = '0' + base%10;
1170 if (base > 10)
1171 *--p = '0' + base/10;
1172 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001173 if (sign)
1174 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 if (p != PyString_AS_STRING(str)) {
1176 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001177 assert(p > q);
1178 do {
1179 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001180 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 _PyString_Resize((PyObject **)&str,
1182 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001183 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001185}
1186
Tim Petersbf2674b2003-02-02 07:51:32 +00001187/* *str points to the first digit in a string of base base digits. base
1188 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1189 * non-digit (which may be *str!). A normalized long is returned.
1190 * The point to this routine is that it takes time linear in the number of
1191 * string characters.
1192 */
1193static PyLongObject *
1194long_from_binary_base(char **str, int base)
1195{
1196 char *p = *str;
1197 char *start = p;
1198 int bits_per_char;
1199 int n;
1200 PyLongObject *z;
1201 twodigits accum;
1202 int bits_in_accum;
1203 digit *pdigit;
1204
1205 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1206 n = base;
1207 for (bits_per_char = -1; n; ++bits_per_char)
1208 n >>= 1;
1209 /* n <- total # of bits needed, while setting p to end-of-string */
1210 n = 0;
1211 for (;;) {
1212 int k = -1;
1213 char ch = *p;
1214
1215 if (ch <= '9')
1216 k = ch - '0';
1217 else if (ch >= 'a')
1218 k = ch - 'a' + 10;
1219 else if (ch >= 'A')
1220 k = ch - 'A' + 10;
1221 if (k < 0 || k >= base)
1222 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001223 ++p;
1224 }
1225 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001226 n = (p - start) * bits_per_char;
1227 if (n / bits_per_char != p - start) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "long string too large to convert");
1230 return NULL;
1231 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001232 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1233 n = (n + SHIFT - 1) / SHIFT;
1234 z = _PyLong_New(n);
1235 if (z == NULL)
1236 return NULL;
1237 /* Read string from right, and fill in long from left; i.e.,
1238 * from least to most significant in both.
1239 */
1240 accum = 0;
1241 bits_in_accum = 0;
1242 pdigit = z->ob_digit;
1243 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001244 int k;
1245 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001246
1247 if (ch <= '9')
1248 k = ch - '0';
1249 else if (ch >= 'a')
1250 k = ch - 'a' + 10;
1251 else {
1252 assert(ch >= 'A');
1253 k = ch - 'A' + 10;
1254 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001255 assert(k >= 0 && k < base);
1256 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001257 bits_in_accum += bits_per_char;
1258 if (bits_in_accum >= SHIFT) {
1259 *pdigit++ = (digit)(accum & MASK);
1260 assert(pdigit - z->ob_digit <= n);
1261 accum >>= SHIFT;
1262 bits_in_accum -= SHIFT;
1263 assert(bits_in_accum < SHIFT);
1264 }
1265 }
1266 if (bits_in_accum) {
1267 assert(bits_in_accum <= SHIFT);
1268 *pdigit++ = (digit)accum;
1269 assert(pdigit - z->ob_digit <= n);
1270 }
1271 while (pdigit - z->ob_digit < n)
1272 *pdigit++ = 0;
1273 return long_normalize(z);
1274}
1275
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001277PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001278{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001279 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001280 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001281 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001282
Guido van Rossum472c04f1996-12-05 21:57:21 +00001283 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001284 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001285 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001286 return NULL;
1287 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001288 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001289 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001290 if (*str == '+')
1291 ++str;
1292 else if (*str == '-') {
1293 ++str;
1294 sign = -1;
1295 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001296 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001297 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001298 if (base == 0) {
1299 if (str[0] != '0')
1300 base = 10;
1301 else if (str[1] == 'x' || str[1] == 'X')
1302 base = 16;
1303 else
1304 base = 8;
1305 }
1306 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1307 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001308 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001309 if ((base & (base - 1)) == 0)
1310 z = long_from_binary_base(&str, base);
1311 else {
1312 z = _PyLong_New(0);
1313 for ( ; z != NULL; ++str) {
1314 int k = -1;
1315 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001316
Tim Petersbf2674b2003-02-02 07:51:32 +00001317 if (*str <= '9')
1318 k = *str - '0';
1319 else if (*str >= 'a')
1320 k = *str - 'a' + 10;
1321 else if (*str >= 'A')
1322 k = *str - 'A' + 10;
1323 if (k < 0 || k >= base)
1324 break;
1325 temp = muladd1(z, (digit)base, (digit)k);
1326 Py_DECREF(z);
1327 z = temp;
1328 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001329 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001330 if (z == NULL)
1331 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001332 if (str == start)
1333 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001334 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001335 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001336 if (*str == 'L' || *str == 'l')
1337 str++;
1338 while (*str && isspace(Py_CHARMASK(*str)))
1339 str++;
1340 if (*str != '\0')
1341 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001342 if (pend)
1343 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001345
1346 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001347 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001348 "invalid literal for long(): %.200s", orig_str);
1349 Py_XDECREF(z);
1350 return NULL;
1351}
1352
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001353#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001354PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001355PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001356{
Walter Dörwald07e14762002-11-06 16:15:14 +00001357 PyObject *result;
1358 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001359
Walter Dörwald07e14762002-11-06 16:15:14 +00001360 if (buffer == NULL)
1361 return NULL;
1362
1363 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1364 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001365 return NULL;
1366 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001367 result = PyLong_FromString(buffer, NULL, base);
1368 PyMem_FREE(buffer);
1369 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001370}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001371#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001372
Tim Peters9f688bf2000-07-07 15:53:28 +00001373/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001374static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001375 (PyLongObject *, PyLongObject *, PyLongObject **);
1376static PyObject *long_pos(PyLongObject *);
1377static int long_divrem(PyLongObject *, PyLongObject *,
1378 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001379
1380/* Long division with remainder, top-level routine */
1381
Guido van Rossume32e0141992-01-19 16:31:05 +00001382static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001383long_divrem(PyLongObject *a, PyLongObject *b,
1384 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001385{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001386 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001388
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001389 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001390 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001391 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001392 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001393 }
1394 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001395 (size_a == size_b &&
1396 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001397 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398 *pdiv = _PyLong_New(0);
1399 Py_INCREF(a);
1400 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001401 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001402 }
1403 if (size_b == 1) {
1404 digit rem = 0;
1405 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001406 if (z == NULL)
1407 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001409 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001410 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001411 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001412 if (z == NULL)
1413 return -1;
1414 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001415 /* Set the signs.
1416 The quotient z has the sign of a*b;
1417 the remainder r has the sign of a,
1418 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001419 if ((a->ob_size < 0) != (b->ob_size < 0))
1420 z->ob_size = -(z->ob_size);
1421 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1422 (*prem)->ob_size = -((*prem)->ob_size);
1423 *pdiv = z;
1424 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001425}
1426
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001427/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001428
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001429static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001430x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001431{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001432 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001433 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434 PyLongObject *v = mul1(v1, d);
1435 PyLongObject *w = mul1(w1, d);
1436 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001437 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001438
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001439 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001440 Py_XDECREF(v);
1441 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001442 return NULL;
1443 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001444
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001445 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001446 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001447 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001448
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001449 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001451
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001452 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1453 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1454 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001455 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001456 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001457
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001458 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001459 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001460 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001461 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001462 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001463 if (vj == w->ob_digit[size_w-1])
1464 q = MASK;
1465 else
1466 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1467 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001468
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001469 while (w->ob_digit[size_w-2]*q >
1470 ((
1471 ((twodigits)vj << SHIFT)
1472 + v->ob_digit[j-1]
1473 - q*w->ob_digit[size_w-1]
1474 ) << SHIFT)
1475 + v->ob_digit[j-2])
1476 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001477
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001478 for (i = 0; i < size_w && i+k < size_v; ++i) {
1479 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001480 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481 carry += v->ob_digit[i+k] - z
1482 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001483 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001484 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1485 carry, SHIFT);
1486 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001487 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001488
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001489 if (i+k < size_v) {
1490 carry += v->ob_digit[i+k];
1491 v->ob_digit[i+k] = 0;
1492 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001493
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001494 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001495 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001496 else {
1497 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001498 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001499 carry = 0;
1500 for (i = 0; i < size_w && i+k < size_v; ++i) {
1501 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001502 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001503 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1504 BASE_TWODIGITS_TYPE,
1505 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001506 }
1507 }
1508 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001509
Guido van Rossumc206c761995-01-10 15:23:19 +00001510 if (a == NULL)
1511 *prem = NULL;
1512 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001513 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001514 *prem = divrem1(v, d, &d);
1515 /* d receives the (unused) remainder */
1516 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001518 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001519 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001520 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 Py_DECREF(v);
1522 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001523 return a;
1524}
1525
1526/* Methods */
1527
1528static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001529long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001530{
Guido van Rossum9475a232001-10-05 20:51:39 +00001531 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001532}
1533
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001534static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001535long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001536{
Fred Drake121ee271999-12-23 15:41:28 +00001537 return long_format(v, 10, 1);
1538}
1539
1540static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001541long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001542{
1543 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001544}
1545
1546static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001547long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001548{
1549 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001550
Guido van Rossumc6913e71991-11-19 20:26:46 +00001551 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001552 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001553 sign = 0;
1554 else
1555 sign = a->ob_size - b->ob_size;
1556 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001557 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001558 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001559 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1560 ;
1561 if (i < 0)
1562 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001563 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001564 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001565 if (a->ob_size < 0)
1566 sign = -sign;
1567 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001568 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001569 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001570}
1571
Guido van Rossum9bfef441993-03-29 10:43:31 +00001572static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001573long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001574{
1575 long x;
1576 int i, sign;
1577
1578 /* This is designed so that Python ints and longs with the
1579 same value hash to the same value, otherwise comparisons
1580 of mapping keys will turn out weird */
1581 i = v->ob_size;
1582 sign = 1;
1583 x = 0;
1584 if (i < 0) {
1585 sign = -1;
1586 i = -(i);
1587 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001588#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001589 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001590 /* Force a native long #-bits (32 or 64) circular shift */
1591 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001592 x += v->ob_digit[i];
1593 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001594#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001595 x = x * sign;
1596 if (x == -1)
1597 x = -2;
1598 return x;
1599}
1600
1601
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001602/* Add the absolute values of two long integers. */
1603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001604static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001605x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001606{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001607 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001608 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001609 int i;
1610 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001611
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001612 /* Ensure a is the larger of the two: */
1613 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001614 { PyLongObject *temp = a; a = b; b = temp; }
1615 { int size_temp = size_a;
1616 size_a = size_b;
1617 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001618 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001619 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001620 if (z == NULL)
1621 return NULL;
1622 for (i = 0; i < size_b; ++i) {
1623 carry += a->ob_digit[i] + b->ob_digit[i];
1624 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001625 carry >>= SHIFT;
1626 }
1627 for (; i < size_a; ++i) {
1628 carry += a->ob_digit[i];
1629 z->ob_digit[i] = carry & MASK;
1630 carry >>= SHIFT;
1631 }
1632 z->ob_digit[i] = carry;
1633 return long_normalize(z);
1634}
1635
1636/* Subtract the absolute values of two integers. */
1637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001639x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001640{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001641 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001642 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001643 int i;
1644 int sign = 1;
1645 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001646
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001647 /* Ensure a is the larger of the two: */
1648 if (size_a < size_b) {
1649 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001650 { PyLongObject *temp = a; a = b; b = temp; }
1651 { int size_temp = size_a;
1652 size_a = size_b;
1653 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001654 }
1655 else if (size_a == size_b) {
1656 /* Find highest digit where a and b differ: */
1657 i = size_a;
1658 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1659 ;
1660 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001661 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001662 if (a->ob_digit[i] < b->ob_digit[i]) {
1663 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001664 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001665 }
1666 size_a = size_b = i+1;
1667 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001668 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001669 if (z == NULL)
1670 return NULL;
1671 for (i = 0; i < size_b; ++i) {
1672 /* The following assumes unsigned arithmetic
1673 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001674 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001675 z->ob_digit[i] = borrow & MASK;
1676 borrow >>= SHIFT;
1677 borrow &= 1; /* Keep only one sign bit */
1678 }
1679 for (; i < size_a; ++i) {
1680 borrow = a->ob_digit[i] - borrow;
1681 z->ob_digit[i] = borrow & MASK;
1682 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001683 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001684 }
1685 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001686 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001687 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001688 return long_normalize(z);
1689}
1690
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001691static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001692long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001693{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001694 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001695
Neil Schemenauerba872e22001-01-04 01:46:03 +00001696 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1697
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001698 if (a->ob_size < 0) {
1699 if (b->ob_size < 0) {
1700 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001701 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001702 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001703 }
1704 else
1705 z = x_sub(b, a);
1706 }
1707 else {
1708 if (b->ob_size < 0)
1709 z = x_sub(a, b);
1710 else
1711 z = x_add(a, b);
1712 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001713 Py_DECREF(a);
1714 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001715 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001716}
1717
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001718static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001719long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001720{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001721 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001722
Neil Schemenauerba872e22001-01-04 01:46:03 +00001723 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1724
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001725 if (a->ob_size < 0) {
1726 if (b->ob_size < 0)
1727 z = x_sub(a, b);
1728 else
1729 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001730 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001731 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001732 }
1733 else {
1734 if (b->ob_size < 0)
1735 z = x_add(a, b);
1736 else
1737 z = x_sub(a, b);
1738 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001739 Py_DECREF(a);
1740 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001741 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742}
1743
Tim Peters5af4e6c2002-08-12 02:31:19 +00001744/* Grade school multiplication, ignoring the signs.
1745 * Returns the absolute value of the product, or NULL if error.
1746 */
1747static PyLongObject *
1748x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001749{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001750 PyLongObject *z;
1751 int size_a = ABS(a->ob_size);
1752 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001753 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001754
Tim Peters5af4e6c2002-08-12 02:31:19 +00001755 z = _PyLong_New(size_a + size_b);
1756 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001757 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001758
1759 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001760 if (a == b) {
1761 /* Efficient squaring per HAC, Algorithm 14.16:
1762 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1763 * Gives slightly less than a 2x speedup when a == b,
1764 * via exploiting that each entry in the multiplication
1765 * pyramid appears twice (except for the size_a squares).
1766 */
1767 for (i = 0; i < size_a; ++i) {
1768 twodigits carry;
1769 twodigits f = a->ob_digit[i];
1770 digit *pz = z->ob_digit + (i << 1);
1771 digit *pa = a->ob_digit + i + 1;
1772 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001773
Tim Peters0973b992004-08-29 22:16:50 +00001774 SIGCHECK({
1775 Py_DECREF(z);
1776 return NULL;
1777 })
1778
1779 carry = *pz + f * f;
1780 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001781 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001782 assert(carry <= MASK);
1783
1784 /* Now f is added in twice in each column of the
1785 * pyramid it appears. Same as adding f<<1 once.
1786 */
1787 f <<= 1;
1788 while (pa < paend) {
1789 carry += *pz + *pa++ * f;
1790 *pz++ = (digit)(carry & MASK);
1791 carry >>= SHIFT;
1792 assert(carry <= (MASK << 1));
1793 }
1794 if (carry) {
1795 carry += *pz;
1796 *pz++ = (digit)(carry & MASK);
1797 carry >>= SHIFT;
1798 }
1799 if (carry)
1800 *pz += (digit)(carry & MASK);
1801 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001802 }
Tim Peters0973b992004-08-29 22:16:50 +00001803 }
1804 else { /* a is not the same as b -- gradeschool long mult */
1805 for (i = 0; i < size_a; ++i) {
1806 twodigits carry = 0;
1807 twodigits f = a->ob_digit[i];
1808 digit *pz = z->ob_digit + i;
1809 digit *pb = b->ob_digit;
1810 digit *pbend = b->ob_digit + size_b;
1811
1812 SIGCHECK({
1813 Py_DECREF(z);
1814 return NULL;
1815 })
1816
1817 while (pb < pbend) {
1818 carry += *pz + *pb++ * f;
1819 *pz++ = (digit)(carry & MASK);
1820 carry >>= SHIFT;
1821 assert(carry <= MASK);
1822 }
1823 if (carry)
1824 *pz += (digit)(carry & MASK);
1825 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001826 }
1827 }
Tim Peters44121a62002-08-12 06:17:58 +00001828 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001829}
1830
1831/* A helper for Karatsuba multiplication (k_mul).
1832 Takes a long "n" and an integer "size" representing the place to
1833 split, and sets low and high such that abs(n) == (high << size) + low,
1834 viewing the shift as being by digits. The sign bit is ignored, and
1835 the return values are >= 0.
1836 Returns 0 on success, -1 on failure.
1837*/
1838static int
1839kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1840{
1841 PyLongObject *hi, *lo;
1842 int size_lo, size_hi;
1843 const int size_n = ABS(n->ob_size);
1844
1845 size_lo = MIN(size_n, size);
1846 size_hi = size_n - size_lo;
1847
1848 if ((hi = _PyLong_New(size_hi)) == NULL)
1849 return -1;
1850 if ((lo = _PyLong_New(size_lo)) == NULL) {
1851 Py_DECREF(hi);
1852 return -1;
1853 }
1854
1855 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1856 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1857
1858 *high = long_normalize(hi);
1859 *low = long_normalize(lo);
1860 return 0;
1861}
1862
Tim Peters60004642002-08-12 22:01:34 +00001863static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1864
Tim Peters5af4e6c2002-08-12 02:31:19 +00001865/* Karatsuba multiplication. Ignores the input signs, and returns the
1866 * absolute value of the product (or NULL if error).
1867 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1868 */
1869static PyLongObject *
1870k_mul(PyLongObject *a, PyLongObject *b)
1871{
Tim Peters738eda72002-08-12 15:08:20 +00001872 int asize = ABS(a->ob_size);
1873 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001874 PyLongObject *ah = NULL;
1875 PyLongObject *al = NULL;
1876 PyLongObject *bh = NULL;
1877 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001878 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001879 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001880 int shift; /* the number of digits we split off */
1881 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001882
Tim Peters5af4e6c2002-08-12 02:31:19 +00001883 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1884 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1885 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001886 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001887 * By picking X to be a power of 2, "*X" is just shifting, and it's
1888 * been reduced to 3 multiplies on numbers half the size.
1889 */
1890
Tim Petersfc07e562002-08-12 02:54:10 +00001891 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001892 * is largest.
1893 */
Tim Peters738eda72002-08-12 15:08:20 +00001894 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001895 t1 = a;
1896 a = b;
1897 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001898
1899 i = asize;
1900 asize = bsize;
1901 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001902 }
1903
1904 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001905 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1906 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001907 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001908 return _PyLong_New(0);
1909 else
1910 return x_mul(a, b);
1911 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001912
Tim Peters60004642002-08-12 22:01:34 +00001913 /* If a is small compared to b, splitting on b gives a degenerate
1914 * case with ah==0, and Karatsuba may be (even much) less efficient
1915 * than "grade school" then. However, we can still win, by viewing
1916 * b as a string of "big digits", each of width a->ob_size. That
1917 * leads to a sequence of balanced calls to k_mul.
1918 */
1919 if (2 * asize <= bsize)
1920 return k_lopsided_mul(a, b);
1921
Tim Petersd6974a52002-08-13 20:37:51 +00001922 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001923 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001924 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001925 assert(ah->ob_size > 0); /* the split isn't degenerate */
1926
Tim Peters0973b992004-08-29 22:16:50 +00001927 if (a == b) {
1928 bh = ah;
1929 bl = al;
1930 Py_INCREF(bh);
1931 Py_INCREF(bl);
1932 }
1933 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001934
Tim Petersd64c1de2002-08-12 17:36:03 +00001935 /* The plan:
1936 * 1. Allocate result space (asize + bsize digits: that's always
1937 * enough).
1938 * 2. Compute ah*bh, and copy into result at 2*shift.
1939 * 3. Compute al*bl, and copy into result at 0. Note that this
1940 * can't overlap with #2.
1941 * 4. Subtract al*bl from the result, starting at shift. This may
1942 * underflow (borrow out of the high digit), but we don't care:
1943 * we're effectively doing unsigned arithmetic mod
1944 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1945 * borrows and carries out of the high digit can be ignored.
1946 * 5. Subtract ah*bh from the result, starting at shift.
1947 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1948 * at shift.
1949 */
1950
1951 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001952 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001953 if (ret == NULL) goto fail;
1954#ifdef Py_DEBUG
1955 /* Fill with trash, to catch reference to uninitialized digits. */
1956 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1957#endif
Tim Peters44121a62002-08-12 06:17:58 +00001958
Tim Petersd64c1de2002-08-12 17:36:03 +00001959 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001960 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1961 assert(t1->ob_size >= 0);
1962 assert(2*shift + t1->ob_size <= ret->ob_size);
1963 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1964 t1->ob_size * sizeof(digit));
1965
1966 /* Zero-out the digits higher than the ah*bh copy. */
1967 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001968 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001969 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001970 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001971
Tim Petersd64c1de2002-08-12 17:36:03 +00001972 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001973 if ((t2 = k_mul(al, bl)) == NULL) {
1974 Py_DECREF(t1);
1975 goto fail;
1976 }
1977 assert(t2->ob_size >= 0);
1978 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1979 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001980
1981 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001982 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001983 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001984 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001985
Tim Petersd64c1de2002-08-12 17:36:03 +00001986 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1987 * because it's fresher in cache.
1988 */
Tim Peters738eda72002-08-12 15:08:20 +00001989 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001990 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001991 Py_DECREF(t2);
1992
Tim Petersd64c1de2002-08-12 17:36:03 +00001993 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001994 Py_DECREF(t1);
1995
Tim Petersd64c1de2002-08-12 17:36:03 +00001996 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001997 if ((t1 = x_add(ah, al)) == NULL) goto fail;
1998 Py_DECREF(ah);
1999 Py_DECREF(al);
2000 ah = al = NULL;
2001
Tim Peters0973b992004-08-29 22:16:50 +00002002 if (a == b) {
2003 t2 = t1;
2004 Py_INCREF(t2);
2005 }
2006 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002007 Py_DECREF(t1);
2008 goto fail;
2009 }
2010 Py_DECREF(bh);
2011 Py_DECREF(bl);
2012 bh = bl = NULL;
2013
Tim Peters738eda72002-08-12 15:08:20 +00002014 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002015 Py_DECREF(t1);
2016 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002017 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002018 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002019
Tim Petersd6974a52002-08-13 20:37:51 +00002020 /* Add t3. It's not obvious why we can't run out of room here.
2021 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002022 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002023 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002024 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002025
Tim Peters5af4e6c2002-08-12 02:31:19 +00002026 return long_normalize(ret);
2027
2028 fail:
2029 Py_XDECREF(ret);
2030 Py_XDECREF(ah);
2031 Py_XDECREF(al);
2032 Py_XDECREF(bh);
2033 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002034 return NULL;
2035}
2036
Tim Petersd6974a52002-08-13 20:37:51 +00002037/* (*) Why adding t3 can't "run out of room" above.
2038
Tim Petersab86c2b2002-08-15 20:06:00 +00002039Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2040to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002041
Tim Petersab86c2b2002-08-15 20:06:00 +000020421. For any integer i, i = c(i/2) + f(i/2). In particular,
2043 bsize = c(bsize/2) + f(bsize/2).
20442. shift = f(bsize/2)
20453. asize <= bsize
20464. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2047 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002048
Tim Petersab86c2b2002-08-15 20:06:00 +00002049We allocated asize + bsize result digits, and add t3 into them at an offset
2050of shift. This leaves asize+bsize-shift allocated digit positions for t3
2051to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2052asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002053
Tim Petersab86c2b2002-08-15 20:06:00 +00002054bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2055at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002056
Tim Petersab86c2b2002-08-15 20:06:00 +00002057If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2058digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2059most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002060
Tim Petersab86c2b2002-08-15 20:06:00 +00002061The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002062
Tim Petersab86c2b2002-08-15 20:06:00 +00002063 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002064
Tim Petersab86c2b2002-08-15 20:06:00 +00002065and we have asize + c(bsize/2) available digit positions. We need to show
2066this is always enough. An instance of c(bsize/2) cancels out in both, so
2067the question reduces to whether asize digits is enough to hold
2068(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2069then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2070asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2071digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2072asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002073c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2074is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2075bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002076
Tim Peters48d52c02002-08-14 17:07:32 +00002077Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2078clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2079ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002080*/
2081
Tim Peters60004642002-08-12 22:01:34 +00002082/* b has at least twice the digits of a, and a is big enough that Karatsuba
2083 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2084 * of slices, each with a->ob_size digits, and multiply the slices by a,
2085 * one at a time. This gives k_mul balanced inputs to work with, and is
2086 * also cache-friendly (we compute one double-width slice of the result
2087 * at a time, then move on, never bactracking except for the helpful
2088 * single-width slice overlap between successive partial sums).
2089 */
2090static PyLongObject *
2091k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2092{
2093 const int asize = ABS(a->ob_size);
2094 int bsize = ABS(b->ob_size);
2095 int nbdone; /* # of b digits already multiplied */
2096 PyLongObject *ret;
2097 PyLongObject *bslice = NULL;
2098
2099 assert(asize > KARATSUBA_CUTOFF);
2100 assert(2 * asize <= bsize);
2101
2102 /* Allocate result space, and zero it out. */
2103 ret = _PyLong_New(asize + bsize);
2104 if (ret == NULL)
2105 return NULL;
2106 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2107
2108 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002109 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002110 if (bslice == NULL)
2111 goto fail;
2112
2113 nbdone = 0;
2114 while (bsize > 0) {
2115 PyLongObject *product;
2116 const int nbtouse = MIN(bsize, asize);
2117
2118 /* Multiply the next slice of b by a. */
2119 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2120 nbtouse * sizeof(digit));
2121 bslice->ob_size = nbtouse;
2122 product = k_mul(a, bslice);
2123 if (product == NULL)
2124 goto fail;
2125
2126 /* Add into result. */
2127 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2128 product->ob_digit, product->ob_size);
2129 Py_DECREF(product);
2130
2131 bsize -= nbtouse;
2132 nbdone += nbtouse;
2133 }
2134
2135 Py_DECREF(bslice);
2136 return long_normalize(ret);
2137
2138 fail:
2139 Py_DECREF(ret);
2140 Py_XDECREF(bslice);
2141 return NULL;
2142}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002143
2144static PyObject *
2145long_mul(PyLongObject *v, PyLongObject *w)
2146{
2147 PyLongObject *a, *b, *z;
2148
2149 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002150 Py_INCREF(Py_NotImplemented);
2151 return Py_NotImplemented;
2152 }
2153
Tim Petersd64c1de2002-08-12 17:36:03 +00002154 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002155 /* Negate if exactly one of the inputs is negative. */
2156 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002157 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002158 Py_DECREF(a);
2159 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002160 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002161}
2162
Guido van Rossume32e0141992-01-19 16:31:05 +00002163/* The / and % operators are now defined in terms of divmod().
2164 The expression a mod b has the value a - b*floor(a/b).
2165 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002166 |a| by |b|, with the sign of a. This is also expressed
2167 as a - b*trunc(a/b), if trunc truncates towards zero.
2168 Some examples:
2169 a b a rem b a mod b
2170 13 10 3 3
2171 -13 10 -3 7
2172 13 -10 3 -7
2173 -13 -10 -3 -3
2174 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002175 have different signs. We then subtract one from the 'div'
2176 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002177
Tim Peters47e52ee2004-08-30 02:44:38 +00002178/* Compute
2179 * *pdiv, *pmod = divmod(v, w)
2180 * NULL can be passed for pdiv or pmod, in which case that part of
2181 * the result is simply thrown away. The caller owns a reference to
2182 * each of these it requests (does not pass NULL for).
2183 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002184static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002185l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002186 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002189
Guido van Rossume32e0141992-01-19 16:31:05 +00002190 if (long_divrem(v, w, &div, &mod) < 0)
2191 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002192 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2193 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002194 PyLongObject *temp;
2195 PyLongObject *one;
2196 temp = (PyLongObject *) long_add(mod, w);
2197 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002198 mod = temp;
2199 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002200 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002201 return -1;
2202 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002204 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002205 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2206 Py_DECREF(mod);
2207 Py_DECREF(div);
2208 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002209 return -1;
2210 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002211 Py_DECREF(one);
2212 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002213 div = temp;
2214 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002215 if (pdiv != NULL)
2216 *pdiv = div;
2217 else
2218 Py_DECREF(div);
2219
2220 if (pmod != NULL)
2221 *pmod = mod;
2222 else
2223 Py_DECREF(mod);
2224
Guido van Rossume32e0141992-01-19 16:31:05 +00002225 return 0;
2226}
2227
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002228static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002229long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002230{
Tim Peters47e52ee2004-08-30 02:44:38 +00002231 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002232
2233 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002234 if (l_divmod(a, b, &div, NULL) < 0)
2235 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002236 Py_DECREF(a);
2237 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002238 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002239}
2240
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002241static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002242long_classic_div(PyObject *v, PyObject *w)
2243{
Tim Peters47e52ee2004-08-30 02:44:38 +00002244 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002245
2246 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002247 if (Py_DivisionWarningFlag &&
2248 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2249 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002250 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002251 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002252 Py_DECREF(a);
2253 Py_DECREF(b);
2254 return (PyObject *)div;
2255}
2256
2257static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002258long_true_divide(PyObject *v, PyObject *w)
2259{
Tim Peterse2a60002001-09-04 06:17:36 +00002260 PyLongObject *a, *b;
2261 double ad, bd;
Tim Peters6f97e492001-11-04 23:09:40 +00002262 int aexp, bexp, failed;
Tim Peterse2a60002001-09-04 06:17:36 +00002263
2264 CONVERT_BINOP(v, w, &a, &b);
2265 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2266 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002267 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2268 Py_DECREF(a);
2269 Py_DECREF(b);
2270 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002271 return NULL;
2272
2273 if (bd == 0.0) {
2274 PyErr_SetString(PyExc_ZeroDivisionError,
2275 "long division or modulo by zero");
2276 return NULL;
2277 }
2278
2279 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2280 ad /= bd; /* overflow/underflow impossible here */
2281 aexp -= bexp;
2282 if (aexp > INT_MAX / SHIFT)
2283 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002284 else if (aexp < -(INT_MAX / SHIFT))
2285 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002286 errno = 0;
2287 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002288 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002289 goto overflow;
2290 return PyFloat_FromDouble(ad);
2291
2292overflow:
2293 PyErr_SetString(PyExc_OverflowError,
2294 "long/long too large for a float");
2295 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002296
Tim Peters20dab9f2001-09-04 05:31:47 +00002297}
2298
2299static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002300long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002301{
Tim Peters47e52ee2004-08-30 02:44:38 +00002302 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002303
2304 CONVERT_BINOP(v, w, &a, &b);
2305
Tim Peters47e52ee2004-08-30 02:44:38 +00002306 if (l_divmod(a, b, NULL, &mod) < 0)
2307 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002308 Py_DECREF(a);
2309 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002310 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002311}
2312
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002313static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002314long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002315{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002316 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002317 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002318
2319 CONVERT_BINOP(v, w, &a, &b);
2320
2321 if (l_divmod(a, b, &div, &mod) < 0) {
2322 Py_DECREF(a);
2323 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002324 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002325 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002326 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002327 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002328 PyTuple_SetItem(z, 0, (PyObject *) div);
2329 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002330 }
2331 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002332 Py_DECREF(div);
2333 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002334 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002335 Py_DECREF(a);
2336 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002337 return z;
2338}
2339
Tim Peters47e52ee2004-08-30 02:44:38 +00002340/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002341static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002342long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002343{
Tim Peters47e52ee2004-08-30 02:44:38 +00002344 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2345 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002346
Tim Peters47e52ee2004-08-30 02:44:38 +00002347 PyLongObject *z = NULL; /* accumulated result */
2348 int i, j, k; /* counters */
2349 PyLongObject *temp = NULL;
2350
2351 /* 5-ary values. If the exponent is large enough, table is
2352 * precomputed so that table[i] == a**i % c for i in range(32).
2353 */
2354 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2355 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2356
2357 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002358 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002359 if (PyLong_Check(x)) {
2360 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002361 Py_INCREF(x);
2362 }
Tim Petersde7990b2005-07-17 23:45:23 +00002363 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002364 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002365 if (c == NULL)
2366 goto Error;
2367 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002368 else if (x == Py_None)
2369 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002370 else {
2371 Py_DECREF(a);
2372 Py_DECREF(b);
2373 Py_INCREF(Py_NotImplemented);
2374 return Py_NotImplemented;
2375 }
Tim Peters4c483c42001-09-05 06:24:58 +00002376
Tim Peters47e52ee2004-08-30 02:44:38 +00002377 if (b->ob_size < 0) { /* if exponent is negative */
2378 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002379 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002380 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002381 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002382 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002383 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002384 /* else return a float. This works because we know
2385 that this calls float_pow() which converts its
2386 arguments to double. */
2387 Py_DECREF(a);
2388 Py_DECREF(b);
2389 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002390 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002391 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002392
2393 if (c) {
2394 /* if modulus == 0:
2395 raise ValueError() */
2396 if (c->ob_size == 0) {
2397 PyErr_SetString(PyExc_ValueError,
2398 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002399 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002400 }
2401
2402 /* if modulus < 0:
2403 negativeOutput = True
2404 modulus = -modulus */
2405 if (c->ob_size < 0) {
2406 negativeOutput = 1;
2407 temp = (PyLongObject *)_PyLong_Copy(c);
2408 if (temp == NULL)
2409 goto Error;
2410 Py_DECREF(c);
2411 c = temp;
2412 temp = NULL;
2413 c->ob_size = - c->ob_size;
2414 }
2415
2416 /* if modulus == 1:
2417 return 0 */
2418 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2419 z = (PyLongObject *)PyLong_FromLong(0L);
2420 goto Done;
2421 }
2422
2423 /* if base < 0:
2424 base = base % modulus
2425 Having the base positive just makes things easier. */
2426 if (a->ob_size < 0) {
2427 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002428 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002429 Py_DECREF(a);
2430 a = temp;
2431 temp = NULL;
2432 }
2433 }
2434
2435 /* At this point a, b, and c are guaranteed non-negative UNLESS
2436 c is NULL, in which case a may be negative. */
2437
2438 z = (PyLongObject *)PyLong_FromLong(1L);
2439 if (z == NULL)
2440 goto Error;
2441
2442 /* Perform a modular reduction, X = X % c, but leave X alone if c
2443 * is NULL.
2444 */
2445#define REDUCE(X) \
2446 if (c != NULL) { \
2447 if (l_divmod(X, c, NULL, &temp) < 0) \
2448 goto Error; \
2449 Py_XDECREF(X); \
2450 X = temp; \
2451 temp = NULL; \
2452 }
2453
2454 /* Multiply two values, then reduce the result:
2455 result = X*Y % c. If c is NULL, skip the mod. */
2456#define MULT(X, Y, result) \
2457{ \
2458 temp = (PyLongObject *)long_mul(X, Y); \
2459 if (temp == NULL) \
2460 goto Error; \
2461 Py_XDECREF(result); \
2462 result = temp; \
2463 temp = NULL; \
2464 REDUCE(result) \
2465}
2466
2467 if (b->ob_size <= FIVEARY_CUTOFF) {
2468 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2469 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2470 for (i = b->ob_size - 1; i >= 0; --i) {
2471 digit bi = b->ob_digit[i];
2472
2473 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2474 MULT(z, z, z)
2475 if (bi & j)
2476 MULT(z, a, z)
2477 }
2478 }
2479 }
2480 else {
2481 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2482 Py_INCREF(z); /* still holds 1L */
2483 table[0] = z;
2484 for (i = 1; i < 32; ++i)
2485 MULT(table[i-1], a, table[i])
2486
2487 for (i = b->ob_size - 1; i >= 0; --i) {
2488 const digit bi = b->ob_digit[i];
2489
2490 for (j = SHIFT - 5; j >= 0; j -= 5) {
2491 const int index = (bi >> j) & 0x1f;
2492 for (k = 0; k < 5; ++k)
2493 MULT(z, z, z)
2494 if (index)
2495 MULT(z, table[index], z)
2496 }
2497 }
2498 }
2499
2500 if (negativeOutput && (z->ob_size != 0)) {
2501 temp = (PyLongObject *)long_sub(z, c);
2502 if (temp == NULL)
2503 goto Error;
2504 Py_DECREF(z);
2505 z = temp;
2506 temp = NULL;
2507 }
2508 goto Done;
2509
2510 Error:
2511 if (z != NULL) {
2512 Py_DECREF(z);
2513 z = NULL;
2514 }
2515 /* fall through */
2516 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002517 if (b->ob_size > FIVEARY_CUTOFF) {
2518 for (i = 0; i < 32; ++i)
2519 Py_XDECREF(table[i]);
2520 }
Tim Petersde7990b2005-07-17 23:45:23 +00002521 Py_DECREF(a);
2522 Py_DECREF(b);
2523 Py_XDECREF(c);
2524 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002525 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002526}
2527
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002528static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002529long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002530{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002531 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002532 PyLongObject *x;
2533 PyLongObject *w;
2534 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002535 if (w == NULL)
2536 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002537 x = (PyLongObject *) long_add(v, w);
2538 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002539 if (x == NULL)
2540 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002541 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002542 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002543}
2544
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002545static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002546long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002547{
Tim Peters69c2de32001-09-11 22:31:33 +00002548 if (PyLong_CheckExact(v)) {
2549 Py_INCREF(v);
2550 return (PyObject *)v;
2551 }
2552 else
2553 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002554}
2555
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002556static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002557long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002558{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002559 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002560 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002561 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002562 Py_INCREF(v);
2563 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002564 }
Tim Peters69c2de32001-09-11 22:31:33 +00002565 z = (PyLongObject *)_PyLong_Copy(v);
2566 if (z != NULL)
2567 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002568 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002569}
2570
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002571static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002572long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002573{
2574 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002575 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002576 else
2577 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002578}
2579
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002580static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002581long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002582{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002583 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002584}
2585
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002586static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002587long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002588{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002589 PyLongObject *a, *b;
2590 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002591 long shiftby;
2592 int newsize, wordshift, loshift, hishift, i, j;
2593 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002594
Neil Schemenauerba872e22001-01-04 01:46:03 +00002595 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2596
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002597 if (a->ob_size < 0) {
2598 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002599 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002600 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002601 if (a1 == NULL)
2602 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002603 a2 = (PyLongObject *) long_rshift(a1, b);
2604 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002605 if (a2 == NULL)
2606 goto rshift_error;
2607 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002608 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002609 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002610 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002611
Neil Schemenauerba872e22001-01-04 01:46:03 +00002612 shiftby = PyLong_AsLong((PyObject *)b);
2613 if (shiftby == -1L && PyErr_Occurred())
2614 goto rshift_error;
2615 if (shiftby < 0) {
2616 PyErr_SetString(PyExc_ValueError,
2617 "negative shift count");
2618 goto rshift_error;
2619 }
2620 wordshift = shiftby / SHIFT;
2621 newsize = ABS(a->ob_size) - wordshift;
2622 if (newsize <= 0) {
2623 z = _PyLong_New(0);
2624 Py_DECREF(a);
2625 Py_DECREF(b);
2626 return (PyObject *)z;
2627 }
2628 loshift = shiftby % SHIFT;
2629 hishift = SHIFT - loshift;
2630 lomask = ((digit)1 << hishift) - 1;
2631 himask = MASK ^ lomask;
2632 z = _PyLong_New(newsize);
2633 if (z == NULL)
2634 goto rshift_error;
2635 if (a->ob_size < 0)
2636 z->ob_size = -(z->ob_size);
2637 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2638 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2639 if (i+1 < newsize)
2640 z->ob_digit[i] |=
2641 (a->ob_digit[j+1] << hishift) & himask;
2642 }
2643 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002644 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002645rshift_error:
2646 Py_DECREF(a);
2647 Py_DECREF(b);
2648 return (PyObject *) z;
2649
Guido van Rossumc6913e71991-11-19 20:26:46 +00002650}
2651
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002652static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002653long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002654{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002655 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002656 PyLongObject *a, *b;
2657 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002658 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002659 int oldsize, newsize, wordshift, remshift, i, j;
2660 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002661
Neil Schemenauerba872e22001-01-04 01:46:03 +00002662 CONVERT_BINOP(v, w, &a, &b);
2663
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002664 shiftby = PyLong_AsLong((PyObject *)b);
2665 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002666 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002667 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002668 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002669 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002670 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002671 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002672 PyErr_SetString(PyExc_ValueError,
2673 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002674 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002675 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002676 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2677 wordshift = (int)shiftby / SHIFT;
2678 remshift = (int)shiftby - wordshift * SHIFT;
2679
2680 oldsize = ABS(a->ob_size);
2681 newsize = oldsize + wordshift;
2682 if (remshift)
2683 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002684 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002685 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002686 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002687 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002688 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002689 for (i = 0; i < wordshift; i++)
2690 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002691 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002692 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002693 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002694 z->ob_digit[i] = (digit)(accum & MASK);
2695 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002696 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002697 if (remshift)
2698 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002699 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002700 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002701 z = long_normalize(z);
2702lshift_error:
2703 Py_DECREF(a);
2704 Py_DECREF(b);
2705 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002706}
2707
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002708
2709/* Bitwise and/xor/or operations */
2710
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002711static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002712long_bitwise(PyLongObject *a,
2713 int op, /* '&', '|', '^' */
2714 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002715{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002716 digit maska, maskb; /* 0 or MASK */
2717 int negz;
2718 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002719 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002720 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002721 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002722 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002723
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002724 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002725 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002726 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002727 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002728 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002729 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002730 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002731 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002732 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002733 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002734 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002735 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002736 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002737 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002738 maskb = 0;
2739 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002740
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002741 negz = 0;
2742 switch (op) {
2743 case '^':
2744 if (maska != maskb) {
2745 maska ^= MASK;
2746 negz = -1;
2747 }
2748 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002749 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002750 if (maska && maskb) {
2751 op = '|';
2752 maska ^= MASK;
2753 maskb ^= MASK;
2754 negz = -1;
2755 }
2756 break;
2757 case '|':
2758 if (maska || maskb) {
2759 op = '&';
2760 maska ^= MASK;
2761 maskb ^= MASK;
2762 negz = -1;
2763 }
2764 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002765 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002766
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002767 /* JRH: The original logic here was to allocate the result value (z)
2768 as the longer of the two operands. However, there are some cases
2769 where the result is guaranteed to be shorter than that: AND of two
2770 positives, OR of two negatives: use the shorter number. AND with
2771 mixed signs: use the positive number. OR with mixed signs: use the
2772 negative number. After the transformations above, op will be '&'
2773 iff one of these cases applies, and mask will be non-0 for operands
2774 whose length should be ignored.
2775 */
2776
2777 size_a = a->ob_size;
2778 size_b = b->ob_size;
2779 size_z = op == '&'
2780 ? (maska
2781 ? size_b
2782 : (maskb ? size_a : MIN(size_a, size_b)))
2783 : MAX(size_a, size_b);
2784 z = _PyLong_New(size_z);
2785 if (a == NULL || b == NULL || z == NULL) {
2786 Py_XDECREF(a);
2787 Py_XDECREF(b);
2788 Py_XDECREF(z);
2789 return NULL;
2790 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002791
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002792 for (i = 0; i < size_z; ++i) {
2793 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2794 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2795 switch (op) {
2796 case '&': z->ob_digit[i] = diga & digb; break;
2797 case '|': z->ob_digit[i] = diga | digb; break;
2798 case '^': z->ob_digit[i] = diga ^ digb; break;
2799 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002800 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002801
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002802 Py_DECREF(a);
2803 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002804 z = long_normalize(z);
2805 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002806 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002807 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002808 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002809 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002810}
2811
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002812static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002813long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002814{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002815 PyLongObject *a, *b;
2816 PyObject *c;
2817 CONVERT_BINOP(v, w, &a, &b);
2818 c = long_bitwise(a, '&', b);
2819 Py_DECREF(a);
2820 Py_DECREF(b);
2821 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002822}
2823
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002824static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002825long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002826{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002827 PyLongObject *a, *b;
2828 PyObject *c;
2829 CONVERT_BINOP(v, w, &a, &b);
2830 c = long_bitwise(a, '^', b);
2831 Py_DECREF(a);
2832 Py_DECREF(b);
2833 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002834}
2835
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002836static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002837long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002838{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002839 PyLongObject *a, *b;
2840 PyObject *c;
2841 CONVERT_BINOP(v, w, &a, &b);
2842 c = long_bitwise(a, '|', b);
2843 Py_DECREF(a);
2844 Py_DECREF(b);
2845 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002846}
2847
Guido van Rossum234f9421993-06-17 12:35:49 +00002848static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002849long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002850{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002851 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002852 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002853 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002854 return 0;
2855 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002856 else if (PyLong_Check(*pw)) {
2857 Py_INCREF(*pv);
2858 Py_INCREF(*pw);
2859 return 0;
2860 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002861 return 1; /* Can't do it */
2862}
2863
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002864static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002865long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002866{
Brett Cannonc3647ac2005-04-26 03:45:26 +00002867 if (PyLong_CheckExact(v))
2868 Py_INCREF(v);
2869 else
2870 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002871 return v;
2872}
2873
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002874static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002875long_int(PyObject *v)
2876{
2877 long x;
2878 x = PyLong_AsLong(v);
2879 if (PyErr_Occurred()) {
2880 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2881 PyErr_Clear();
2882 if (PyLong_CheckExact(v)) {
2883 Py_INCREF(v);
2884 return v;
2885 }
2886 else
2887 return _PyLong_Copy((PyLongObject *)v);
2888 }
2889 else
2890 return NULL;
2891 }
2892 return PyInt_FromLong(x);
2893}
2894
2895static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002896long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002897{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002898 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002899 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002900 if (result == -1.0 && PyErr_Occurred())
2901 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002902 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002903}
2904
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002905static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002906long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002907{
Fred Drake121ee271999-12-23 15:41:28 +00002908 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002909}
2910
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002911static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002912long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002913{
Fred Drake121ee271999-12-23 15:41:28 +00002914 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002915}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002916
2917static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002918long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002919
Tim Peters6d6c1a32001-08-02 04:15:00 +00002920static PyObject *
2921long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2922{
2923 PyObject *x = NULL;
2924 int base = -909; /* unlikely! */
2925 static char *kwlist[] = {"x", "base", 0};
2926
Guido van Rossumbef14172001-08-29 15:47:46 +00002927 if (type != &PyLong_Type)
2928 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002929 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2930 &x, &base))
2931 return NULL;
2932 if (x == NULL)
2933 return PyLong_FromLong(0L);
2934 if (base == -909)
2935 return PyNumber_Long(x);
2936 else if (PyString_Check(x))
2937 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002938#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002939 else if (PyUnicode_Check(x))
2940 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2941 PyUnicode_GET_SIZE(x),
2942 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002943#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002944 else {
2945 PyErr_SetString(PyExc_TypeError,
2946 "long() can't convert non-string with explicit base");
2947 return NULL;
2948 }
2949}
2950
Guido van Rossumbef14172001-08-29 15:47:46 +00002951/* Wimpy, slow approach to tp_new calls for subtypes of long:
2952 first create a regular long from whatever arguments we got,
2953 then allocate a subtype instance and initialize it from
2954 the regular long. The regular long is then thrown away.
2955*/
2956static PyObject *
2957long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2958{
2959 PyLongObject *tmp, *new;
2960 int i, n;
2961
2962 assert(PyType_IsSubtype(type, &PyLong_Type));
2963 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2964 if (tmp == NULL)
2965 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002966 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002967 n = tmp->ob_size;
2968 if (n < 0)
2969 n = -n;
2970 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002971 if (new == NULL) {
2972 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002973 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002974 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002975 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002976 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002977 for (i = 0; i < n; i++)
2978 new->ob_digit[i] = tmp->ob_digit[i];
2979 Py_DECREF(tmp);
2980 return (PyObject *)new;
2981}
2982
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002983static PyObject *
2984long_getnewargs(PyLongObject *v)
2985{
2986 return Py_BuildValue("(N)", _PyLong_Copy(v));
2987}
2988
2989static PyMethodDef long_methods[] = {
2990 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2991 {NULL, NULL} /* sentinel */
2992};
2993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002995"long(x[, base]) -> integer\n\
2996\n\
2997Convert a string or number to a long integer, if possible. A floating\n\
2998point argument will be truncated towards zero (this does not include a\n\
2999string representation of a floating point number!) When converting a\n\
3000string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003001converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003003static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003004 (binaryfunc) long_add, /*nb_add*/
3005 (binaryfunc) long_sub, /*nb_subtract*/
3006 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00003007 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003008 (binaryfunc) long_mod, /*nb_remainder*/
3009 (binaryfunc) long_divmod, /*nb_divmod*/
3010 (ternaryfunc) long_pow, /*nb_power*/
3011 (unaryfunc) long_neg, /*nb_negative*/
3012 (unaryfunc) long_pos, /*tp_positive*/
3013 (unaryfunc) long_abs, /*tp_absolute*/
3014 (inquiry) long_nonzero, /*tp_nonzero*/
3015 (unaryfunc) long_invert, /*nb_invert*/
3016 (binaryfunc) long_lshift, /*nb_lshift*/
3017 (binaryfunc) long_rshift, /*nb_rshift*/
3018 (binaryfunc) long_and, /*nb_and*/
3019 (binaryfunc) long_xor, /*nb_xor*/
3020 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00003021 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003022 (unaryfunc) long_int, /*nb_int*/
3023 (unaryfunc) long_long, /*nb_long*/
3024 (unaryfunc) long_float, /*nb_float*/
3025 (unaryfunc) long_oct, /*nb_oct*/
3026 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003027 0, /* nb_inplace_add */
3028 0, /* nb_inplace_subtract */
3029 0, /* nb_inplace_multiply */
3030 0, /* nb_inplace_divide */
3031 0, /* nb_inplace_remainder */
3032 0, /* nb_inplace_power */
3033 0, /* nb_inplace_lshift */
3034 0, /* nb_inplace_rshift */
3035 0, /* nb_inplace_and */
3036 0, /* nb_inplace_xor */
3037 0, /* nb_inplace_or */
3038 (binaryfunc)long_div, /* nb_floor_divide */
3039 long_true_divide, /* nb_true_divide */
3040 0, /* nb_inplace_floor_divide */
3041 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003042};
3043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003044PyTypeObject PyLong_Type = {
3045 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003046 0, /* ob_size */
3047 "long", /* tp_name */
3048 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3049 sizeof(digit), /* tp_itemsize */
3050 (destructor)long_dealloc, /* tp_dealloc */
3051 0, /* tp_print */
3052 0, /* tp_getattr */
3053 0, /* tp_setattr */
3054 (cmpfunc)long_compare, /* tp_compare */
3055 (reprfunc)long_repr, /* tp_repr */
3056 &long_as_number, /* tp_as_number */
3057 0, /* tp_as_sequence */
3058 0, /* tp_as_mapping */
3059 (hashfunc)long_hash, /* tp_hash */
3060 0, /* tp_call */
3061 (reprfunc)long_str, /* tp_str */
3062 PyObject_GenericGetAttr, /* tp_getattro */
3063 0, /* tp_setattro */
3064 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003065 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3066 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003067 long_doc, /* tp_doc */
3068 0, /* tp_traverse */
3069 0, /* tp_clear */
3070 0, /* tp_richcompare */
3071 0, /* tp_weaklistoffset */
3072 0, /* tp_iter */
3073 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003074 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003075 0, /* tp_members */
3076 0, /* tp_getset */
3077 0, /* tp_base */
3078 0, /* tp_dict */
3079 0, /* tp_descr_get */
3080 0, /* tp_descr_set */
3081 0, /* tp_dictoffset */
3082 0, /* tp_init */
3083 0, /* tp_alloc */
3084 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003085 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003086};