blob: 32f79580b3205ec5213fee532e6cdb41c5d68bfd [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{
Thomas Wouters553489a2006-02-01 21:32:04 +0000655 int e = -1;
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;
Thomas Wouters553489a2006-02-01 21:32:04 +0000665 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
666 set correctly after a successful _PyLong_AsScaledDouble() call */
667 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000668 if (e > INT_MAX / SHIFT)
669 goto overflow;
670 errno = 0;
671 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000672 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000673 goto overflow;
674 return x;
675
676overflow:
677 PyErr_SetString(PyExc_OverflowError,
678 "long int too large to convert to float");
679 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000680}
681
Guido van Rossum78694d91998-09-18 14:14:13 +0000682/* Create a new long (or int) object from a C pointer */
683
684PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000685PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000686{
Tim Peters70128a12001-06-16 08:48:40 +0000687#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000688 return PyInt_FromLong((long)p);
689#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000690
Tim Peters70128a12001-06-16 08:48:40 +0000691#ifndef HAVE_LONG_LONG
692# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
693#endif
694#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000695# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000696#endif
697 /* optimize null pointers */
698 if (p == NULL)
699 return PyInt_FromLong(0);
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000700 return PyLong_FromLongLong((PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000701
702#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000703}
704
705/* Get a C pointer from a long object (or an int object in some cases) */
706
707void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000708PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000709{
710 /* This function will allow int or long objects. If vv is neither,
711 then the PyLong_AsLong*() functions will raise the exception:
712 PyExc_SystemError, "bad argument to internal function"
713 */
Tim Peters70128a12001-06-16 08:48:40 +0000714#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000715 long x;
716
Tim Peters70128a12001-06-16 08:48:40 +0000717 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000718 x = PyInt_AS_LONG(vv);
719 else
720 x = PyLong_AsLong(vv);
721#else
Tim Peters70128a12001-06-16 08:48:40 +0000722
723#ifndef HAVE_LONG_LONG
724# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
725#endif
726#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000727# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000728#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000729 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000730
Tim Peters70128a12001-06-16 08:48:40 +0000731 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000732 x = PyInt_AS_LONG(vv);
733 else
734 x = PyLong_AsLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000735
736#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000737
738 if (x == -1 && PyErr_Occurred())
739 return NULL;
740 return (void *)x;
741}
742
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000743#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000744
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000745/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000746 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000747 */
748
Tim Peterscf37dfc2001-06-14 18:42:50 +0000749#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000750
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000751/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000752
753PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000754PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000755{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000756 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000757 int one = 1;
758 return _PyLong_FromByteArray(
759 (unsigned char *)&bytes,
760 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000761}
762
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000763/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000764
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000765PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000766PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000767{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000768 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000769 int one = 1;
770 return _PyLong_FromByteArray(
771 (unsigned char *)&bytes,
772 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000773}
774
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000775/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000776 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000777
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000778PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000779PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000780{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000781 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000782 int one = 1;
783 int res;
784
Tim Petersd38b1c72001-09-30 05:09:37 +0000785 if (vv == NULL) {
786 PyErr_BadInternalCall();
787 return -1;
788 }
789 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000790 PyNumberMethods *nb;
791 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000792 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000793 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000794 if ((nb = vv->ob_type->tp_as_number) == NULL ||
795 nb->nb_int == NULL) {
796 PyErr_SetString(PyExc_TypeError, "an integer is required");
797 return -1;
798 }
799 io = (*nb->nb_int) (vv);
800 if (io == NULL)
801 return -1;
802 if (PyInt_Check(io)) {
803 bytes = PyInt_AsLong(io);
804 Py_DECREF(io);
805 return bytes;
806 }
807 if (PyLong_Check(io)) {
808 bytes = PyLong_AsLongLong(io);
809 Py_DECREF(io);
810 return bytes;
811 }
812 Py_DECREF(io);
813 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000814 return -1;
815 }
816
Tim Petersd1a7da62001-06-13 00:35:57 +0000817 res = _PyLong_AsByteArray(
818 (PyLongObject *)vv, (unsigned char *)&bytes,
819 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000820
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000821 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000822 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000823 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000824 else
825 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000826}
827
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000829 Return -1 and set an error if overflow occurs. */
830
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000831unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000832PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000833{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000834 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000835 int one = 1;
836 int res;
837
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000838 if (vv == NULL || !PyLong_Check(vv)) {
839 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000840 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000841 }
842
Tim Petersd1a7da62001-06-13 00:35:57 +0000843 res = _PyLong_AsByteArray(
844 (PyLongObject *)vv, (unsigned char *)&bytes,
845 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000846
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000847 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000848 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000849 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000850 else
851 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000852}
Tim Petersd1a7da62001-06-13 00:35:57 +0000853
Thomas Hellera4ea6032003-04-17 18:55:45 +0000854/* Get a C unsigned long int from a long int object, ignoring the high bits.
855 Returns -1 and sets an error condition if an error occurs. */
856
857unsigned PY_LONG_LONG
858PyLong_AsUnsignedLongLongMask(PyObject *vv)
859{
860 register PyLongObject *v;
861 unsigned PY_LONG_LONG x;
862 int i, sign;
863
864 if (vv == NULL || !PyLong_Check(vv)) {
865 PyErr_BadInternalCall();
866 return (unsigned long) -1;
867 }
868 v = (PyLongObject *)vv;
869 i = v->ob_size;
870 sign = 1;
871 x = 0;
872 if (i < 0) {
873 sign = -1;
874 i = -i;
875 }
876 while (--i >= 0) {
877 x = (x << SHIFT) + v->ob_digit[i];
878 }
879 return x * sign;
880}
Tim Petersd1a7da62001-06-13 00:35:57 +0000881#undef IS_LITTLE_ENDIAN
882
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000883#endif /* HAVE_LONG_LONG */
884
Neil Schemenauerba872e22001-01-04 01:46:03 +0000885
886static int
887convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +0000888 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000889 *a = (PyLongObject *) v;
890 Py_INCREF(v);
891 }
892 else if (PyInt_Check(v)) {
893 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
894 }
895 else {
896 return 0;
897 }
Tim Peters5af4e6c2002-08-12 02:31:19 +0000898 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +0000899 *b = (PyLongObject *) w;
900 Py_INCREF(w);
901 }
902 else if (PyInt_Check(w)) {
903 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
904 }
905 else {
906 Py_DECREF(*a);
907 return 0;
908 }
909 return 1;
910}
911
912#define CONVERT_BINOP(v, w, a, b) \
913 if (!convert_binop(v, w, a, b)) { \
914 Py_INCREF(Py_NotImplemented); \
915 return Py_NotImplemented; \
916 }
917
Tim Peters877a2122002-08-12 05:09:36 +0000918/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
919 * is modified in place, by adding y to it. Carries are propagated as far as
920 * x[m-1], and the remaining carry (0 or 1) is returned.
921 */
922static digit
923v_iadd(digit *x, int m, digit *y, int n)
924{
925 int i;
926 digit carry = 0;
927
928 assert(m >= n);
929 for (i = 0; i < n; ++i) {
930 carry += x[i] + y[i];
931 x[i] = carry & MASK;
932 carry >>= SHIFT;
933 assert((carry & 1) == carry);
934 }
935 for (; carry && i < m; ++i) {
936 carry += x[i];
937 x[i] = carry & MASK;
938 carry >>= SHIFT;
939 assert((carry & 1) == carry);
940 }
941 return carry;
942}
943
944/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
945 * is modified in place, by subtracting y from it. Borrows are propagated as
946 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
947 */
948static digit
949v_isub(digit *x, int m, digit *y, int n)
950{
951 int i;
952 digit borrow = 0;
953
954 assert(m >= n);
955 for (i = 0; i < n; ++i) {
956 borrow = x[i] - y[i] - borrow;
957 x[i] = borrow & MASK;
958 borrow >>= SHIFT;
959 borrow &= 1; /* keep only 1 sign bit */
960 }
961 for (; borrow && i < m; ++i) {
962 borrow = x[i] - borrow;
963 x[i] = borrow & MASK;
964 borrow >>= SHIFT;
965 borrow &= 1;
966 }
967 return borrow;
968}
Neil Schemenauerba872e22001-01-04 01:46:03 +0000969
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000970/* Multiply by a single digit, ignoring the sign. */
971
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000972static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000973mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000974{
975 return muladd1(a, n, (digit)0);
976}
977
978/* Multiply by a single digit and add a single digit, ignoring the sign. */
979
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000981muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000982{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000983 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000985 twodigits carry = extra;
986 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000987
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000988 if (z == NULL)
989 return NULL;
990 for (i = 0; i < size_a; ++i) {
991 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000992 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000993 carry >>= SHIFT;
994 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000995 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000996 return long_normalize(z);
997}
998
Tim Peters212e6142001-07-14 12:23:19 +0000999/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1000 in pout, and returning the remainder. pin and pout point at the LSD.
1001 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1002 long_format, but that should be done with great care since longs are
1003 immutable. */
1004
1005static digit
1006inplace_divrem1(digit *pout, digit *pin, int size, digit n)
1007{
1008 twodigits rem = 0;
1009
1010 assert(n > 0 && n <= MASK);
1011 pin += size;
1012 pout += size;
1013 while (--size >= 0) {
1014 digit hi;
1015 rem = (rem << SHIFT) + *--pin;
1016 *--pout = hi = (digit)(rem / n);
1017 rem -= hi * n;
1018 }
1019 return (digit)rem;
1020}
1021
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001022/* Divide a long integer by a digit, returning both the quotient
1023 (as function result) and the remainder (through *prem).
1024 The sign of a is ignored; n should not be zero. */
1025
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001027divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001028{
Tim Peters212e6142001-07-14 12:23:19 +00001029 const int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001031
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001032 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001034 if (z == NULL)
1035 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001036 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001037 return long_normalize(z);
1038}
1039
1040/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001041 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001042 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001045long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 register PyLongObject *a = (PyLongObject *)aa;
1048 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001049 int i;
Tim Peters212e6142001-07-14 12:23:19 +00001050 const int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001051 char *p;
1052 int bits;
1053 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001054
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055 if (a == NULL || !PyLong_Check(a)) {
1056 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001057 return NULL;
1058 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001059 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001060
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001061 /* Compute a rough upper bound for the length of the string */
1062 i = base;
1063 bits = 0;
1064 while (i > 1) {
1065 ++bits;
1066 i >>= 1;
1067 }
Fred Drake121ee271999-12-23 15:41:28 +00001068 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001069 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001070 if (str == NULL)
1071 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001072 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001073 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001074 if (addL)
1075 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001076 if (a->ob_size < 0)
1077 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001078
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001079 if (a->ob_size == 0) {
1080 *--p = '0';
1081 }
1082 else if ((base & (base - 1)) == 0) {
1083 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001084 twodigits accum = 0;
1085 int accumbits = 0; /* # of bits in accum */
1086 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001087 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001088 while ((i >>= 1) > 1)
1089 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001090
1091 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001092 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001093 accumbits += SHIFT;
1094 assert(accumbits >= basebits);
1095 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001096 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001097 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001098 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001099 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001100 accumbits -= basebits;
1101 accum >>= basebits;
1102 } while (i < size_a-1 ? accumbits >= basebits :
1103 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001104 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001105 }
1106 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001107 /* Not 0, and base not a power of 2. Divide repeatedly by
1108 base, but for speed use the highest power of base that
1109 fits in a digit. */
Tim Peters212e6142001-07-14 12:23:19 +00001110 int size = size_a;
1111 digit *pin = a->ob_digit;
1112 PyLongObject *scratch;
1113 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001114 digit powbase = base; /* powbase == base ** power */
1115 int power = 1;
1116 for (;;) {
1117 unsigned long newpow = powbase * (unsigned long)base;
1118 if (newpow >> SHIFT) /* doesn't fit in a digit */
1119 break;
1120 powbase = (digit)newpow;
1121 ++power;
1122 }
Tim Peters212e6142001-07-14 12:23:19 +00001123
1124 /* Get a scratch area for repeated division. */
1125 scratch = _PyLong_New(size);
1126 if (scratch == NULL) {
1127 Py_DECREF(str);
1128 return NULL;
1129 }
1130
1131 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001132 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001133 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001134 digit rem = inplace_divrem1(scratch->ob_digit,
1135 pin, size, powbase);
1136 pin = scratch->ob_digit; /* no need to use a again */
1137 if (pin[size - 1] == 0)
1138 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001139 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001140 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001141 Py_DECREF(str);
1142 return NULL;
1143 })
Tim Peters212e6142001-07-14 12:23:19 +00001144
1145 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001146 assert(ntostore > 0);
1147 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001148 digit nextrem = (digit)(rem / base);
1149 char c = (char)(rem - nextrem * base);
1150 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001151 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001152 *--p = c;
1153 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001154 --ntostore;
1155 /* Termination is a bit delicate: must not
1156 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001157 remaining quotient and rem are both 0. */
1158 } while (ntostore && (size || rem));
1159 } while (size != 0);
1160 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001161 }
1162
Guido van Rossum2c475421992-08-14 15:13:07 +00001163 if (base == 8) {
1164 if (size_a != 0)
1165 *--p = '0';
1166 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001167 else if (base == 16) {
1168 *--p = 'x';
1169 *--p = '0';
1170 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001171 else if (base != 10) {
1172 *--p = '#';
1173 *--p = '0' + base%10;
1174 if (base > 10)
1175 *--p = '0' + base/10;
1176 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001177 if (sign)
1178 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179 if (p != PyString_AS_STRING(str)) {
1180 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001181 assert(p > q);
1182 do {
1183 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001184 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 _PyString_Resize((PyObject **)&str,
1186 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001187 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001189}
1190
Tim Petersbf2674b2003-02-02 07:51:32 +00001191/* *str points to the first digit in a string of base base digits. base
1192 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1193 * non-digit (which may be *str!). A normalized long is returned.
1194 * The point to this routine is that it takes time linear in the number of
1195 * string characters.
1196 */
1197static PyLongObject *
1198long_from_binary_base(char **str, int base)
1199{
1200 char *p = *str;
1201 char *start = p;
1202 int bits_per_char;
1203 int n;
1204 PyLongObject *z;
1205 twodigits accum;
1206 int bits_in_accum;
1207 digit *pdigit;
1208
1209 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1210 n = base;
1211 for (bits_per_char = -1; n; ++bits_per_char)
1212 n >>= 1;
1213 /* n <- total # of bits needed, while setting p to end-of-string */
1214 n = 0;
1215 for (;;) {
1216 int k = -1;
1217 char ch = *p;
1218
1219 if (ch <= '9')
1220 k = ch - '0';
1221 else if (ch >= 'a')
1222 k = ch - 'a' + 10;
1223 else if (ch >= 'A')
1224 k = ch - 'A' + 10;
1225 if (k < 0 || k >= base)
1226 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001227 ++p;
1228 }
1229 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001230 n = (p - start) * bits_per_char;
1231 if (n / bits_per_char != p - start) {
1232 PyErr_SetString(PyExc_ValueError,
1233 "long string too large to convert");
1234 return NULL;
1235 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001236 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1237 n = (n + SHIFT - 1) / SHIFT;
1238 z = _PyLong_New(n);
1239 if (z == NULL)
1240 return NULL;
1241 /* Read string from right, and fill in long from left; i.e.,
1242 * from least to most significant in both.
1243 */
1244 accum = 0;
1245 bits_in_accum = 0;
1246 pdigit = z->ob_digit;
1247 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001248 int k;
1249 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001250
1251 if (ch <= '9')
1252 k = ch - '0';
1253 else if (ch >= 'a')
1254 k = ch - 'a' + 10;
1255 else {
1256 assert(ch >= 'A');
1257 k = ch - 'A' + 10;
1258 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001259 assert(k >= 0 && k < base);
1260 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001261 bits_in_accum += bits_per_char;
1262 if (bits_in_accum >= SHIFT) {
1263 *pdigit++ = (digit)(accum & MASK);
1264 assert(pdigit - z->ob_digit <= n);
1265 accum >>= SHIFT;
1266 bits_in_accum -= SHIFT;
1267 assert(bits_in_accum < SHIFT);
1268 }
1269 }
1270 if (bits_in_accum) {
1271 assert(bits_in_accum <= SHIFT);
1272 *pdigit++ = (digit)accum;
1273 assert(pdigit - z->ob_digit <= n);
1274 }
1275 while (pdigit - z->ob_digit < n)
1276 *pdigit++ = 0;
1277 return long_normalize(z);
1278}
1279
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001281PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001282{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001283 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001284 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001286
Guido van Rossum472c04f1996-12-05 21:57:21 +00001287 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001289 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001290 return NULL;
1291 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001292 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001293 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001294 if (*str == '+')
1295 ++str;
1296 else if (*str == '-') {
1297 ++str;
1298 sign = -1;
1299 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001300 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001301 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001302 if (base == 0) {
1303 if (str[0] != '0')
1304 base = 10;
1305 else if (str[1] == 'x' || str[1] == 'X')
1306 base = 16;
1307 else
1308 base = 8;
1309 }
1310 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1311 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001312 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001313 if ((base & (base - 1)) == 0)
1314 z = long_from_binary_base(&str, base);
1315 else {
1316 z = _PyLong_New(0);
1317 for ( ; z != NULL; ++str) {
1318 int k = -1;
1319 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001320
Tim Petersbf2674b2003-02-02 07:51:32 +00001321 if (*str <= '9')
1322 k = *str - '0';
1323 else if (*str >= 'a')
1324 k = *str - 'a' + 10;
1325 else if (*str >= 'A')
1326 k = *str - 'A' + 10;
1327 if (k < 0 || k >= base)
1328 break;
1329 temp = muladd1(z, (digit)base, (digit)k);
1330 Py_DECREF(z);
1331 z = temp;
1332 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001333 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001334 if (z == NULL)
1335 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001336 if (str == start)
1337 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001338 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001339 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001340 if (*str == 'L' || *str == 'l')
1341 str++;
1342 while (*str && isspace(Py_CHARMASK(*str)))
1343 str++;
1344 if (*str != '\0')
1345 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001346 if (pend)
1347 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001349
1350 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001351 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001352 "invalid literal for long(): %.200s", orig_str);
1353 Py_XDECREF(z);
1354 return NULL;
1355}
1356
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001357#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001358PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001359PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001360{
Walter Dörwald07e14762002-11-06 16:15:14 +00001361 PyObject *result;
1362 char *buffer = PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001363
Walter Dörwald07e14762002-11-06 16:15:14 +00001364 if (buffer == NULL)
1365 return NULL;
1366
1367 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1368 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001369 return NULL;
1370 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001371 result = PyLong_FromString(buffer, NULL, base);
1372 PyMem_FREE(buffer);
1373 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001374}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001375#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001376
Tim Peters9f688bf2000-07-07 15:53:28 +00001377/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001378static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001379 (PyLongObject *, PyLongObject *, PyLongObject **);
1380static PyObject *long_pos(PyLongObject *);
1381static int long_divrem(PyLongObject *, PyLongObject *,
1382 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001383
1384/* Long division with remainder, top-level routine */
1385
Guido van Rossume32e0141992-01-19 16:31:05 +00001386static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001387long_divrem(PyLongObject *a, PyLongObject *b,
1388 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001389{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001390 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001391 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001392
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001393 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001394 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001395 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001396 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001397 }
1398 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001399 (size_a == size_b &&
1400 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001401 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 *pdiv = _PyLong_New(0);
1403 Py_INCREF(a);
1404 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001405 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001406 }
1407 if (size_b == 1) {
1408 digit rem = 0;
1409 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001410 if (z == NULL)
1411 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001413 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001414 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001415 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001416 if (z == NULL)
1417 return -1;
1418 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001419 /* Set the signs.
1420 The quotient z has the sign of a*b;
1421 the remainder r has the sign of a,
1422 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001423 if ((a->ob_size < 0) != (b->ob_size < 0))
1424 z->ob_size = -(z->ob_size);
1425 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1426 (*prem)->ob_size = -((*prem)->ob_size);
1427 *pdiv = z;
1428 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001429}
1430
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001431/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001432
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001433static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001434x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001435{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001436 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001437 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 PyLongObject *v = mul1(v1, d);
1439 PyLongObject *w = mul1(w1, d);
1440 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001441 int j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001442
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001443 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 Py_XDECREF(v);
1445 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001446 return NULL;
1447 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001448
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001449 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001450 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001451 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001452
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001453 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001455
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001456 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1457 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1458 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001459 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001460 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001461
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001462 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001464 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001465 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001466 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001467 if (vj == w->ob_digit[size_w-1])
1468 q = MASK;
1469 else
1470 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1471 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001472
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001473 while (w->ob_digit[size_w-2]*q >
1474 ((
1475 ((twodigits)vj << SHIFT)
1476 + v->ob_digit[j-1]
1477 - q*w->ob_digit[size_w-1]
1478 ) << SHIFT)
1479 + v->ob_digit[j-2])
1480 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001481
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001482 for (i = 0; i < size_w && i+k < size_v; ++i) {
1483 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001484 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001485 carry += v->ob_digit[i+k] - z
1486 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001487 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001488 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1489 carry, SHIFT);
1490 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001491 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001492
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001493 if (i+k < size_v) {
1494 carry += v->ob_digit[i+k];
1495 v->ob_digit[i+k] = 0;
1496 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001497
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001498 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001499 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001500 else {
1501 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001502 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001503 carry = 0;
1504 for (i = 0; i < size_w && i+k < size_v; ++i) {
1505 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001506 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001507 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1508 BASE_TWODIGITS_TYPE,
1509 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001510 }
1511 }
1512 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001513
Guido van Rossumc206c761995-01-10 15:23:19 +00001514 if (a == NULL)
1515 *prem = NULL;
1516 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001517 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001518 *prem = divrem1(v, d, &d);
1519 /* d receives the (unused) remainder */
1520 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001522 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001523 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001524 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001525 Py_DECREF(v);
1526 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001527 return a;
1528}
1529
1530/* Methods */
1531
1532static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001533long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001534{
Guido van Rossum9475a232001-10-05 20:51:39 +00001535 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001536}
1537
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001538static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001539long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001540{
Fred Drake121ee271999-12-23 15:41:28 +00001541 return long_format(v, 10, 1);
1542}
1543
1544static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001545long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001546{
1547 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001548}
1549
1550static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001551long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001552{
1553 int sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001554
Guido van Rossumc6913e71991-11-19 20:26:46 +00001555 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001556 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001557 sign = 0;
1558 else
1559 sign = a->ob_size - b->ob_size;
1560 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001561 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001562 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001563 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1564 ;
1565 if (i < 0)
1566 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001567 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001568 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001569 if (a->ob_size < 0)
1570 sign = -sign;
1571 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001572 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001573 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001574}
1575
Guido van Rossum9bfef441993-03-29 10:43:31 +00001576static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001577long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001578{
1579 long x;
1580 int i, sign;
1581
1582 /* This is designed so that Python ints and longs with the
1583 same value hash to the same value, otherwise comparisons
1584 of mapping keys will turn out weird */
1585 i = v->ob_size;
1586 sign = 1;
1587 x = 0;
1588 if (i < 0) {
1589 sign = -1;
1590 i = -(i);
1591 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001592#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001593 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001594 /* Force a native long #-bits (32 or 64) circular shift */
1595 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001596 x += v->ob_digit[i];
1597 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001598#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001599 x = x * sign;
1600 if (x == -1)
1601 x = -2;
1602 return x;
1603}
1604
1605
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001606/* Add the absolute values of two long integers. */
1607
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001608static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001609x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001610{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001611 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001612 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001613 int i;
1614 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001615
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001616 /* Ensure a is the larger of the two: */
1617 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001618 { PyLongObject *temp = a; a = b; b = temp; }
1619 { int size_temp = size_a;
1620 size_a = size_b;
1621 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001622 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001623 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001624 if (z == NULL)
1625 return NULL;
1626 for (i = 0; i < size_b; ++i) {
1627 carry += a->ob_digit[i] + b->ob_digit[i];
1628 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001629 carry >>= SHIFT;
1630 }
1631 for (; i < size_a; ++i) {
1632 carry += a->ob_digit[i];
1633 z->ob_digit[i] = carry & MASK;
1634 carry >>= SHIFT;
1635 }
1636 z->ob_digit[i] = carry;
1637 return long_normalize(z);
1638}
1639
1640/* Subtract the absolute values of two integers. */
1641
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001642static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001643x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001644{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001645 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001647 int i;
1648 int sign = 1;
1649 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001650
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001651 /* Ensure a is the larger of the two: */
1652 if (size_a < size_b) {
1653 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001654 { PyLongObject *temp = a; a = b; b = temp; }
1655 { int size_temp = size_a;
1656 size_a = size_b;
1657 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001658 }
1659 else if (size_a == size_b) {
1660 /* Find highest digit where a and b differ: */
1661 i = size_a;
1662 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1663 ;
1664 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001665 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001666 if (a->ob_digit[i] < b->ob_digit[i]) {
1667 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001668 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001669 }
1670 size_a = size_b = i+1;
1671 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001672 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001673 if (z == NULL)
1674 return NULL;
1675 for (i = 0; i < size_b; ++i) {
1676 /* The following assumes unsigned arithmetic
1677 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001678 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001679 z->ob_digit[i] = borrow & MASK;
1680 borrow >>= SHIFT;
1681 borrow &= 1; /* Keep only one sign bit */
1682 }
1683 for (; i < size_a; ++i) {
1684 borrow = a->ob_digit[i] - borrow;
1685 z->ob_digit[i] = borrow & MASK;
1686 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001687 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001688 }
1689 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001690 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001691 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001692 return long_normalize(z);
1693}
1694
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001695static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001696long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001697{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001698 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001699
Neil Schemenauerba872e22001-01-04 01:46:03 +00001700 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1701
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001702 if (a->ob_size < 0) {
1703 if (b->ob_size < 0) {
1704 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001705 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001706 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001707 }
1708 else
1709 z = x_sub(b, a);
1710 }
1711 else {
1712 if (b->ob_size < 0)
1713 z = x_sub(a, b);
1714 else
1715 z = x_add(a, b);
1716 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001717 Py_DECREF(a);
1718 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001719 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001720}
1721
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001722static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001723long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001724{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001725 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001726
Neil Schemenauerba872e22001-01-04 01:46:03 +00001727 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1728
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001729 if (a->ob_size < 0) {
1730 if (b->ob_size < 0)
1731 z = x_sub(a, b);
1732 else
1733 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001734 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001735 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736 }
1737 else {
1738 if (b->ob_size < 0)
1739 z = x_add(a, b);
1740 else
1741 z = x_sub(a, b);
1742 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001743 Py_DECREF(a);
1744 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001745 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001746}
1747
Tim Peters5af4e6c2002-08-12 02:31:19 +00001748/* Grade school multiplication, ignoring the signs.
1749 * Returns the absolute value of the product, or NULL if error.
1750 */
1751static PyLongObject *
1752x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001753{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001754 PyLongObject *z;
1755 int size_a = ABS(a->ob_size);
1756 int size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001757 int i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001758
Tim Peters5af4e6c2002-08-12 02:31:19 +00001759 z = _PyLong_New(size_a + size_b);
1760 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001761 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001762
1763 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001764 if (a == b) {
1765 /* Efficient squaring per HAC, Algorithm 14.16:
1766 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1767 * Gives slightly less than a 2x speedup when a == b,
1768 * via exploiting that each entry in the multiplication
1769 * pyramid appears twice (except for the size_a squares).
1770 */
1771 for (i = 0; i < size_a; ++i) {
1772 twodigits carry;
1773 twodigits f = a->ob_digit[i];
1774 digit *pz = z->ob_digit + (i << 1);
1775 digit *pa = a->ob_digit + i + 1;
1776 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001777
Tim Peters0973b992004-08-29 22:16:50 +00001778 SIGCHECK({
1779 Py_DECREF(z);
1780 return NULL;
1781 })
1782
1783 carry = *pz + f * f;
1784 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001785 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001786 assert(carry <= MASK);
1787
1788 /* Now f is added in twice in each column of the
1789 * pyramid it appears. Same as adding f<<1 once.
1790 */
1791 f <<= 1;
1792 while (pa < paend) {
1793 carry += *pz + *pa++ * f;
1794 *pz++ = (digit)(carry & MASK);
1795 carry >>= SHIFT;
1796 assert(carry <= (MASK << 1));
1797 }
1798 if (carry) {
1799 carry += *pz;
1800 *pz++ = (digit)(carry & MASK);
1801 carry >>= SHIFT;
1802 }
1803 if (carry)
1804 *pz += (digit)(carry & MASK);
1805 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001806 }
Tim Peters0973b992004-08-29 22:16:50 +00001807 }
1808 else { /* a is not the same as b -- gradeschool long mult */
1809 for (i = 0; i < size_a; ++i) {
1810 twodigits carry = 0;
1811 twodigits f = a->ob_digit[i];
1812 digit *pz = z->ob_digit + i;
1813 digit *pb = b->ob_digit;
1814 digit *pbend = b->ob_digit + size_b;
1815
1816 SIGCHECK({
1817 Py_DECREF(z);
1818 return NULL;
1819 })
1820
1821 while (pb < pbend) {
1822 carry += *pz + *pb++ * f;
1823 *pz++ = (digit)(carry & MASK);
1824 carry >>= SHIFT;
1825 assert(carry <= MASK);
1826 }
1827 if (carry)
1828 *pz += (digit)(carry & MASK);
1829 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001830 }
1831 }
Tim Peters44121a62002-08-12 06:17:58 +00001832 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001833}
1834
1835/* A helper for Karatsuba multiplication (k_mul).
1836 Takes a long "n" and an integer "size" representing the place to
1837 split, and sets low and high such that abs(n) == (high << size) + low,
1838 viewing the shift as being by digits. The sign bit is ignored, and
1839 the return values are >= 0.
1840 Returns 0 on success, -1 on failure.
1841*/
1842static int
1843kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low)
1844{
1845 PyLongObject *hi, *lo;
1846 int size_lo, size_hi;
1847 const int size_n = ABS(n->ob_size);
1848
1849 size_lo = MIN(size_n, size);
1850 size_hi = size_n - size_lo;
1851
1852 if ((hi = _PyLong_New(size_hi)) == NULL)
1853 return -1;
1854 if ((lo = _PyLong_New(size_lo)) == NULL) {
1855 Py_DECREF(hi);
1856 return -1;
1857 }
1858
1859 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1860 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1861
1862 *high = long_normalize(hi);
1863 *low = long_normalize(lo);
1864 return 0;
1865}
1866
Tim Peters60004642002-08-12 22:01:34 +00001867static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1868
Tim Peters5af4e6c2002-08-12 02:31:19 +00001869/* Karatsuba multiplication. Ignores the input signs, and returns the
1870 * absolute value of the product (or NULL if error).
1871 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1872 */
1873static PyLongObject *
1874k_mul(PyLongObject *a, PyLongObject *b)
1875{
Tim Peters738eda72002-08-12 15:08:20 +00001876 int asize = ABS(a->ob_size);
1877 int bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001878 PyLongObject *ah = NULL;
1879 PyLongObject *al = NULL;
1880 PyLongObject *bh = NULL;
1881 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001882 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00001883 PyLongObject *t1, *t2, *t3;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001884 int shift; /* the number of digits we split off */
1885 int i;
Tim Peters738eda72002-08-12 15:08:20 +00001886
Tim Peters5af4e6c2002-08-12 02:31:19 +00001887 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
1888 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
1889 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00001890 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00001891 * By picking X to be a power of 2, "*X" is just shifting, and it's
1892 * been reduced to 3 multiplies on numbers half the size.
1893 */
1894
Tim Petersfc07e562002-08-12 02:54:10 +00001895 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00001896 * is largest.
1897 */
Tim Peters738eda72002-08-12 15:08:20 +00001898 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001899 t1 = a;
1900 a = b;
1901 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00001902
1903 i = asize;
1904 asize = bsize;
1905 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001906 }
1907
1908 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00001909 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1910 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00001911 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00001912 return _PyLong_New(0);
1913 else
1914 return x_mul(a, b);
1915 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001916
Tim Peters60004642002-08-12 22:01:34 +00001917 /* If a is small compared to b, splitting on b gives a degenerate
1918 * case with ah==0, and Karatsuba may be (even much) less efficient
1919 * than "grade school" then. However, we can still win, by viewing
1920 * b as a string of "big digits", each of width a->ob_size. That
1921 * leads to a sequence of balanced calls to k_mul.
1922 */
1923 if (2 * asize <= bsize)
1924 return k_lopsided_mul(a, b);
1925
Tim Petersd6974a52002-08-13 20:37:51 +00001926 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00001927 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001928 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00001929 assert(ah->ob_size > 0); /* the split isn't degenerate */
1930
Tim Peters0973b992004-08-29 22:16:50 +00001931 if (a == b) {
1932 bh = ah;
1933 bl = al;
1934 Py_INCREF(bh);
1935 Py_INCREF(bl);
1936 }
1937 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001938
Tim Petersd64c1de2002-08-12 17:36:03 +00001939 /* The plan:
1940 * 1. Allocate result space (asize + bsize digits: that's always
1941 * enough).
1942 * 2. Compute ah*bh, and copy into result at 2*shift.
1943 * 3. Compute al*bl, and copy into result at 0. Note that this
1944 * can't overlap with #2.
1945 * 4. Subtract al*bl from the result, starting at shift. This may
1946 * underflow (borrow out of the high digit), but we don't care:
1947 * we're effectively doing unsigned arithmetic mod
1948 * BASE**(sizea + sizeb), and so long as the *final* result fits,
1949 * borrows and carries out of the high digit can be ignored.
1950 * 5. Subtract ah*bh from the result, starting at shift.
1951 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
1952 * at shift.
1953 */
1954
1955 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00001956 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001957 if (ret == NULL) goto fail;
1958#ifdef Py_DEBUG
1959 /* Fill with trash, to catch reference to uninitialized digits. */
1960 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
1961#endif
Tim Peters44121a62002-08-12 06:17:58 +00001962
Tim Petersd64c1de2002-08-12 17:36:03 +00001963 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00001964 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
1965 assert(t1->ob_size >= 0);
1966 assert(2*shift + t1->ob_size <= ret->ob_size);
1967 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
1968 t1->ob_size * sizeof(digit));
1969
1970 /* Zero-out the digits higher than the ah*bh copy. */
1971 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00001972 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001973 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00001974 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001975
Tim Petersd64c1de2002-08-12 17:36:03 +00001976 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001977 if ((t2 = k_mul(al, bl)) == NULL) {
1978 Py_DECREF(t1);
1979 goto fail;
1980 }
1981 assert(t2->ob_size >= 0);
1982 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
1983 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001984
1985 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00001986 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001987 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00001988 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001989
Tim Petersd64c1de2002-08-12 17:36:03 +00001990 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
1991 * because it's fresher in cache.
1992 */
Tim Peters738eda72002-08-12 15:08:20 +00001993 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00001994 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001995 Py_DECREF(t2);
1996
Tim Petersd64c1de2002-08-12 17:36:03 +00001997 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00001998 Py_DECREF(t1);
1999
Tim Petersd64c1de2002-08-12 17:36:03 +00002000 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002001 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2002 Py_DECREF(ah);
2003 Py_DECREF(al);
2004 ah = al = NULL;
2005
Tim Peters0973b992004-08-29 22:16:50 +00002006 if (a == b) {
2007 t2 = t1;
2008 Py_INCREF(t2);
2009 }
2010 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002011 Py_DECREF(t1);
2012 goto fail;
2013 }
2014 Py_DECREF(bh);
2015 Py_DECREF(bl);
2016 bh = bl = NULL;
2017
Tim Peters738eda72002-08-12 15:08:20 +00002018 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002019 Py_DECREF(t1);
2020 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002021 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002022 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002023
Tim Petersd6974a52002-08-13 20:37:51 +00002024 /* Add t3. It's not obvious why we can't run out of room here.
2025 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002026 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002027 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002028 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002029
Tim Peters5af4e6c2002-08-12 02:31:19 +00002030 return long_normalize(ret);
2031
2032 fail:
2033 Py_XDECREF(ret);
2034 Py_XDECREF(ah);
2035 Py_XDECREF(al);
2036 Py_XDECREF(bh);
2037 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002038 return NULL;
2039}
2040
Tim Petersd6974a52002-08-13 20:37:51 +00002041/* (*) Why adding t3 can't "run out of room" above.
2042
Tim Petersab86c2b2002-08-15 20:06:00 +00002043Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2044to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002045
Tim Petersab86c2b2002-08-15 20:06:00 +000020461. For any integer i, i = c(i/2) + f(i/2). In particular,
2047 bsize = c(bsize/2) + f(bsize/2).
20482. shift = f(bsize/2)
20493. asize <= bsize
20504. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2051 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002052
Tim Petersab86c2b2002-08-15 20:06:00 +00002053We allocated asize + bsize result digits, and add t3 into them at an offset
2054of shift. This leaves asize+bsize-shift allocated digit positions for t3
2055to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2056asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002057
Tim Petersab86c2b2002-08-15 20:06:00 +00002058bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2059at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002060
Tim Petersab86c2b2002-08-15 20:06:00 +00002061If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2062digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2063most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002064
Tim Petersab86c2b2002-08-15 20:06:00 +00002065The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002066
Tim Petersab86c2b2002-08-15 20:06:00 +00002067 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002068
Tim Petersab86c2b2002-08-15 20:06:00 +00002069and we have asize + c(bsize/2) available digit positions. We need to show
2070this is always enough. An instance of c(bsize/2) cancels out in both, so
2071the question reduces to whether asize digits is enough to hold
2072(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2073then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2074asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2075digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2076asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002077c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2078is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2079bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002080
Tim Peters48d52c02002-08-14 17:07:32 +00002081Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2082clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2083ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002084*/
2085
Tim Peters60004642002-08-12 22:01:34 +00002086/* b has at least twice the digits of a, and a is big enough that Karatsuba
2087 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2088 * of slices, each with a->ob_size digits, and multiply the slices by a,
2089 * one at a time. This gives k_mul balanced inputs to work with, and is
2090 * also cache-friendly (we compute one double-width slice of the result
2091 * at a time, then move on, never bactracking except for the helpful
2092 * single-width slice overlap between successive partial sums).
2093 */
2094static PyLongObject *
2095k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2096{
2097 const int asize = ABS(a->ob_size);
2098 int bsize = ABS(b->ob_size);
2099 int nbdone; /* # of b digits already multiplied */
2100 PyLongObject *ret;
2101 PyLongObject *bslice = NULL;
2102
2103 assert(asize > KARATSUBA_CUTOFF);
2104 assert(2 * asize <= bsize);
2105
2106 /* Allocate result space, and zero it out. */
2107 ret = _PyLong_New(asize + bsize);
2108 if (ret == NULL)
2109 return NULL;
2110 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2111
2112 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002113 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002114 if (bslice == NULL)
2115 goto fail;
2116
2117 nbdone = 0;
2118 while (bsize > 0) {
2119 PyLongObject *product;
2120 const int nbtouse = MIN(bsize, asize);
2121
2122 /* Multiply the next slice of b by a. */
2123 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2124 nbtouse * sizeof(digit));
2125 bslice->ob_size = nbtouse;
2126 product = k_mul(a, bslice);
2127 if (product == NULL)
2128 goto fail;
2129
2130 /* Add into result. */
2131 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2132 product->ob_digit, product->ob_size);
2133 Py_DECREF(product);
2134
2135 bsize -= nbtouse;
2136 nbdone += nbtouse;
2137 }
2138
2139 Py_DECREF(bslice);
2140 return long_normalize(ret);
2141
2142 fail:
2143 Py_DECREF(ret);
2144 Py_XDECREF(bslice);
2145 return NULL;
2146}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002147
2148static PyObject *
2149long_mul(PyLongObject *v, PyLongObject *w)
2150{
2151 PyLongObject *a, *b, *z;
2152
2153 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002154 Py_INCREF(Py_NotImplemented);
2155 return Py_NotImplemented;
2156 }
2157
Tim Petersd64c1de2002-08-12 17:36:03 +00002158 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002159 /* Negate if exactly one of the inputs is negative. */
2160 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002161 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002162 Py_DECREF(a);
2163 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002164 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002165}
2166
Guido van Rossume32e0141992-01-19 16:31:05 +00002167/* The / and % operators are now defined in terms of divmod().
2168 The expression a mod b has the value a - b*floor(a/b).
2169 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002170 |a| by |b|, with the sign of a. This is also expressed
2171 as a - b*trunc(a/b), if trunc truncates towards zero.
2172 Some examples:
2173 a b a rem b a mod b
2174 13 10 3 3
2175 -13 10 -3 7
2176 13 -10 3 -7
2177 -13 -10 -3 -3
2178 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002179 have different signs. We then subtract one from the 'div'
2180 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002181
Tim Peters47e52ee2004-08-30 02:44:38 +00002182/* Compute
2183 * *pdiv, *pmod = divmod(v, w)
2184 * NULL can be passed for pdiv or pmod, in which case that part of
2185 * the result is simply thrown away. The caller owns a reference to
2186 * each of these it requests (does not pass NULL for).
2187 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002188static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002189l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002190 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002191{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002192 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002193
Guido van Rossume32e0141992-01-19 16:31:05 +00002194 if (long_divrem(v, w, &div, &mod) < 0)
2195 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002196 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2197 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002198 PyLongObject *temp;
2199 PyLongObject *one;
2200 temp = (PyLongObject *) long_add(mod, w);
2201 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002202 mod = temp;
2203 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002204 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002205 return -1;
2206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002208 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2210 Py_DECREF(mod);
2211 Py_DECREF(div);
2212 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002213 return -1;
2214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002215 Py_DECREF(one);
2216 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002217 div = temp;
2218 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002219 if (pdiv != NULL)
2220 *pdiv = div;
2221 else
2222 Py_DECREF(div);
2223
2224 if (pmod != NULL)
2225 *pmod = mod;
2226 else
2227 Py_DECREF(mod);
2228
Guido van Rossume32e0141992-01-19 16:31:05 +00002229 return 0;
2230}
2231
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002232static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002233long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002234{
Tim Peters47e52ee2004-08-30 02:44:38 +00002235 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002236
2237 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002238 if (l_divmod(a, b, &div, NULL) < 0)
2239 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002240 Py_DECREF(a);
2241 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002242 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002243}
2244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002245static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002246long_classic_div(PyObject *v, PyObject *w)
2247{
Tim Peters47e52ee2004-08-30 02:44:38 +00002248 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002249
2250 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002251 if (Py_DivisionWarningFlag &&
2252 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2253 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002254 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002255 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002256 Py_DECREF(a);
2257 Py_DECREF(b);
2258 return (PyObject *)div;
2259}
2260
2261static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002262long_true_divide(PyObject *v, PyObject *w)
2263{
Tim Peterse2a60002001-09-04 06:17:36 +00002264 PyLongObject *a, *b;
2265 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002266 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002267
2268 CONVERT_BINOP(v, w, &a, &b);
2269 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2270 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002271 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2272 Py_DECREF(a);
2273 Py_DECREF(b);
2274 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002275 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002276 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2277 but should really be set correctly after sucessful calls to
2278 _PyLong_AsScaledDouble() */
2279 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002280
2281 if (bd == 0.0) {
2282 PyErr_SetString(PyExc_ZeroDivisionError,
2283 "long division or modulo by zero");
2284 return NULL;
2285 }
2286
2287 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2288 ad /= bd; /* overflow/underflow impossible here */
2289 aexp -= bexp;
2290 if (aexp > INT_MAX / SHIFT)
2291 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002292 else if (aexp < -(INT_MAX / SHIFT))
2293 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002294 errno = 0;
2295 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002296 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002297 goto overflow;
2298 return PyFloat_FromDouble(ad);
2299
2300overflow:
2301 PyErr_SetString(PyExc_OverflowError,
2302 "long/long too large for a float");
2303 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002304
Tim Peters20dab9f2001-09-04 05:31:47 +00002305}
2306
2307static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002308long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002309{
Tim Peters47e52ee2004-08-30 02:44:38 +00002310 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002311
2312 CONVERT_BINOP(v, w, &a, &b);
2313
Tim Peters47e52ee2004-08-30 02:44:38 +00002314 if (l_divmod(a, b, NULL, &mod) < 0)
2315 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002316 Py_DECREF(a);
2317 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002318 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002319}
2320
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002321static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002322long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002323{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002324 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002325 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002326
2327 CONVERT_BINOP(v, w, &a, &b);
2328
2329 if (l_divmod(a, b, &div, &mod) < 0) {
2330 Py_DECREF(a);
2331 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002332 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002333 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002334 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002335 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002336 PyTuple_SetItem(z, 0, (PyObject *) div);
2337 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002338 }
2339 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002340 Py_DECREF(div);
2341 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002342 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002343 Py_DECREF(a);
2344 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002345 return z;
2346}
2347
Tim Peters47e52ee2004-08-30 02:44:38 +00002348/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002349static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002350long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002351{
Tim Peters47e52ee2004-08-30 02:44:38 +00002352 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2353 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002354
Tim Peters47e52ee2004-08-30 02:44:38 +00002355 PyLongObject *z = NULL; /* accumulated result */
2356 int i, j, k; /* counters */
2357 PyLongObject *temp = NULL;
2358
2359 /* 5-ary values. If the exponent is large enough, table is
2360 * precomputed so that table[i] == a**i % c for i in range(32).
2361 */
2362 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2363 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2364
2365 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002366 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002367 if (PyLong_Check(x)) {
2368 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002369 Py_INCREF(x);
2370 }
Tim Petersde7990b2005-07-17 23:45:23 +00002371 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002372 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002373 if (c == NULL)
2374 goto Error;
2375 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002376 else if (x == Py_None)
2377 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002378 else {
2379 Py_DECREF(a);
2380 Py_DECREF(b);
2381 Py_INCREF(Py_NotImplemented);
2382 return Py_NotImplemented;
2383 }
Tim Peters4c483c42001-09-05 06:24:58 +00002384
Tim Peters47e52ee2004-08-30 02:44:38 +00002385 if (b->ob_size < 0) { /* if exponent is negative */
2386 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002387 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002388 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002389 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002390 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002391 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002392 /* else return a float. This works because we know
2393 that this calls float_pow() which converts its
2394 arguments to double. */
2395 Py_DECREF(a);
2396 Py_DECREF(b);
2397 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002398 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002399 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002400
2401 if (c) {
2402 /* if modulus == 0:
2403 raise ValueError() */
2404 if (c->ob_size == 0) {
2405 PyErr_SetString(PyExc_ValueError,
2406 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002407 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002408 }
2409
2410 /* if modulus < 0:
2411 negativeOutput = True
2412 modulus = -modulus */
2413 if (c->ob_size < 0) {
2414 negativeOutput = 1;
2415 temp = (PyLongObject *)_PyLong_Copy(c);
2416 if (temp == NULL)
2417 goto Error;
2418 Py_DECREF(c);
2419 c = temp;
2420 temp = NULL;
2421 c->ob_size = - c->ob_size;
2422 }
2423
2424 /* if modulus == 1:
2425 return 0 */
2426 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2427 z = (PyLongObject *)PyLong_FromLong(0L);
2428 goto Done;
2429 }
2430
2431 /* if base < 0:
2432 base = base % modulus
2433 Having the base positive just makes things easier. */
2434 if (a->ob_size < 0) {
2435 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002436 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002437 Py_DECREF(a);
2438 a = temp;
2439 temp = NULL;
2440 }
2441 }
2442
2443 /* At this point a, b, and c are guaranteed non-negative UNLESS
2444 c is NULL, in which case a may be negative. */
2445
2446 z = (PyLongObject *)PyLong_FromLong(1L);
2447 if (z == NULL)
2448 goto Error;
2449
2450 /* Perform a modular reduction, X = X % c, but leave X alone if c
2451 * is NULL.
2452 */
2453#define REDUCE(X) \
2454 if (c != NULL) { \
2455 if (l_divmod(X, c, NULL, &temp) < 0) \
2456 goto Error; \
2457 Py_XDECREF(X); \
2458 X = temp; \
2459 temp = NULL; \
2460 }
2461
2462 /* Multiply two values, then reduce the result:
2463 result = X*Y % c. If c is NULL, skip the mod. */
2464#define MULT(X, Y, result) \
2465{ \
2466 temp = (PyLongObject *)long_mul(X, Y); \
2467 if (temp == NULL) \
2468 goto Error; \
2469 Py_XDECREF(result); \
2470 result = temp; \
2471 temp = NULL; \
2472 REDUCE(result) \
2473}
2474
2475 if (b->ob_size <= FIVEARY_CUTOFF) {
2476 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2477 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2478 for (i = b->ob_size - 1; i >= 0; --i) {
2479 digit bi = b->ob_digit[i];
2480
2481 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2482 MULT(z, z, z)
2483 if (bi & j)
2484 MULT(z, a, z)
2485 }
2486 }
2487 }
2488 else {
2489 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2490 Py_INCREF(z); /* still holds 1L */
2491 table[0] = z;
2492 for (i = 1; i < 32; ++i)
2493 MULT(table[i-1], a, table[i])
2494
2495 for (i = b->ob_size - 1; i >= 0; --i) {
2496 const digit bi = b->ob_digit[i];
2497
2498 for (j = SHIFT - 5; j >= 0; j -= 5) {
2499 const int index = (bi >> j) & 0x1f;
2500 for (k = 0; k < 5; ++k)
2501 MULT(z, z, z)
2502 if (index)
2503 MULT(z, table[index], z)
2504 }
2505 }
2506 }
2507
2508 if (negativeOutput && (z->ob_size != 0)) {
2509 temp = (PyLongObject *)long_sub(z, c);
2510 if (temp == NULL)
2511 goto Error;
2512 Py_DECREF(z);
2513 z = temp;
2514 temp = NULL;
2515 }
2516 goto Done;
2517
2518 Error:
2519 if (z != NULL) {
2520 Py_DECREF(z);
2521 z = NULL;
2522 }
2523 /* fall through */
2524 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002525 if (b->ob_size > FIVEARY_CUTOFF) {
2526 for (i = 0; i < 32; ++i)
2527 Py_XDECREF(table[i]);
2528 }
Tim Petersde7990b2005-07-17 23:45:23 +00002529 Py_DECREF(a);
2530 Py_DECREF(b);
2531 Py_XDECREF(c);
2532 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002533 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002534}
2535
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002536static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002537long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002538{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002539 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002540 PyLongObject *x;
2541 PyLongObject *w;
2542 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002543 if (w == NULL)
2544 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002545 x = (PyLongObject *) long_add(v, w);
2546 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002547 if (x == NULL)
2548 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002549 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002550 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002551}
2552
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002553static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002554long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002555{
Tim Peters69c2de32001-09-11 22:31:33 +00002556 if (PyLong_CheckExact(v)) {
2557 Py_INCREF(v);
2558 return (PyObject *)v;
2559 }
2560 else
2561 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002562}
2563
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002564static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002565long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002566{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002567 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002568 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002569 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002570 Py_INCREF(v);
2571 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002572 }
Tim Peters69c2de32001-09-11 22:31:33 +00002573 z = (PyLongObject *)_PyLong_Copy(v);
2574 if (z != NULL)
2575 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002576 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002577}
2578
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002579static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002580long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002581{
2582 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002583 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002584 else
2585 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002586}
2587
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002588static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002589long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002590{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002591 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002592}
2593
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002594static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002595long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002596{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002597 PyLongObject *a, *b;
2598 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002599 long shiftby;
2600 int newsize, wordshift, loshift, hishift, i, j;
2601 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002602
Neil Schemenauerba872e22001-01-04 01:46:03 +00002603 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2604
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002605 if (a->ob_size < 0) {
2606 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002607 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002608 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002609 if (a1 == NULL)
2610 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002611 a2 = (PyLongObject *) long_rshift(a1, b);
2612 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002613 if (a2 == NULL)
2614 goto rshift_error;
2615 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002616 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002617 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002618 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002619
Neil Schemenauerba872e22001-01-04 01:46:03 +00002620 shiftby = PyLong_AsLong((PyObject *)b);
2621 if (shiftby == -1L && PyErr_Occurred())
2622 goto rshift_error;
2623 if (shiftby < 0) {
2624 PyErr_SetString(PyExc_ValueError,
2625 "negative shift count");
2626 goto rshift_error;
2627 }
2628 wordshift = shiftby / SHIFT;
2629 newsize = ABS(a->ob_size) - wordshift;
2630 if (newsize <= 0) {
2631 z = _PyLong_New(0);
2632 Py_DECREF(a);
2633 Py_DECREF(b);
2634 return (PyObject *)z;
2635 }
2636 loshift = shiftby % SHIFT;
2637 hishift = SHIFT - loshift;
2638 lomask = ((digit)1 << hishift) - 1;
2639 himask = MASK ^ lomask;
2640 z = _PyLong_New(newsize);
2641 if (z == NULL)
2642 goto rshift_error;
2643 if (a->ob_size < 0)
2644 z->ob_size = -(z->ob_size);
2645 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2646 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2647 if (i+1 < newsize)
2648 z->ob_digit[i] |=
2649 (a->ob_digit[j+1] << hishift) & himask;
2650 }
2651 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002652 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002653rshift_error:
2654 Py_DECREF(a);
2655 Py_DECREF(b);
2656 return (PyObject *) z;
2657
Guido van Rossumc6913e71991-11-19 20:26:46 +00002658}
2659
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002660static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002661long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002662{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002663 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002664 PyLongObject *a, *b;
2665 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002666 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002667 int oldsize, newsize, wordshift, remshift, i, j;
2668 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002669
Neil Schemenauerba872e22001-01-04 01:46:03 +00002670 CONVERT_BINOP(v, w, &a, &b);
2671
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002672 shiftby = PyLong_AsLong((PyObject *)b);
2673 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002674 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002675 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002676 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002677 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002678 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002679 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002680 PyErr_SetString(PyExc_ValueError,
2681 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002682 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002683 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002684 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2685 wordshift = (int)shiftby / SHIFT;
2686 remshift = (int)shiftby - wordshift * SHIFT;
2687
2688 oldsize = ABS(a->ob_size);
2689 newsize = oldsize + wordshift;
2690 if (remshift)
2691 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002692 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002693 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002694 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002695 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002696 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002697 for (i = 0; i < wordshift; i++)
2698 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002699 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002700 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002701 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002702 z->ob_digit[i] = (digit)(accum & MASK);
2703 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002704 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002705 if (remshift)
2706 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002707 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002708 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002709 z = long_normalize(z);
2710lshift_error:
2711 Py_DECREF(a);
2712 Py_DECREF(b);
2713 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002714}
2715
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002716
2717/* Bitwise and/xor/or operations */
2718
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002719static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002720long_bitwise(PyLongObject *a,
2721 int op, /* '&', '|', '^' */
2722 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002723{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002724 digit maska, maskb; /* 0 or MASK */
2725 int negz;
2726 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002727 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002728 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002729 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002730 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002731
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002732 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002733 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002734 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002735 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002736 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002737 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002738 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002739 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002740 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002741 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002742 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002743 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002744 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002745 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002746 maskb = 0;
2747 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002748
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002749 negz = 0;
2750 switch (op) {
2751 case '^':
2752 if (maska != maskb) {
2753 maska ^= MASK;
2754 negz = -1;
2755 }
2756 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002757 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002758 if (maska && maskb) {
2759 op = '|';
2760 maska ^= MASK;
2761 maskb ^= MASK;
2762 negz = -1;
2763 }
2764 break;
2765 case '|':
2766 if (maska || maskb) {
2767 op = '&';
2768 maska ^= MASK;
2769 maskb ^= MASK;
2770 negz = -1;
2771 }
2772 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002773 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002774
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002775 /* JRH: The original logic here was to allocate the result value (z)
2776 as the longer of the two operands. However, there are some cases
2777 where the result is guaranteed to be shorter than that: AND of two
2778 positives, OR of two negatives: use the shorter number. AND with
2779 mixed signs: use the positive number. OR with mixed signs: use the
2780 negative number. After the transformations above, op will be '&'
2781 iff one of these cases applies, and mask will be non-0 for operands
2782 whose length should be ignored.
2783 */
2784
2785 size_a = a->ob_size;
2786 size_b = b->ob_size;
2787 size_z = op == '&'
2788 ? (maska
2789 ? size_b
2790 : (maskb ? size_a : MIN(size_a, size_b)))
2791 : MAX(size_a, size_b);
2792 z = _PyLong_New(size_z);
2793 if (a == NULL || b == NULL || z == NULL) {
2794 Py_XDECREF(a);
2795 Py_XDECREF(b);
2796 Py_XDECREF(z);
2797 return NULL;
2798 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002799
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002800 for (i = 0; i < size_z; ++i) {
2801 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2802 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2803 switch (op) {
2804 case '&': z->ob_digit[i] = diga & digb; break;
2805 case '|': z->ob_digit[i] = diga | digb; break;
2806 case '^': z->ob_digit[i] = diga ^ digb; break;
2807 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002808 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002809
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002810 Py_DECREF(a);
2811 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002812 z = long_normalize(z);
2813 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002814 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002815 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002816 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002817 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002818}
2819
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002820static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002821long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002822{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002823 PyLongObject *a, *b;
2824 PyObject *c;
2825 CONVERT_BINOP(v, w, &a, &b);
2826 c = long_bitwise(a, '&', b);
2827 Py_DECREF(a);
2828 Py_DECREF(b);
2829 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002830}
2831
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002832static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002833long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002834{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002835 PyLongObject *a, *b;
2836 PyObject *c;
2837 CONVERT_BINOP(v, w, &a, &b);
2838 c = long_bitwise(a, '^', b);
2839 Py_DECREF(a);
2840 Py_DECREF(b);
2841 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002842}
2843
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002844static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002845long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002846{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002847 PyLongObject *a, *b;
2848 PyObject *c;
2849 CONVERT_BINOP(v, w, &a, &b);
2850 c = long_bitwise(a, '|', b);
2851 Py_DECREF(a);
2852 Py_DECREF(b);
2853 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002854}
2855
Guido van Rossum234f9421993-06-17 12:35:49 +00002856static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002857long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002858{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002859 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002860 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002861 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002862 return 0;
2863 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002864 else if (PyLong_Check(*pw)) {
2865 Py_INCREF(*pv);
2866 Py_INCREF(*pw);
2867 return 0;
2868 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002869 return 1; /* Can't do it */
2870}
2871
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002872static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002873long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002874{
Brett Cannonc3647ac2005-04-26 03:45:26 +00002875 if (PyLong_CheckExact(v))
2876 Py_INCREF(v);
2877 else
2878 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002879 return v;
2880}
2881
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002882static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00002883long_int(PyObject *v)
2884{
2885 long x;
2886 x = PyLong_AsLong(v);
2887 if (PyErr_Occurred()) {
2888 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
2889 PyErr_Clear();
2890 if (PyLong_CheckExact(v)) {
2891 Py_INCREF(v);
2892 return v;
2893 }
2894 else
2895 return _PyLong_Copy((PyLongObject *)v);
2896 }
2897 else
2898 return NULL;
2899 }
2900 return PyInt_FromLong(x);
2901}
2902
2903static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002904long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002905{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00002906 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002907 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00002908 if (result == -1.0 && PyErr_Occurred())
2909 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002910 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002911}
2912
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002913static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002914long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002915{
Fred Drake121ee271999-12-23 15:41:28 +00002916 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002917}
2918
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002919static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002920long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002921{
Fred Drake121ee271999-12-23 15:41:28 +00002922 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002923}
Jeremy Hylton938ace62002-07-17 16:30:39 +00002924
2925static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00002926long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002927
Tim Peters6d6c1a32001-08-02 04:15:00 +00002928static PyObject *
2929long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2930{
2931 PyObject *x = NULL;
2932 int base = -909; /* unlikely! */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002933 static const char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002934
Guido van Rossumbef14172001-08-29 15:47:46 +00002935 if (type != &PyLong_Type)
2936 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002937 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
2938 &x, &base))
2939 return NULL;
2940 if (x == NULL)
2941 return PyLong_FromLong(0L);
2942 if (base == -909)
2943 return PyNumber_Long(x);
2944 else if (PyString_Check(x))
2945 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002946#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002947 else if (PyUnicode_Check(x))
2948 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
2949 PyUnicode_GET_SIZE(x),
2950 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002951#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002952 else {
2953 PyErr_SetString(PyExc_TypeError,
2954 "long() can't convert non-string with explicit base");
2955 return NULL;
2956 }
2957}
2958
Guido van Rossumbef14172001-08-29 15:47:46 +00002959/* Wimpy, slow approach to tp_new calls for subtypes of long:
2960 first create a regular long from whatever arguments we got,
2961 then allocate a subtype instance and initialize it from
2962 the regular long. The regular long is then thrown away.
2963*/
2964static PyObject *
2965long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2966{
2967 PyLongObject *tmp, *new;
2968 int i, n;
2969
2970 assert(PyType_IsSubtype(type, &PyLong_Type));
2971 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2972 if (tmp == NULL)
2973 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00002974 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00002975 n = tmp->ob_size;
2976 if (n < 0)
2977 n = -n;
2978 new = (PyLongObject *)type->tp_alloc(type, n);
Raymond Hettingerf4667932003-06-28 20:04:25 +00002979 if (new == NULL) {
2980 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00002981 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00002982 }
Guido van Rossumbef14172001-08-29 15:47:46 +00002983 assert(PyLong_Check(new));
Guido van Rossum13228a62001-08-30 15:54:44 +00002984 new->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00002985 for (i = 0; i < n; i++)
2986 new->ob_digit[i] = tmp->ob_digit[i];
2987 Py_DECREF(tmp);
2988 return (PyObject *)new;
2989}
2990
Guido van Rossum5d9113d2003-01-29 17:58:45 +00002991static PyObject *
2992long_getnewargs(PyLongObject *v)
2993{
2994 return Py_BuildValue("(N)", _PyLong_Copy(v));
2995}
2996
2997static PyMethodDef long_methods[] = {
2998 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
2999 {NULL, NULL} /* sentinel */
3000};
3001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003002PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003"long(x[, base]) -> integer\n\
3004\n\
3005Convert a string or number to a long integer, if possible. A floating\n\
3006point argument will be truncated towards zero (this does not include a\n\
3007string representation of a floating point number!) When converting a\n\
3008string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003011static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003012 (binaryfunc) long_add, /*nb_add*/
3013 (binaryfunc) long_sub, /*nb_subtract*/
3014 (binaryfunc) long_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00003015 (binaryfunc) long_classic_div, /*nb_divide*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003016 (binaryfunc) long_mod, /*nb_remainder*/
3017 (binaryfunc) long_divmod, /*nb_divmod*/
3018 (ternaryfunc) long_pow, /*nb_power*/
3019 (unaryfunc) long_neg, /*nb_negative*/
3020 (unaryfunc) long_pos, /*tp_positive*/
3021 (unaryfunc) long_abs, /*tp_absolute*/
3022 (inquiry) long_nonzero, /*tp_nonzero*/
3023 (unaryfunc) long_invert, /*nb_invert*/
3024 (binaryfunc) long_lshift, /*nb_lshift*/
3025 (binaryfunc) long_rshift, /*nb_rshift*/
3026 (binaryfunc) long_and, /*nb_and*/
3027 (binaryfunc) long_xor, /*nb_xor*/
3028 (binaryfunc) long_or, /*nb_or*/
Tim Peters9ace6bc2000-07-08 00:32:04 +00003029 (coercion) long_coerce, /*nb_coerce*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003030 (unaryfunc) long_int, /*nb_int*/
3031 (unaryfunc) long_long, /*nb_long*/
3032 (unaryfunc) long_float, /*nb_float*/
3033 (unaryfunc) long_oct, /*nb_oct*/
3034 (unaryfunc) long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003035 0, /* nb_inplace_add */
3036 0, /* nb_inplace_subtract */
3037 0, /* nb_inplace_multiply */
3038 0, /* nb_inplace_divide */
3039 0, /* nb_inplace_remainder */
3040 0, /* nb_inplace_power */
3041 0, /* nb_inplace_lshift */
3042 0, /* nb_inplace_rshift */
3043 0, /* nb_inplace_and */
3044 0, /* nb_inplace_xor */
3045 0, /* nb_inplace_or */
3046 (binaryfunc)long_div, /* nb_floor_divide */
3047 long_true_divide, /* nb_true_divide */
3048 0, /* nb_inplace_floor_divide */
3049 0, /* nb_inplace_true_divide */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003050};
3051
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003052PyTypeObject PyLong_Type = {
3053 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003054 0, /* ob_size */
3055 "long", /* tp_name */
3056 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3057 sizeof(digit), /* tp_itemsize */
3058 (destructor)long_dealloc, /* tp_dealloc */
3059 0, /* tp_print */
3060 0, /* tp_getattr */
3061 0, /* tp_setattr */
3062 (cmpfunc)long_compare, /* tp_compare */
3063 (reprfunc)long_repr, /* tp_repr */
3064 &long_as_number, /* tp_as_number */
3065 0, /* tp_as_sequence */
3066 0, /* tp_as_mapping */
3067 (hashfunc)long_hash, /* tp_hash */
3068 0, /* tp_call */
3069 (reprfunc)long_str, /* tp_str */
3070 PyObject_GenericGetAttr, /* tp_getattro */
3071 0, /* tp_setattro */
3072 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003073 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3074 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003075 long_doc, /* tp_doc */
3076 0, /* tp_traverse */
3077 0, /* tp_clear */
3078 0, /* tp_richcompare */
3079 0, /* tp_weaklistoffset */
3080 0, /* tp_iter */
3081 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003082 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003083 0, /* tp_members */
3084 0, /* tp_getset */
3085 0, /* tp_base */
3086 0, /* tp_dict */
3087 0, /* tp_descr_get */
3088 0, /* tp_descr_set */
3089 0, /* tp_dictoffset */
3090 0, /* tp_init */
3091 0, /* tp_alloc */
3092 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003093 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003094};