blob: 6f98d448dc96516dfadc8287337e2e5ca5feae10 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003/* Long (arbitrary precision) integer object implementation */
4
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00005/* XXX The functional organization of this file is terrible */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00008#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Tim Peters5af4e6c2002-08-12 02:31:19 +000012/* For long multiplication, use the O(N**2) school algorithm unless
13 * both operands contain more than KARATSUBA_CUTOFF digits (this
14 * being an internal Python long digit, in base BASE).
15 */
Tim Peters0973b992004-08-29 22:16:50 +000016#define KARATSUBA_CUTOFF 70
17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000018
Tim Peters47e52ee2004-08-30 02:44:38 +000019/* For exponentiation, use the binary left-to-right algorithm
20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
21 * In that case, do 5 bits at a time. The potential drawback is that
22 * a table of 2**5 intermediate results is computed.
23 */
24#define FIVEARY_CUTOFF 8
25
Guido van Rossume32e0141992-01-19 16:31:05 +000026#define ABS(x) ((x) < 0 ? -(x) : (x))
27
Tim Peters5af4e6c2002-08-12 02:31:19 +000028#undef MIN
29#undef MAX
30#define MAX(x, y) ((x) < (y) ? (y) : (x))
31#define MIN(x, y) ((x) > (y) ? (y) : (x))
32
Guido van Rossume32e0141992-01-19 16:31:05 +000033/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000034static PyLongObject *long_normalize(PyLongObject *);
35static PyLongObject *mul1(PyLongObject *, wdigit);
36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000037static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Tim Peters9f688bf2000-07-07 15:53:28 +000038static PyObject *long_format(PyObject *aa, int base, int addL);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 if (PyErr_CheckSignals()) { PyTryBlock; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000044 }
45
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046/* Normalize (remove leading zeros from) a long int object.
47 Doesn't attempt to free the storage--in most cases, due to the nature
48 of the algorithms used, this could save at most be one word anyway. */
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000051long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052{
Guido van Rossum4c260ff1992-01-14 18:36:43 +000053 int j = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000054 register int i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000055
Guido van Rossumedcc38a1991-05-05 20:09:44 +000056 while (i > 0 && v->ob_digit[i-1] == 0)
57 --i;
58 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000059 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060 return v;
61}
62
63/* Allocate a new long int object with size digits.
64 Return NULL and set exception if we run out of memory. */
65
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000067_PyLong_New(int size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000070}
71
Tim Peters64b5ce32001-09-10 20:52:51 +000072PyObject *
73_PyLong_Copy(PyLongObject *src)
74{
75 PyLongObject *result;
76 int i;
77
78 assert(src != NULL);
79 i = src->ob_size;
80 if (i < 0)
81 i = -(i);
82 result = _PyLong_New(i);
83 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000084 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000085 while (--i >= 0)
86 result->ob_digit[i] = src->ob_digit[i];
87 }
88 return (PyObject *)result;
89}
90
Guido van Rossumedcc38a1991-05-05 20:09:44 +000091/* Create a new long int object from a C long int */
92
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000094PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000095{
Tim Petersce9de2f2001-06-14 04:56:19 +000096 PyLongObject *v;
97 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
98 int ndigits = 0;
99 int negative = 0;
100
101 if (ival < 0) {
102 ival = -ival;
103 negative = 1;
104 }
105
106 /* Count the number of Python digits.
107 We used to pick 5 ("big enough for anything"), but that's a
108 waste of time and space given that 5*15 = 75 bits are rarely
109 needed. */
110 t = (unsigned long)ival;
111 while (t) {
112 ++ndigits;
113 t >>= SHIFT;
114 }
115 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000116 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000117 digit *p = v->ob_digit;
118 v->ob_size = negative ? -ndigits : ndigits;
119 t = (unsigned long)ival;
120 while (t) {
121 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000122 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000123 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000124 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000126}
127
Guido van Rossum53756b11997-01-03 17:14:46 +0000128/* Create a new long int object from a C unsigned long int */
129
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000131PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000132{
Tim Petersce9de2f2001-06-14 04:56:19 +0000133 PyLongObject *v;
134 unsigned long t;
135 int ndigits = 0;
136
137 /* Count the number of Python digits. */
138 t = (unsigned long)ival;
139 while (t) {
140 ++ndigits;
141 t >>= SHIFT;
142 }
143 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000144 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000145 digit *p = v->ob_digit;
146 v->ob_size = ndigits;
147 while (ival) {
148 *p++ = (digit)(ival & MASK);
149 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000150 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000151 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000153}
154
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000155/* Create a new long int object from a C double */
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000159{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000161 double frac;
162 int i, ndig, expo, neg;
163 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000164 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000165 PyErr_SetString(PyExc_OverflowError,
166 "cannot convert float infinity to long");
167 return NULL;
168 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000169 if (dval < 0.0) {
170 neg = 1;
171 dval = -dval;
172 }
173 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
174 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000176 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000178 if (v == NULL)
179 return NULL;
180 frac = ldexp(frac, (expo-1) % SHIFT + 1);
181 for (i = ndig; --i >= 0; ) {
182 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000183 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000184 frac = frac - (double)bits;
185 frac = ldexp(frac, SHIFT);
186 }
187 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000188 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000190}
191
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000192/* Get a C long int from a long int object.
193 Returns -1 and sets an error condition if overflow occurs. */
194
195long
Tim Peters9f688bf2000-07-07 15:53:28 +0000196PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000197{
Guido van Rossumf7531811998-05-26 14:33:37 +0000198 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000200 unsigned long x, prev;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000201 int i, sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000204 if (vv != NULL && PyInt_Check(vv))
205 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000207 return -1;
208 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000210 i = v->ob_size;
211 sign = 1;
212 x = 0;
213 if (i < 0) {
214 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000215 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000216 }
217 while (--i >= 0) {
218 prev = x;
219 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000220 if ((x >> SHIFT) != prev)
221 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000222 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000223 /* Haven't lost any bits, but if the sign bit is set we're in
224 * trouble *unless* this is the min negative number. So,
225 * trouble iff sign bit set && (positive || some bit set other
226 * than the sign bit).
227 */
228 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
229 goto overflow;
230 return (long)x * sign;
231
232 overflow:
233 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000234 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000235 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000236}
237
Guido van Rossumd8c80482002-08-13 00:24:58 +0000238/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000239 Returns -1 and sets an error condition if overflow occurs. */
240
241unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000242PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000243{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000245 unsigned long x, prev;
246 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000247
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000249 if (vv != NULL && PyInt_Check(vv)) {
250 long val = PyInt_AsLong(vv);
251 if (val < 0) {
252 PyErr_SetString(PyExc_OverflowError,
253 "can't convert negative value to unsigned long");
254 return (unsigned long) -1;
255 }
256 return val;
257 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000259 return (unsigned long) -1;
260 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000262 i = v->ob_size;
263 x = 0;
264 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000266 "can't convert negative value to unsigned long");
267 return (unsigned long) -1;
268 }
269 while (--i >= 0) {
270 prev = x;
271 x = (x << SHIFT) + v->ob_digit[i];
272 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000274 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000275 return (unsigned long) -1;
276 }
277 }
278 return x;
279}
280
Thomas Hellera4ea6032003-04-17 18:55:45 +0000281/* Get a C unsigned long int from a long int object, ignoring the high bits.
282 Returns -1 and sets an error condition if an error occurs. */
283
284unsigned long
285PyLong_AsUnsignedLongMask(PyObject *vv)
286{
287 register PyLongObject *v;
288 unsigned long x;
289 int i, sign;
290
291 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000292 if (vv != NULL && PyInt_Check(vv))
293 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000294 PyErr_BadInternalCall();
295 return (unsigned long) -1;
296 }
297 v = (PyLongObject *)vv;
298 i = v->ob_size;
299 sign = 1;
300 x = 0;
301 if (i < 0) {
302 sign = -1;
303 i = -i;
304 }
305 while (--i >= 0) {
306 x = (x << SHIFT) + v->ob_digit[i];
307 }
308 return x * sign;
309}
310
Tim Peters5b8132f2003-01-31 15:52:05 +0000311int
312_PyLong_Sign(PyObject *vv)
313{
314 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000315
316 assert(v != NULL);
317 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000318
Tim Petersee1a53c2003-02-02 02:57:53 +0000319 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000320}
321
Tim Petersbaefd9e2003-01-28 20:37:45 +0000322size_t
323_PyLong_NumBits(PyObject *vv)
324{
325 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000326 size_t result = 0;
Guido van Rossum004a65c2003-02-03 15:28:19 +0000327 int ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000328
329 assert(v != NULL);
330 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000331 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000332 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
333 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000334 digit msd = v->ob_digit[ndigits - 1];
335
Tim Peters5b8132f2003-01-31 15:52:05 +0000336 result = (ndigits - 1) * SHIFT;
Tim Peters08a1d9c2003-01-31 21:45:13 +0000337 if (result / SHIFT != (size_t)ndigits - 1)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000338 goto Overflow;
339 do {
340 ++result;
341 if (result == 0)
342 goto Overflow;
343 msd >>= 1;
344 } while (msd);
345 }
346 return result;
347
348Overflow:
349 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
350 "to express in a platform size_t");
351 return (size_t)-1;
352}
353
Tim Peters2a9b3672001-06-11 21:23:58 +0000354PyObject *
355_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
356 int little_endian, int is_signed)
357{
358 const unsigned char* pstartbyte;/* LSB of bytes */
359 int incr; /* direction to move pstartbyte */
360 const unsigned char* pendbyte; /* MSB of bytes */
361 size_t numsignificantbytes; /* number of bytes that matter */
362 size_t ndigits; /* number of Python long digits */
363 PyLongObject* v; /* result */
364 int idigit = 0; /* next free index in v->ob_digit */
365
366 if (n == 0)
367 return PyLong_FromLong(0L);
368
369 if (little_endian) {
370 pstartbyte = bytes;
371 pendbyte = bytes + n - 1;
372 incr = 1;
373 }
374 else {
375 pstartbyte = bytes + n - 1;
376 pendbyte = bytes;
377 incr = -1;
378 }
379
380 if (is_signed)
381 is_signed = *pendbyte >= 0x80;
382
383 /* Compute numsignificantbytes. This consists of finding the most
384 significant byte. Leading 0 bytes are insignficant if the number
385 is positive, and leading 0xff bytes if negative. */
386 {
387 size_t i;
388 const unsigned char* p = pendbyte;
389 const int pincr = -incr; /* search MSB to LSB */
390 const unsigned char insignficant = is_signed ? 0xff : 0x00;
391
392 for (i = 0; i < n; ++i, p += pincr) {
393 if (*p != insignficant)
394 break;
395 }
396 numsignificantbytes = n - i;
397 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
398 actually has 2 significant bytes. OTOH, 0xff0001 ==
399 -0x00ffff, so we wouldn't *need* to bump it there; but we
400 do for 0xffff = -0x0001. To be safe without bothering to
401 check every case, bump it regardless. */
402 if (is_signed && numsignificantbytes < n)
403 ++numsignificantbytes;
404 }
405
406 /* How many Python long digits do we need? We have
407 8*numsignificantbytes bits, and each Python long digit has SHIFT
408 bits, so it's the ceiling of the quotient. */
409 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
410 if (ndigits > (size_t)INT_MAX)
411 return PyErr_NoMemory();
412 v = _PyLong_New((int)ndigits);
413 if (v == NULL)
414 return NULL;
415
416 /* Copy the bits over. The tricky parts are computing 2's-comp on
417 the fly for signed numbers, and dealing with the mismatch between
418 8-bit bytes and (probably) 15-bit Python digits.*/
419 {
420 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000421 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000422 twodigits accum = 0; /* sliding register */
423 unsigned int accumbits = 0; /* number of bits in accum */
424 const unsigned char* p = pstartbyte;
425
426 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000427 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000428 /* Compute correction for 2's comp, if needed. */
429 if (is_signed) {
430 thisbyte = (0xff ^ thisbyte) + carry;
431 carry = thisbyte >> 8;
432 thisbyte &= 0xff;
433 }
434 /* Because we're going LSB to MSB, thisbyte is
435 more significant than what's already in accum,
436 so needs to be prepended to accum. */
437 accum |= thisbyte << accumbits;
438 accumbits += 8;
439 if (accumbits >= SHIFT) {
440 /* There's enough to fill a Python digit. */
441 assert(idigit < (int)ndigits);
442 v->ob_digit[idigit] = (digit)(accum & MASK);
443 ++idigit;
444 accum >>= SHIFT;
445 accumbits -= SHIFT;
446 assert(accumbits < SHIFT);
447 }
448 }
449 assert(accumbits < SHIFT);
450 if (accumbits) {
451 assert(idigit < (int)ndigits);
452 v->ob_digit[idigit] = (digit)accum;
453 ++idigit;
454 }
455 }
456
457 v->ob_size = is_signed ? -idigit : idigit;
458 return (PyObject *)long_normalize(v);
459}
460
461int
462_PyLong_AsByteArray(PyLongObject* v,
463 unsigned char* bytes, size_t n,
464 int little_endian, int is_signed)
465{
466 int i; /* index into v->ob_digit */
467 int ndigits; /* |v->ob_size| */
468 twodigits accum; /* sliding register */
469 unsigned int accumbits; /* # bits in accum */
470 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
471 twodigits carry; /* for computing 2's-comp */
472 size_t j; /* # bytes filled */
473 unsigned char* p; /* pointer to next byte in bytes */
474 int pincr; /* direction to move p */
475
476 assert(v != NULL && PyLong_Check(v));
477
478 if (v->ob_size < 0) {
479 ndigits = -(v->ob_size);
480 if (!is_signed) {
481 PyErr_SetString(PyExc_TypeError,
482 "can't convert negative long to unsigned");
483 return -1;
484 }
485 do_twos_comp = 1;
486 }
487 else {
488 ndigits = v->ob_size;
489 do_twos_comp = 0;
490 }
491
492 if (little_endian) {
493 p = bytes;
494 pincr = 1;
495 }
496 else {
497 p = bytes + n - 1;
498 pincr = -1;
499 }
500
Tim Peters898cf852001-06-13 20:50:08 +0000501 /* Copy over all the Python digits.
502 It's crucial that every Python digit except for the MSD contribute
503 exactly SHIFT bits to the total, so first assert that the long is
504 normalized. */
505 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000506 j = 0;
507 accum = 0;
508 accumbits = 0;
509 carry = do_twos_comp ? 1 : 0;
510 for (i = 0; i < ndigits; ++i) {
511 twodigits thisdigit = v->ob_digit[i];
512 if (do_twos_comp) {
513 thisdigit = (thisdigit ^ MASK) + carry;
514 carry = thisdigit >> SHIFT;
515 thisdigit &= MASK;
516 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000517 /* Because we're going LSB to MSB, thisdigit is more
518 significant than what's already in accum, so needs to be
519 prepended to accum. */
520 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000521 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000522
Tim Petersede05092001-06-14 08:53:38 +0000523 /* The most-significant digit may be (probably is) at least
524 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000525 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000526 /* Count # of sign bits -- they needn't be stored,
527 * although for signed conversion we need later to
528 * make sure at least one sign bit gets stored.
529 * First shift conceptual sign bit to real sign bit.
530 */
531 stwodigits s = (stwodigits)(thisdigit <<
532 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000533 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000534 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000535 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000536 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000537 }
Tim Petersede05092001-06-14 08:53:38 +0000538 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000539 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000540
Tim Peters2a9b3672001-06-11 21:23:58 +0000541 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000542 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000543 if (j >= n)
544 goto Overflow;
545 ++j;
546 *p = (unsigned char)(accum & 0xff);
547 p += pincr;
548 accumbits -= 8;
549 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000550 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000551 }
552
553 /* Store the straggler (if any). */
554 assert(accumbits < 8);
555 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000556 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000557 if (j >= n)
558 goto Overflow;
559 ++j;
560 if (do_twos_comp) {
561 /* Fill leading bits of the byte with sign bits
562 (appropriately pretending that the long had an
563 infinite supply of sign bits). */
564 accum |= (~(twodigits)0) << accumbits;
565 }
566 *p = (unsigned char)(accum & 0xff);
567 p += pincr;
568 }
Tim Peters05607ad2001-06-13 21:01:27 +0000569 else if (j == n && n > 0 && is_signed) {
570 /* The main loop filled the byte array exactly, so the code
571 just above didn't get to ensure there's a sign bit, and the
572 loop below wouldn't add one either. Make sure a sign bit
573 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000574 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000575 int sign_bit_set = msb >= 0x80;
576 assert(accumbits == 0);
577 if (sign_bit_set == do_twos_comp)
578 return 0;
579 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000580 goto Overflow;
581 }
Tim Peters05607ad2001-06-13 21:01:27 +0000582
583 /* Fill remaining bytes with copies of the sign bit. */
584 {
585 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
586 for ( ; j < n; ++j, p += pincr)
587 *p = signbyte;
588 }
589
Tim Peters2a9b3672001-06-11 21:23:58 +0000590 return 0;
591
592Overflow:
593 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
594 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000595
Tim Peters2a9b3672001-06-11 21:23:58 +0000596}
597
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000598double
599_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
600{
601/* NBITS_WANTED should be > the number of bits in a double's precision,
602 but small enough so that 2**NBITS_WANTED is within the normal double
603 range. nbitsneeded is set to 1 less than that because the most-significant
604 Python digit contains at least 1 significant bit, but we don't want to
605 bother counting them (catering to the worst case cheaply).
606
607 57 is one more than VAX-D double precision; I (Tim) don't know of a double
608 format with more precision than that; it's 1 larger so that we add in at
609 least one round bit to stand in for the ignored least-significant bits.
610*/
611#define NBITS_WANTED 57
612 PyLongObject *v;
613 double x;
614 const double multiplier = (double)(1L << SHIFT);
615 int i, sign;
616 int nbitsneeded;
617
618 if (vv == NULL || !PyLong_Check(vv)) {
619 PyErr_BadInternalCall();
620 return -1;
621 }
622 v = (PyLongObject *)vv;
623 i = v->ob_size;
624 sign = 1;
625 if (i < 0) {
626 sign = -1;
627 i = -(i);
628 }
629 else if (i == 0) {
630 *exponent = 0;
631 return 0.0;
632 }
633 --i;
634 x = (double)v->ob_digit[i];
635 nbitsneeded = NBITS_WANTED - 1;
636 /* Invariant: i Python digits remain unaccounted for. */
637 while (i > 0 && nbitsneeded > 0) {
638 --i;
639 x = x * multiplier + (double)v->ob_digit[i];
640 nbitsneeded -= SHIFT;
641 }
642 /* There are i digits we didn't shift in. Pretending they're all
643 zeroes, the true value is x * 2**(i*SHIFT). */
644 *exponent = i;
645 assert(x > 0.0);
646 return x * sign;
647#undef NBITS_WANTED
648}
649
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000650/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000651
652double
Tim Peters9f688bf2000-07-07 15:53:28 +0000653PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000654{
Tim Peters9fffa3e2001-09-04 05:14:19 +0000655 int e;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000656 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000657
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 if (vv == NULL || !PyLong_Check(vv)) {
659 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000660 return -1;
661 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000662 x = _PyLong_AsScaledDouble(vv, &e);
663 if (x == -1.0 && PyErr_Occurred())
664 return -1.0;
665 if (e > INT_MAX / SHIFT)
666 goto overflow;
667 errno = 0;
668 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000669 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000670 goto overflow;
671 return x;
672
673overflow:
674 PyErr_SetString(PyExc_OverflowError,
675 "long int too large to convert to float");
676 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000677}
678
Guido van Rossum78694d91998-09-18 14:14:13 +0000679/* Create a new long (or int) object from a C pointer */
680
681PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000682PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000683{
Tim Peters70128a12001-06-16 08:48:40 +0000684#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000685 return PyInt_FromLong((long)p);
686#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000687
Tim Peters70128a12001-06-16 08:48:40 +0000688#ifndef HAVE_LONG_LONG
689# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
690#endif
691#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000692# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000693#endif
694 /* optimize null pointers */
695 if (p == NULL)
696 return PyInt_FromLong(0);
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000697 return PyLong_FromLongLong((PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000698
699#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000700}
701
702/* Get a C pointer from a long object (or an int object in some cases) */
703
704void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000705PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000706{
707 /* This function will allow int or long objects. If vv is neither,
708 then the PyLong_AsLong*() functions will raise the exception:
709 PyExc_SystemError, "bad argument to internal function"
710 */
Tim Peters70128a12001-06-16 08:48:40 +0000711#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000712 long x;
713
Tim Peters70128a12001-06-16 08:48:40 +0000714 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000715 x = PyInt_AS_LONG(vv);
716 else
717 x = PyLong_AsLong(vv);
718#else
Tim Peters70128a12001-06-16 08:48:40 +0000719
720#ifndef HAVE_LONG_LONG
721# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
722#endif
723#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000724# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000725#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000726 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000727
Tim Peters70128a12001-06-16 08:48:40 +0000728 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000729 x = PyInt_AS_LONG(vv);
730 else
731 x = PyLong_AsLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000732
733#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000734
735 if (x == -1 && PyErr_Occurred())
736 return NULL;
737 return (void *)x;
738}
739
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000740#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000741
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000742/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000743 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000744 */
745
Tim Peterscf37dfc2001-06-14 18:42:50 +0000746#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000747
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000748/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000749
750PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000751PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000752{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000753 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000754 int one = 1;
755 return _PyLong_FromByteArray(
756 (unsigned char *)&bytes,
757 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000758}
759
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000760/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000761
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000762PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000763PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000764{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000765 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000766 int one = 1;
767 return _PyLong_FromByteArray(
768 (unsigned char *)&bytes,
769 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000770}
771
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000772/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000773 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000774
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000775PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000776PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000777{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000778 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000779 int one = 1;
780 int res;
781
Tim Petersd38b1c72001-09-30 05:09:37 +0000782 if (vv == NULL) {
783 PyErr_BadInternalCall();
784 return -1;
785 }
786 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000787 PyNumberMethods *nb;
788 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000789 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000790 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000791 if ((nb = vv->ob_type->tp_as_number) == NULL ||
792 nb->nb_int == NULL) {
793 PyErr_SetString(PyExc_TypeError, "an integer is required");
794 return -1;
795 }
796 io = (*nb->nb_int) (vv);
797 if (io == NULL)
798 return -1;
799 if (PyInt_Check(io)) {
800 bytes = PyInt_AsLong(io);
801 Py_DECREF(io);
802 return bytes;
803 }
804 if (PyLong_Check(io)) {
805 bytes = PyLong_AsLongLong(io);
806 Py_DECREF(io);
807 return bytes;
808 }
809 Py_DECREF(io);
810 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000811 return -1;
812 }
813
Tim Petersd1a7da62001-06-13 00:35:57 +0000814 res = _PyLong_AsByteArray(
815 (PyLongObject *)vv, (unsigned char *)&bytes,
816 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000817
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000818 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000819 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000820 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000821 else
822 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000823}
824
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000825/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000826 Return -1 and set an error if overflow occurs. */
827
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000829PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000830{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000831 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000832 int one = 1;
833 int res;
834
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000835 if (vv == NULL || !PyLong_Check(vv)) {
836 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000837 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000838 }
839
Tim Petersd1a7da62001-06-13 00:35:57 +0000840 res = _PyLong_AsByteArray(
841 (PyLongObject *)vv, (unsigned char *)&bytes,
842 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000843
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000844 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000845 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000846 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000847 else
848 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000849}
Tim Petersd1a7da62001-06-13 00:35:57 +0000850
Thomas Hellera4ea6032003-04-17 18:55:45 +0000851/* Get a C unsigned long int from a long int object, ignoring the high bits.
852 Returns -1 and sets an error condition if an error occurs. */
853
854unsigned PY_LONG_LONG
855PyLong_AsUnsignedLongLongMask(PyObject *vv)
856{
857 register PyLongObject *v;
858 unsigned PY_LONG_LONG x;
859 int i, sign;
860
861 if (vv == NULL || !PyLong_Check(vv)) {
862 PyErr_BadInternalCall();
863 return (unsigned long) -1;
864 }
865 v = (PyLongObject *)vv;
866 i = v->ob_size;
867 sign = 1;
868 x = 0;
869 if (i < 0) {
870 sign = -1;
871 i = -i;
872 }
873 while (--i >= 0) {
874 x = (x << SHIFT) + v->ob_digit[i];
875 }
876 return x * sign;
877}
Tim Petersd1a7da62001-06-13 00:35:57 +0000878#undef IS_LITTLE_ENDIAN
879
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000880#endif /* HAVE_LONG_LONG */
881
Neil Schemenauerba872e22001-01-04 01:46:03 +0000882
883static int
884convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000885 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000886 *a = (PyLongObject *) v;
887 Py_INCREF(v);
888 }
889 else if (PyInt_Check(v)) {
890 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
891 }
892 else {
893 return 0;
894 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000895 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000896 *b = (PyLongObject *) w;
897 Py_INCREF(w);
898 }
899 else if (PyInt_Check(w)) {
900 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
901 }
902 else {
903 Py_DECREF(*a);
904 return 0;
905 }
906 return 1;
907}
908
909#define CONVERT_BINOP(v, w, a, b) \
910 if (!convert_binop(v, w, a, b)) { \
911 Py_INCREF(Py_NotImplemented); \
912 return Py_NotImplemented; \
913 }
914
Tim Peters877a2122002-08-12 05:09:36 +0000915/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
916 * is modified in place, by adding y to it. Carries are propagated as far as
917 * x[m-1], and the remaining carry (0 or 1) is returned.
918 */
919static digit
920v_iadd(digit *x, int m, digit *y, int n)
921{
922 int i;
923 digit carry = 0;
924
925 assert(m >= n);
926 for (i = 0; i < n; ++i) {
927 carry += x[i] + y[i];
928 x[i] = carry & MASK;
929 carry >>= SHIFT;
930 assert((carry & 1) == carry);
931 }
932 for (; carry && i < m; ++i) {
933 carry += x[i];
934 x[i] = carry & MASK;
935 carry >>= SHIFT;
936 assert((carry & 1) == carry);
937 }
938 return carry;
939}
940
941/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
942 * is modified in place, by subtracting y from it. Borrows are propagated as
943 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
944 */
945static digit
946v_isub(digit *x, int m, digit *y, int n)
947{
948 int i;
949 digit borrow = 0;
950
951 assert(m >= n);
952 for (i = 0; i < n; ++i) {
953 borrow = x[i] - y[i] - borrow;
954 x[i] = borrow & MASK;
955 borrow >>= SHIFT;
956 borrow &= 1; /* keep only 1 sign bit */
957 }
958 for (; borrow && i < m; ++i) {
959 borrow = x[i] - borrow;
960 x[i] = borrow & MASK;
961 borrow >>= SHIFT;
962 borrow &= 1;
963 }
964 return borrow;
965}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000966
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000967/* Multiply by a single digit, ignoring the sign. */
968
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000970mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000971{
972 return muladd1(a, n, (digit)0);
973}
974
975/* Multiply by a single digit and add a single digit, ignoring the sign. */
976
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000978muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000979{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000980 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000981 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000982 twodigits carry = extra;
983 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000984
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000985 if (z == NULL)
986 return NULL;
987 for (i = 0; i < size_a; ++i) {
988 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000989 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000990 carry >>= SHIFT;
991 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000992 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000993 return long_normalize(z);
994}
995
Tim Peters212e6142001-07-14 12:23:19 +0000996/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
997 in pout, and returning the remainder. pin and pout point at the LSD.
998 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
999 long_format, but that should be done with great care since longs are
1000 immutable. */
1001
1002static digit
1003inplace_divrem1(digit *pout, digit *pin, int size, digit n)
1004{
1005 twodigits rem = 0;
1006
1007 assert(n > 0 && n <= MASK);
1008 pin += size;
1009 pout += size;
1010 while (--size >= 0) {
1011 digit hi;
1012 rem = (rem << SHIFT) + *--pin;
1013 *--pout = hi = (digit)(rem / n);
1014 rem -= hi * n;
1015 }
1016 return (digit)rem;
1017}
1018
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001019/* Divide a long integer by a digit, returning both the quotient
1020 (as function result) and the remainder (through *prem).
1021 The sign of a is ignored; n should not be zero. */
1022
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001024divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001025{
Tim Peters212e6142001-07-14 12:23:19 +00001026 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001028
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001029 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001031 if (z == NULL)
1032 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001033 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001034 return long_normalize(z);
1035}
1036
1037/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001038 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001039 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001040
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001042long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001043{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 register PyLongObject *a = (PyLongObject *)aa;
1045 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001046 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001047 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001048 char *p;
1049 int bits;
1050 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001051
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001052 if (a == NULL || !PyLong_Check(a)) {
1053 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001054 return NULL;
1055 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001056 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001057
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001058 /* Compute a rough upper bound for the length of the string */
1059 i = base;
1060 bits = 0;
1061 while (i > 1) {
1062 ++bits;
1063 i >>= 1;
1064 }
Fred Drake121ee271999-12-23 15:41:28 +00001065 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001066 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001067 if (str == NULL)
1068 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001069 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001070 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001071 if (addL)
1072 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001073 if (a->ob_size < 0)
1074 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001075
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001076 if (a->ob_size == 0) {
1077 *--p = '0';
1078 }
1079 else if ((base & (base - 1)) == 0) {
1080 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001081 twodigits accum = 0;
1082 int accumbits = 0; /* # of bits in accum */
1083 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001084 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001085 while ((i >>= 1) > 1)
1086 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001087
1088 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001089 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001090 accumbits += SHIFT;
1091 assert(accumbits >= basebits);
1092 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001093 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001094 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001095 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001096 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001097 accumbits -= basebits;
1098 accum >>= basebits;
1099 } while (i < size_a-1 ? accumbits >= basebits :
1100 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001101 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001102 }
1103 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001104 /* Not 0, and base not a power of 2. Divide repeatedly by
1105 base, but for speed use the highest power of base that
1106 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001107 int size = size_a;
1108 digit *pin = a->ob_digit;
1109 PyLongObject *scratch;
1110 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001111 digit powbase = base; /* powbase == base ** power */
1112 int power = 1;
1113 for (;;) {
1114 unsigned long newpow = powbase * (unsigned long)base;
1115 if (newpow >> SHIFT) /* doesn't fit in a digit */
1116 break;
1117 powbase = (digit)newpow;
1118 ++power;
1119 }
Tim Peters212e6142001-07-14 12:23:19 +00001120
1121 /* Get a scratch area for repeated division. */
1122 scratch = _PyLong_New(size);
1123 if (scratch == NULL) {
1124 Py_DECREF(str);
1125 return NULL;
1126 }
1127
1128 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001129 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001130 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001131 digit rem = inplace_divrem1(scratch->ob_digit,
1132 pin, size, powbase);
1133 pin = scratch->ob_digit; /* no need to use a again */
1134 if (pin[size - 1] == 0)
1135 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001136 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001137 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001138 Py_DECREF(str);
1139 return NULL;
1140 })
Tim Peters212e6142001-07-14 12:23:19 +00001141
1142 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001143 assert(ntostore > 0);
1144 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001145 digit nextrem = (digit)(rem / base);
1146 char c = (char)(rem - nextrem * base);
1147 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001148 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001149 *--p = c;
1150 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001151 --ntostore;
1152 /* Termination is a bit delicate: must not
1153 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001154 remaining quotient and rem are both 0. */
1155 } while (ntostore && (size || rem));
1156 } while (size != 0);
1157 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001158 }
1159
Guido van Rossum2c475421992-08-14 15:13:07 +00001160 if (base == 8) {
1161 if (size_a != 0)
1162 *--p = '0';
1163 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001164 else if (base == 16) {
1165 *--p = 'x';
1166 *--p = '0';
1167 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001168 else if (base != 10) {
1169 *--p = '#';
1170 *--p = '0' + base%10;
1171 if (base > 10)
1172 *--p = '0' + base/10;
1173 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001174 if (sign)
1175 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 if (p != PyString_AS_STRING(str)) {
1177 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178 assert(p > q);
1179 do {
1180 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001181 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 _PyString_Resize((PyObject **)&str,
1183 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001184 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001186}
1187
Tim Petersbf2674b2003-02-02 07:51:32 +00001188/* *str points to the first digit in a string of base base digits. base
1189 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1190 * non-digit (which may be *str!). A normalized long is returned.
1191 * The point to this routine is that it takes time linear in the number of
1192 * string characters.
1193 */
1194static PyLongObject *
1195long_from_binary_base(char **str, int base)
1196{
1197 char *p = *str;
1198 char *start = p;
1199 int bits_per_char;
1200 int n;
1201 PyLongObject *z;
1202 twodigits accum;
1203 int bits_in_accum;
1204 digit *pdigit;
1205
1206 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1207 n = base;
1208 for (bits_per_char = -1; n; ++bits_per_char)
1209 n >>= 1;
1210 /* n <- total # of bits needed, while setting p to end-of-string */
1211 n = 0;
1212 for (;;) {
1213 int k = -1;
1214 char ch = *p;
1215
1216 if (ch <= '9')
1217 k = ch - '0';
1218 else if (ch >= 'a')
1219 k = ch - 'a' + 10;
1220 else if (ch >= 'A')
1221 k = ch - 'A' + 10;
1222 if (k < 0 || k >= base)
1223 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001224 ++p;
1225 }
1226 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001227 n = (p - start) * bits_per_char;
1228 if (n / bits_per_char != p - start) {
1229 PyErr_SetString(PyExc_ValueError,
1230 "long string too large to convert");
1231 return NULL;
1232 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001233 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1234 n = (n + SHIFT - 1) / SHIFT;
1235 z = _PyLong_New(n);
1236 if (z == NULL)
1237 return NULL;
1238 /* Read string from right, and fill in long from left; i.e.,
1239 * from least to most significant in both.
1240 */
1241 accum = 0;
1242 bits_in_accum = 0;
1243 pdigit = z->ob_digit;
1244 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001245 int k;
1246 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001247
1248 if (ch <= '9')
1249 k = ch - '0';
1250 else if (ch >= 'a')
1251 k = ch - 'a' + 10;
1252 else {
1253 assert(ch >= 'A');
1254 k = ch - 'A' + 10;
1255 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001256 assert(k >= 0 && k < base);
1257 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001258 bits_in_accum += bits_per_char;
1259 if (bits_in_accum >= SHIFT) {
1260 *pdigit++ = (digit)(accum & MASK);
1261 assert(pdigit - z->ob_digit <= n);
1262 accum >>= SHIFT;
1263 bits_in_accum -= SHIFT;
1264 assert(bits_in_accum < SHIFT);
1265 }
1266 }
1267 if (bits_in_accum) {
1268 assert(bits_in_accum <= SHIFT);
1269 *pdigit++ = (digit)accum;
1270 assert(pdigit - z->ob_digit <= n);
1271 }
1272 while (pdigit - z->ob_digit < n)
1273 *pdigit++ = 0;
1274 return long_normalize(z);
1275}
1276
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001277PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001278PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001279{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001280 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001281 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001283
Guido van Rossum472c04f1996-12-05 21:57:21 +00001284 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001286 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001287 return NULL;
1288 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001289 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001290 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001291 if (*str == '+')
1292 ++str;
1293 else if (*str == '-') {
1294 ++str;
1295 sign = -1;
1296 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001297 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001298 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001299 if (base == 0) {
1300 if (str[0] != '0')
1301 base = 10;
1302 else if (str[1] == 'x' || str[1] == 'X')
1303 base = 16;
1304 else
1305 base = 8;
1306 }
1307 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1308 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001309 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001310 if ((base & (base - 1)) == 0)
1311 z = long_from_binary_base(&str, base);
1312 else {
1313 z = _PyLong_New(0);
1314 for ( ; z != NULL; ++str) {
1315 int k = -1;
1316 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001317
Tim Petersbf2674b2003-02-02 07:51:32 +00001318 if (*str <= '9')
1319 k = *str - '0';
1320 else if (*str >= 'a')
1321 k = *str - 'a' + 10;
1322 else if (*str >= 'A')
1323 k = *str - 'A' + 10;
1324 if (k < 0 || k >= base)
1325 break;
1326 temp = muladd1(z, (digit)base, (digit)k);
1327 Py_DECREF(z);
1328 z = temp;
1329 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001330 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001331 if (z == NULL)
1332 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001333 if (str == start)
1334 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001335 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001336 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001337 if (*str == 'L' || *str == 'l')
1338 str++;
1339 while (*str && isspace(Py_CHARMASK(*str)))
1340 str++;
1341 if (*str != '\0')
1342 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001343 if (pend)
1344 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001346
1347 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001348 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001349 "invalid literal for long(): %.200s", orig_str);
1350 Py_XDECREF(z);
1351 return NULL;
1352}
1353
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001354#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001355PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001356PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001357{
Walter Dörwald07e14762002-11-06 16:15:14 +00001358 PyObject *result;
1359 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001360
Walter Dörwald07e14762002-11-06 16:15:14 +00001361 if (buffer == NULL)
1362 return NULL;
1363
1364 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1365 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001366 return NULL;
1367 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001368 result = PyLong_FromString(buffer, NULL, base);
1369 PyMem_FREE(buffer);
1370 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001371}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001372#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001373
Tim Peters9f688bf2000-07-07 15:53:28 +00001374/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001376 (PyLongObject *, PyLongObject *, PyLongObject **);
1377static PyObject *long_pos(PyLongObject *);
1378static int long_divrem(PyLongObject *, PyLongObject *,
1379 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001380
1381/* Long division with remainder, top-level routine */
1382
Guido van Rossume32e0141992-01-19 16:31:05 +00001383static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001384long_divrem(PyLongObject *a, PyLongObject *b,
1385 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001386{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001387 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001389
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001390 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001391 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001392 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001393 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001394 }
1395 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001396 (size_a == size_b &&
1397 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001398 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399 *pdiv = _PyLong_New(0);
1400 Py_INCREF(a);
1401 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001402 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001403 }
1404 if (size_b == 1) {
1405 digit rem = 0;
1406 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001407 if (z == NULL)
1408 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001410 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001411 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001412 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001413 if (z == NULL)
1414 return -1;
1415 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001416 /* Set the signs.
1417 The quotient z has the sign of a*b;
1418 the remainder r has the sign of a,
1419 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001420 if ((a->ob_size < 0) != (b->ob_size < 0))
1421 z->ob_size = -(z->ob_size);
1422 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1423 (*prem)->ob_size = -((*prem)->ob_size);
1424 *pdiv = z;
1425 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001426}
1427
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001428/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001429
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001430static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001431x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001432{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001433 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001434 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001435 PyLongObject *v = mul1(v1, d);
1436 PyLongObject *w = mul1(w1, d);
1437 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001438 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001439
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001440 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001441 Py_XDECREF(v);
1442 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001443 return NULL;
1444 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001445
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001446 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001447 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001448 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001449
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001450 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001451 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001452
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001453 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1454 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1455 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001456 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001457 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001458
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001459 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001461 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001462 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001463 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001464 if (vj == w->ob_digit[size_w-1])
1465 q = MASK;
1466 else
1467 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1468 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001469
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001470 while (w->ob_digit[size_w-2]*q >
1471 ((
1472 ((twodigits)vj << SHIFT)
1473 + v->ob_digit[j-1]
1474 - q*w->ob_digit[size_w-1]
1475 ) << SHIFT)
1476 + v->ob_digit[j-2])
1477 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001478
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001479 for (i = 0; i < size_w && i+k < size_v; ++i) {
1480 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001481 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 carry += v->ob_digit[i+k] - z
1483 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001484 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001485 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1486 carry, SHIFT);
1487 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001488 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001489
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001490 if (i+k < size_v) {
1491 carry += v->ob_digit[i+k];
1492 v->ob_digit[i+k] = 0;
1493 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001494
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001495 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001496 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001497 else {
1498 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001499 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001500 carry = 0;
1501 for (i = 0; i < size_w && i+k < size_v; ++i) {
1502 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001503 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001504 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1505 BASE_TWODIGITS_TYPE,
1506 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001507 }
1508 }
1509 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001510
Guido van Rossumc206c761995-01-10 15:23:19 +00001511 if (a == NULL)
1512 *prem = NULL;
1513 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001514 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001515 *prem = divrem1(v, d, &d);
1516 /* d receives the (unused) remainder */
1517 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001518 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001519 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001520 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001521 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001522 Py_DECREF(v);
1523 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001524 return a;
1525}
1526
1527/* Methods */
1528
1529static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001530long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001531{
Guido van Rossum9475a232001-10-05 20:51:39 +00001532 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001533}
1534
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001535static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001536long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001537{
Fred Drake121ee271999-12-23 15:41:28 +00001538 return long_format(v, 10, 1);
1539}
1540
1541static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001542long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001543{
1544 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001545}
1546
1547static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001548long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001549{
1550 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001551
Guido van Rossumc6913e71991-11-19 20:26:46 +00001552 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001553 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001554 sign = 0;
1555 else
1556 sign = a->ob_size - b->ob_size;
1557 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001558 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001559 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001560 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1561 ;
1562 if (i < 0)
1563 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001564 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001565 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001566 if (a->ob_size < 0)
1567 sign = -sign;
1568 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001569 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001570 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001571}
1572
Guido van Rossum9bfef441993-03-29 10:43:31 +00001573static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001574long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001575{
1576 long x;
1577 int i, sign;
1578
1579 /* This is designed so that Python ints and longs with the
1580 same value hash to the same value, otherwise comparisons
1581 of mapping keys will turn out weird */
1582 i = v->ob_size;
1583 sign = 1;
1584 x = 0;
1585 if (i < 0) {
1586 sign = -1;
1587 i = -(i);
1588 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001589#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001590 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001591 /* Force a native long #-bits (32 or 64) circular shift */
1592 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001593 x += v->ob_digit[i];
1594 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001595#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001596 x = x * sign;
1597 if (x == -1)
1598 x = -2;
1599 return x;
1600}
1601
1602
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001603/* Add the absolute values of two long integers. */
1604
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001605static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001606x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001607{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001608 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001610 int i;
1611 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001612
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001613 /* Ensure a is the larger of the two: */
1614 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001615 { PyLongObject *temp = a; a = b; b = temp; }
1616 { int size_temp = size_a;
1617 size_a = size_b;
1618 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001620 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001621 if (z == NULL)
1622 return NULL;
1623 for (i = 0; i < size_b; ++i) {
1624 carry += a->ob_digit[i] + b->ob_digit[i];
1625 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001626 carry >>= SHIFT;
1627 }
1628 for (; i < size_a; ++i) {
1629 carry += a->ob_digit[i];
1630 z->ob_digit[i] = carry & MASK;
1631 carry >>= SHIFT;
1632 }
1633 z->ob_digit[i] = carry;
1634 return long_normalize(z);
1635}
1636
1637/* Subtract the absolute values of two integers. */
1638
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001639static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001640x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001641{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001642 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001644 int i;
1645 int sign = 1;
1646 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001647
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001648 /* Ensure a is the larger of the two: */
1649 if (size_a < size_b) {
1650 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001651 { PyLongObject *temp = a; a = b; b = temp; }
1652 { int size_temp = size_a;
1653 size_a = size_b;
1654 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001655 }
1656 else if (size_a == size_b) {
1657 /* Find highest digit where a and b differ: */
1658 i = size_a;
1659 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1660 ;
1661 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001662 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001663 if (a->ob_digit[i] < b->ob_digit[i]) {
1664 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001665 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001666 }
1667 size_a = size_b = i+1;
1668 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001669 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001670 if (z == NULL)
1671 return NULL;
1672 for (i = 0; i < size_b; ++i) {
1673 /* The following assumes unsigned arithmetic
1674 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001675 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001676 z->ob_digit[i] = borrow & MASK;
1677 borrow >>= SHIFT;
1678 borrow &= 1; /* Keep only one sign bit */
1679 }
1680 for (; i < size_a; ++i) {
1681 borrow = a->ob_digit[i] - borrow;
1682 z->ob_digit[i] = borrow & MASK;
1683 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001684 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001685 }
1686 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001687 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001688 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001689 return long_normalize(z);
1690}
1691
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001692static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001693long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001694{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001695 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001696
Neil Schemenauerba872e22001-01-04 01:46:03 +00001697 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1698
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001699 if (a->ob_size < 0) {
1700 if (b->ob_size < 0) {
1701 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001702 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001703 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001704 }
1705 else
1706 z = x_sub(b, a);
1707 }
1708 else {
1709 if (b->ob_size < 0)
1710 z = x_sub(a, b);
1711 else
1712 z = x_add(a, b);
1713 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001714 Py_DECREF(a);
1715 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001716 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001717}
1718
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001719static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001720long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001721{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001722 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001723
Neil Schemenauerba872e22001-01-04 01:46:03 +00001724 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1725
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001726 if (a->ob_size < 0) {
1727 if (b->ob_size < 0)
1728 z = x_sub(a, b);
1729 else
1730 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001731 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001732 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001733 }
1734 else {
1735 if (b->ob_size < 0)
1736 z = x_add(a, b);
1737 else
1738 z = x_sub(a, b);
1739 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001740 Py_DECREF(a);
1741 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001742 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001743}
1744
Tim Peters5af4e6c2002-08-12 02:31:19 +00001745/* Grade school multiplication, ignoring the signs.
1746 * Returns the absolute value of the product, or NULL if error.
1747 */
1748static PyLongObject *
1749x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001750{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001751 PyLongObject *z;
1752 int size_a = ABS(a->ob_size);
1753 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001754 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001755
Tim Peters5af4e6c2002-08-12 02:31:19 +00001756 z = _PyLong_New(size_a + size_b);
1757 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001758 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001759
1760 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001761 if (a == b) {
1762 /* Efficient squaring per HAC, Algorithm 14.16:
1763 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1764 * Gives slightly less than a 2x speedup when a == b,
1765 * via exploiting that each entry in the multiplication
1766 * pyramid appears twice (except for the size_a squares).
1767 */
1768 for (i = 0; i < size_a; ++i) {
1769 twodigits carry;
1770 twodigits f = a->ob_digit[i];
1771 digit *pz = z->ob_digit + (i << 1);
1772 digit *pa = a->ob_digit + i + 1;
1773 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001774
Tim Peters0973b992004-08-29 22:16:50 +00001775 SIGCHECK({
1776 Py_DECREF(z);
1777 return NULL;
1778 })
1779
1780 carry = *pz + f * f;
1781 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001782 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001783 assert(carry <= MASK);
1784
1785 /* Now f is added in twice in each column of the
1786 * pyramid it appears. Same as adding f<<1 once.
1787 */
1788 f <<= 1;
1789 while (pa < paend) {
1790 carry += *pz + *pa++ * f;
1791 *pz++ = (digit)(carry & MASK);
1792 carry >>= SHIFT;
1793 assert(carry <= (MASK << 1));
1794 }
1795 if (carry) {
1796 carry += *pz;
1797 *pz++ = (digit)(carry & MASK);
1798 carry >>= SHIFT;
1799 }
1800 if (carry)
1801 *pz += (digit)(carry & MASK);
1802 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001803 }
Tim Peters0973b992004-08-29 22:16:50 +00001804 }
1805 else { /* a is not the same as b -- gradeschool long mult */
1806 for (i = 0; i < size_a; ++i) {
1807 twodigits carry = 0;
1808 twodigits f = a->ob_digit[i];
1809 digit *pz = z->ob_digit + i;
1810 digit *pb = b->ob_digit;
1811 digit *pbend = b->ob_digit + size_b;
1812
1813 SIGCHECK({
1814 Py_DECREF(z);
1815 return NULL;
1816 })
1817
1818 while (pb < pbend) {
1819 carry += *pz + *pb++ * f;
1820 *pz++ = (digit)(carry & MASK);
1821 carry >>= SHIFT;
1822 assert(carry <= MASK);
1823 }
1824 if (carry)
1825 *pz += (digit)(carry & MASK);
1826 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001827 }
1828 }
Tim Peters44121a62002-08-12 06:17:58 +00001829 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001830}
1831
1832/* A helper for Karatsuba multiplication (k_mul).
1833 Takes a long "n" and an integer "size" representing the place to
1834 split, and sets low and high such that abs(n) == (high << size) + low,
1835 viewing the shift as being by digits. The sign bit is ignored, and
1836 the return values are >= 0.
1837 Returns 0 on success, -1 on failure.
1838*/
1839static int
1840kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1841{
1842 PyLongObject *hi, *lo;
1843 int size_lo, size_hi;
1844 const int size_n = ABS(n->ob_size);
1845
1846 size_lo = MIN(size_n, size);
1847 size_hi = size_n - size_lo;
1848
1849 if ((hi = _PyLong_New(size_hi)) == NULL)
1850 return -1;
1851 if ((lo = _PyLong_New(size_lo)) == NULL) {
1852 Py_DECREF(hi);
1853 return -1;
1854 }
1855
1856 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1857 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1858
1859 *high = long_normalize(hi);
1860 *low = long_normalize(lo);
1861 return 0;
1862}
1863
Tim Peters60004642002-08-12 22:01:34 +00001864static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1865
Tim Peters5af4e6c2002-08-12 02:31:19 +00001866/* Karatsuba multiplication. Ignores the input signs, and returns the
1867 * absolute value of the product (or NULL if error).
1868 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1869 */
1870static PyLongObject *
1871k_mul(PyLongObject *a, PyLongObject *b)
1872{
Tim Peters738eda72002-08-12 15:08:20 +00001873 int asize = ABS(a->ob_size);
1874 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001875 PyLongObject *ah = NULL;
1876 PyLongObject *al = NULL;
1877 PyLongObject *bh = NULL;
1878 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001879 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001880 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001881 int shift; /* the number of digits we split off */
1882 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001883
Tim Peters5af4e6c2002-08-12 02:31:19 +00001884 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1885 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1886 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001887 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001888 * By picking X to be a power of 2, "*X" is just shifting, and it's
1889 * been reduced to 3 multiplies on numbers half the size.
1890 */
1891
Tim Petersfc07e562002-08-12 02:54:10 +00001892 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001893 * is largest.
1894 */
Tim Peters738eda72002-08-12 15:08:20 +00001895 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001896 t1 = a;
1897 a = b;
1898 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001899
1900 i = asize;
1901 asize = bsize;
1902 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001903 }
1904
1905 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001906 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1907 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001908 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001909 return _PyLong_New(0);
1910 else
1911 return x_mul(a, b);
1912 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001913
Tim Peters60004642002-08-12 22:01:34 +00001914 /* If a is small compared to b, splitting on b gives a degenerate
1915 * case with ah==0, and Karatsuba may be (even much) less efficient
1916 * than "grade school" then. However, we can still win, by viewing
1917 * b as a string of "big digits", each of width a->ob_size. That
1918 * leads to a sequence of balanced calls to k_mul.
1919 */
1920 if (2 * asize <= bsize)
1921 return k_lopsided_mul(a, b);
1922
Tim Petersd6974a52002-08-13 20:37:51 +00001923 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001924 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001925 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001926 assert(ah->ob_size > 0); /* the split isn't degenerate */
1927
Tim Peters0973b992004-08-29 22:16:50 +00001928 if (a == b) {
1929 bh = ah;
1930 bl = al;
1931 Py_INCREF(bh);
1932 Py_INCREF(bl);
1933 }
1934 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001935
Tim Petersd64c1de2002-08-12 17:36:03 +00001936 /* The plan:
1937 * 1. Allocate result space (asize + bsize digits: that's always
1938 * enough).
1939 * 2. Compute ah*bh, and copy into result at 2*shift.
1940 * 3. Compute al*bl, and copy into result at 0. Note that this
1941 * can't overlap with #2.
1942 * 4. Subtract al*bl from the result, starting at shift. This may
1943 * underflow (borrow out of the high digit), but we don't care:
1944 * we're effectively doing unsigned arithmetic mod
1945 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1946 * borrows and carries out of the high digit can be ignored.
1947 * 5. Subtract ah*bh from the result, starting at shift.
1948 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1949 * at shift.
1950 */
1951
1952 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001953 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001954 if (ret == NULL) goto fail;
1955#ifdef Py_DEBUG
1956 /* Fill with trash, to catch reference to uninitialized digits. */
1957 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1958#endif
Tim Peters44121a62002-08-12 06:17:58 +00001959
Tim Petersd64c1de2002-08-12 17:36:03 +00001960 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001961 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1962 assert(t1->ob_size >= 0);
1963 assert(2*shift + t1->ob_size <= ret->ob_size);
1964 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1965 t1->ob_size * sizeof(digit));
1966
1967 /* Zero-out the digits higher than the ah*bh copy. */
1968 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001969 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001970 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001971 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001972
Tim Petersd64c1de2002-08-12 17:36:03 +00001973 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001974 if ((t2 = k_mul(al, bl)) == NULL) {
1975 Py_DECREF(t1);
1976 goto fail;
1977 }
1978 assert(t2->ob_size >= 0);
1979 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1980 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001981
1982 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001983 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001984 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001985 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001986
Tim Petersd64c1de2002-08-12 17:36:03 +00001987 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1988 * because it's fresher in cache.
1989 */
Tim Peters738eda72002-08-12 15:08:20 +00001990 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001991 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001992 Py_DECREF(t2);
1993
Tim Petersd64c1de2002-08-12 17:36:03 +00001994 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001995 Py_DECREF(t1);
1996
Tim Petersd64c1de2002-08-12 17:36:03 +00001997 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001998 if ((t1 = x_add(ah, al)) == NULL) goto fail;
1999 Py_DECREF(ah);
2000 Py_DECREF(al);
2001 ah = al = NULL;
2002
Tim Peters0973b992004-08-29 22:16:50 +00002003 if (a == b) {
2004 t2 = t1;
2005 Py_INCREF(t2);
2006 }
2007 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002008 Py_DECREF(t1);
2009 goto fail;
2010 }
2011 Py_DECREF(bh);
2012 Py_DECREF(bl);
2013 bh = bl = NULL;
2014
Tim Peters738eda72002-08-12 15:08:20 +00002015 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002016 Py_DECREF(t1);
2017 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002018 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002019 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002020
Tim Petersd6974a52002-08-13 20:37:51 +00002021 /* Add t3. It's not obvious why we can't run out of room here.
2022 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002023 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002024 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002025 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002026
Tim Peters5af4e6c2002-08-12 02:31:19 +00002027 return long_normalize(ret);
2028
2029 fail:
2030 Py_XDECREF(ret);
2031 Py_XDECREF(ah);
2032 Py_XDECREF(al);
2033 Py_XDECREF(bh);
2034 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002035 return NULL;
2036}
2037
Tim Petersd6974a52002-08-13 20:37:51 +00002038/* (*) Why adding t3 can't "run out of room" above.
2039
Tim Petersab86c2b2002-08-15 20:06:00 +00002040Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2041to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002042
Tim Petersab86c2b2002-08-15 20:06:00 +000020431. For any integer i, i = c(i/2) + f(i/2). In particular,
2044 bsize = c(bsize/2) + f(bsize/2).
20452. shift = f(bsize/2)
20463. asize <= bsize
20474. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2048 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002049
Tim Petersab86c2b2002-08-15 20:06:00 +00002050We allocated asize + bsize result digits, and add t3 into them at an offset
2051of shift. This leaves asize+bsize-shift allocated digit positions for t3
2052to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2053asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002054
Tim Petersab86c2b2002-08-15 20:06:00 +00002055bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2056at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002057
Tim Petersab86c2b2002-08-15 20:06:00 +00002058If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2059digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2060most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002061
Tim Petersab86c2b2002-08-15 20:06:00 +00002062The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002063
Tim Petersab86c2b2002-08-15 20:06:00 +00002064 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002065
Tim Petersab86c2b2002-08-15 20:06:00 +00002066and we have asize + c(bsize/2) available digit positions. We need to show
2067this is always enough. An instance of c(bsize/2) cancels out in both, so
2068the question reduces to whether asize digits is enough to hold
2069(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2070then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2071asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2072digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2073asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002074c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2075is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2076bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002077
Tim Peters48d52c02002-08-14 17:07:32 +00002078Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2079clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2080ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002081*/
2082
Tim Peters60004642002-08-12 22:01:34 +00002083/* b has at least twice the digits of a, and a is big enough that Karatsuba
2084 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2085 * of slices, each with a->ob_size digits, and multiply the slices by a,
2086 * one at a time. This gives k_mul balanced inputs to work with, and is
2087 * also cache-friendly (we compute one double-width slice of the result
2088 * at a time, then move on, never bactracking except for the helpful
2089 * single-width slice overlap between successive partial sums).
2090 */
2091static PyLongObject *
2092k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2093{
2094 const int asize = ABS(a->ob_size);
2095 int bsize = ABS(b->ob_size);
2096 int nbdone; /* # of b digits already multiplied */
2097 PyLongObject *ret;
2098 PyLongObject *bslice = NULL;
2099
2100 assert(asize > KARATSUBA_CUTOFF);
2101 assert(2 * asize <= bsize);
2102
2103 /* Allocate result space, and zero it out. */
2104 ret = _PyLong_New(asize + bsize);
2105 if (ret == NULL)
2106 return NULL;
2107 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2108
2109 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002110 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002111 if (bslice == NULL)
2112 goto fail;
2113
2114 nbdone = 0;
2115 while (bsize > 0) {
2116 PyLongObject *product;
2117 const int nbtouse = MIN(bsize, asize);
2118
2119 /* Multiply the next slice of b by a. */
2120 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2121 nbtouse * sizeof(digit));
2122 bslice->ob_size = nbtouse;
2123 product = k_mul(a, bslice);
2124 if (product == NULL)
2125 goto fail;
2126
2127 /* Add into result. */
2128 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2129 product->ob_digit, product->ob_size);
2130 Py_DECREF(product);
2131
2132 bsize -= nbtouse;
2133 nbdone += nbtouse;
2134 }
2135
2136 Py_DECREF(bslice);
2137 return long_normalize(ret);
2138
2139 fail:
2140 Py_DECREF(ret);
2141 Py_XDECREF(bslice);
2142 return NULL;
2143}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002144
2145static PyObject *
2146long_mul(PyLongObject *v, PyLongObject *w)
2147{
2148 PyLongObject *a, *b, *z;
2149
2150 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002151 Py_INCREF(Py_NotImplemented);
2152 return Py_NotImplemented;
2153 }
2154
Tim Petersd64c1de2002-08-12 17:36:03 +00002155 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002156 /* Negate if exactly one of the inputs is negative. */
2157 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002158 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002159 Py_DECREF(a);
2160 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002161 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002162}
2163
Guido van Rossume32e0141992-01-19 16:31:05 +00002164/* The / and % operators are now defined in terms of divmod().
2165 The expression a mod b has the value a - b*floor(a/b).
2166 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002167 |a| by |b|, with the sign of a. This is also expressed
2168 as a - b*trunc(a/b), if trunc truncates towards zero.
2169 Some examples:
2170 a b a rem b a mod b
2171 13 10 3 3
2172 -13 10 -3 7
2173 13 -10 3 -7
2174 -13 -10 -3 -3
2175 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002176 have different signs. We then subtract one from the 'div'
2177 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002178
Tim Peters47e52ee2004-08-30 02:44:38 +00002179/* Compute
2180 * *pdiv, *pmod = divmod(v, w)
2181 * NULL can be passed for pdiv or pmod, in which case that part of
2182 * the result is simply thrown away. The caller owns a reference to
2183 * each of these it requests (does not pass NULL for).
2184 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002185static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002186l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002187 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002188{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002189 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002190
Guido van Rossume32e0141992-01-19 16:31:05 +00002191 if (long_divrem(v, w, &div, &mod) < 0)
2192 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002193 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2194 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002195 PyLongObject *temp;
2196 PyLongObject *one;
2197 temp = (PyLongObject *) long_add(mod, w);
2198 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002199 mod = temp;
2200 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002201 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002202 return -1;
2203 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002204 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002205 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002206 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2207 Py_DECREF(mod);
2208 Py_DECREF(div);
2209 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002210 return -1;
2211 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002212 Py_DECREF(one);
2213 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002214 div = temp;
2215 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002216 if (pdiv != NULL)
2217 *pdiv = div;
2218 else
2219 Py_DECREF(div);
2220
2221 if (pmod != NULL)
2222 *pmod = mod;
2223 else
2224 Py_DECREF(mod);
2225
Guido van Rossume32e0141992-01-19 16:31:05 +00002226 return 0;
2227}
2228
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002229static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002230long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002231{
Tim Peters47e52ee2004-08-30 02:44:38 +00002232 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002233
2234 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002235 if (l_divmod(a, b, &div, NULL) < 0)
2236 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002237 Py_DECREF(a);
2238 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002239 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002240}
2241
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002242static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002243long_classic_div(PyObject *v, PyObject *w)
2244{
Tim Peters47e52ee2004-08-30 02:44:38 +00002245 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002246
2247 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002248 if (Py_DivisionWarningFlag &&
2249 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2250 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002251 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002252 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002253 Py_DECREF(a);
2254 Py_DECREF(b);
2255 return (PyObject *)div;
2256}
2257
2258static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002259long_true_divide(PyObject *v, PyObject *w)
2260{
Tim Peterse2a60002001-09-04 06:17:36 +00002261 PyLongObject *a, *b;
2262 double ad, bd;
Tim Peters6f97e492001-11-04 23:09:40 +00002263 int aexp, bexp, failed;
Tim Peterse2a60002001-09-04 06:17:36 +00002264
2265 CONVERT_BINOP(v, w, &a, &b);
2266 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2267 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002268 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2269 Py_DECREF(a);
2270 Py_DECREF(b);
2271 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002272 return NULL;
2273
2274 if (bd == 0.0) {
2275 PyErr_SetString(PyExc_ZeroDivisionError,
2276 "long division or modulo by zero");
2277 return NULL;
2278 }
2279
2280 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2281 ad /= bd; /* overflow/underflow impossible here */
2282 aexp -= bexp;
2283 if (aexp > INT_MAX / SHIFT)
2284 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002285 else if (aexp < -(INT_MAX / SHIFT))
2286 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002287 errno = 0;
2288 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002289 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002290 goto overflow;
2291 return PyFloat_FromDouble(ad);
2292
2293overflow:
2294 PyErr_SetString(PyExc_OverflowError,
2295 "long/long too large for a float");
2296 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002297
Tim Peters20dab9f2001-09-04 05:31:47 +00002298}
2299
2300static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002301long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002302{
Tim Peters47e52ee2004-08-30 02:44:38 +00002303 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002304
2305 CONVERT_BINOP(v, w, &a, &b);
2306
Tim Peters47e52ee2004-08-30 02:44:38 +00002307 if (l_divmod(a, b, NULL, &mod) < 0)
2308 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002309 Py_DECREF(a);
2310 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002311 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002312}
2313
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002314static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002315long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002316{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002317 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002318 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002319
2320 CONVERT_BINOP(v, w, &a, &b);
2321
2322 if (l_divmod(a, b, &div, &mod) < 0) {
2323 Py_DECREF(a);
2324 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002325 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002326 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002327 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002328 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002329 PyTuple_SetItem(z, 0, (PyObject *) div);
2330 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002331 }
2332 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002333 Py_DECREF(div);
2334 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002335 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002336 Py_DECREF(a);
2337 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002338 return z;
2339}
2340
Tim Peters47e52ee2004-08-30 02:44:38 +00002341/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002342static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002343long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002344{
Tim Peters47e52ee2004-08-30 02:44:38 +00002345 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2346 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002347
Tim Peters47e52ee2004-08-30 02:44:38 +00002348 PyLongObject *z = NULL; /* accumulated result */
2349 int i, j, k; /* counters */
2350 PyLongObject *temp = NULL;
2351
2352 /* 5-ary values. If the exponent is large enough, table is
2353 * precomputed so that table[i] == a**i % c for i in range(32).
2354 */
2355 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2356 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2357
2358 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002359 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002360 if (PyLong_Check(x)) {
2361 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002362 Py_INCREF(x);
2363 }
Tim Petersde7990b2005-07-17 23:45:23 +00002364 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002365 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002366 if (c == NULL)
2367 goto Error;
2368 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002369 else if (x == Py_None)
2370 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002371 else {
2372 Py_DECREF(a);
2373 Py_DECREF(b);
2374 Py_INCREF(Py_NotImplemented);
2375 return Py_NotImplemented;
2376 }
Tim Peters4c483c42001-09-05 06:24:58 +00002377
Tim Peters47e52ee2004-08-30 02:44:38 +00002378 if (b->ob_size < 0) { /* if exponent is negative */
2379 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002380 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002381 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002382 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002383 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002384 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002385 /* else return a float. This works because we know
2386 that this calls float_pow() which converts its
2387 arguments to double. */
2388 Py_DECREF(a);
2389 Py_DECREF(b);
2390 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002391 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002392 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002393
2394 if (c) {
2395 /* if modulus == 0:
2396 raise ValueError() */
2397 if (c->ob_size == 0) {
2398 PyErr_SetString(PyExc_ValueError,
2399 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002400 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002401 }
2402
2403 /* if modulus < 0:
2404 negativeOutput = True
2405 modulus = -modulus */
2406 if (c->ob_size < 0) {
2407 negativeOutput = 1;
2408 temp = (PyLongObject *)_PyLong_Copy(c);
2409 if (temp == NULL)
2410 goto Error;
2411 Py_DECREF(c);
2412 c = temp;
2413 temp = NULL;
2414 c->ob_size = - c->ob_size;
2415 }
2416
2417 /* if modulus == 1:
2418 return 0 */
2419 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2420 z = (PyLongObject *)PyLong_FromLong(0L);
2421 goto Done;
2422 }
2423
2424 /* if base < 0:
2425 base = base % modulus
2426 Having the base positive just makes things easier. */
2427 if (a->ob_size < 0) {
2428 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002429 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002430 Py_DECREF(a);
2431 a = temp;
2432 temp = NULL;
2433 }
2434 }
2435
2436 /* At this point a, b, and c are guaranteed non-negative UNLESS
2437 c is NULL, in which case a may be negative. */
2438
2439 z = (PyLongObject *)PyLong_FromLong(1L);
2440 if (z == NULL)
2441 goto Error;
2442
2443 /* Perform a modular reduction, X = X % c, but leave X alone if c
2444 * is NULL.
2445 */
2446#define REDUCE(X) \
2447 if (c != NULL) { \
2448 if (l_divmod(X, c, NULL, &temp) < 0) \
2449 goto Error; \
2450 Py_XDECREF(X); \
2451 X = temp; \
2452 temp = NULL; \
2453 }
2454
2455 /* Multiply two values, then reduce the result:
2456 result = X*Y % c. If c is NULL, skip the mod. */
2457#define MULT(X, Y, result) \
2458{ \
2459 temp = (PyLongObject *)long_mul(X, Y); \
2460 if (temp == NULL) \
2461 goto Error; \
2462 Py_XDECREF(result); \
2463 result = temp; \
2464 temp = NULL; \
2465 REDUCE(result) \
2466}
2467
2468 if (b->ob_size <= FIVEARY_CUTOFF) {
2469 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2470 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2471 for (i = b->ob_size - 1; i >= 0; --i) {
2472 digit bi = b->ob_digit[i];
2473
2474 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2475 MULT(z, z, z)
2476 if (bi & j)
2477 MULT(z, a, z)
2478 }
2479 }
2480 }
2481 else {
2482 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2483 Py_INCREF(z); /* still holds 1L */
2484 table[0] = z;
2485 for (i = 1; i < 32; ++i)
2486 MULT(table[i-1], a, table[i])
2487
2488 for (i = b->ob_size - 1; i >= 0; --i) {
2489 const digit bi = b->ob_digit[i];
2490
2491 for (j = SHIFT - 5; j >= 0; j -= 5) {
2492 const int index = (bi >> j) & 0x1f;
2493 for (k = 0; k < 5; ++k)
2494 MULT(z, z, z)
2495 if (index)
2496 MULT(z, table[index], z)
2497 }
2498 }
2499 }
2500
2501 if (negativeOutput && (z->ob_size != 0)) {
2502 temp = (PyLongObject *)long_sub(z, c);
2503 if (temp == NULL)
2504 goto Error;
2505 Py_DECREF(z);
2506 z = temp;
2507 temp = NULL;
2508 }
2509 goto Done;
2510
2511 Error:
2512 if (z != NULL) {
2513 Py_DECREF(z);
2514 z = NULL;
2515 }
2516 /* fall through */
2517 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002518 if (b->ob_size > FIVEARY_CUTOFF) {
2519 for (i = 0; i < 32; ++i)
2520 Py_XDECREF(table[i]);
2521 }
Tim Petersde7990b2005-07-17 23:45:23 +00002522 Py_DECREF(a);
2523 Py_DECREF(b);
2524 Py_XDECREF(c);
2525 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002526 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002527}
2528
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002529static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002530long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002531{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002532 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002533 PyLongObject *x;
2534 PyLongObject *w;
2535 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002536 if (w == NULL)
2537 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002538 x = (PyLongObject *) long_add(v, w);
2539 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002540 if (x == NULL)
2541 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002542 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002543 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002544}
2545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002546static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002547long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002548{
Tim Peters69c2de32001-09-11 22:31:33 +00002549 if (PyLong_CheckExact(v)) {
2550 Py_INCREF(v);
2551 return (PyObject *)v;
2552 }
2553 else
2554 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002555}
2556
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002557static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002558long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002559{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002560 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002561 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002562 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002563 Py_INCREF(v);
2564 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002565 }
Tim Peters69c2de32001-09-11 22:31:33 +00002566 z = (PyLongObject *)_PyLong_Copy(v);
2567 if (z != NULL)
2568 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002569 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002570}
2571
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002572static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002573long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002574{
2575 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002576 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002577 else
2578 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002579}
2580
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002581static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002582long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002583{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002584 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002585}
2586
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002587static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002588long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002589{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002590 PyLongObject *a, *b;
2591 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002592 long shiftby;
2593 int newsize, wordshift, loshift, hishift, i, j;
2594 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002595
Neil Schemenauerba872e22001-01-04 01:46:03 +00002596 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2597
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002598 if (a->ob_size < 0) {
2599 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002600 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002601 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002602 if (a1 == NULL)
2603 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604 a2 = (PyLongObject *) long_rshift(a1, b);
2605 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002606 if (a2 == NULL)
2607 goto rshift_error;
2608 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002609 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002610 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002611 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002612
Neil Schemenauerba872e22001-01-04 01:46:03 +00002613 shiftby = PyLong_AsLong((PyObject *)b);
2614 if (shiftby == -1L && PyErr_Occurred())
2615 goto rshift_error;
2616 if (shiftby < 0) {
2617 PyErr_SetString(PyExc_ValueError,
2618 "negative shift count");
2619 goto rshift_error;
2620 }
2621 wordshift = shiftby / SHIFT;
2622 newsize = ABS(a->ob_size) - wordshift;
2623 if (newsize <= 0) {
2624 z = _PyLong_New(0);
2625 Py_DECREF(a);
2626 Py_DECREF(b);
2627 return (PyObject *)z;
2628 }
2629 loshift = shiftby % SHIFT;
2630 hishift = SHIFT - loshift;
2631 lomask = ((digit)1 << hishift) - 1;
2632 himask = MASK ^ lomask;
2633 z = _PyLong_New(newsize);
2634 if (z == NULL)
2635 goto rshift_error;
2636 if (a->ob_size < 0)
2637 z->ob_size = -(z->ob_size);
2638 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2639 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2640 if (i+1 < newsize)
2641 z->ob_digit[i] |=
2642 (a->ob_digit[j+1] << hishift) & himask;
2643 }
2644 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002645 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002646rshift_error:
2647 Py_DECREF(a);
2648 Py_DECREF(b);
2649 return (PyObject *) z;
2650
Guido van Rossumc6913e71991-11-19 20:26:46 +00002651}
2652
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002653static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002654long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002655{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002656 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002657 PyLongObject *a, *b;
2658 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002659 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002660 int oldsize, newsize, wordshift, remshift, i, j;
2661 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002662
Neil Schemenauerba872e22001-01-04 01:46:03 +00002663 CONVERT_BINOP(v, w, &a, &b);
2664
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002665 shiftby = PyLong_AsLong((PyObject *)b);
2666 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002667 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002668 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002669 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002670 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002671 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002672 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002673 PyErr_SetString(PyExc_ValueError,
2674 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002675 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002676 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002677 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2678 wordshift = (int)shiftby / SHIFT;
2679 remshift = (int)shiftby - wordshift * SHIFT;
2680
2681 oldsize = ABS(a->ob_size);
2682 newsize = oldsize + wordshift;
2683 if (remshift)
2684 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002685 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002686 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002687 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002688 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002689 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002690 for (i = 0; i < wordshift; i++)
2691 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002692 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002693 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002694 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002695 z->ob_digit[i] = (digit)(accum & MASK);
2696 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002697 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002698 if (remshift)
2699 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002700 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002701 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002702 z = long_normalize(z);
2703lshift_error:
2704 Py_DECREF(a);
2705 Py_DECREF(b);
2706 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002707}
2708
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002709
2710/* Bitwise and/xor/or operations */
2711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002712static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002713long_bitwise(PyLongObject *a,
2714 int op, /* '&', '|', '^' */
2715 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002716{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002717 digit maska, maskb; /* 0 or MASK */
2718 int negz;
2719 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002720 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002721 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002722 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002723 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002724
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002725 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002726 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002727 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002728 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002729 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002730 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002731 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002732 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002733 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002734 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002735 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002736 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002737 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002738 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002739 maskb = 0;
2740 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002741
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002742 negz = 0;
2743 switch (op) {
2744 case '^':
2745 if (maska != maskb) {
2746 maska ^= MASK;
2747 negz = -1;
2748 }
2749 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002750 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002751 if (maska && maskb) {
2752 op = '|';
2753 maska ^= MASK;
2754 maskb ^= MASK;
2755 negz = -1;
2756 }
2757 break;
2758 case '|':
2759 if (maska || maskb) {
2760 op = '&';
2761 maska ^= MASK;
2762 maskb ^= MASK;
2763 negz = -1;
2764 }
2765 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002766 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002767
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002768 /* JRH: The original logic here was to allocate the result value (z)
2769 as the longer of the two operands. However, there are some cases
2770 where the result is guaranteed to be shorter than that: AND of two
2771 positives, OR of two negatives: use the shorter number. AND with
2772 mixed signs: use the positive number. OR with mixed signs: use the
2773 negative number. After the transformations above, op will be '&'
2774 iff one of these cases applies, and mask will be non-0 for operands
2775 whose length should be ignored.
2776 */
2777
2778 size_a = a->ob_size;
2779 size_b = b->ob_size;
2780 size_z = op == '&'
2781 ? (maska
2782 ? size_b
2783 : (maskb ? size_a : MIN(size_a, size_b)))
2784 : MAX(size_a, size_b);
2785 z = _PyLong_New(size_z);
2786 if (a == NULL || b == NULL || z == NULL) {
2787 Py_XDECREF(a);
2788 Py_XDECREF(b);
2789 Py_XDECREF(z);
2790 return NULL;
2791 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002792
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002793 for (i = 0; i < size_z; ++i) {
2794 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2795 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2796 switch (op) {
2797 case '&': z->ob_digit[i] = diga & digb; break;
2798 case '|': z->ob_digit[i] = diga | digb; break;
2799 case '^': z->ob_digit[i] = diga ^ digb; break;
2800 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002801 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002802
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002803 Py_DECREF(a);
2804 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002805 z = long_normalize(z);
2806 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002807 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002808 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002809 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002810 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002811}
2812
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002813static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002814long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002815{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002816 PyLongObject *a, *b;
2817 PyObject *c;
2818 CONVERT_BINOP(v, w, &a, &b);
2819 c = long_bitwise(a, '&', b);
2820 Py_DECREF(a);
2821 Py_DECREF(b);
2822 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002823}
2824
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002825static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002826long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002827{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002828 PyLongObject *a, *b;
2829 PyObject *c;
2830 CONVERT_BINOP(v, w, &a, &b);
2831 c = long_bitwise(a, '^', b);
2832 Py_DECREF(a);
2833 Py_DECREF(b);
2834 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002835}
2836
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002837static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002838long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002839{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002840 PyLongObject *a, *b;
2841 PyObject *c;
2842 CONVERT_BINOP(v, w, &a, &b);
2843 c = long_bitwise(a, '|', b);
2844 Py_DECREF(a);
2845 Py_DECREF(b);
2846 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002847}
2848
Guido van Rossum234f9421993-06-17 12:35:49 +00002849static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002850long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002851{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002852 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002853 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002854 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002855 return 0;
2856 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002857 else if (PyLong_Check(*pw)) {
2858 Py_INCREF(*pv);
2859 Py_INCREF(*pw);
2860 return 0;
2861 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002862 return 1; /* Can't do it */
2863}
2864
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002865static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002866long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002867{
Brett Cannonc3647ac2005-04-26 03:45:26 +00002868 if (PyLong_CheckExact(v))
2869 Py_INCREF(v);
2870 else
2871 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002872 return v;
2873}
2874
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002875static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002876long_int(PyObject *v)
2877{
2878 long x;
2879 x = PyLong_AsLong(v);
2880 if (PyErr_Occurred()) {
2881 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2882 PyErr_Clear();
2883 if (PyLong_CheckExact(v)) {
2884 Py_INCREF(v);
2885 return v;
2886 }
2887 else
2888 return _PyLong_Copy((PyLongObject *)v);
2889 }
2890 else
2891 return NULL;
2892 }
2893 return PyInt_FromLong(x);
2894}
2895
2896static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002897long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002898{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002899 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002900 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002901 if (result == -1.0 && PyErr_Occurred())
2902 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002903 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002904}
2905
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002906static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002907long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002908{
Fred Drake121ee271999-12-23 15:41:28 +00002909 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002910}
2911
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002912static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002913long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002914{
Fred Drake121ee271999-12-23 15:41:28 +00002915 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002916}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002917
2918static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002919long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002920
Tim Peters6d6c1a32001-08-02 04:15:00 +00002921static PyObject *
2922long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2923{
2924 PyObject *x = NULL;
2925 int base = -909; /* unlikely! */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002926 static const char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002927
Guido van Rossumbef14172001-08-29 15:47:46 +00002928 if (type != &PyLong_Type)
2929 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002930 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2931 &x, &base))
2932 return NULL;
2933 if (x == NULL)
2934 return PyLong_FromLong(0L);
2935 if (base == -909)
2936 return PyNumber_Long(x);
2937 else if (PyString_Check(x))
2938 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002939#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002940 else if (PyUnicode_Check(x))
2941 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2942 PyUnicode_GET_SIZE(x),
2943 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002944#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002945 else {
2946 PyErr_SetString(PyExc_TypeError,
2947 "long() can't convert non-string with explicit base");
2948 return NULL;
2949 }
2950}
2951
Guido van Rossumbef14172001-08-29 15:47:46 +00002952/* Wimpy, slow approach to tp_new calls for subtypes of long:
2953 first create a regular long from whatever arguments we got,
2954 then allocate a subtype instance and initialize it from
2955 the regular long. The regular long is then thrown away.
2956*/
2957static PyObject *
2958long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2959{
2960 PyLongObject *tmp, *new;
2961 int i, n;
2962
2963 assert(PyType_IsSubtype(type, &PyLong_Type));
2964 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2965 if (tmp == NULL)
2966 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002967 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002968 n = tmp->ob_size;
2969 if (n < 0)
2970 n = -n;
2971 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002972 if (new == NULL) {
2973 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002974 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002975 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002976 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002977 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002978 for (i = 0; i < n; i++)
2979 new->ob_digit[i] = tmp->ob_digit[i];
2980 Py_DECREF(tmp);
2981 return (PyObject *)new;
2982}
2983
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002984static PyObject *
2985long_getnewargs(PyLongObject *v)
2986{
2987 return Py_BuildValue("(N)", _PyLong_Copy(v));
2988}
2989
2990static PyMethodDef long_methods[] = {
2991 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2992 {NULL, NULL} /* sentinel */
2993};
2994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002996"long(x[, base]) -> integer\n\
2997\n\
2998Convert a string or number to a long integer, if possible. A floating\n\
2999point argument will be truncated towards zero (this does not include a\n\
3000string representation of a floating point number!) When converting a\n\
3001string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003002converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003004static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003005 (binaryfunc) long_add, /*nb_add*/
3006 (binaryfunc) long_sub, /*nb_subtract*/
3007 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00003008 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003009 (binaryfunc) long_mod, /*nb_remainder*/
3010 (binaryfunc) long_divmod, /*nb_divmod*/
3011 (ternaryfunc) long_pow, /*nb_power*/
3012 (unaryfunc) long_neg, /*nb_negative*/
3013 (unaryfunc) long_pos, /*tp_positive*/
3014 (unaryfunc) long_abs, /*tp_absolute*/
3015 (inquiry) long_nonzero, /*tp_nonzero*/
3016 (unaryfunc) long_invert, /*nb_invert*/
3017 (binaryfunc) long_lshift, /*nb_lshift*/
3018 (binaryfunc) long_rshift, /*nb_rshift*/
3019 (binaryfunc) long_and, /*nb_and*/
3020 (binaryfunc) long_xor, /*nb_xor*/
3021 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00003022 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003023 (unaryfunc) long_int, /*nb_int*/
3024 (unaryfunc) long_long, /*nb_long*/
3025 (unaryfunc) long_float, /*nb_float*/
3026 (unaryfunc) long_oct, /*nb_oct*/
3027 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003028 0, /* nb_inplace_add */
3029 0, /* nb_inplace_subtract */
3030 0, /* nb_inplace_multiply */
3031 0, /* nb_inplace_divide */
3032 0, /* nb_inplace_remainder */
3033 0, /* nb_inplace_power */
3034 0, /* nb_inplace_lshift */
3035 0, /* nb_inplace_rshift */
3036 0, /* nb_inplace_and */
3037 0, /* nb_inplace_xor */
3038 0, /* nb_inplace_or */
3039 (binaryfunc)long_div, /* nb_floor_divide */
3040 long_true_divide, /* nb_true_divide */
3041 0, /* nb_inplace_floor_divide */
3042 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003043};
3044
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003045PyTypeObject PyLong_Type = {
3046 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003047 0, /* ob_size */
3048 "long", /* tp_name */
3049 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3050 sizeof(digit), /* tp_itemsize */
3051 (destructor)long_dealloc, /* tp_dealloc */
3052 0, /* tp_print */
3053 0, /* tp_getattr */
3054 0, /* tp_setattr */
3055 (cmpfunc)long_compare, /* tp_compare */
3056 (reprfunc)long_repr, /* tp_repr */
3057 &long_as_number, /* tp_as_number */
3058 0, /* tp_as_sequence */
3059 0, /* tp_as_mapping */
3060 (hashfunc)long_hash, /* tp_hash */
3061 0, /* tp_call */
3062 (reprfunc)long_str, /* tp_str */
3063 PyObject_GenericGetAttr, /* tp_getattro */
3064 0, /* tp_setattro */
3065 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003066 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3067 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003068 long_doc, /* tp_doc */
3069 0, /* tp_traverse */
3070 0, /* tp_clear */
3071 0, /* tp_richcompare */
3072 0, /* tp_weaklistoffset */
3073 0, /* tp_iter */
3074 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003075 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003076 0, /* tp_members */
3077 0, /* tp_getset */
3078 0, /* tp_base */
3079 0, /* tp_dict */
3080 0, /* tp_descr_get */
3081 0, /* tp_descr_set */
3082 0, /* tp_dictoffset */
3083 0, /* tp_init */
3084 0, /* tp_alloc */
3085 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003086 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003087};