blob: 0ee9a694a99e4903956af3877b2c6164d60f2b92 [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)) {
786 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000787 return (PY_LONG_LONG)PyInt_AsLong(vv);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000788 PyErr_BadInternalCall();
789 return -1;
790 }
791
Tim Petersd1a7da62001-06-13 00:35:57 +0000792 res = _PyLong_AsByteArray(
793 (PyLongObject *)vv, (unsigned char *)&bytes,
794 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000795
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000796 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000797 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000798 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000799 else
800 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000801}
802
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000803/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000804 Return -1 and set an error if overflow occurs. */
805
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000806unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000807PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000808{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000809 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000810 int one = 1;
811 int res;
812
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000813 if (vv == NULL || !PyLong_Check(vv)) {
814 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000815 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000816 }
817
Tim Petersd1a7da62001-06-13 00:35:57 +0000818 res = _PyLong_AsByteArray(
819 (PyLongObject *)vv, (unsigned char *)&bytes,
820 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000821
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000822 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000823 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000824 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000825 else
826 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000827}
Tim Petersd1a7da62001-06-13 00:35:57 +0000828
Thomas Hellera4ea6032003-04-17 18:55:45 +0000829/* Get a C unsigned long int from a long int object, ignoring the high bits.
830 Returns -1 and sets an error condition if an error occurs. */
831
832unsigned PY_LONG_LONG
833PyLong_AsUnsignedLongLongMask(PyObject *vv)
834{
835 register PyLongObject *v;
836 unsigned PY_LONG_LONG x;
837 int i, sign;
838
839 if (vv == NULL || !PyLong_Check(vv)) {
840 PyErr_BadInternalCall();
841 return (unsigned long) -1;
842 }
843 v = (PyLongObject *)vv;
844 i = v->ob_size;
845 sign = 1;
846 x = 0;
847 if (i < 0) {
848 sign = -1;
849 i = -i;
850 }
851 while (--i >= 0) {
852 x = (x << SHIFT) + v->ob_digit[i];
853 }
854 return x * sign;
855}
Tim Petersd1a7da62001-06-13 00:35:57 +0000856#undef IS_LITTLE_ENDIAN
857
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000858#endif /* HAVE_LONG_LONG */
859
Neil Schemenauerba872e22001-01-04 01:46:03 +0000860
861static int
862convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000863 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000864 *a = (PyLongObject *) v;
865 Py_INCREF(v);
866 }
867 else if (PyInt_Check(v)) {
868 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
869 }
870 else {
871 return 0;
872 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000873 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000874 *b = (PyLongObject *) w;
875 Py_INCREF(w);
876 }
877 else if (PyInt_Check(w)) {
878 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
879 }
880 else {
881 Py_DECREF(*a);
882 return 0;
883 }
884 return 1;
885}
886
887#define CONVERT_BINOP(v, w, a, b) \
888 if (!convert_binop(v, w, a, b)) { \
889 Py_INCREF(Py_NotImplemented); \
890 return Py_NotImplemented; \
891 }
892
Tim Peters877a2122002-08-12 05:09:36 +0000893/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
894 * is modified in place, by adding y to it. Carries are propagated as far as
895 * x[m-1], and the remaining carry (0 or 1) is returned.
896 */
897static digit
898v_iadd(digit *x, int m, digit *y, int n)
899{
900 int i;
901 digit carry = 0;
902
903 assert(m >= n);
904 for (i = 0; i < n; ++i) {
905 carry += x[i] + y[i];
906 x[i] = carry & MASK;
907 carry >>= SHIFT;
908 assert((carry & 1) == carry);
909 }
910 for (; carry && i < m; ++i) {
911 carry += x[i];
912 x[i] = carry & MASK;
913 carry >>= SHIFT;
914 assert((carry & 1) == carry);
915 }
916 return carry;
917}
918
919/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
920 * is modified in place, by subtracting y from it. Borrows are propagated as
921 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
922 */
923static digit
924v_isub(digit *x, int m, digit *y, int n)
925{
926 int i;
927 digit borrow = 0;
928
929 assert(m >= n);
930 for (i = 0; i < n; ++i) {
931 borrow = x[i] - y[i] - borrow;
932 x[i] = borrow & MASK;
933 borrow >>= SHIFT;
934 borrow &= 1; /* keep only 1 sign bit */
935 }
936 for (; borrow && i < m; ++i) {
937 borrow = x[i] - borrow;
938 x[i] = borrow & MASK;
939 borrow >>= SHIFT;
940 borrow &= 1;
941 }
942 return borrow;
943}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000944
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000945/* Multiply by a single digit, ignoring the sign. */
946
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000948mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000949{
950 return muladd1(a, n, (digit)0);
951}
952
953/* Multiply by a single digit and add a single digit, ignoring the sign. */
954
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000956muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000957{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000958 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000960 twodigits carry = extra;
961 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000962
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000963 if (z == NULL)
964 return NULL;
965 for (i = 0; i < size_a; ++i) {
966 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000967 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000968 carry >>= SHIFT;
969 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000970 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000971 return long_normalize(z);
972}
973
Tim Peters212e6142001-07-14 12:23:19 +0000974/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
975 in pout, and returning the remainder. pin and pout point at the LSD.
976 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
977 long_format, but that should be done with great care since longs are
978 immutable. */
979
980static digit
981inplace_divrem1(digit *pout, digit *pin, int size, digit n)
982{
983 twodigits rem = 0;
984
985 assert(n > 0 && n <= MASK);
986 pin += size;
987 pout += size;
988 while (--size >= 0) {
989 digit hi;
990 rem = (rem << SHIFT) + *--pin;
991 *--pout = hi = (digit)(rem / n);
992 rem -= hi * n;
993 }
994 return (digit)rem;
995}
996
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000997/* Divide a long integer by a digit, returning both the quotient
998 (as function result) and the remainder (through *prem).
999 The sign of a is ignored; n should not be zero. */
1000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001001static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001002divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001003{
Tim Peters212e6142001-07-14 12:23:19 +00001004 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001006
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001007 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001008 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001009 if (z == NULL)
1010 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001011 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001012 return long_normalize(z);
1013}
1014
1015/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001016 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001017 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001020long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001021{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 register PyLongObject *a = (PyLongObject *)aa;
1023 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001024 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001025 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001026 char *p;
1027 int bits;
1028 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030 if (a == NULL || !PyLong_Check(a)) {
1031 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001032 return NULL;
1033 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001034 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001035
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001036 /* Compute a rough upper bound for the length of the string */
1037 i = base;
1038 bits = 0;
1039 while (i > 1) {
1040 ++bits;
1041 i >>= 1;
1042 }
Fred Drake121ee271999-12-23 15:41:28 +00001043 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001045 if (str == NULL)
1046 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001048 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001049 if (addL)
1050 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001051 if (a->ob_size < 0)
1052 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001053
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001054 if (a->ob_size == 0) {
1055 *--p = '0';
1056 }
1057 else if ((base & (base - 1)) == 0) {
1058 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001059 twodigits accum = 0;
1060 int accumbits = 0; /* # of bits in accum */
1061 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001062 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001063 while ((i >>= 1) > 1)
1064 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001065
1066 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001067 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001068 accumbits += SHIFT;
1069 assert(accumbits >= basebits);
1070 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001071 char cdigit = (char)(accum & (base - 1));
1072 cdigit += (cdigit < 10) ? '0' : 'A'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001073 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001074 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001075 accumbits -= basebits;
1076 accum >>= basebits;
1077 } while (i < size_a-1 ? accumbits >= basebits :
1078 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001079 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001080 }
1081 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001082 /* Not 0, and base not a power of 2. Divide repeatedly by
1083 base, but for speed use the highest power of base that
1084 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001085 int size = size_a;
1086 digit *pin = a->ob_digit;
1087 PyLongObject *scratch;
1088 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001089 digit powbase = base; /* powbase == base ** power */
1090 int power = 1;
1091 for (;;) {
1092 unsigned long newpow = powbase * (unsigned long)base;
1093 if (newpow >> SHIFT) /* doesn't fit in a digit */
1094 break;
1095 powbase = (digit)newpow;
1096 ++power;
1097 }
Tim Peters212e6142001-07-14 12:23:19 +00001098
1099 /* Get a scratch area for repeated division. */
1100 scratch = _PyLong_New(size);
1101 if (scratch == NULL) {
1102 Py_DECREF(str);
1103 return NULL;
1104 }
1105
1106 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001107 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001108 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001109 digit rem = inplace_divrem1(scratch->ob_digit,
1110 pin, size, powbase);
1111 pin = scratch->ob_digit; /* no need to use a again */
1112 if (pin[size - 1] == 0)
1113 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001114 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001115 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001116 Py_DECREF(str);
1117 return NULL;
1118 })
Tim Peters212e6142001-07-14 12:23:19 +00001119
1120 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001121 assert(ntostore > 0);
1122 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001123 digit nextrem = (digit)(rem / base);
1124 char c = (char)(rem - nextrem * base);
1125 assert(p > PyString_AS_STRING(str));
1126 c += (c < 10) ? '0' : 'A'-10;
1127 *--p = c;
1128 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001129 --ntostore;
1130 /* Termination is a bit delicate: must not
1131 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001132 remaining quotient and rem are both 0. */
1133 } while (ntostore && (size || rem));
1134 } while (size != 0);
1135 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001136 }
1137
Guido van Rossum2c475421992-08-14 15:13:07 +00001138 if (base == 8) {
1139 if (size_a != 0)
1140 *--p = '0';
1141 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001142 else if (base == 16) {
1143 *--p = 'x';
1144 *--p = '0';
1145 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001146 else if (base != 10) {
1147 *--p = '#';
1148 *--p = '0' + base%10;
1149 if (base > 10)
1150 *--p = '0' + base/10;
1151 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001152 if (sign)
1153 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 if (p != PyString_AS_STRING(str)) {
1155 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001156 assert(p > q);
1157 do {
1158 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001159 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001160 _PyString_Resize((PyObject **)&str,
1161 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001162 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001164}
1165
Tim Petersbf2674b2003-02-02 07:51:32 +00001166/* *str points to the first digit in a string of base base digits. base
1167 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1168 * non-digit (which may be *str!). A normalized long is returned.
1169 * The point to this routine is that it takes time linear in the number of
1170 * string characters.
1171 */
1172static PyLongObject *
1173long_from_binary_base(char **str, int base)
1174{
1175 char *p = *str;
1176 char *start = p;
1177 int bits_per_char;
1178 int n;
1179 PyLongObject *z;
1180 twodigits accum;
1181 int bits_in_accum;
1182 digit *pdigit;
1183
1184 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1185 n = base;
1186 for (bits_per_char = -1; n; ++bits_per_char)
1187 n >>= 1;
1188 /* n <- total # of bits needed, while setting p to end-of-string */
1189 n = 0;
1190 for (;;) {
1191 int k = -1;
1192 char ch = *p;
1193
1194 if (ch <= '9')
1195 k = ch - '0';
1196 else if (ch >= 'a')
1197 k = ch - 'a' + 10;
1198 else if (ch >= 'A')
1199 k = ch - 'A' + 10;
1200 if (k < 0 || k >= base)
1201 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001202 ++p;
1203 }
1204 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001205 n = (p - start) * bits_per_char;
1206 if (n / bits_per_char != p - start) {
1207 PyErr_SetString(PyExc_ValueError,
1208 "long string too large to convert");
1209 return NULL;
1210 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001211 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1212 n = (n + SHIFT - 1) / SHIFT;
1213 z = _PyLong_New(n);
1214 if (z == NULL)
1215 return NULL;
1216 /* Read string from right, and fill in long from left; i.e.,
1217 * from least to most significant in both.
1218 */
1219 accum = 0;
1220 bits_in_accum = 0;
1221 pdigit = z->ob_digit;
1222 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001223 int k;
1224 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001225
1226 if (ch <= '9')
1227 k = ch - '0';
1228 else if (ch >= 'a')
1229 k = ch - 'a' + 10;
1230 else {
1231 assert(ch >= 'A');
1232 k = ch - 'A' + 10;
1233 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001234 assert(k >= 0 && k < base);
1235 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001236 bits_in_accum += bits_per_char;
1237 if (bits_in_accum >= SHIFT) {
1238 *pdigit++ = (digit)(accum & MASK);
1239 assert(pdigit - z->ob_digit <= n);
1240 accum >>= SHIFT;
1241 bits_in_accum -= SHIFT;
1242 assert(bits_in_accum < SHIFT);
1243 }
1244 }
1245 if (bits_in_accum) {
1246 assert(bits_in_accum <= SHIFT);
1247 *pdigit++ = (digit)accum;
1248 assert(pdigit - z->ob_digit <= n);
1249 }
1250 while (pdigit - z->ob_digit < n)
1251 *pdigit++ = 0;
1252 return long_normalize(z);
1253}
1254
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001256PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001257{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001258 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001259 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001261
Guido van Rossum472c04f1996-12-05 21:57:21 +00001262 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001263 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001264 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001265 return NULL;
1266 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001267 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001268 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001269 if (*str == '+')
1270 ++str;
1271 else if (*str == '-') {
1272 ++str;
1273 sign = -1;
1274 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001275 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001276 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001277 if (base == 0) {
1278 if (str[0] != '0')
1279 base = 10;
1280 else if (str[1] == 'x' || str[1] == 'X')
1281 base = 16;
1282 else
1283 base = 8;
1284 }
1285 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1286 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001287 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001288 if ((base & (base - 1)) == 0)
1289 z = long_from_binary_base(&str, base);
1290 else {
1291 z = _PyLong_New(0);
1292 for ( ; z != NULL; ++str) {
1293 int k = -1;
1294 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001295
Tim Petersbf2674b2003-02-02 07:51:32 +00001296 if (*str <= '9')
1297 k = *str - '0';
1298 else if (*str >= 'a')
1299 k = *str - 'a' + 10;
1300 else if (*str >= 'A')
1301 k = *str - 'A' + 10;
1302 if (k < 0 || k >= base)
1303 break;
1304 temp = muladd1(z, (digit)base, (digit)k);
1305 Py_DECREF(z);
1306 z = temp;
1307 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001308 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001309 if (z == NULL)
1310 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001311 if (str == start)
1312 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001313 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001314 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001315 if (*str == 'L' || *str == 'l')
1316 str++;
1317 while (*str && isspace(Py_CHARMASK(*str)))
1318 str++;
1319 if (*str != '\0')
1320 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001321 if (pend)
1322 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001324
1325 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001326 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001327 "invalid literal for long(): %.200s", orig_str);
1328 Py_XDECREF(z);
1329 return NULL;
1330}
1331
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001332#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001333PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001334PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001335{
Walter Dörwald07e14762002-11-06 16:15:14 +00001336 PyObject *result;
1337 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001338
Walter Dörwald07e14762002-11-06 16:15:14 +00001339 if (buffer == NULL)
1340 return NULL;
1341
1342 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1343 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001344 return NULL;
1345 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001346 result = PyLong_FromString(buffer, NULL, base);
1347 PyMem_FREE(buffer);
1348 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001349}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001350#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001351
Tim Peters9f688bf2000-07-07 15:53:28 +00001352/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001354 (PyLongObject *, PyLongObject *, PyLongObject **);
1355static PyObject *long_pos(PyLongObject *);
1356static int long_divrem(PyLongObject *, PyLongObject *,
1357 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001358
1359/* Long division with remainder, top-level routine */
1360
Guido van Rossume32e0141992-01-19 16:31:05 +00001361static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001362long_divrem(PyLongObject *a, PyLongObject *b,
1363 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001364{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001365 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001367
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001368 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001369 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001370 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001371 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001372 }
1373 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001374 (size_a == size_b &&
1375 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001376 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 *pdiv = _PyLong_New(0);
1378 Py_INCREF(a);
1379 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001380 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001381 }
1382 if (size_b == 1) {
1383 digit rem = 0;
1384 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001385 if (z == NULL)
1386 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001388 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001389 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001390 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001391 if (z == NULL)
1392 return -1;
1393 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001394 /* Set the signs.
1395 The quotient z has the sign of a*b;
1396 the remainder r has the sign of a,
1397 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001398 if ((a->ob_size < 0) != (b->ob_size < 0))
1399 z->ob_size = -(z->ob_size);
1400 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1401 (*prem)->ob_size = -((*prem)->ob_size);
1402 *pdiv = z;
1403 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001404}
1405
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001406/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001407
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001409x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001410{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001411 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001412 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001413 PyLongObject *v = mul1(v1, d);
1414 PyLongObject *w = mul1(w1, d);
1415 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001416 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001417
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001418 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419 Py_XDECREF(v);
1420 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001421 return NULL;
1422 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001423
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001424 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001425 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001426 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001427
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001428 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001429 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001430
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001431 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1432 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1433 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001434 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001435 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001436
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001437 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001439 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001440 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001441 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001442 if (vj == w->ob_digit[size_w-1])
1443 q = MASK;
1444 else
1445 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1446 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001447
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001448 while (w->ob_digit[size_w-2]*q >
1449 ((
1450 ((twodigits)vj << SHIFT)
1451 + v->ob_digit[j-1]
1452 - q*w->ob_digit[size_w-1]
1453 ) << SHIFT)
1454 + v->ob_digit[j-2])
1455 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001456
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001457 for (i = 0; i < size_w && i+k < size_v; ++i) {
1458 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001459 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460 carry += v->ob_digit[i+k] - z
1461 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001462 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001463 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1464 carry, SHIFT);
1465 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001466 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001467
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001468 if (i+k < size_v) {
1469 carry += v->ob_digit[i+k];
1470 v->ob_digit[i+k] = 0;
1471 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001472
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001473 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001474 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001475 else {
1476 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001477 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001478 carry = 0;
1479 for (i = 0; i < size_w && i+k < size_v; ++i) {
1480 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001481 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001482 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1483 BASE_TWODIGITS_TYPE,
1484 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001485 }
1486 }
1487 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001488
Guido van Rossumc206c761995-01-10 15:23:19 +00001489 if (a == NULL)
1490 *prem = NULL;
1491 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001492 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001493 *prem = divrem1(v, d, &d);
1494 /* d receives the (unused) remainder */
1495 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001496 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001497 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001498 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001499 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500 Py_DECREF(v);
1501 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001502 return a;
1503}
1504
1505/* Methods */
1506
1507static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001508long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001509{
Guido van Rossum9475a232001-10-05 20:51:39 +00001510 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001511}
1512
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001513static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001514long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001515{
Fred Drake121ee271999-12-23 15:41:28 +00001516 return long_format(v, 10, 1);
1517}
1518
1519static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001520long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001521{
1522 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001523}
1524
1525static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001526long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001527{
1528 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001529
Guido van Rossumc6913e71991-11-19 20:26:46 +00001530 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001531 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001532 sign = 0;
1533 else
1534 sign = a->ob_size - b->ob_size;
1535 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001536 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001537 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001538 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1539 ;
1540 if (i < 0)
1541 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001542 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001543 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001544 if (a->ob_size < 0)
1545 sign = -sign;
1546 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001547 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001548 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001549}
1550
Guido van Rossum9bfef441993-03-29 10:43:31 +00001551static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001552long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001553{
1554 long x;
1555 int i, sign;
1556
1557 /* This is designed so that Python ints and longs with the
1558 same value hash to the same value, otherwise comparisons
1559 of mapping keys will turn out weird */
1560 i = v->ob_size;
1561 sign = 1;
1562 x = 0;
1563 if (i < 0) {
1564 sign = -1;
1565 i = -(i);
1566 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001567#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001568 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001569 /* Force a native long #-bits (32 or 64) circular shift */
1570 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001571 x += v->ob_digit[i];
1572 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001573#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001574 x = x * sign;
1575 if (x == -1)
1576 x = -2;
1577 return x;
1578}
1579
1580
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001581/* Add the absolute values of two long integers. */
1582
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001583static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001584x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001585{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001586 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001587 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001588 int i;
1589 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001590
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001591 /* Ensure a is the larger of the two: */
1592 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001593 { PyLongObject *temp = a; a = b; b = temp; }
1594 { int size_temp = size_a;
1595 size_a = size_b;
1596 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001597 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001598 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001599 if (z == NULL)
1600 return NULL;
1601 for (i = 0; i < size_b; ++i) {
1602 carry += a->ob_digit[i] + b->ob_digit[i];
1603 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001604 carry >>= SHIFT;
1605 }
1606 for (; i < size_a; ++i) {
1607 carry += a->ob_digit[i];
1608 z->ob_digit[i] = carry & MASK;
1609 carry >>= SHIFT;
1610 }
1611 z->ob_digit[i] = carry;
1612 return long_normalize(z);
1613}
1614
1615/* Subtract the absolute values of two integers. */
1616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001618x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001619{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001620 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001621 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001622 int i;
1623 int sign = 1;
1624 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001625
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001626 /* Ensure a is the larger of the two: */
1627 if (size_a < size_b) {
1628 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629 { PyLongObject *temp = a; a = b; b = temp; }
1630 { int size_temp = size_a;
1631 size_a = size_b;
1632 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001633 }
1634 else if (size_a == size_b) {
1635 /* Find highest digit where a and b differ: */
1636 i = size_a;
1637 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1638 ;
1639 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001640 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001641 if (a->ob_digit[i] < b->ob_digit[i]) {
1642 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001644 }
1645 size_a = size_b = i+1;
1646 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001647 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001648 if (z == NULL)
1649 return NULL;
1650 for (i = 0; i < size_b; ++i) {
1651 /* The following assumes unsigned arithmetic
1652 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001653 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001654 z->ob_digit[i] = borrow & MASK;
1655 borrow >>= SHIFT;
1656 borrow &= 1; /* Keep only one sign bit */
1657 }
1658 for (; i < size_a; ++i) {
1659 borrow = a->ob_digit[i] - borrow;
1660 z->ob_digit[i] = borrow & MASK;
1661 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001662 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001663 }
1664 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001665 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001666 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001667 return long_normalize(z);
1668}
1669
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001670static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001671long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001672{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001673 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001674
Neil Schemenauerba872e22001-01-04 01:46:03 +00001675 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1676
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001677 if (a->ob_size < 0) {
1678 if (b->ob_size < 0) {
1679 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001680 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001681 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001682 }
1683 else
1684 z = x_sub(b, a);
1685 }
1686 else {
1687 if (b->ob_size < 0)
1688 z = x_sub(a, b);
1689 else
1690 z = x_add(a, b);
1691 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001692 Py_DECREF(a);
1693 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001694 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001695}
1696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001697static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001698long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001699{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001700 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001701
Neil Schemenauerba872e22001-01-04 01:46:03 +00001702 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1703
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001704 if (a->ob_size < 0) {
1705 if (b->ob_size < 0)
1706 z = x_sub(a, b);
1707 else
1708 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001709 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001710 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001711 }
1712 else {
1713 if (b->ob_size < 0)
1714 z = x_add(a, b);
1715 else
1716 z = x_sub(a, b);
1717 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001718 Py_DECREF(a);
1719 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001720 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001721}
1722
Tim Peters5af4e6c2002-08-12 02:31:19 +00001723/* Grade school multiplication, ignoring the signs.
1724 * Returns the absolute value of the product, or NULL if error.
1725 */
1726static PyLongObject *
1727x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001728{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001729 PyLongObject *z;
1730 int size_a = ABS(a->ob_size);
1731 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001732 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001733
Tim Peters5af4e6c2002-08-12 02:31:19 +00001734 z = _PyLong_New(size_a + size_b);
1735 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001737
1738 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001739 if (a == b) {
1740 /* Efficient squaring per HAC, Algorithm 14.16:
1741 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1742 * Gives slightly less than a 2x speedup when a == b,
1743 * via exploiting that each entry in the multiplication
1744 * pyramid appears twice (except for the size_a squares).
1745 */
1746 for (i = 0; i < size_a; ++i) {
1747 twodigits carry;
1748 twodigits f = a->ob_digit[i];
1749 digit *pz = z->ob_digit + (i << 1);
1750 digit *pa = a->ob_digit + i + 1;
1751 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001752
Tim Peters0973b992004-08-29 22:16:50 +00001753 SIGCHECK({
1754 Py_DECREF(z);
1755 return NULL;
1756 })
1757
1758 carry = *pz + f * f;
1759 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001760 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001761 assert(carry <= MASK);
1762
1763 /* Now f is added in twice in each column of the
1764 * pyramid it appears. Same as adding f<<1 once.
1765 */
1766 f <<= 1;
1767 while (pa < paend) {
1768 carry += *pz + *pa++ * f;
1769 *pz++ = (digit)(carry & MASK);
1770 carry >>= SHIFT;
1771 assert(carry <= (MASK << 1));
1772 }
1773 if (carry) {
1774 carry += *pz;
1775 *pz++ = (digit)(carry & MASK);
1776 carry >>= SHIFT;
1777 }
1778 if (carry)
1779 *pz += (digit)(carry & MASK);
1780 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001781 }
Tim Peters0973b992004-08-29 22:16:50 +00001782 }
1783 else { /* a is not the same as b -- gradeschool long mult */
1784 for (i = 0; i < size_a; ++i) {
1785 twodigits carry = 0;
1786 twodigits f = a->ob_digit[i];
1787 digit *pz = z->ob_digit + i;
1788 digit *pb = b->ob_digit;
1789 digit *pbend = b->ob_digit + size_b;
1790
1791 SIGCHECK({
1792 Py_DECREF(z);
1793 return NULL;
1794 })
1795
1796 while (pb < pbend) {
1797 carry += *pz + *pb++ * f;
1798 *pz++ = (digit)(carry & MASK);
1799 carry >>= SHIFT;
1800 assert(carry <= MASK);
1801 }
1802 if (carry)
1803 *pz += (digit)(carry & MASK);
1804 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001805 }
1806 }
Tim Peters44121a62002-08-12 06:17:58 +00001807 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001808}
1809
1810/* A helper for Karatsuba multiplication (k_mul).
1811 Takes a long "n" and an integer "size" representing the place to
1812 split, and sets low and high such that abs(n) == (high << size) + low,
1813 viewing the shift as being by digits. The sign bit is ignored, and
1814 the return values are >= 0.
1815 Returns 0 on success, -1 on failure.
1816*/
1817static int
1818kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1819{
1820 PyLongObject *hi, *lo;
1821 int size_lo, size_hi;
1822 const int size_n = ABS(n->ob_size);
1823
1824 size_lo = MIN(size_n, size);
1825 size_hi = size_n - size_lo;
1826
1827 if ((hi = _PyLong_New(size_hi)) == NULL)
1828 return -1;
1829 if ((lo = _PyLong_New(size_lo)) == NULL) {
1830 Py_DECREF(hi);
1831 return -1;
1832 }
1833
1834 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1835 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1836
1837 *high = long_normalize(hi);
1838 *low = long_normalize(lo);
1839 return 0;
1840}
1841
Tim Peters60004642002-08-12 22:01:34 +00001842static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1843
Tim Peters5af4e6c2002-08-12 02:31:19 +00001844/* Karatsuba multiplication. Ignores the input signs, and returns the
1845 * absolute value of the product (or NULL if error).
1846 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1847 */
1848static PyLongObject *
1849k_mul(PyLongObject *a, PyLongObject *b)
1850{
Tim Peters738eda72002-08-12 15:08:20 +00001851 int asize = ABS(a->ob_size);
1852 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001853 PyLongObject *ah = NULL;
1854 PyLongObject *al = NULL;
1855 PyLongObject *bh = NULL;
1856 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001857 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001858 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001859 int shift; /* the number of digits we split off */
1860 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001861
Tim Peters5af4e6c2002-08-12 02:31:19 +00001862 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1863 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1864 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001865 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001866 * By picking X to be a power of 2, "*X" is just shifting, and it's
1867 * been reduced to 3 multiplies on numbers half the size.
1868 */
1869
Tim Petersfc07e562002-08-12 02:54:10 +00001870 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001871 * is largest.
1872 */
Tim Peters738eda72002-08-12 15:08:20 +00001873 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001874 t1 = a;
1875 a = b;
1876 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001877
1878 i = asize;
1879 asize = bsize;
1880 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001881 }
1882
1883 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001884 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1885 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001886 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001887 return _PyLong_New(0);
1888 else
1889 return x_mul(a, b);
1890 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001891
Tim Peters60004642002-08-12 22:01:34 +00001892 /* If a is small compared to b, splitting on b gives a degenerate
1893 * case with ah==0, and Karatsuba may be (even much) less efficient
1894 * than "grade school" then. However, we can still win, by viewing
1895 * b as a string of "big digits", each of width a->ob_size. That
1896 * leads to a sequence of balanced calls to k_mul.
1897 */
1898 if (2 * asize <= bsize)
1899 return k_lopsided_mul(a, b);
1900
Tim Petersd6974a52002-08-13 20:37:51 +00001901 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001902 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001903 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001904 assert(ah->ob_size > 0); /* the split isn't degenerate */
1905
Tim Peters0973b992004-08-29 22:16:50 +00001906 if (a == b) {
1907 bh = ah;
1908 bl = al;
1909 Py_INCREF(bh);
1910 Py_INCREF(bl);
1911 }
1912 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001913
Tim Petersd64c1de2002-08-12 17:36:03 +00001914 /* The plan:
1915 * 1. Allocate result space (asize + bsize digits: that's always
1916 * enough).
1917 * 2. Compute ah*bh, and copy into result at 2*shift.
1918 * 3. Compute al*bl, and copy into result at 0. Note that this
1919 * can't overlap with #2.
1920 * 4. Subtract al*bl from the result, starting at shift. This may
1921 * underflow (borrow out of the high digit), but we don't care:
1922 * we're effectively doing unsigned arithmetic mod
1923 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1924 * borrows and carries out of the high digit can be ignored.
1925 * 5. Subtract ah*bh from the result, starting at shift.
1926 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1927 * at shift.
1928 */
1929
1930 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001931 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001932 if (ret == NULL) goto fail;
1933#ifdef Py_DEBUG
1934 /* Fill with trash, to catch reference to uninitialized digits. */
1935 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1936#endif
Tim Peters44121a62002-08-12 06:17:58 +00001937
Tim Petersd64c1de2002-08-12 17:36:03 +00001938 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001939 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1940 assert(t1->ob_size >= 0);
1941 assert(2*shift + t1->ob_size <= ret->ob_size);
1942 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1943 t1->ob_size * sizeof(digit));
1944
1945 /* Zero-out the digits higher than the ah*bh copy. */
1946 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001947 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001948 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001949 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001950
Tim Petersd64c1de2002-08-12 17:36:03 +00001951 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001952 if ((t2 = k_mul(al, bl)) == NULL) {
1953 Py_DECREF(t1);
1954 goto fail;
1955 }
1956 assert(t2->ob_size >= 0);
1957 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1958 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001959
1960 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001961 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001962 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001963 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001964
Tim Petersd64c1de2002-08-12 17:36:03 +00001965 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1966 * because it's fresher in cache.
1967 */
Tim Peters738eda72002-08-12 15:08:20 +00001968 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001969 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001970 Py_DECREF(t2);
1971
Tim Petersd64c1de2002-08-12 17:36:03 +00001972 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001973 Py_DECREF(t1);
1974
Tim Petersd64c1de2002-08-12 17:36:03 +00001975 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001976 if ((t1 = x_add(ah, al)) == NULL) goto fail;
1977 Py_DECREF(ah);
1978 Py_DECREF(al);
1979 ah = al = NULL;
1980
Tim Peters0973b992004-08-29 22:16:50 +00001981 if (a == b) {
1982 t2 = t1;
1983 Py_INCREF(t2);
1984 }
1985 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001986 Py_DECREF(t1);
1987 goto fail;
1988 }
1989 Py_DECREF(bh);
1990 Py_DECREF(bl);
1991 bh = bl = NULL;
1992
Tim Peters738eda72002-08-12 15:08:20 +00001993 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001994 Py_DECREF(t1);
1995 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00001996 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00001997 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001998
Tim Petersd6974a52002-08-13 20:37:51 +00001999 /* Add t3. It's not obvious why we can't run out of room here.
2000 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002001 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002002 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002003 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002004
Tim Peters5af4e6c2002-08-12 02:31:19 +00002005 return long_normalize(ret);
2006
2007 fail:
2008 Py_XDECREF(ret);
2009 Py_XDECREF(ah);
2010 Py_XDECREF(al);
2011 Py_XDECREF(bh);
2012 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002013 return NULL;
2014}
2015
Tim Petersd6974a52002-08-13 20:37:51 +00002016/* (*) Why adding t3 can't "run out of room" above.
2017
Tim Petersab86c2b2002-08-15 20:06:00 +00002018Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2019to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002020
Tim Petersab86c2b2002-08-15 20:06:00 +000020211. For any integer i, i = c(i/2) + f(i/2). In particular,
2022 bsize = c(bsize/2) + f(bsize/2).
20232. shift = f(bsize/2)
20243. asize <= bsize
20254. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2026 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002027
Tim Petersab86c2b2002-08-15 20:06:00 +00002028We allocated asize + bsize result digits, and add t3 into them at an offset
2029of shift. This leaves asize+bsize-shift allocated digit positions for t3
2030to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2031asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002032
Tim Petersab86c2b2002-08-15 20:06:00 +00002033bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2034at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002035
Tim Petersab86c2b2002-08-15 20:06:00 +00002036If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2037digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2038most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002039
Tim Petersab86c2b2002-08-15 20:06:00 +00002040The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002041
Tim Petersab86c2b2002-08-15 20:06:00 +00002042 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002043
Tim Petersab86c2b2002-08-15 20:06:00 +00002044and we have asize + c(bsize/2) available digit positions. We need to show
2045this is always enough. An instance of c(bsize/2) cancels out in both, so
2046the question reduces to whether asize digits is enough to hold
2047(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2048then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2049asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2050digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2051asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002052c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2053is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2054bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002055
Tim Peters48d52c02002-08-14 17:07:32 +00002056Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2057clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2058ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002059*/
2060
Tim Peters60004642002-08-12 22:01:34 +00002061/* b has at least twice the digits of a, and a is big enough that Karatsuba
2062 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2063 * of slices, each with a->ob_size digits, and multiply the slices by a,
2064 * one at a time. This gives k_mul balanced inputs to work with, and is
2065 * also cache-friendly (we compute one double-width slice of the result
2066 * at a time, then move on, never bactracking except for the helpful
2067 * single-width slice overlap between successive partial sums).
2068 */
2069static PyLongObject *
2070k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2071{
2072 const int asize = ABS(a->ob_size);
2073 int bsize = ABS(b->ob_size);
2074 int nbdone; /* # of b digits already multiplied */
2075 PyLongObject *ret;
2076 PyLongObject *bslice = NULL;
2077
2078 assert(asize > KARATSUBA_CUTOFF);
2079 assert(2 * asize <= bsize);
2080
2081 /* Allocate result space, and zero it out. */
2082 ret = _PyLong_New(asize + bsize);
2083 if (ret == NULL)
2084 return NULL;
2085 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2086
2087 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002088 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002089 if (bslice == NULL)
2090 goto fail;
2091
2092 nbdone = 0;
2093 while (bsize > 0) {
2094 PyLongObject *product;
2095 const int nbtouse = MIN(bsize, asize);
2096
2097 /* Multiply the next slice of b by a. */
2098 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2099 nbtouse * sizeof(digit));
2100 bslice->ob_size = nbtouse;
2101 product = k_mul(a, bslice);
2102 if (product == NULL)
2103 goto fail;
2104
2105 /* Add into result. */
2106 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2107 product->ob_digit, product->ob_size);
2108 Py_DECREF(product);
2109
2110 bsize -= nbtouse;
2111 nbdone += nbtouse;
2112 }
2113
2114 Py_DECREF(bslice);
2115 return long_normalize(ret);
2116
2117 fail:
2118 Py_DECREF(ret);
2119 Py_XDECREF(bslice);
2120 return NULL;
2121}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002122
2123static PyObject *
2124long_mul(PyLongObject *v, PyLongObject *w)
2125{
2126 PyLongObject *a, *b, *z;
2127
2128 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002129 Py_INCREF(Py_NotImplemented);
2130 return Py_NotImplemented;
2131 }
2132
Tim Petersd64c1de2002-08-12 17:36:03 +00002133 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002134 /* Negate if exactly one of the inputs is negative. */
2135 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002136 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002137 Py_DECREF(a);
2138 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002139 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002140}
2141
Guido van Rossume32e0141992-01-19 16:31:05 +00002142/* The / and % operators are now defined in terms of divmod().
2143 The expression a mod b has the value a - b*floor(a/b).
2144 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002145 |a| by |b|, with the sign of a. This is also expressed
2146 as a - b*trunc(a/b), if trunc truncates towards zero.
2147 Some examples:
2148 a b a rem b a mod b
2149 13 10 3 3
2150 -13 10 -3 7
2151 13 -10 3 -7
2152 -13 -10 -3 -3
2153 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002154 have different signs. We then subtract one from the 'div'
2155 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002156
Tim Peters47e52ee2004-08-30 02:44:38 +00002157/* Compute
2158 * *pdiv, *pmod = divmod(v, w)
2159 * NULL can be passed for pdiv or pmod, in which case that part of
2160 * the result is simply thrown away. The caller owns a reference to
2161 * each of these it requests (does not pass NULL for).
2162 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002163static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002164l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002165 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002166{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002167 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002168
Guido van Rossume32e0141992-01-19 16:31:05 +00002169 if (long_divrem(v, w, &div, &mod) < 0)
2170 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002171 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2172 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002173 PyLongObject *temp;
2174 PyLongObject *one;
2175 temp = (PyLongObject *) long_add(mod, w);
2176 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002177 mod = temp;
2178 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002179 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002180 return -1;
2181 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002182 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002183 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002184 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2185 Py_DECREF(mod);
2186 Py_DECREF(div);
2187 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002188 return -1;
2189 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002190 Py_DECREF(one);
2191 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002192 div = temp;
2193 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002194 if (pdiv != NULL)
2195 *pdiv = div;
2196 else
2197 Py_DECREF(div);
2198
2199 if (pmod != NULL)
2200 *pmod = mod;
2201 else
2202 Py_DECREF(mod);
2203
Guido van Rossume32e0141992-01-19 16:31:05 +00002204 return 0;
2205}
2206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002208long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002209{
Tim Peters47e52ee2004-08-30 02:44:38 +00002210 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002211
2212 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002213 if (l_divmod(a, b, &div, NULL) < 0)
2214 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002215 Py_DECREF(a);
2216 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002217 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002218}
2219
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002220static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002221long_classic_div(PyObject *v, PyObject *w)
2222{
Tim Peters47e52ee2004-08-30 02:44:38 +00002223 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002224
2225 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002226 if (Py_DivisionWarningFlag &&
2227 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2228 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002229 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002230 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002231 Py_DECREF(a);
2232 Py_DECREF(b);
2233 return (PyObject *)div;
2234}
2235
2236static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002237long_true_divide(PyObject *v, PyObject *w)
2238{
Tim Peterse2a60002001-09-04 06:17:36 +00002239 PyLongObject *a, *b;
2240 double ad, bd;
Tim Peters6f97e492001-11-04 23:09:40 +00002241 int aexp, bexp, failed;
Tim Peterse2a60002001-09-04 06:17:36 +00002242
2243 CONVERT_BINOP(v, w, &a, &b);
2244 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2245 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002246 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2247 Py_DECREF(a);
2248 Py_DECREF(b);
2249 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002250 return NULL;
2251
2252 if (bd == 0.0) {
2253 PyErr_SetString(PyExc_ZeroDivisionError,
2254 "long division or modulo by zero");
2255 return NULL;
2256 }
2257
2258 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2259 ad /= bd; /* overflow/underflow impossible here */
2260 aexp -= bexp;
2261 if (aexp > INT_MAX / SHIFT)
2262 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002263 else if (aexp < -(INT_MAX / SHIFT))
2264 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002265 errno = 0;
2266 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002267 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002268 goto overflow;
2269 return PyFloat_FromDouble(ad);
2270
2271overflow:
2272 PyErr_SetString(PyExc_OverflowError,
2273 "long/long too large for a float");
2274 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002275
Tim Peters20dab9f2001-09-04 05:31:47 +00002276}
2277
2278static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002279long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002280{
Tim Peters47e52ee2004-08-30 02:44:38 +00002281 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002282
2283 CONVERT_BINOP(v, w, &a, &b);
2284
Tim Peters47e52ee2004-08-30 02:44:38 +00002285 if (l_divmod(a, b, NULL, &mod) < 0)
2286 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002287 Py_DECREF(a);
2288 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002289 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002290}
2291
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002292static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002293long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002294{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002295 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002296 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002297
2298 CONVERT_BINOP(v, w, &a, &b);
2299
2300 if (l_divmod(a, b, &div, &mod) < 0) {
2301 Py_DECREF(a);
2302 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002303 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002304 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002305 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002306 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002307 PyTuple_SetItem(z, 0, (PyObject *) div);
2308 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002309 }
2310 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002311 Py_DECREF(div);
2312 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002313 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002314 Py_DECREF(a);
2315 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002316 return z;
2317}
2318
Tim Peters47e52ee2004-08-30 02:44:38 +00002319/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002320static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002321long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002322{
Tim Peters47e52ee2004-08-30 02:44:38 +00002323 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2324 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002325
Tim Peters47e52ee2004-08-30 02:44:38 +00002326 PyLongObject *z = NULL; /* accumulated result */
2327 int i, j, k; /* counters */
2328 PyLongObject *temp = NULL;
2329
2330 /* 5-ary values. If the exponent is large enough, table is
2331 * precomputed so that table[i] == a**i % c for i in range(32).
2332 */
2333 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2334 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2335
2336 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002337 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002338 if (PyLong_Check(x)) {
2339 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002340 Py_INCREF(x);
2341 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002342 else if (PyInt_Check(x))
2343 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
2344 else if (x == Py_None)
2345 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002346 else {
2347 Py_DECREF(a);
2348 Py_DECREF(b);
2349 Py_INCREF(Py_NotImplemented);
2350 return Py_NotImplemented;
2351 }
Tim Peters4c483c42001-09-05 06:24:58 +00002352
Tim Peters47e52ee2004-08-30 02:44:38 +00002353 if (b->ob_size < 0) { /* if exponent is negative */
2354 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002355 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002356 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002357 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002358 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002359 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002360 /* else return a float. This works because we know
2361 that this calls float_pow() which converts its
2362 arguments to double. */
2363 Py_DECREF(a);
2364 Py_DECREF(b);
2365 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002366 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002367 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002368
2369 if (c) {
2370 /* if modulus == 0:
2371 raise ValueError() */
2372 if (c->ob_size == 0) {
2373 PyErr_SetString(PyExc_ValueError,
2374 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002375 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002376 }
2377
2378 /* if modulus < 0:
2379 negativeOutput = True
2380 modulus = -modulus */
2381 if (c->ob_size < 0) {
2382 negativeOutput = 1;
2383 temp = (PyLongObject *)_PyLong_Copy(c);
2384 if (temp == NULL)
2385 goto Error;
2386 Py_DECREF(c);
2387 c = temp;
2388 temp = NULL;
2389 c->ob_size = - c->ob_size;
2390 }
2391
2392 /* if modulus == 1:
2393 return 0 */
2394 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2395 z = (PyLongObject *)PyLong_FromLong(0L);
2396 goto Done;
2397 }
2398
2399 /* if base < 0:
2400 base = base % modulus
2401 Having the base positive just makes things easier. */
2402 if (a->ob_size < 0) {
2403 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002404 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002405 Py_DECREF(a);
2406 a = temp;
2407 temp = NULL;
2408 }
2409 }
2410
2411 /* At this point a, b, and c are guaranteed non-negative UNLESS
2412 c is NULL, in which case a may be negative. */
2413
2414 z = (PyLongObject *)PyLong_FromLong(1L);
2415 if (z == NULL)
2416 goto Error;
2417
2418 /* Perform a modular reduction, X = X % c, but leave X alone if c
2419 * is NULL.
2420 */
2421#define REDUCE(X) \
2422 if (c != NULL) { \
2423 if (l_divmod(X, c, NULL, &temp) < 0) \
2424 goto Error; \
2425 Py_XDECREF(X); \
2426 X = temp; \
2427 temp = NULL; \
2428 }
2429
2430 /* Multiply two values, then reduce the result:
2431 result = X*Y % c. If c is NULL, skip the mod. */
2432#define MULT(X, Y, result) \
2433{ \
2434 temp = (PyLongObject *)long_mul(X, Y); \
2435 if (temp == NULL) \
2436 goto Error; \
2437 Py_XDECREF(result); \
2438 result = temp; \
2439 temp = NULL; \
2440 REDUCE(result) \
2441}
2442
2443 if (b->ob_size <= FIVEARY_CUTOFF) {
2444 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2445 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2446 for (i = b->ob_size - 1; i >= 0; --i) {
2447 digit bi = b->ob_digit[i];
2448
2449 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2450 MULT(z, z, z)
2451 if (bi & j)
2452 MULT(z, a, z)
2453 }
2454 }
2455 }
2456 else {
2457 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2458 Py_INCREF(z); /* still holds 1L */
2459 table[0] = z;
2460 for (i = 1; i < 32; ++i)
2461 MULT(table[i-1], a, table[i])
2462
2463 for (i = b->ob_size - 1; i >= 0; --i) {
2464 const digit bi = b->ob_digit[i];
2465
2466 for (j = SHIFT - 5; j >= 0; j -= 5) {
2467 const int index = (bi >> j) & 0x1f;
2468 for (k = 0; k < 5; ++k)
2469 MULT(z, z, z)
2470 if (index)
2471 MULT(z, table[index], z)
2472 }
2473 }
2474 }
2475
2476 if (negativeOutput && (z->ob_size != 0)) {
2477 temp = (PyLongObject *)long_sub(z, c);
2478 if (temp == NULL)
2479 goto Error;
2480 Py_DECREF(z);
2481 z = temp;
2482 temp = NULL;
2483 }
2484 goto Done;
2485
2486 Error:
2487 if (z != NULL) {
2488 Py_DECREF(z);
2489 z = NULL;
2490 }
2491 /* fall through */
2492 Done:
Neil Schemenauerba872e22001-01-04 01:46:03 +00002493 Py_XDECREF(a);
Tim Peters47e52ee2004-08-30 02:44:38 +00002494 Py_XDECREF(b);
2495 Py_XDECREF(c);
2496 Py_XDECREF(temp);
2497 if (b->ob_size > FIVEARY_CUTOFF) {
2498 for (i = 0; i < 32; ++i)
2499 Py_XDECREF(table[i]);
2500 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002501 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002502}
2503
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002504static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002505long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002506{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002507 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002508 PyLongObject *x;
2509 PyLongObject *w;
2510 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002511 if (w == NULL)
2512 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002513 x = (PyLongObject *) long_add(v, w);
2514 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002515 if (x == NULL)
2516 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002517 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002518 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002519}
2520
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002521static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002522long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002523{
Tim Peters69c2de32001-09-11 22:31:33 +00002524 if (PyLong_CheckExact(v)) {
2525 Py_INCREF(v);
2526 return (PyObject *)v;
2527 }
2528 else
2529 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002530}
2531
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002532static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002533long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002534{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002535 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002536 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002537 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002538 Py_INCREF(v);
2539 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002540 }
Tim Peters69c2de32001-09-11 22:31:33 +00002541 z = (PyLongObject *)_PyLong_Copy(v);
2542 if (z != NULL)
2543 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002544 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002545}
2546
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002547static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002548long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002549{
2550 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002551 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002552 else
2553 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002554}
2555
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002556static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002557long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002558{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002559 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002560}
2561
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002562static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002563long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002564{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002565 PyLongObject *a, *b;
2566 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002567 long shiftby;
2568 int newsize, wordshift, loshift, hishift, i, j;
2569 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002570
Neil Schemenauerba872e22001-01-04 01:46:03 +00002571 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2572
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002573 if (a->ob_size < 0) {
2574 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002575 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002576 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002577 if (a1 == NULL)
2578 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002579 a2 = (PyLongObject *) long_rshift(a1, b);
2580 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002581 if (a2 == NULL)
2582 goto rshift_error;
2583 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002584 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002585 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002586 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002587
Neil Schemenauerba872e22001-01-04 01:46:03 +00002588 shiftby = PyLong_AsLong((PyObject *)b);
2589 if (shiftby == -1L && PyErr_Occurred())
2590 goto rshift_error;
2591 if (shiftby < 0) {
2592 PyErr_SetString(PyExc_ValueError,
2593 "negative shift count");
2594 goto rshift_error;
2595 }
2596 wordshift = shiftby / SHIFT;
2597 newsize = ABS(a->ob_size) - wordshift;
2598 if (newsize <= 0) {
2599 z = _PyLong_New(0);
2600 Py_DECREF(a);
2601 Py_DECREF(b);
2602 return (PyObject *)z;
2603 }
2604 loshift = shiftby % SHIFT;
2605 hishift = SHIFT - loshift;
2606 lomask = ((digit)1 << hishift) - 1;
2607 himask = MASK ^ lomask;
2608 z = _PyLong_New(newsize);
2609 if (z == NULL)
2610 goto rshift_error;
2611 if (a->ob_size < 0)
2612 z->ob_size = -(z->ob_size);
2613 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2614 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2615 if (i+1 < newsize)
2616 z->ob_digit[i] |=
2617 (a->ob_digit[j+1] << hishift) & himask;
2618 }
2619 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002620 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002621rshift_error:
2622 Py_DECREF(a);
2623 Py_DECREF(b);
2624 return (PyObject *) z;
2625
Guido van Rossumc6913e71991-11-19 20:26:46 +00002626}
2627
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002628static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002629long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002630{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002631 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002632 PyLongObject *a, *b;
2633 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002634 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002635 int oldsize, newsize, wordshift, remshift, i, j;
2636 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002637
Neil Schemenauerba872e22001-01-04 01:46:03 +00002638 CONVERT_BINOP(v, w, &a, &b);
2639
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002640 shiftby = PyLong_AsLong((PyObject *)b);
2641 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002642 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002643 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002644 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002645 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002646 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002647 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002648 PyErr_SetString(PyExc_ValueError,
2649 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002650 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002651 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002652 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2653 wordshift = (int)shiftby / SHIFT;
2654 remshift = (int)shiftby - wordshift * SHIFT;
2655
2656 oldsize = ABS(a->ob_size);
2657 newsize = oldsize + wordshift;
2658 if (remshift)
2659 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002660 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002661 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002662 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002663 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002664 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002665 for (i = 0; i < wordshift; i++)
2666 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002667 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002668 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002669 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002670 z->ob_digit[i] = (digit)(accum & MASK);
2671 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002672 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002673 if (remshift)
2674 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002675 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002676 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002677 z = long_normalize(z);
2678lshift_error:
2679 Py_DECREF(a);
2680 Py_DECREF(b);
2681 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002682}
2683
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002684
2685/* Bitwise and/xor/or operations */
2686
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002687static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002688long_bitwise(PyLongObject *a,
2689 int op, /* '&', '|', '^' */
2690 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002691{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002692 digit maska, maskb; /* 0 or MASK */
2693 int negz;
2694 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002695 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002696 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002697 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002698 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002699
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002700 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002701 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002702 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002703 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002704 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002705 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002706 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002707 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002708 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002709 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002710 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002711 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002712 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002713 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002714 maskb = 0;
2715 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002716
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002717 negz = 0;
2718 switch (op) {
2719 case '^':
2720 if (maska != maskb) {
2721 maska ^= MASK;
2722 negz = -1;
2723 }
2724 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002725 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002726 if (maska && maskb) {
2727 op = '|';
2728 maska ^= MASK;
2729 maskb ^= MASK;
2730 negz = -1;
2731 }
2732 break;
2733 case '|':
2734 if (maska || maskb) {
2735 op = '&';
2736 maska ^= MASK;
2737 maskb ^= MASK;
2738 negz = -1;
2739 }
2740 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002741 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002742
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002743 /* JRH: The original logic here was to allocate the result value (z)
2744 as the longer of the two operands. However, there are some cases
2745 where the result is guaranteed to be shorter than that: AND of two
2746 positives, OR of two negatives: use the shorter number. AND with
2747 mixed signs: use the positive number. OR with mixed signs: use the
2748 negative number. After the transformations above, op will be '&'
2749 iff one of these cases applies, and mask will be non-0 for operands
2750 whose length should be ignored.
2751 */
2752
2753 size_a = a->ob_size;
2754 size_b = b->ob_size;
2755 size_z = op == '&'
2756 ? (maska
2757 ? size_b
2758 : (maskb ? size_a : MIN(size_a, size_b)))
2759 : MAX(size_a, size_b);
2760 z = _PyLong_New(size_z);
2761 if (a == NULL || b == NULL || z == NULL) {
2762 Py_XDECREF(a);
2763 Py_XDECREF(b);
2764 Py_XDECREF(z);
2765 return NULL;
2766 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002767
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002768 for (i = 0; i < size_z; ++i) {
2769 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2770 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2771 switch (op) {
2772 case '&': z->ob_digit[i] = diga & digb; break;
2773 case '|': z->ob_digit[i] = diga | digb; break;
2774 case '^': z->ob_digit[i] = diga ^ digb; break;
2775 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002776 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002777
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002778 Py_DECREF(a);
2779 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002780 z = long_normalize(z);
2781 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002782 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002783 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002784 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002785 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002786}
2787
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002788static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002789long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002790{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002791 PyLongObject *a, *b;
2792 PyObject *c;
2793 CONVERT_BINOP(v, w, &a, &b);
2794 c = long_bitwise(a, '&', b);
2795 Py_DECREF(a);
2796 Py_DECREF(b);
2797 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002798}
2799
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002800static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002801long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002802{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002803 PyLongObject *a, *b;
2804 PyObject *c;
2805 CONVERT_BINOP(v, w, &a, &b);
2806 c = long_bitwise(a, '^', b);
2807 Py_DECREF(a);
2808 Py_DECREF(b);
2809 return c;
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_or(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 Rossum23d6f0e1991-05-14 12:06:49 +00002822}
2823
Guido van Rossum234f9421993-06-17 12:35:49 +00002824static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002825long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002826{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002827 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002828 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002829 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002830 return 0;
2831 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002832 else if (PyLong_Check(*pw)) {
2833 Py_INCREF(*pv);
2834 Py_INCREF(*pw);
2835 return 0;
2836 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002837 return 1; /* Can't do it */
2838}
2839
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002840static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002841long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002842{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002843 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002844 return v;
2845}
2846
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002847static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002848long_int(PyObject *v)
2849{
2850 long x;
2851 x = PyLong_AsLong(v);
2852 if (PyErr_Occurred()) {
2853 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2854 PyErr_Clear();
2855 if (PyLong_CheckExact(v)) {
2856 Py_INCREF(v);
2857 return v;
2858 }
2859 else
2860 return _PyLong_Copy((PyLongObject *)v);
2861 }
2862 else
2863 return NULL;
2864 }
2865 return PyInt_FromLong(x);
2866}
2867
2868static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002869long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002870{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002871 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002872 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002873 if (result == -1.0 && PyErr_Occurred())
2874 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002875 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002876}
2877
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002878static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002879long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002880{
Fred Drake121ee271999-12-23 15:41:28 +00002881 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002882}
2883
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002884static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002885long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002886{
Fred Drake121ee271999-12-23 15:41:28 +00002887 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002888}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002889
2890static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002891long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002892
Tim Peters6d6c1a32001-08-02 04:15:00 +00002893static PyObject *
2894long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2895{
2896 PyObject *x = NULL;
2897 int base = -909; /* unlikely! */
2898 static char *kwlist[] = {"x", "base", 0};
2899
Guido van Rossumbef14172001-08-29 15:47:46 +00002900 if (type != &PyLong_Type)
2901 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002902 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2903 &x, &base))
2904 return NULL;
2905 if (x == NULL)
2906 return PyLong_FromLong(0L);
2907 if (base == -909)
2908 return PyNumber_Long(x);
2909 else if (PyString_Check(x))
2910 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002911#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002912 else if (PyUnicode_Check(x))
2913 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2914 PyUnicode_GET_SIZE(x),
2915 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002916#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002917 else {
2918 PyErr_SetString(PyExc_TypeError,
2919 "long() can't convert non-string with explicit base");
2920 return NULL;
2921 }
2922}
2923
Guido van Rossumbef14172001-08-29 15:47:46 +00002924/* Wimpy, slow approach to tp_new calls for subtypes of long:
2925 first create a regular long from whatever arguments we got,
2926 then allocate a subtype instance and initialize it from
2927 the regular long. The regular long is then thrown away.
2928*/
2929static PyObject *
2930long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2931{
2932 PyLongObject *tmp, *new;
2933 int i, n;
2934
2935 assert(PyType_IsSubtype(type, &PyLong_Type));
2936 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2937 if (tmp == NULL)
2938 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002939 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002940 n = tmp->ob_size;
2941 if (n < 0)
2942 n = -n;
2943 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002944 if (new == NULL) {
2945 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002946 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002947 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002948 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002949 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002950 for (i = 0; i < n; i++)
2951 new->ob_digit[i] = tmp->ob_digit[i];
2952 Py_DECREF(tmp);
2953 return (PyObject *)new;
2954}
2955
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002956static PyObject *
2957long_getnewargs(PyLongObject *v)
2958{
2959 return Py_BuildValue("(N)", _PyLong_Copy(v));
2960}
2961
2962static PyMethodDef long_methods[] = {
2963 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2964 {NULL, NULL} /* sentinel */
2965};
2966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002967PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002968"long(x[, base]) -> integer\n\
2969\n\
2970Convert a string or number to a long integer, if possible. A floating\n\
2971point argument will be truncated towards zero (this does not include a\n\
2972string representation of a floating point number!) When converting a\n\
2973string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002975
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002976static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00002977 (binaryfunc) long_add, /*nb_add*/
2978 (binaryfunc) long_sub, /*nb_subtract*/
2979 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00002980 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002981 (binaryfunc) long_mod, /*nb_remainder*/
2982 (binaryfunc) long_divmod, /*nb_divmod*/
2983 (ternaryfunc) long_pow, /*nb_power*/
2984 (unaryfunc) long_neg, /*nb_negative*/
2985 (unaryfunc) long_pos, /*tp_positive*/
2986 (unaryfunc) long_abs, /*tp_absolute*/
2987 (inquiry) long_nonzero, /*tp_nonzero*/
2988 (unaryfunc) long_invert, /*nb_invert*/
2989 (binaryfunc) long_lshift, /*nb_lshift*/
2990 (binaryfunc) long_rshift, /*nb_rshift*/
2991 (binaryfunc) long_and, /*nb_and*/
2992 (binaryfunc) long_xor, /*nb_xor*/
2993 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00002994 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00002995 (unaryfunc) long_int, /*nb_int*/
2996 (unaryfunc) long_long, /*nb_long*/
2997 (unaryfunc) long_float, /*nb_float*/
2998 (unaryfunc) long_oct, /*nb_oct*/
2999 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003000 0, /* nb_inplace_add */
3001 0, /* nb_inplace_subtract */
3002 0, /* nb_inplace_multiply */
3003 0, /* nb_inplace_divide */
3004 0, /* nb_inplace_remainder */
3005 0, /* nb_inplace_power */
3006 0, /* nb_inplace_lshift */
3007 0, /* nb_inplace_rshift */
3008 0, /* nb_inplace_and */
3009 0, /* nb_inplace_xor */
3010 0, /* nb_inplace_or */
3011 (binaryfunc)long_div, /* nb_floor_divide */
3012 long_true_divide, /* nb_true_divide */
3013 0, /* nb_inplace_floor_divide */
3014 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003015};
3016
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003017PyTypeObject PyLong_Type = {
3018 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003019 0, /* ob_size */
3020 "long", /* tp_name */
3021 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3022 sizeof(digit), /* tp_itemsize */
3023 (destructor)long_dealloc, /* tp_dealloc */
3024 0, /* tp_print */
3025 0, /* tp_getattr */
3026 0, /* tp_setattr */
3027 (cmpfunc)long_compare, /* tp_compare */
3028 (reprfunc)long_repr, /* tp_repr */
3029 &long_as_number, /* tp_as_number */
3030 0, /* tp_as_sequence */
3031 0, /* tp_as_mapping */
3032 (hashfunc)long_hash, /* tp_hash */
3033 0, /* tp_call */
3034 (reprfunc)long_str, /* tp_str */
3035 PyObject_GenericGetAttr, /* tp_getattro */
3036 0, /* tp_setattro */
3037 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003038 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3039 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003040 long_doc, /* tp_doc */
3041 0, /* tp_traverse */
3042 0, /* tp_clear */
3043 0, /* tp_richcompare */
3044 0, /* tp_weaklistoffset */
3045 0, /* tp_iter */
3046 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003047 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003048 0, /* tp_members */
3049 0, /* tp_getset */
3050 0, /* tp_base */
3051 0, /* tp_dict */
3052 0, /* tp_descr_get */
3053 0, /* tp_descr_set */
3054 0, /* tp_dictoffset */
3055 0, /* tp_init */
3056 0, /* tp_alloc */
3057 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003058 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003059};