blob: 566e6a7547cd98a3eada9a0de87dda2da1e585e1 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/* Long (arbitrary precision) integer object implementation */
2
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003/* XXX The functional organization of this file is terrible */
4
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00006#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00008#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +00009
Tim Peters5af4e6c2002-08-12 02:31:19 +000010/* For long multiplication, use the O(N**2) school algorithm unless
11 * both operands contain more than KARATSUBA_CUTOFF digits (this
12 * being an internal Python long digit, in base BASE).
13 */
Tim Peters0973b992004-08-29 22:16:50 +000014#define KARATSUBA_CUTOFF 70
15#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000016
Tim Peters47e52ee2004-08-30 02:44:38 +000017/* For exponentiation, use the binary left-to-right algorithm
18 * unless the exponent contains more than FIVEARY_CUTOFF digits.
19 * In that case, do 5 bits at a time. The potential drawback is that
20 * a table of 2**5 intermediate results is computed.
21 */
22#define FIVEARY_CUTOFF 8
23
Guido van Rossume32e0141992-01-19 16:31:05 +000024#define ABS(x) ((x) < 0 ? -(x) : (x))
25
Tim Peters5af4e6c2002-08-12 02:31:19 +000026#undef MIN
27#undef MAX
28#define MAX(x, y) ((x) < (y) ? (y) : (x))
29#define MIN(x, y) ((x) > (y) ? (y) : (x))
30
Guido van Rossume32e0141992-01-19 16:31:05 +000031/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000032static PyLongObject *long_normalize(PyLongObject *);
33static PyLongObject *mul1(PyLongObject *, wdigit);
34static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000035static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossumd2dbecb2006-08-18 16:29:54 +000036static PyObject *long_format(PyObject *aa, int base);
Guido van Rossume32e0141992-01-19 16:31:05 +000037
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000039 if (--_Py_Ticker < 0) { \
40 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000041 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000042 }
43
Guido van Rossumedcc38a1991-05-05 20:09:44 +000044/* Normalize (remove leading zeros from) a long int object.
45 Doesn't attempt to free the storage--in most cases, due to the nature
46 of the algorithms used, this could save at most be one word anyway. */
47
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000049long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000050{
Martin v. Löwis18e16552006-02-15 17:27:45 +000051 Py_ssize_t j = ABS(v->ob_size);
52 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000053
Guido van Rossumedcc38a1991-05-05 20:09:44 +000054 while (i > 0 && v->ob_digit[i-1] == 0)
55 --i;
56 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000057 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000058 return v;
59}
60
61/* Allocate a new long int object with size digits.
62 Return NULL and set exception if we run out of memory. */
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000065_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000066{
Thomas Wouters477c8d52006-05-27 19:21:47 +000067 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 PyErr_NoMemory();
69 return NULL;
70 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000072}
73
Tim Peters64b5ce32001-09-10 20:52:51 +000074PyObject *
75_PyLong_Copy(PyLongObject *src)
76{
77 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000079
80 assert(src != NULL);
81 i = src->ob_size;
82 if (i < 0)
83 i = -(i);
84 result = _PyLong_New(i);
85 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000086 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000087 while (--i >= 0)
88 result->ob_digit[i] = src->ob_digit[i];
89 }
90 return (PyObject *)result;
91}
92
Guido van Rossumedcc38a1991-05-05 20:09:44 +000093/* Create a new long int object from a C long int */
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000096PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000097{
Tim Petersce9de2f2001-06-14 04:56:19 +000098 PyLongObject *v;
99 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
100 int ndigits = 0;
101 int negative = 0;
102
103 if (ival < 0) {
104 ival = -ival;
105 negative = 1;
106 }
107
108 /* Count the number of Python digits.
109 We used to pick 5 ("big enough for anything"), but that's a
110 waste of time and space given that 5*15 = 75 bits are rarely
111 needed. */
112 t = (unsigned long)ival;
113 while (t) {
114 ++ndigits;
115 t >>= SHIFT;
116 }
117 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000118 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000119 digit *p = v->ob_digit;
120 v->ob_size = negative ? -ndigits : ndigits;
121 t = (unsigned long)ival;
122 while (t) {
123 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000124 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000125 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000126 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128}
129
Guido van Rossum53756b11997-01-03 17:14:46 +0000130/* Create a new long int object from a C unsigned long int */
131
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000133PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000134{
Tim Petersce9de2f2001-06-14 04:56:19 +0000135 PyLongObject *v;
136 unsigned long t;
137 int ndigits = 0;
138
139 /* Count the number of Python digits. */
140 t = (unsigned long)ival;
141 while (t) {
142 ++ndigits;
143 t >>= SHIFT;
144 }
145 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000146 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000147 digit *p = v->ob_digit;
148 v->ob_size = ndigits;
149 while (ival) {
150 *p++ = (digit)(ival & MASK);
151 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000152 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000153 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000155}
156
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000157/* Create a new long int object from a C double */
158
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000161{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163 double frac;
164 int i, ndig, expo, neg;
165 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000166 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000167 PyErr_SetString(PyExc_OverflowError,
168 "cannot convert float infinity to long");
169 return NULL;
170 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000171 if (dval < 0.0) {
172 neg = 1;
173 dval = -dval;
174 }
175 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
176 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000178 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000180 if (v == NULL)
181 return NULL;
182 frac = ldexp(frac, (expo-1) % SHIFT + 1);
183 for (i = ndig; --i >= 0; ) {
184 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000185 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000186 frac = frac - (double)bits;
187 frac = ldexp(frac, SHIFT);
188 }
189 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000190 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000192}
193
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000194/* Get a C long int from a long int object.
195 Returns -1 and sets an error condition if overflow occurs. */
196
197long
Tim Peters9f688bf2000-07-07 15:53:28 +0000198PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000199{
Guido van Rossumf7531811998-05-26 14:33:37 +0000200 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000202 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000203 Py_ssize_t i;
204 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000205
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000207 if (vv != NULL && PyInt_Check(vv))
208 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000210 return -1;
211 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000213 i = v->ob_size;
214 sign = 1;
215 x = 0;
216 if (i < 0) {
217 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000218 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000219 }
220 while (--i >= 0) {
221 prev = x;
222 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000223 if ((x >> SHIFT) != prev)
224 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000225 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000226 /* Haven't lost any bits, but if the sign bit is set we're in
227 * trouble *unless* this is the min negative number. So,
228 * trouble iff sign bit set && (positive || some bit set other
229 * than the sign bit).
230 */
231 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
232 goto overflow;
233 return (long)x * sign;
234
235 overflow:
236 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000237 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000238 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000239}
240
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000241/* Get a Py_ssize_t from a long int object.
242 Returns -1 and sets an error condition if overflow occurs. */
243
244Py_ssize_t
245_PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000246 register PyLongObject *v;
247 size_t x, prev;
248 Py_ssize_t i;
249 int sign;
250
251 if (vv == NULL || !PyLong_Check(vv)) {
252 PyErr_BadInternalCall();
253 return -1;
254 }
255 v = (PyLongObject *)vv;
256 i = v->ob_size;
257 sign = 1;
258 x = 0;
259 if (i < 0) {
260 sign = -1;
261 i = -(i);
262 }
263 while (--i >= 0) {
264 prev = x;
265 x = (x << SHIFT) + v->ob_digit[i];
266 if ((x >> SHIFT) != prev)
267 goto overflow;
268 }
269 /* Haven't lost any bits, but if the sign bit is set we're in
270 * trouble *unless* this is the min negative number. So,
271 * trouble iff sign bit set && (positive || some bit set other
272 * than the sign bit).
273 */
274 if ((Py_ssize_t)x < 0 && (sign > 0 || (x << 1) != 0))
275 goto overflow;
276 return (Py_ssize_t)x * sign;
277
278 overflow:
279 PyErr_SetString(PyExc_OverflowError,
280 "long int too large to convert to int");
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000281 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000282}
283
Guido van Rossumd8c80482002-08-13 00:24:58 +0000284/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000285 Returns -1 and sets an error condition if overflow occurs. */
286
287unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000288PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000289{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000291 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000292 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000295 if (vv != NULL && PyInt_Check(vv)) {
296 long val = PyInt_AsLong(vv);
297 if (val < 0) {
298 PyErr_SetString(PyExc_OverflowError,
299 "can't convert negative value to unsigned long");
300 return (unsigned long) -1;
301 }
302 return val;
303 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000305 return (unsigned long) -1;
306 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000308 i = v->ob_size;
309 x = 0;
310 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000312 "can't convert negative value to unsigned long");
313 return (unsigned long) -1;
314 }
315 while (--i >= 0) {
316 prev = x;
317 x = (x << SHIFT) + v->ob_digit[i];
318 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000320 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000321 return (unsigned long) -1;
322 }
323 }
324 return x;
325}
326
Thomas Hellera4ea6032003-04-17 18:55:45 +0000327/* Get a C unsigned long int from a long int object, ignoring the high bits.
328 Returns -1 and sets an error condition if an error occurs. */
329
330unsigned long
331PyLong_AsUnsignedLongMask(PyObject *vv)
332{
333 register PyLongObject *v;
334 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000335 Py_ssize_t i;
336 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000337
338 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000339 if (vv != NULL && PyInt_Check(vv))
340 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000341 PyErr_BadInternalCall();
342 return (unsigned long) -1;
343 }
344 v = (PyLongObject *)vv;
345 i = v->ob_size;
346 sign = 1;
347 x = 0;
348 if (i < 0) {
349 sign = -1;
350 i = -i;
351 }
352 while (--i >= 0) {
353 x = (x << SHIFT) + v->ob_digit[i];
354 }
355 return x * sign;
356}
357
Tim Peters5b8132f2003-01-31 15:52:05 +0000358int
359_PyLong_Sign(PyObject *vv)
360{
361 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000362
363 assert(v != NULL);
364 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000365
Tim Petersee1a53c2003-02-02 02:57:53 +0000366 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000367}
368
Tim Petersbaefd9e2003-01-28 20:37:45 +0000369size_t
370_PyLong_NumBits(PyObject *vv)
371{
372 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000373 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000374 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000375
376 assert(v != NULL);
377 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000378 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000379 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
380 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000381 digit msd = v->ob_digit[ndigits - 1];
382
Tim Peters5b8132f2003-01-31 15:52:05 +0000383 result = (ndigits - 1) * SHIFT;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 if (result / SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000385 goto Overflow;
386 do {
387 ++result;
388 if (result == 0)
389 goto Overflow;
390 msd >>= 1;
391 } while (msd);
392 }
393 return result;
394
395Overflow:
396 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
397 "to express in a platform size_t");
398 return (size_t)-1;
399}
400
Tim Peters2a9b3672001-06-11 21:23:58 +0000401PyObject *
402_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
403 int little_endian, int is_signed)
404{
405 const unsigned char* pstartbyte;/* LSB of bytes */
406 int incr; /* direction to move pstartbyte */
407 const unsigned char* pendbyte; /* MSB of bytes */
408 size_t numsignificantbytes; /* number of bytes that matter */
409 size_t ndigits; /* number of Python long digits */
410 PyLongObject* v; /* result */
411 int idigit = 0; /* next free index in v->ob_digit */
412
413 if (n == 0)
414 return PyLong_FromLong(0L);
415
416 if (little_endian) {
417 pstartbyte = bytes;
418 pendbyte = bytes + n - 1;
419 incr = 1;
420 }
421 else {
422 pstartbyte = bytes + n - 1;
423 pendbyte = bytes;
424 incr = -1;
425 }
426
427 if (is_signed)
428 is_signed = *pendbyte >= 0x80;
429
430 /* Compute numsignificantbytes. This consists of finding the most
431 significant byte. Leading 0 bytes are insignficant if the number
432 is positive, and leading 0xff bytes if negative. */
433 {
434 size_t i;
435 const unsigned char* p = pendbyte;
436 const int pincr = -incr; /* search MSB to LSB */
437 const unsigned char insignficant = is_signed ? 0xff : 0x00;
438
439 for (i = 0; i < n; ++i, p += pincr) {
440 if (*p != insignficant)
441 break;
442 }
443 numsignificantbytes = n - i;
444 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
445 actually has 2 significant bytes. OTOH, 0xff0001 ==
446 -0x00ffff, so we wouldn't *need* to bump it there; but we
447 do for 0xffff = -0x0001. To be safe without bothering to
448 check every case, bump it regardless. */
449 if (is_signed && numsignificantbytes < n)
450 ++numsignificantbytes;
451 }
452
453 /* How many Python long digits do we need? We have
454 8*numsignificantbytes bits, and each Python long digit has SHIFT
455 bits, so it's the ceiling of the quotient. */
456 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
457 if (ndigits > (size_t)INT_MAX)
458 return PyErr_NoMemory();
459 v = _PyLong_New((int)ndigits);
460 if (v == NULL)
461 return NULL;
462
463 /* Copy the bits over. The tricky parts are computing 2's-comp on
464 the fly for signed numbers, and dealing with the mismatch between
465 8-bit bytes and (probably) 15-bit Python digits.*/
466 {
467 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000468 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000469 twodigits accum = 0; /* sliding register */
470 unsigned int accumbits = 0; /* number of bits in accum */
471 const unsigned char* p = pstartbyte;
472
473 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000474 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000475 /* Compute correction for 2's comp, if needed. */
476 if (is_signed) {
477 thisbyte = (0xff ^ thisbyte) + carry;
478 carry = thisbyte >> 8;
479 thisbyte &= 0xff;
480 }
481 /* Because we're going LSB to MSB, thisbyte is
482 more significant than what's already in accum,
483 so needs to be prepended to accum. */
484 accum |= thisbyte << accumbits;
485 accumbits += 8;
486 if (accumbits >= SHIFT) {
487 /* There's enough to fill a Python digit. */
488 assert(idigit < (int)ndigits);
489 v->ob_digit[idigit] = (digit)(accum & MASK);
490 ++idigit;
491 accum >>= SHIFT;
492 accumbits -= SHIFT;
493 assert(accumbits < SHIFT);
494 }
495 }
496 assert(accumbits < SHIFT);
497 if (accumbits) {
498 assert(idigit < (int)ndigits);
499 v->ob_digit[idigit] = (digit)accum;
500 ++idigit;
501 }
502 }
503
504 v->ob_size = is_signed ? -idigit : idigit;
505 return (PyObject *)long_normalize(v);
506}
507
508int
509_PyLong_AsByteArray(PyLongObject* v,
510 unsigned char* bytes, size_t n,
511 int little_endian, int is_signed)
512{
513 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000515 twodigits accum; /* sliding register */
516 unsigned int accumbits; /* # bits in accum */
517 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
518 twodigits carry; /* for computing 2's-comp */
519 size_t j; /* # bytes filled */
520 unsigned char* p; /* pointer to next byte in bytes */
521 int pincr; /* direction to move p */
522
523 assert(v != NULL && PyLong_Check(v));
524
525 if (v->ob_size < 0) {
526 ndigits = -(v->ob_size);
527 if (!is_signed) {
528 PyErr_SetString(PyExc_TypeError,
529 "can't convert negative long to unsigned");
530 return -1;
531 }
532 do_twos_comp = 1;
533 }
534 else {
535 ndigits = v->ob_size;
536 do_twos_comp = 0;
537 }
538
539 if (little_endian) {
540 p = bytes;
541 pincr = 1;
542 }
543 else {
544 p = bytes + n - 1;
545 pincr = -1;
546 }
547
Tim Peters898cf852001-06-13 20:50:08 +0000548 /* Copy over all the Python digits.
549 It's crucial that every Python digit except for the MSD contribute
550 exactly SHIFT bits to the total, so first assert that the long is
551 normalized. */
552 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000553 j = 0;
554 accum = 0;
555 accumbits = 0;
556 carry = do_twos_comp ? 1 : 0;
557 for (i = 0; i < ndigits; ++i) {
558 twodigits thisdigit = v->ob_digit[i];
559 if (do_twos_comp) {
560 thisdigit = (thisdigit ^ MASK) + carry;
561 carry = thisdigit >> SHIFT;
562 thisdigit &= MASK;
563 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000564 /* Because we're going LSB to MSB, thisdigit is more
565 significant than what's already in accum, so needs to be
566 prepended to accum. */
567 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000568 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000569
Tim Petersede05092001-06-14 08:53:38 +0000570 /* The most-significant digit may be (probably is) at least
571 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000572 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000573 /* Count # of sign bits -- they needn't be stored,
574 * although for signed conversion we need later to
575 * make sure at least one sign bit gets stored.
576 * First shift conceptual sign bit to real sign bit.
577 */
578 stwodigits s = (stwodigits)(thisdigit <<
579 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000580 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000581 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000582 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000583 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000584 }
Tim Petersede05092001-06-14 08:53:38 +0000585 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000586 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000587
Tim Peters2a9b3672001-06-11 21:23:58 +0000588 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000589 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000590 if (j >= n)
591 goto Overflow;
592 ++j;
593 *p = (unsigned char)(accum & 0xff);
594 p += pincr;
595 accumbits -= 8;
596 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000597 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000598 }
599
600 /* Store the straggler (if any). */
601 assert(accumbits < 8);
602 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000603 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000604 if (j >= n)
605 goto Overflow;
606 ++j;
607 if (do_twos_comp) {
608 /* Fill leading bits of the byte with sign bits
609 (appropriately pretending that the long had an
610 infinite supply of sign bits). */
611 accum |= (~(twodigits)0) << accumbits;
612 }
613 *p = (unsigned char)(accum & 0xff);
614 p += pincr;
615 }
Tim Peters05607ad2001-06-13 21:01:27 +0000616 else if (j == n && n > 0 && is_signed) {
617 /* The main loop filled the byte array exactly, so the code
618 just above didn't get to ensure there's a sign bit, and the
619 loop below wouldn't add one either. Make sure a sign bit
620 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000621 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000622 int sign_bit_set = msb >= 0x80;
623 assert(accumbits == 0);
624 if (sign_bit_set == do_twos_comp)
625 return 0;
626 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000627 goto Overflow;
628 }
Tim Peters05607ad2001-06-13 21:01:27 +0000629
630 /* Fill remaining bytes with copies of the sign bit. */
631 {
632 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
633 for ( ; j < n; ++j, p += pincr)
634 *p = signbyte;
635 }
636
Tim Peters2a9b3672001-06-11 21:23:58 +0000637 return 0;
638
639Overflow:
640 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
641 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000642
Tim Peters2a9b3672001-06-11 21:23:58 +0000643}
644
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000645double
646_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
647{
648/* NBITS_WANTED should be > the number of bits in a double's precision,
649 but small enough so that 2**NBITS_WANTED is within the normal double
650 range. nbitsneeded is set to 1 less than that because the most-significant
651 Python digit contains at least 1 significant bit, but we don't want to
652 bother counting them (catering to the worst case cheaply).
653
654 57 is one more than VAX-D double precision; I (Tim) don't know of a double
655 format with more precision than that; it's 1 larger so that we add in at
656 least one round bit to stand in for the ignored least-significant bits.
657*/
658#define NBITS_WANTED 57
659 PyLongObject *v;
660 double x;
661 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000662 Py_ssize_t i;
663 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000664 int nbitsneeded;
665
666 if (vv == NULL || !PyLong_Check(vv)) {
667 PyErr_BadInternalCall();
668 return -1;
669 }
670 v = (PyLongObject *)vv;
671 i = v->ob_size;
672 sign = 1;
673 if (i < 0) {
674 sign = -1;
675 i = -(i);
676 }
677 else if (i == 0) {
678 *exponent = 0;
679 return 0.0;
680 }
681 --i;
682 x = (double)v->ob_digit[i];
683 nbitsneeded = NBITS_WANTED - 1;
684 /* Invariant: i Python digits remain unaccounted for. */
685 while (i > 0 && nbitsneeded > 0) {
686 --i;
687 x = x * multiplier + (double)v->ob_digit[i];
688 nbitsneeded -= SHIFT;
689 }
690 /* There are i digits we didn't shift in. Pretending they're all
691 zeroes, the true value is x * 2**(i*SHIFT). */
692 *exponent = i;
693 assert(x > 0.0);
694 return x * sign;
695#undef NBITS_WANTED
696}
697
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000698/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000699
700double
Tim Peters9f688bf2000-07-07 15:53:28 +0000701PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000702{
Thomas Wouters553489a2006-02-01 21:32:04 +0000703 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000704 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000705
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 if (vv == NULL || !PyLong_Check(vv)) {
707 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000708 return -1;
709 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000710 x = _PyLong_AsScaledDouble(vv, &e);
711 if (x == -1.0 && PyErr_Occurred())
712 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000713 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
714 set correctly after a successful _PyLong_AsScaledDouble() call */
715 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000716 if (e > INT_MAX / SHIFT)
717 goto overflow;
718 errno = 0;
719 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000720 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000721 goto overflow;
722 return x;
723
724overflow:
725 PyErr_SetString(PyExc_OverflowError,
726 "long int too large to convert to float");
727 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000728}
729
Guido van Rossum78694d91998-09-18 14:14:13 +0000730/* Create a new long (or int) object from a C pointer */
731
732PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000733PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000734{
Tim Peters70128a12001-06-16 08:48:40 +0000735#if SIZEOF_VOID_P <= SIZEOF_LONG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 if ((long)p < 0)
737 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000738 return PyInt_FromLong((long)p);
739#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000740
Tim Peters70128a12001-06-16 08:48:40 +0000741#ifndef HAVE_LONG_LONG
742# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
743#endif
744#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000745# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000746#endif
747 /* optimize null pointers */
748 if (p == NULL)
749 return PyInt_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000751
752#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000753}
754
755/* Get a C pointer from a long object (or an int object in some cases) */
756
757void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000758PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000759{
760 /* This function will allow int or long objects. If vv is neither,
761 then the PyLong_AsLong*() functions will raise the exception:
762 PyExc_SystemError, "bad argument to internal function"
763 */
Tim Peters70128a12001-06-16 08:48:40 +0000764#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000765 long x;
766
Tim Peters70128a12001-06-16 08:48:40 +0000767 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000768 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000770 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771 else
772 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000773#else
Tim Peters70128a12001-06-16 08:48:40 +0000774
775#ifndef HAVE_LONG_LONG
776# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
777#endif
778#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000779# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000780#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000781 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000782
Tim Peters70128a12001-06-16 08:48:40 +0000783 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000784 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000786 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 else
788 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000789
790#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000791
792 if (x == -1 && PyErr_Occurred())
793 return NULL;
794 return (void *)x;
795}
796
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000797#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000798
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000799/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000800 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000801 */
802
Tim Peterscf37dfc2001-06-14 18:42:50 +0000803#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000804
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000805/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000806
807PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000808PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000809{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000810 PyLongObject *v;
811 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
812 int ndigits = 0;
813 int negative = 0;
814
815 if (ival < 0) {
816 ival = -ival;
817 negative = 1;
818 }
819
820 /* Count the number of Python digits.
821 We used to pick 5 ("big enough for anything"), but that's a
822 waste of time and space given that 5*15 = 75 bits are rarely
823 needed. */
824 t = (unsigned PY_LONG_LONG)ival;
825 while (t) {
826 ++ndigits;
827 t >>= SHIFT;
828 }
829 v = _PyLong_New(ndigits);
830 if (v != NULL) {
831 digit *p = v->ob_digit;
832 v->ob_size = negative ? -ndigits : ndigits;
833 t = (unsigned PY_LONG_LONG)ival;
834 while (t) {
835 *p++ = (digit)(t & MASK);
836 t >>= SHIFT;
837 }
838 }
839 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000840}
841
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000842/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000843
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000844PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000845PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000846{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 PyLongObject *v;
848 unsigned PY_LONG_LONG t;
849 int ndigits = 0;
850
851 /* Count the number of Python digits. */
852 t = (unsigned PY_LONG_LONG)ival;
853 while (t) {
854 ++ndigits;
855 t >>= SHIFT;
856 }
857 v = _PyLong_New(ndigits);
858 if (v != NULL) {
859 digit *p = v->ob_digit;
860 v->ob_size = ndigits;
861 while (ival) {
862 *p++ = (digit)(ival & MASK);
863 ival >>= SHIFT;
864 }
865 }
866 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000867}
868
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869/* Create a new long int object from a C Py_ssize_t. */
870
871PyObject *
872_PyLong_FromSsize_t(Py_ssize_t ival)
873{
874 Py_ssize_t bytes = ival;
875 int one = 1;
876 return _PyLong_FromByteArray(
877 (unsigned char *)&bytes,
878 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
879}
880
881/* Create a new long int object from a C size_t. */
882
883PyObject *
884_PyLong_FromSize_t(size_t ival)
885{
886 size_t bytes = ival;
887 int one = 1;
888 return _PyLong_FromByteArray(
889 (unsigned char *)&bytes,
890 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
891}
892
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000893/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000894 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000895
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000896PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000897PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000898{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000899 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000900 int one = 1;
901 int res;
902
Tim Petersd38b1c72001-09-30 05:09:37 +0000903 if (vv == NULL) {
904 PyErr_BadInternalCall();
905 return -1;
906 }
907 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000908 PyNumberMethods *nb;
909 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000910 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000911 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000912 if ((nb = vv->ob_type->tp_as_number) == NULL ||
913 nb->nb_int == NULL) {
914 PyErr_SetString(PyExc_TypeError, "an integer is required");
915 return -1;
916 }
917 io = (*nb->nb_int) (vv);
918 if (io == NULL)
919 return -1;
920 if (PyInt_Check(io)) {
921 bytes = PyInt_AsLong(io);
922 Py_DECREF(io);
923 return bytes;
924 }
925 if (PyLong_Check(io)) {
926 bytes = PyLong_AsLongLong(io);
927 Py_DECREF(io);
928 return bytes;
929 }
930 Py_DECREF(io);
931 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000932 return -1;
933 }
934
Tim Petersd1a7da62001-06-13 00:35:57 +0000935 res = _PyLong_AsByteArray(
936 (PyLongObject *)vv, (unsigned char *)&bytes,
937 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000938
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000939 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000940 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000941 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000942 else
943 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000944}
945
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000946/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000947 Return -1 and set an error if overflow occurs. */
948
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000949unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000950PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000951{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000952 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000953 int one = 1;
954 int res;
955
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000956 if (vv == NULL || !PyLong_Check(vv)) {
957 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000959 }
960
Tim Petersd1a7da62001-06-13 00:35:57 +0000961 res = _PyLong_AsByteArray(
962 (PyLongObject *)vv, (unsigned char *)&bytes,
963 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000964
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000965 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000966 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000967 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000968 else
969 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000970}
Tim Petersd1a7da62001-06-13 00:35:57 +0000971
Thomas Hellera4ea6032003-04-17 18:55:45 +0000972/* Get a C unsigned long int from a long int object, ignoring the high bits.
973 Returns -1 and sets an error condition if an error occurs. */
974
975unsigned PY_LONG_LONG
976PyLong_AsUnsignedLongLongMask(PyObject *vv)
977{
978 register PyLongObject *v;
979 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000980 Py_ssize_t i;
981 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000982
983 if (vv == NULL || !PyLong_Check(vv)) {
984 PyErr_BadInternalCall();
985 return (unsigned long) -1;
986 }
987 v = (PyLongObject *)vv;
988 i = v->ob_size;
989 sign = 1;
990 x = 0;
991 if (i < 0) {
992 sign = -1;
993 i = -i;
994 }
995 while (--i >= 0) {
996 x = (x << SHIFT) + v->ob_digit[i];
997 }
998 return x * sign;
999}
Tim Petersd1a7da62001-06-13 00:35:57 +00001000#undef IS_LITTLE_ENDIAN
1001
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001002#endif /* HAVE_LONG_LONG */
1003
Neil Schemenauerba872e22001-01-04 01:46:03 +00001004
1005static int
1006convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001007 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001008 *a = (PyLongObject *) v;
1009 Py_INCREF(v);
1010 }
1011 else if (PyInt_Check(v)) {
1012 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1013 }
1014 else {
1015 return 0;
1016 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001017 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001018 *b = (PyLongObject *) w;
1019 Py_INCREF(w);
1020 }
1021 else if (PyInt_Check(w)) {
1022 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1023 }
1024 else {
1025 Py_DECREF(*a);
1026 return 0;
1027 }
1028 return 1;
1029}
1030
1031#define CONVERT_BINOP(v, w, a, b) \
1032 if (!convert_binop(v, w, a, b)) { \
1033 Py_INCREF(Py_NotImplemented); \
1034 return Py_NotImplemented; \
1035 }
1036
Tim Peters877a2122002-08-12 05:09:36 +00001037/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1038 * is modified in place, by adding y to it. Carries are propagated as far as
1039 * x[m-1], and the remaining carry (0 or 1) is returned.
1040 */
1041static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001042v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001043{
1044 int i;
1045 digit carry = 0;
1046
1047 assert(m >= n);
1048 for (i = 0; i < n; ++i) {
1049 carry += x[i] + y[i];
1050 x[i] = carry & MASK;
1051 carry >>= SHIFT;
1052 assert((carry & 1) == carry);
1053 }
1054 for (; carry && i < m; ++i) {
1055 carry += x[i];
1056 x[i] = carry & MASK;
1057 carry >>= SHIFT;
1058 assert((carry & 1) == carry);
1059 }
1060 return carry;
1061}
1062
1063/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1064 * is modified in place, by subtracting y from it. Borrows are propagated as
1065 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1066 */
1067static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001068v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001069{
1070 int i;
1071 digit borrow = 0;
1072
1073 assert(m >= n);
1074 for (i = 0; i < n; ++i) {
1075 borrow = x[i] - y[i] - borrow;
1076 x[i] = borrow & MASK;
1077 borrow >>= SHIFT;
1078 borrow &= 1; /* keep only 1 sign bit */
1079 }
1080 for (; borrow && i < m; ++i) {
1081 borrow = x[i] - borrow;
1082 x[i] = borrow & MASK;
1083 borrow >>= SHIFT;
1084 borrow &= 1;
1085 }
1086 return borrow;
1087}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001088
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001089/* Multiply by a single digit, ignoring the sign. */
1090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001091static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001092mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001093{
1094 return muladd1(a, n, (digit)0);
1095}
1096
1097/* Multiply by a single digit and add a single digit, ignoring the sign. */
1098
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001099static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001100muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001101{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001102 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001103 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001104 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001105 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001106
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001107 if (z == NULL)
1108 return NULL;
1109 for (i = 0; i < size_a; ++i) {
1110 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001111 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001112 carry >>= SHIFT;
1113 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001114 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001115 return long_normalize(z);
1116}
1117
Tim Peters212e6142001-07-14 12:23:19 +00001118/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1119 in pout, and returning the remainder. pin and pout point at the LSD.
1120 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1121 long_format, but that should be done with great care since longs are
1122 immutable. */
1123
1124static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001125inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001126{
1127 twodigits rem = 0;
1128
1129 assert(n > 0 && n <= MASK);
1130 pin += size;
1131 pout += size;
1132 while (--size >= 0) {
1133 digit hi;
1134 rem = (rem << SHIFT) + *--pin;
1135 *--pout = hi = (digit)(rem / n);
1136 rem -= hi * n;
1137 }
1138 return (digit)rem;
1139}
1140
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001141/* Divide a long integer by a digit, returning both the quotient
1142 (as function result) and the remainder (through *prem).
1143 The sign of a is ignored; n should not be zero. */
1144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001146divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001147{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001148 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001149 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001150
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001151 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001153 if (z == NULL)
1154 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001155 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001156 return long_normalize(z);
1157}
1158
1159/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001160 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001161 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163static PyObject *
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001164long_format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001165{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 register PyLongObject *a = (PyLongObject *)aa;
1167 PyStringObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001169 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001170 char *p;
1171 int bits;
1172 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 if (a == NULL || !PyLong_Check(a)) {
1175 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001176 return NULL;
1177 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178 assert(base >= 2 && base <= 36);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001179 size_a = ABS(a->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001180
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001181 /* Compute a rough upper bound for the length of the string */
1182 i = base;
1183 bits = 0;
1184 while (i > 1) {
1185 ++bits;
1186 i >>= 1;
1187 }
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001188 i = 5 + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001190 if (str == NULL)
1191 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001192 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001193 *p = '\0';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001194 if (a->ob_size < 0)
1195 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001196
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001197 if (a->ob_size == 0) {
1198 *--p = '0';
1199 }
1200 else if ((base & (base - 1)) == 0) {
1201 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001202 twodigits accum = 0;
1203 int accumbits = 0; /* # of bits in accum */
1204 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001205 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001206 while ((i >>= 1) > 1)
1207 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001208
1209 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001210 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001211 accumbits += SHIFT;
1212 assert(accumbits >= basebits);
1213 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001214 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001215 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001216 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001217 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001218 accumbits -= basebits;
1219 accum >>= basebits;
1220 } while (i < size_a-1 ? accumbits >= basebits :
1221 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001222 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001223 }
1224 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001225 /* Not 0, and base not a power of 2. Divide repeatedly by
1226 base, but for speed use the highest power of base that
1227 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001228 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001229 digit *pin = a->ob_digit;
1230 PyLongObject *scratch;
1231 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001232 digit powbase = base; /* powbase == base ** power */
1233 int power = 1;
1234 for (;;) {
1235 unsigned long newpow = powbase * (unsigned long)base;
1236 if (newpow >> SHIFT) /* doesn't fit in a digit */
1237 break;
1238 powbase = (digit)newpow;
1239 ++power;
1240 }
Tim Peters212e6142001-07-14 12:23:19 +00001241
1242 /* Get a scratch area for repeated division. */
1243 scratch = _PyLong_New(size);
1244 if (scratch == NULL) {
1245 Py_DECREF(str);
1246 return NULL;
1247 }
1248
1249 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001250 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001251 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001252 digit rem = inplace_divrem1(scratch->ob_digit,
1253 pin, size, powbase);
1254 pin = scratch->ob_digit; /* no need to use a again */
1255 if (pin[size - 1] == 0)
1256 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001257 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001258 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001259 Py_DECREF(str);
1260 return NULL;
1261 })
Tim Peters212e6142001-07-14 12:23:19 +00001262
1263 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001264 assert(ntostore > 0);
1265 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001266 digit nextrem = (digit)(rem / base);
1267 char c = (char)(rem - nextrem * base);
1268 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001269 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001270 *--p = c;
1271 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001272 --ntostore;
1273 /* Termination is a bit delicate: must not
1274 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001275 remaining quotient and rem are both 0. */
1276 } while (ntostore && (size || rem));
1277 } while (size != 0);
1278 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001279 }
1280
Guido van Rossum2c475421992-08-14 15:13:07 +00001281 if (base == 8) {
1282 if (size_a != 0)
1283 *--p = '0';
1284 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001285 else if (base == 16) {
1286 *--p = 'x';
1287 *--p = '0';
1288 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001289 else if (base != 10) {
1290 *--p = '#';
1291 *--p = '0' + base%10;
1292 if (base > 10)
1293 *--p = '0' + base/10;
1294 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001295 if (sign)
1296 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 if (p != PyString_AS_STRING(str)) {
1298 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001299 assert(p > q);
1300 do {
1301 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001302 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 _PyString_Resize((PyObject **)&str,
1304 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001305 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001306 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001307}
1308
Thomas Wouters477c8d52006-05-27 19:21:47 +00001309/* Table of digit values for 8-bit string -> integer conversion.
1310 * '0' maps to 0, ..., '9' maps to 9.
1311 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1312 * All other indices map to 37.
1313 * Note that when converting a base B string, a char c is a legitimate
1314 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1315 */
1316int _PyLong_DigitValue[256] = {
1317 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1318 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1319 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1320 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1321 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1322 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1323 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1324 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1325 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1326 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1327 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1328 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1329 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1330 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1331 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1332 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1333};
1334
1335/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001336 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1337 * non-digit (which may be *str!). A normalized long is returned.
1338 * The point to this routine is that it takes time linear in the number of
1339 * string characters.
1340 */
1341static PyLongObject *
1342long_from_binary_base(char **str, int base)
1343{
1344 char *p = *str;
1345 char *start = p;
1346 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001347 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001348 PyLongObject *z;
1349 twodigits accum;
1350 int bits_in_accum;
1351 digit *pdigit;
1352
1353 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1354 n = base;
1355 for (bits_per_char = -1; n; ++bits_per_char)
1356 n >>= 1;
1357 /* n <- total # of bits needed, while setting p to end-of-string */
1358 n = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001360 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001361 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001362 n = (p - start) * bits_per_char;
1363 if (n / bits_per_char != p - start) {
1364 PyErr_SetString(PyExc_ValueError,
1365 "long string too large to convert");
1366 return NULL;
1367 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001368 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1369 n = (n + SHIFT - 1) / SHIFT;
1370 z = _PyLong_New(n);
1371 if (z == NULL)
1372 return NULL;
1373 /* Read string from right, and fill in long from left; i.e.,
1374 * from least to most significant in both.
1375 */
1376 accum = 0;
1377 bits_in_accum = 0;
1378 pdigit = z->ob_digit;
1379 while (--p >= start) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001381 assert(k >= 0 && k < base);
1382 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001383 bits_in_accum += bits_per_char;
1384 if (bits_in_accum >= SHIFT) {
1385 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001386 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001387 accum >>= SHIFT;
1388 bits_in_accum -= SHIFT;
1389 assert(bits_in_accum < SHIFT);
1390 }
1391 }
1392 if (bits_in_accum) {
1393 assert(bits_in_accum <= SHIFT);
1394 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001395 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001396 }
1397 while (pdigit - z->ob_digit < n)
1398 *pdigit++ = 0;
1399 return long_normalize(z);
1400}
1401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001403PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001404{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001405 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001406 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407 PyLongObject *z;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001408 PyObject *strobj, *strrepr;
1409 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001410
Guido van Rossum472c04f1996-12-05 21:57:21 +00001411 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001413 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001414 return NULL;
1415 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001416 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001417 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001418 if (*str == '+')
1419 ++str;
1420 else if (*str == '-') {
1421 ++str;
1422 sign = -1;
1423 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001424 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001425 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001426 if (base == 0) {
1427 if (str[0] != '0')
1428 base = 10;
1429 else if (str[1] == 'x' || str[1] == 'X')
1430 base = 16;
1431 else
1432 base = 8;
1433 }
1434 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1435 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001436
Guido van Rossume6762971998-06-22 03:54:46 +00001437 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001438 if ((base & (base - 1)) == 0)
1439 z = long_from_binary_base(&str, base);
1440 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001441/***
1442Binary bases can be converted in time linear in the number of digits, because
1443Python's representation base is binary. Other bases (including decimal!) use
1444the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001445
Thomas Wouters477c8d52006-05-27 19:21:47 +00001446First some math: the largest integer that can be expressed in N base-B digits
1447is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1448case number of Python digits needed to hold it is the smallest integer n s.t.
1449
1450 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1451 BASE**n >= B**N [taking logs to base BASE]
1452 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1453
1454The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1455this quickly. A Python long with that much space is reserved near the start,
1456and the result is computed into it.
1457
1458The input string is actually treated as being in base base**i (i.e., i digits
1459are processed at a time), where two more static arrays hold:
1460
1461 convwidth_base[base] = the largest integer i such that base**i <= BASE
1462 convmultmax_base[base] = base ** convwidth_base[base]
1463
1464The first of these is the largest i such that i consecutive input digits
1465must fit in a single Python digit. The second is effectively the input
1466base we're really using.
1467
1468Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1469convmultmax_base[base], the result is "simply"
1470
1471 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1472
1473where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001474
1475Error analysis: as above, the number of Python digits `n` needed is worst-
1476case
1477
1478 n >= N * log(B)/log(BASE)
1479
1480where `N` is the number of input digits in base `B`. This is computed via
1481
1482 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1483
1484below. Two numeric concerns are how much space this can waste, and whether
1485the computed result can be too small. To be concrete, assume BASE = 2**15,
1486which is the default (and it's unlikely anyone changes that).
1487
1488Waste isn't a problem: provided the first input digit isn't 0, the difference
1489between the worst-case input with N digits and the smallest input with N
1490digits is about a factor of B, but B is small compared to BASE so at most
1491one allocated Python digit can remain unused on that count. If
1492N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1493and adding 1 returns a result 1 larger than necessary. However, that can't
1494happen: whenever B is a power of 2, long_from_binary_base() is called
1495instead, and it's impossible for B**i to be an integer power of 2**15 when
1496B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1497an exact integer when B is not a power of 2, since B**i has a prime factor
1498other than 2 in that case, but (2**15)**j's only prime factor is 2).
1499
1500The computed result can be too small if the true value of N*log(B)/log(BASE)
1501is a little bit larger than an exact integer, but due to roundoff errors (in
1502computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1503yields a numeric result a little less than that integer. Unfortunately, "how
1504close can a transcendental function get to an integer over some range?"
1505questions are generally theoretically intractable. Computer analysis via
1506continued fractions is practical: expand log(B)/log(BASE) via continued
1507fractions, giving a sequence i/j of "the best" rational approximations. Then
1508j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1509we can get very close to being in trouble, but very rarely. For example,
151076573 is a denominator in one of the continued-fraction approximations to
1511log(10)/log(2**15), and indeed:
1512
1513 >>> log(10)/log(2**15)*76573
1514 16958.000000654003
1515
1516is very close to an integer. If we were working with IEEE single-precision,
1517rounding errors could kill us. Finding worst cases in IEEE double-precision
1518requires better-than-double-precision log() functions, and Tim didn't bother.
1519Instead the code checks to see whether the allocated space is enough as each
1520new Python digit is added, and copies the whole thing to a larger long if not.
1521This should happen extremely rarely, and in fact I don't have a test case
1522that triggers it(!). Instead the code was tested by artificially allocating
1523just 1 digit at the start, so that the copying code was exercised for every
1524digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001525***/
1526 register twodigits c; /* current input character */
1527 Py_ssize_t size_z;
1528 int i;
1529 int convwidth;
1530 twodigits convmultmax, convmult;
1531 digit *pz, *pzstop;
1532 char* scan;
1533
1534 static double log_base_BASE[37] = {0.0e0,};
1535 static int convwidth_base[37] = {0,};
1536 static twodigits convmultmax_base[37] = {0,};
1537
1538 if (log_base_BASE[base] == 0.0) {
1539 twodigits convmax = base;
1540 int i = 1;
1541
1542 log_base_BASE[base] = log((double)base) /
1543 log((double)BASE);
1544 for (;;) {
1545 twodigits next = convmax * base;
1546 if (next > BASE)
1547 break;
1548 convmax = next;
1549 ++i;
1550 }
1551 convmultmax_base[base] = convmax;
1552 assert(i > 0);
1553 convwidth_base[base] = i;
1554 }
1555
1556 /* Find length of the string of numeric characters. */
1557 scan = str;
1558 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
1559 ++scan;
1560
1561 /* Create a long object that can contain the largest possible
1562 * integer with this base and length. Note that there's no
1563 * need to initialize z->ob_digit -- no slot is read up before
1564 * being stored into.
1565 */
1566 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001567 /* Uncomment next line to test exceedingly rare copy code */
1568 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569 assert(size_z > 0);
1570 z = _PyLong_New(size_z);
1571 if (z == NULL)
1572 return NULL;
1573 z->ob_size = 0;
1574
1575 /* `convwidth` consecutive input digits are treated as a single
1576 * digit in base `convmultmax`.
1577 */
1578 convwidth = convwidth_base[base];
1579 convmultmax = convmultmax_base[base];
1580
1581 /* Work ;-) */
1582 while (str < scan) {
1583 /* grab up to convwidth digits from the input string */
1584 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
1585 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1586 c = (twodigits)(c * base +
1587 _PyLong_DigitValue[Py_CHARMASK(*str)]);
1588 assert(c < BASE);
1589 }
1590
1591 convmult = convmultmax;
1592 /* Calculate the shift only if we couldn't get
1593 * convwidth digits.
1594 */
1595 if (i != convwidth) {
1596 convmult = base;
1597 for ( ; i > 1; --i)
1598 convmult *= base;
1599 }
1600
1601 /* Multiply z by convmult, and add c. */
1602 pz = z->ob_digit;
1603 pzstop = pz + z->ob_size;
1604 for (; pz < pzstop; ++pz) {
1605 c += (twodigits)*pz * convmult;
1606 *pz = (digit)(c & MASK);
1607 c >>= SHIFT;
1608 }
1609 /* carry off the current end? */
1610 if (c) {
1611 assert(c < BASE);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001612 if (z->ob_size < size_z) {
1613 *pz = (digit)c;
1614 ++z->ob_size;
1615 }
1616 else {
1617 PyLongObject *tmp;
1618 /* Extremely rare. Get more space. */
1619 assert(z->ob_size == size_z);
1620 tmp = _PyLong_New(size_z + 1);
1621 if (tmp == NULL) {
1622 Py_DECREF(z);
1623 return NULL;
1624 }
1625 memcpy(tmp->ob_digit,
1626 z->ob_digit,
1627 sizeof(digit) * size_z);
1628 Py_DECREF(z);
1629 z = tmp;
1630 z->ob_digit[size_z] = (digit)c;
1631 ++size_z;
1632 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001634 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001635 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001636 if (z == NULL)
1637 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001638 if (str == start)
1639 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001640 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001641 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001642 if (*str == 'L' || *str == 'l')
1643 str++;
1644 while (*str && isspace(Py_CHARMASK(*str)))
1645 str++;
1646 if (*str != '\0')
1647 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001648 if (pend)
1649 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001650 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001651
1652 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001653 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1655 strobj = PyString_FromStringAndSize(orig_str, slen);
1656 if (strobj == NULL)
1657 return NULL;
1658 strrepr = PyObject_Repr(strobj);
1659 Py_DECREF(strobj);
1660 if (strrepr == NULL)
1661 return NULL;
1662 PyErr_Format(PyExc_ValueError,
1663 "invalid literal for long() with base %d: %s",
1664 base, PyString_AS_STRING(strrepr));
1665 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001666 return NULL;
1667}
1668
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001669#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001670PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001671PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001672{
Walter Dörwald07e14762002-11-06 16:15:14 +00001673 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001674 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001675
Walter Dörwald07e14762002-11-06 16:15:14 +00001676 if (buffer == NULL)
1677 return NULL;
1678
1679 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1680 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001681 return NULL;
1682 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001683 result = PyLong_FromString(buffer, NULL, base);
1684 PyMem_FREE(buffer);
1685 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001686}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001687#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001688
Tim Peters9f688bf2000-07-07 15:53:28 +00001689/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001690static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001691 (PyLongObject *, PyLongObject *, PyLongObject **);
1692static PyObject *long_pos(PyLongObject *);
1693static int long_divrem(PyLongObject *, PyLongObject *,
1694 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001695
1696/* Long division with remainder, top-level routine */
1697
Guido van Rossume32e0141992-01-19 16:31:05 +00001698static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001699long_divrem(PyLongObject *a, PyLongObject *b,
1700 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001701{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001702 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001703 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001704
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001705 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001706 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001707 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001708 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001709 }
1710 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001711 (size_a == size_b &&
1712 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001713 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001714 *pdiv = _PyLong_New(0);
1715 Py_INCREF(a);
1716 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001717 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001718 }
1719 if (size_b == 1) {
1720 digit rem = 0;
1721 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001722 if (z == NULL)
1723 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001724 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001725 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001726 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001727 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001728 if (z == NULL)
1729 return -1;
1730 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001731 /* Set the signs.
1732 The quotient z has the sign of a*b;
1733 the remainder r has the sign of a,
1734 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001735 if ((a->ob_size < 0) != (b->ob_size < 0))
1736 z->ob_size = -(z->ob_size);
1737 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1738 (*prem)->ob_size = -((*prem)->ob_size);
1739 *pdiv = z;
1740 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001741}
1742
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001743/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001744
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001745static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001746x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001747{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001748 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001749 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001750 PyLongObject *v = mul1(v1, d);
1751 PyLongObject *w = mul1(w1, d);
1752 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001753 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001754
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001755 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001756 Py_XDECREF(v);
1757 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001758 return NULL;
1759 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001760
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001761 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001762 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001763 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001764
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001765 size_v = ABS(v->ob_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001766 k = size_v - size_w;
1767 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001768
Thomas Wouters477c8d52006-05-27 19:21:47 +00001769 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001770 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1771 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001772 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001773 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001774
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001775 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001776 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001777 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001778 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001779 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001780 if (vj == w->ob_digit[size_w-1])
1781 q = MASK;
1782 else
1783 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1784 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001785
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001786 while (w->ob_digit[size_w-2]*q >
1787 ((
1788 ((twodigits)vj << SHIFT)
1789 + v->ob_digit[j-1]
1790 - q*w->ob_digit[size_w-1]
1791 ) << SHIFT)
1792 + v->ob_digit[j-2])
1793 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001794
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001795 for (i = 0; i < size_w && i+k < size_v; ++i) {
1796 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001797 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001798 carry += v->ob_digit[i+k] - z
1799 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001800 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001801 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1802 carry, SHIFT);
1803 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001804 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001805
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001806 if (i+k < size_v) {
1807 carry += v->ob_digit[i+k];
1808 v->ob_digit[i+k] = 0;
1809 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001810
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001811 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001812 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001813 else {
1814 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001815 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001816 carry = 0;
1817 for (i = 0; i < size_w && i+k < size_v; ++i) {
1818 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001819 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001820 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1821 BASE_TWODIGITS_TYPE,
1822 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001823 }
1824 }
1825 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001826
Guido van Rossumc206c761995-01-10 15:23:19 +00001827 if (a == NULL)
1828 *prem = NULL;
1829 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001830 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001831 *prem = divrem1(v, d, &d);
1832 /* d receives the (unused) remainder */
1833 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001834 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001835 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001836 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001837 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001838 Py_DECREF(v);
1839 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001840 return a;
1841}
1842
1843/* Methods */
1844
1845static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001846long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001847{
Guido van Rossum9475a232001-10-05 20:51:39 +00001848 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001849}
1850
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001851static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001852long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001853{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001854 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001855}
1856
1857static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001858long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001859{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001860 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001861
Guido van Rossumc6913e71991-11-19 20:26:46 +00001862 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001863 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001864 sign = 0;
1865 else
1866 sign = a->ob_size - b->ob_size;
1867 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001868 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001869 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001870 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1871 ;
1872 if (i < 0)
1873 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001874 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001875 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001876 if (a->ob_size < 0)
1877 sign = -sign;
1878 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001879 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001880 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001881}
1882
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001883static PyObject *
1884long_richcompare(PyObject *self, PyObject *other, int op)
1885{
1886 PyLongObject *a, *b;
1887 CONVERT_BINOP((PyObject *)self, (PyObject *)other, &a, &b);
1888 return Py_CmpToRich(op, long_compare(a, b));
1889}
1890
Guido van Rossum9bfef441993-03-29 10:43:31 +00001891static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001892long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001893{
1894 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001895 Py_ssize_t i;
1896 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001897
1898 /* This is designed so that Python ints and longs with the
1899 same value hash to the same value, otherwise comparisons
1900 of mapping keys will turn out weird */
1901 i = v->ob_size;
1902 sign = 1;
1903 x = 0;
1904 if (i < 0) {
1905 sign = -1;
1906 i = -(i);
1907 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001908#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001909 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001910 /* Force a native long #-bits (32 or 64) circular shift */
1911 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001912 x += v->ob_digit[i];
1913 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001914#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001915 x = x * sign;
1916 if (x == -1)
1917 x = -2;
1918 return x;
1919}
1920
1921
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001922/* Add the absolute values of two long integers. */
1923
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001924static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001925x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001926{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001927 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001928 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001929 int i;
1930 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001931
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001932 /* Ensure a is the larger of the two: */
1933 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001934 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001935 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001936 size_a = size_b;
1937 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001938 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001939 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001940 if (z == NULL)
1941 return NULL;
1942 for (i = 0; i < size_b; ++i) {
1943 carry += a->ob_digit[i] + b->ob_digit[i];
1944 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001945 carry >>= SHIFT;
1946 }
1947 for (; i < size_a; ++i) {
1948 carry += a->ob_digit[i];
1949 z->ob_digit[i] = carry & MASK;
1950 carry >>= SHIFT;
1951 }
1952 z->ob_digit[i] = carry;
1953 return long_normalize(z);
1954}
1955
1956/* Subtract the absolute values of two integers. */
1957
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001958static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001959x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001960{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001961 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001962 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001963 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001964 int sign = 1;
1965 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001966
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001967 /* Ensure a is the larger of the two: */
1968 if (size_a < size_b) {
1969 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001970 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001971 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001972 size_a = size_b;
1973 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001974 }
1975 else if (size_a == size_b) {
1976 /* Find highest digit where a and b differ: */
1977 i = size_a;
1978 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1979 ;
1980 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001981 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001982 if (a->ob_digit[i] < b->ob_digit[i]) {
1983 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001984 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001985 }
1986 size_a = size_b = i+1;
1987 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001989 if (z == NULL)
1990 return NULL;
1991 for (i = 0; i < size_b; ++i) {
1992 /* The following assumes unsigned arithmetic
1993 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001994 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001995 z->ob_digit[i] = borrow & MASK;
1996 borrow >>= SHIFT;
1997 borrow &= 1; /* Keep only one sign bit */
1998 }
1999 for (; i < size_a; ++i) {
2000 borrow = a->ob_digit[i] - borrow;
2001 z->ob_digit[i] = borrow & MASK;
2002 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002003 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002004 }
2005 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002006 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002007 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002008 return long_normalize(z);
2009}
2010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002012long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002013{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002014 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002015
Neil Schemenauerba872e22001-01-04 01:46:03 +00002016 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2017
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002018 if (a->ob_size < 0) {
2019 if (b->ob_size < 0) {
2020 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002021 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002022 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002023 }
2024 else
2025 z = x_sub(b, a);
2026 }
2027 else {
2028 if (b->ob_size < 0)
2029 z = x_sub(a, b);
2030 else
2031 z = x_add(a, b);
2032 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002033 Py_DECREF(a);
2034 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002035 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002036}
2037
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002038static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002039long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002040{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002041 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002042
Neil Schemenauerba872e22001-01-04 01:46:03 +00002043 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2044
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002045 if (a->ob_size < 0) {
2046 if (b->ob_size < 0)
2047 z = x_sub(a, b);
2048 else
2049 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002050 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002051 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002052 }
2053 else {
2054 if (b->ob_size < 0)
2055 z = x_add(a, b);
2056 else
2057 z = x_sub(a, b);
2058 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002059 Py_DECREF(a);
2060 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002062}
2063
Tim Peters5af4e6c2002-08-12 02:31:19 +00002064/* Grade school multiplication, ignoring the signs.
2065 * Returns the absolute value of the product, or NULL if error.
2066 */
2067static PyLongObject *
2068x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002069{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002070 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002071 Py_ssize_t size_a = ABS(a->ob_size);
2072 Py_ssize_t size_b = ABS(b->ob_size);
2073 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002074
Tim Peters5af4e6c2002-08-12 02:31:19 +00002075 z = _PyLong_New(size_a + size_b);
2076 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002077 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002078
2079 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002080 if (a == b) {
2081 /* Efficient squaring per HAC, Algorithm 14.16:
2082 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2083 * Gives slightly less than a 2x speedup when a == b,
2084 * via exploiting that each entry in the multiplication
2085 * pyramid appears twice (except for the size_a squares).
2086 */
2087 for (i = 0; i < size_a; ++i) {
2088 twodigits carry;
2089 twodigits f = a->ob_digit[i];
2090 digit *pz = z->ob_digit + (i << 1);
2091 digit *pa = a->ob_digit + i + 1;
2092 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002093
Tim Peters0973b992004-08-29 22:16:50 +00002094 SIGCHECK({
2095 Py_DECREF(z);
2096 return NULL;
2097 })
2098
2099 carry = *pz + f * f;
2100 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002101 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002102 assert(carry <= MASK);
2103
2104 /* Now f is added in twice in each column of the
2105 * pyramid it appears. Same as adding f<<1 once.
2106 */
2107 f <<= 1;
2108 while (pa < paend) {
2109 carry += *pz + *pa++ * f;
2110 *pz++ = (digit)(carry & MASK);
2111 carry >>= SHIFT;
2112 assert(carry <= (MASK << 1));
2113 }
2114 if (carry) {
2115 carry += *pz;
2116 *pz++ = (digit)(carry & MASK);
2117 carry >>= SHIFT;
2118 }
2119 if (carry)
2120 *pz += (digit)(carry & MASK);
2121 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002122 }
Tim Peters0973b992004-08-29 22:16:50 +00002123 }
2124 else { /* a is not the same as b -- gradeschool long mult */
2125 for (i = 0; i < size_a; ++i) {
2126 twodigits carry = 0;
2127 twodigits f = a->ob_digit[i];
2128 digit *pz = z->ob_digit + i;
2129 digit *pb = b->ob_digit;
2130 digit *pbend = b->ob_digit + size_b;
2131
2132 SIGCHECK({
2133 Py_DECREF(z);
2134 return NULL;
2135 })
2136
2137 while (pb < pbend) {
2138 carry += *pz + *pb++ * f;
2139 *pz++ = (digit)(carry & MASK);
2140 carry >>= SHIFT;
2141 assert(carry <= MASK);
2142 }
2143 if (carry)
2144 *pz += (digit)(carry & MASK);
2145 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002146 }
2147 }
Tim Peters44121a62002-08-12 06:17:58 +00002148 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002149}
2150
2151/* A helper for Karatsuba multiplication (k_mul).
2152 Takes a long "n" and an integer "size" representing the place to
2153 split, and sets low and high such that abs(n) == (high << size) + low,
2154 viewing the shift as being by digits. The sign bit is ignored, and
2155 the return values are >= 0.
2156 Returns 0 on success, -1 on failure.
2157*/
2158static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002159kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002160{
2161 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002162 Py_ssize_t size_lo, size_hi;
2163 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002164
2165 size_lo = MIN(size_n, size);
2166 size_hi = size_n - size_lo;
2167
2168 if ((hi = _PyLong_New(size_hi)) == NULL)
2169 return -1;
2170 if ((lo = _PyLong_New(size_lo)) == NULL) {
2171 Py_DECREF(hi);
2172 return -1;
2173 }
2174
2175 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2176 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2177
2178 *high = long_normalize(hi);
2179 *low = long_normalize(lo);
2180 return 0;
2181}
2182
Tim Peters60004642002-08-12 22:01:34 +00002183static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2184
Tim Peters5af4e6c2002-08-12 02:31:19 +00002185/* Karatsuba multiplication. Ignores the input signs, and returns the
2186 * absolute value of the product (or NULL if error).
2187 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2188 */
2189static PyLongObject *
2190k_mul(PyLongObject *a, PyLongObject *b)
2191{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002192 Py_ssize_t asize = ABS(a->ob_size);
2193 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002194 PyLongObject *ah = NULL;
2195 PyLongObject *al = NULL;
2196 PyLongObject *bh = NULL;
2197 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002198 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002199 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002200 Py_ssize_t shift; /* the number of digits we split off */
2201 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002202
Tim Peters5af4e6c2002-08-12 02:31:19 +00002203 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2204 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2205 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002206 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002207 * By picking X to be a power of 2, "*X" is just shifting, and it's
2208 * been reduced to 3 multiplies on numbers half the size.
2209 */
2210
Tim Petersfc07e562002-08-12 02:54:10 +00002211 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002212 * is largest.
2213 */
Tim Peters738eda72002-08-12 15:08:20 +00002214 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002215 t1 = a;
2216 a = b;
2217 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002218
2219 i = asize;
2220 asize = bsize;
2221 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002222 }
2223
2224 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002225 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2226 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002227 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002228 return _PyLong_New(0);
2229 else
2230 return x_mul(a, b);
2231 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002232
Tim Peters60004642002-08-12 22:01:34 +00002233 /* If a is small compared to b, splitting on b gives a degenerate
2234 * case with ah==0, and Karatsuba may be (even much) less efficient
2235 * than "grade school" then. However, we can still win, by viewing
2236 * b as a string of "big digits", each of width a->ob_size. That
2237 * leads to a sequence of balanced calls to k_mul.
2238 */
2239 if (2 * asize <= bsize)
2240 return k_lopsided_mul(a, b);
2241
Tim Petersd6974a52002-08-13 20:37:51 +00002242 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002243 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002244 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002245 assert(ah->ob_size > 0); /* the split isn't degenerate */
2246
Tim Peters0973b992004-08-29 22:16:50 +00002247 if (a == b) {
2248 bh = ah;
2249 bl = al;
2250 Py_INCREF(bh);
2251 Py_INCREF(bl);
2252 }
2253 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002254
Tim Petersd64c1de2002-08-12 17:36:03 +00002255 /* The plan:
2256 * 1. Allocate result space (asize + bsize digits: that's always
2257 * enough).
2258 * 2. Compute ah*bh, and copy into result at 2*shift.
2259 * 3. Compute al*bl, and copy into result at 0. Note that this
2260 * can't overlap with #2.
2261 * 4. Subtract al*bl from the result, starting at shift. This may
2262 * underflow (borrow out of the high digit), but we don't care:
2263 * we're effectively doing unsigned arithmetic mod
2264 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2265 * borrows and carries out of the high digit can be ignored.
2266 * 5. Subtract ah*bh from the result, starting at shift.
2267 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2268 * at shift.
2269 */
2270
2271 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002272 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002273 if (ret == NULL) goto fail;
2274#ifdef Py_DEBUG
2275 /* Fill with trash, to catch reference to uninitialized digits. */
2276 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2277#endif
Tim Peters44121a62002-08-12 06:17:58 +00002278
Tim Petersd64c1de2002-08-12 17:36:03 +00002279 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002280 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2281 assert(t1->ob_size >= 0);
2282 assert(2*shift + t1->ob_size <= ret->ob_size);
2283 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2284 t1->ob_size * sizeof(digit));
2285
2286 /* Zero-out the digits higher than the ah*bh copy. */
2287 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002288 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002289 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002290 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002291
Tim Petersd64c1de2002-08-12 17:36:03 +00002292 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002293 if ((t2 = k_mul(al, bl)) == NULL) {
2294 Py_DECREF(t1);
2295 goto fail;
2296 }
2297 assert(t2->ob_size >= 0);
2298 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2299 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002300
2301 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002302 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002303 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002304 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002305
Tim Petersd64c1de2002-08-12 17:36:03 +00002306 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2307 * because it's fresher in cache.
2308 */
Tim Peters738eda72002-08-12 15:08:20 +00002309 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002310 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002311 Py_DECREF(t2);
2312
Tim Petersd64c1de2002-08-12 17:36:03 +00002313 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002314 Py_DECREF(t1);
2315
Tim Petersd64c1de2002-08-12 17:36:03 +00002316 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002317 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2318 Py_DECREF(ah);
2319 Py_DECREF(al);
2320 ah = al = NULL;
2321
Tim Peters0973b992004-08-29 22:16:50 +00002322 if (a == b) {
2323 t2 = t1;
2324 Py_INCREF(t2);
2325 }
2326 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002327 Py_DECREF(t1);
2328 goto fail;
2329 }
2330 Py_DECREF(bh);
2331 Py_DECREF(bl);
2332 bh = bl = NULL;
2333
Tim Peters738eda72002-08-12 15:08:20 +00002334 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002335 Py_DECREF(t1);
2336 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002337 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002338 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002339
Tim Petersd6974a52002-08-13 20:37:51 +00002340 /* Add t3. It's not obvious why we can't run out of room here.
2341 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002342 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002343 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002344 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002345
Tim Peters5af4e6c2002-08-12 02:31:19 +00002346 return long_normalize(ret);
2347
2348 fail:
2349 Py_XDECREF(ret);
2350 Py_XDECREF(ah);
2351 Py_XDECREF(al);
2352 Py_XDECREF(bh);
2353 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002354 return NULL;
2355}
2356
Tim Petersd6974a52002-08-13 20:37:51 +00002357/* (*) Why adding t3 can't "run out of room" above.
2358
Tim Petersab86c2b2002-08-15 20:06:00 +00002359Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2360to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002361
Tim Petersab86c2b2002-08-15 20:06:00 +000023621. For any integer i, i = c(i/2) + f(i/2). In particular,
2363 bsize = c(bsize/2) + f(bsize/2).
23642. shift = f(bsize/2)
23653. asize <= bsize
23664. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2367 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002368
Tim Petersab86c2b2002-08-15 20:06:00 +00002369We allocated asize + bsize result digits, and add t3 into them at an offset
2370of shift. This leaves asize+bsize-shift allocated digit positions for t3
2371to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2372asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002373
Tim Petersab86c2b2002-08-15 20:06:00 +00002374bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2375at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002376
Tim Petersab86c2b2002-08-15 20:06:00 +00002377If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2378digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2379most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002380
Tim Petersab86c2b2002-08-15 20:06:00 +00002381The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002382
Tim Petersab86c2b2002-08-15 20:06:00 +00002383 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002384
Tim Petersab86c2b2002-08-15 20:06:00 +00002385and we have asize + c(bsize/2) available digit positions. We need to show
2386this is always enough. An instance of c(bsize/2) cancels out in both, so
2387the question reduces to whether asize digits is enough to hold
2388(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2389then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2390asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2391digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2392asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002393c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2394is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2395bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002396
Tim Peters48d52c02002-08-14 17:07:32 +00002397Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2398clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2399ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002400*/
2401
Tim Peters60004642002-08-12 22:01:34 +00002402/* b has at least twice the digits of a, and a is big enough that Karatsuba
2403 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2404 * of slices, each with a->ob_size digits, and multiply the slices by a,
2405 * one at a time. This gives k_mul balanced inputs to work with, and is
2406 * also cache-friendly (we compute one double-width slice of the result
2407 * at a time, then move on, never bactracking except for the helpful
2408 * single-width slice overlap between successive partial sums).
2409 */
2410static PyLongObject *
2411k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2412{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002413 const Py_ssize_t asize = ABS(a->ob_size);
2414 Py_ssize_t bsize = ABS(b->ob_size);
2415 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002416 PyLongObject *ret;
2417 PyLongObject *bslice = NULL;
2418
2419 assert(asize > KARATSUBA_CUTOFF);
2420 assert(2 * asize <= bsize);
2421
2422 /* Allocate result space, and zero it out. */
2423 ret = _PyLong_New(asize + bsize);
2424 if (ret == NULL)
2425 return NULL;
2426 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2427
2428 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002429 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002430 if (bslice == NULL)
2431 goto fail;
2432
2433 nbdone = 0;
2434 while (bsize > 0) {
2435 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002436 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002437
2438 /* Multiply the next slice of b by a. */
2439 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2440 nbtouse * sizeof(digit));
2441 bslice->ob_size = nbtouse;
2442 product = k_mul(a, bslice);
2443 if (product == NULL)
2444 goto fail;
2445
2446 /* Add into result. */
2447 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2448 product->ob_digit, product->ob_size);
2449 Py_DECREF(product);
2450
2451 bsize -= nbtouse;
2452 nbdone += nbtouse;
2453 }
2454
2455 Py_DECREF(bslice);
2456 return long_normalize(ret);
2457
2458 fail:
2459 Py_DECREF(ret);
2460 Py_XDECREF(bslice);
2461 return NULL;
2462}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002463
2464static PyObject *
2465long_mul(PyLongObject *v, PyLongObject *w)
2466{
2467 PyLongObject *a, *b, *z;
2468
2469 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002470 Py_INCREF(Py_NotImplemented);
2471 return Py_NotImplemented;
2472 }
2473
Tim Petersd64c1de2002-08-12 17:36:03 +00002474 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002475 /* Negate if exactly one of the inputs is negative. */
2476 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002477 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002478 Py_DECREF(a);
2479 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002480 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002481}
2482
Guido van Rossume32e0141992-01-19 16:31:05 +00002483/* The / and % operators are now defined in terms of divmod().
2484 The expression a mod b has the value a - b*floor(a/b).
2485 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002486 |a| by |b|, with the sign of a. This is also expressed
2487 as a - b*trunc(a/b), if trunc truncates towards zero.
2488 Some examples:
2489 a b a rem b a mod b
2490 13 10 3 3
2491 -13 10 -3 7
2492 13 -10 3 -7
2493 -13 -10 -3 -3
2494 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002495 have different signs. We then subtract one from the 'div'
2496 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002497
Tim Peters47e52ee2004-08-30 02:44:38 +00002498/* Compute
2499 * *pdiv, *pmod = divmod(v, w)
2500 * NULL can be passed for pdiv or pmod, in which case that part of
2501 * the result is simply thrown away. The caller owns a reference to
2502 * each of these it requests (does not pass NULL for).
2503 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002504static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002505l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002506 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002507{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002508 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002509
Guido van Rossume32e0141992-01-19 16:31:05 +00002510 if (long_divrem(v, w, &div, &mod) < 0)
2511 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002512 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2513 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002514 PyLongObject *temp;
2515 PyLongObject *one;
2516 temp = (PyLongObject *) long_add(mod, w);
2517 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002518 mod = temp;
2519 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002520 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002521 return -1;
2522 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002523 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002524 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002525 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2526 Py_DECREF(mod);
2527 Py_DECREF(div);
2528 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002529 return -1;
2530 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002531 Py_DECREF(one);
2532 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002533 div = temp;
2534 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002535 if (pdiv != NULL)
2536 *pdiv = div;
2537 else
2538 Py_DECREF(div);
2539
2540 if (pmod != NULL)
2541 *pmod = mod;
2542 else
2543 Py_DECREF(mod);
2544
Guido van Rossume32e0141992-01-19 16:31:05 +00002545 return 0;
2546}
2547
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002548static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002549long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002550{
Tim Peters47e52ee2004-08-30 02:44:38 +00002551 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002552
2553 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002554 if (l_divmod(a, b, &div, NULL) < 0)
2555 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002556 Py_DECREF(a);
2557 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002558 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002559}
2560
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002561static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002562long_true_divide(PyObject *v, PyObject *w)
2563{
Tim Peterse2a60002001-09-04 06:17:36 +00002564 PyLongObject *a, *b;
2565 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002566 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002567
2568 CONVERT_BINOP(v, w, &a, &b);
2569 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2570 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002571 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2572 Py_DECREF(a);
2573 Py_DECREF(b);
2574 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002575 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002576 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2577 but should really be set correctly after sucessful calls to
2578 _PyLong_AsScaledDouble() */
2579 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002580
2581 if (bd == 0.0) {
2582 PyErr_SetString(PyExc_ZeroDivisionError,
2583 "long division or modulo by zero");
2584 return NULL;
2585 }
2586
2587 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2588 ad /= bd; /* overflow/underflow impossible here */
2589 aexp -= bexp;
2590 if (aexp > INT_MAX / SHIFT)
2591 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002592 else if (aexp < -(INT_MAX / SHIFT))
2593 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002594 errno = 0;
2595 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002596 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002597 goto overflow;
2598 return PyFloat_FromDouble(ad);
2599
2600overflow:
2601 PyErr_SetString(PyExc_OverflowError,
2602 "long/long too large for a float");
2603 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002604
Tim Peters20dab9f2001-09-04 05:31:47 +00002605}
2606
2607static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002608long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002609{
Tim Peters47e52ee2004-08-30 02:44:38 +00002610 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002611
2612 CONVERT_BINOP(v, w, &a, &b);
2613
Tim Peters47e52ee2004-08-30 02:44:38 +00002614 if (l_divmod(a, b, NULL, &mod) < 0)
2615 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002616 Py_DECREF(a);
2617 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002618 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002619}
2620
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002621static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002622long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002623{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002624 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002625 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002626
2627 CONVERT_BINOP(v, w, &a, &b);
2628
2629 if (l_divmod(a, b, &div, &mod) < 0) {
2630 Py_DECREF(a);
2631 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002632 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002633 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002634 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002635 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002636 PyTuple_SetItem(z, 0, (PyObject *) div);
2637 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002638 }
2639 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002640 Py_DECREF(div);
2641 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002642 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002643 Py_DECREF(a);
2644 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002645 return z;
2646}
2647
Tim Peters47e52ee2004-08-30 02:44:38 +00002648/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002649static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002650long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002651{
Tim Peters47e52ee2004-08-30 02:44:38 +00002652 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2653 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002654
Tim Peters47e52ee2004-08-30 02:44:38 +00002655 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002656 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002657 PyLongObject *temp = NULL;
2658
2659 /* 5-ary values. If the exponent is large enough, table is
2660 * precomputed so that table[i] == a**i % c for i in range(32).
2661 */
2662 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2663 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2664
2665 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002666 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002667 if (PyLong_Check(x)) {
2668 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002669 Py_INCREF(x);
2670 }
Tim Petersde7990b2005-07-17 23:45:23 +00002671 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002672 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002673 if (c == NULL)
2674 goto Error;
2675 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002676 else if (x == Py_None)
2677 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002678 else {
2679 Py_DECREF(a);
2680 Py_DECREF(b);
2681 Py_INCREF(Py_NotImplemented);
2682 return Py_NotImplemented;
2683 }
Tim Peters4c483c42001-09-05 06:24:58 +00002684
Tim Peters47e52ee2004-08-30 02:44:38 +00002685 if (b->ob_size < 0) { /* if exponent is negative */
2686 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002687 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002688 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002689 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002690 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002691 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002692 /* else return a float. This works because we know
2693 that this calls float_pow() which converts its
2694 arguments to double. */
2695 Py_DECREF(a);
2696 Py_DECREF(b);
2697 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002698 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002699 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002700
2701 if (c) {
2702 /* if modulus == 0:
2703 raise ValueError() */
2704 if (c->ob_size == 0) {
2705 PyErr_SetString(PyExc_ValueError,
2706 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002707 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002708 }
2709
2710 /* if modulus < 0:
2711 negativeOutput = True
2712 modulus = -modulus */
2713 if (c->ob_size < 0) {
2714 negativeOutput = 1;
2715 temp = (PyLongObject *)_PyLong_Copy(c);
2716 if (temp == NULL)
2717 goto Error;
2718 Py_DECREF(c);
2719 c = temp;
2720 temp = NULL;
2721 c->ob_size = - c->ob_size;
2722 }
2723
2724 /* if modulus == 1:
2725 return 0 */
2726 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2727 z = (PyLongObject *)PyLong_FromLong(0L);
2728 goto Done;
2729 }
2730
2731 /* if base < 0:
2732 base = base % modulus
2733 Having the base positive just makes things easier. */
2734 if (a->ob_size < 0) {
2735 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002736 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002737 Py_DECREF(a);
2738 a = temp;
2739 temp = NULL;
2740 }
2741 }
2742
2743 /* At this point a, b, and c are guaranteed non-negative UNLESS
2744 c is NULL, in which case a may be negative. */
2745
2746 z = (PyLongObject *)PyLong_FromLong(1L);
2747 if (z == NULL)
2748 goto Error;
2749
2750 /* Perform a modular reduction, X = X % c, but leave X alone if c
2751 * is NULL.
2752 */
2753#define REDUCE(X) \
2754 if (c != NULL) { \
2755 if (l_divmod(X, c, NULL, &temp) < 0) \
2756 goto Error; \
2757 Py_XDECREF(X); \
2758 X = temp; \
2759 temp = NULL; \
2760 }
2761
2762 /* Multiply two values, then reduce the result:
2763 result = X*Y % c. If c is NULL, skip the mod. */
2764#define MULT(X, Y, result) \
2765{ \
2766 temp = (PyLongObject *)long_mul(X, Y); \
2767 if (temp == NULL) \
2768 goto Error; \
2769 Py_XDECREF(result); \
2770 result = temp; \
2771 temp = NULL; \
2772 REDUCE(result) \
2773}
2774
2775 if (b->ob_size <= FIVEARY_CUTOFF) {
2776 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2777 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2778 for (i = b->ob_size - 1; i >= 0; --i) {
2779 digit bi = b->ob_digit[i];
2780
2781 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2782 MULT(z, z, z)
2783 if (bi & j)
2784 MULT(z, a, z)
2785 }
2786 }
2787 }
2788 else {
2789 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2790 Py_INCREF(z); /* still holds 1L */
2791 table[0] = z;
2792 for (i = 1; i < 32; ++i)
2793 MULT(table[i-1], a, table[i])
2794
2795 for (i = b->ob_size - 1; i >= 0; --i) {
2796 const digit bi = b->ob_digit[i];
2797
2798 for (j = SHIFT - 5; j >= 0; j -= 5) {
2799 const int index = (bi >> j) & 0x1f;
2800 for (k = 0; k < 5; ++k)
2801 MULT(z, z, z)
2802 if (index)
2803 MULT(z, table[index], z)
2804 }
2805 }
2806 }
2807
2808 if (negativeOutput && (z->ob_size != 0)) {
2809 temp = (PyLongObject *)long_sub(z, c);
2810 if (temp == NULL)
2811 goto Error;
2812 Py_DECREF(z);
2813 z = temp;
2814 temp = NULL;
2815 }
2816 goto Done;
2817
2818 Error:
2819 if (z != NULL) {
2820 Py_DECREF(z);
2821 z = NULL;
2822 }
2823 /* fall through */
2824 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002825 if (b->ob_size > FIVEARY_CUTOFF) {
2826 for (i = 0; i < 32; ++i)
2827 Py_XDECREF(table[i]);
2828 }
Tim Petersde7990b2005-07-17 23:45:23 +00002829 Py_DECREF(a);
2830 Py_DECREF(b);
2831 Py_XDECREF(c);
2832 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002833 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002834}
2835
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002836static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002837long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002838{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002839 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002840 PyLongObject *x;
2841 PyLongObject *w;
2842 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002843 if (w == NULL)
2844 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002845 x = (PyLongObject *) long_add(v, w);
2846 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002847 if (x == NULL)
2848 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002849 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002850 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002851}
2852
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002853static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002854long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002855{
Tim Peters69c2de32001-09-11 22:31:33 +00002856 if (PyLong_CheckExact(v)) {
2857 Py_INCREF(v);
2858 return (PyObject *)v;
2859 }
2860 else
2861 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002862}
2863
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002864static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002865long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002866{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002867 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002868 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002869 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002870 Py_INCREF(v);
2871 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002872 }
Tim Peters69c2de32001-09-11 22:31:33 +00002873 z = (PyLongObject *)_PyLong_Copy(v);
2874 if (z != NULL)
2875 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002876 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002877}
2878
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002879static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002880long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002881{
2882 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002883 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002884 else
2885 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002886}
2887
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002888static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00002889long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002890{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002891 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002892}
2893
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002894static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002895long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002896{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002897 PyLongObject *a, *b;
2898 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002899 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002900 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002901 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002902
Neil Schemenauerba872e22001-01-04 01:46:03 +00002903 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2904
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002905 if (a->ob_size < 0) {
2906 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002907 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002908 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002909 if (a1 == NULL)
2910 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002911 a2 = (PyLongObject *) long_rshift(a1, b);
2912 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002913 if (a2 == NULL)
2914 goto rshift_error;
2915 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002916 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002917 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002918 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002919
Neil Schemenauerba872e22001-01-04 01:46:03 +00002920 shiftby = PyLong_AsLong((PyObject *)b);
2921 if (shiftby == -1L && PyErr_Occurred())
2922 goto rshift_error;
2923 if (shiftby < 0) {
2924 PyErr_SetString(PyExc_ValueError,
2925 "negative shift count");
2926 goto rshift_error;
2927 }
2928 wordshift = shiftby / SHIFT;
2929 newsize = ABS(a->ob_size) - wordshift;
2930 if (newsize <= 0) {
2931 z = _PyLong_New(0);
2932 Py_DECREF(a);
2933 Py_DECREF(b);
2934 return (PyObject *)z;
2935 }
2936 loshift = shiftby % SHIFT;
2937 hishift = SHIFT - loshift;
2938 lomask = ((digit)1 << hishift) - 1;
2939 himask = MASK ^ lomask;
2940 z = _PyLong_New(newsize);
2941 if (z == NULL)
2942 goto rshift_error;
2943 if (a->ob_size < 0)
2944 z->ob_size = -(z->ob_size);
2945 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2946 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2947 if (i+1 < newsize)
2948 z->ob_digit[i] |=
2949 (a->ob_digit[j+1] << hishift) & himask;
2950 }
2951 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002952 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002953rshift_error:
2954 Py_DECREF(a);
2955 Py_DECREF(b);
2956 return (PyObject *) z;
2957
Guido van Rossumc6913e71991-11-19 20:26:46 +00002958}
2959
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002960static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002961long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002962{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002963 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002964 PyLongObject *a, *b;
2965 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002966 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002967 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002968 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002969
Neil Schemenauerba872e22001-01-04 01:46:03 +00002970 CONVERT_BINOP(v, w, &a, &b);
2971
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002972 shiftby = PyLong_AsLong((PyObject *)b);
2973 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002974 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002975 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002976 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002977 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002978 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002979 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002980 PyErr_SetString(PyExc_ValueError,
2981 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002982 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002983 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002984 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2985 wordshift = (int)shiftby / SHIFT;
2986 remshift = (int)shiftby - wordshift * SHIFT;
2987
2988 oldsize = ABS(a->ob_size);
2989 newsize = oldsize + wordshift;
2990 if (remshift)
2991 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002992 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002993 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002994 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002995 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002996 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002997 for (i = 0; i < wordshift; i++)
2998 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002999 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003000 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003001 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003002 z->ob_digit[i] = (digit)(accum & MASK);
3003 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003004 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003005 if (remshift)
3006 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003007 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003008 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003009 z = long_normalize(z);
3010lshift_error:
3011 Py_DECREF(a);
3012 Py_DECREF(b);
3013 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003014}
3015
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003016
3017/* Bitwise and/xor/or operations */
3018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003019static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003020long_bitwise(PyLongObject *a,
3021 int op, /* '&', '|', '^' */
3022 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003023{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003024 digit maska, maskb; /* 0 or MASK */
3025 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003026 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003027 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003028 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003029 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003030 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003031
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003032 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003033 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003034 if (a == NULL)
3035 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003036 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003037 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003038 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003039 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003040 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003041 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003042 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003043 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003044 if (b == NULL) {
3045 Py_DECREF(a);
3046 return NULL;
3047 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003048 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003049 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003050 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003051 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003052 maskb = 0;
3053 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003054
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003055 negz = 0;
3056 switch (op) {
3057 case '^':
3058 if (maska != maskb) {
3059 maska ^= MASK;
3060 negz = -1;
3061 }
3062 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003063 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003064 if (maska && maskb) {
3065 op = '|';
3066 maska ^= MASK;
3067 maskb ^= MASK;
3068 negz = -1;
3069 }
3070 break;
3071 case '|':
3072 if (maska || maskb) {
3073 op = '&';
3074 maska ^= MASK;
3075 maskb ^= MASK;
3076 negz = -1;
3077 }
3078 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003079 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003080
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003081 /* JRH: The original logic here was to allocate the result value (z)
3082 as the longer of the two operands. However, there are some cases
3083 where the result is guaranteed to be shorter than that: AND of two
3084 positives, OR of two negatives: use the shorter number. AND with
3085 mixed signs: use the positive number. OR with mixed signs: use the
3086 negative number. After the transformations above, op will be '&'
3087 iff one of these cases applies, and mask will be non-0 for operands
3088 whose length should be ignored.
3089 */
3090
3091 size_a = a->ob_size;
3092 size_b = b->ob_size;
3093 size_z = op == '&'
3094 ? (maska
3095 ? size_b
3096 : (maskb ? size_a : MIN(size_a, size_b)))
3097 : MAX(size_a, size_b);
3098 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003099 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003100 Py_DECREF(a);
3101 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003102 return NULL;
3103 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003104
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003105 for (i = 0; i < size_z; ++i) {
3106 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3107 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3108 switch (op) {
3109 case '&': z->ob_digit[i] = diga & digb; break;
3110 case '|': z->ob_digit[i] = diga | digb; break;
3111 case '^': z->ob_digit[i] = diga ^ digb; break;
3112 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003113 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003114
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003115 Py_DECREF(a);
3116 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003117 z = long_normalize(z);
3118 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003119 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003120 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003121 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003122 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003123}
3124
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003125static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003126long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003127{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003128 PyLongObject *a, *b;
3129 PyObject *c;
3130 CONVERT_BINOP(v, w, &a, &b);
3131 c = long_bitwise(a, '&', b);
3132 Py_DECREF(a);
3133 Py_DECREF(b);
3134 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003135}
3136
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003137static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003138long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003139{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003140 PyLongObject *a, *b;
3141 PyObject *c;
3142 CONVERT_BINOP(v, w, &a, &b);
3143 c = long_bitwise(a, '^', b);
3144 Py_DECREF(a);
3145 Py_DECREF(b);
3146 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003147}
3148
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003149static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003150long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003151{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003152 PyLongObject *a, *b;
3153 PyObject *c;
3154 CONVERT_BINOP(v, w, &a, &b);
3155 c = long_bitwise(a, '|', b);
3156 Py_DECREF(a);
3157 Py_DECREF(b);
3158 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003159}
3160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003161static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003162long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003163{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003164 if (PyLong_CheckExact(v))
3165 Py_INCREF(v);
3166 else
3167 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003168 return v;
3169}
3170
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003171static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003172long_int(PyObject *v)
3173{
3174 long x;
3175 x = PyLong_AsLong(v);
3176 if (PyErr_Occurred()) {
3177 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3178 PyErr_Clear();
3179 if (PyLong_CheckExact(v)) {
3180 Py_INCREF(v);
3181 return v;
3182 }
3183 else
3184 return _PyLong_Copy((PyLongObject *)v);
3185 }
3186 else
3187 return NULL;
3188 }
3189 return PyInt_FromLong(x);
3190}
3191
3192static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003193long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003194{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003195 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003196 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003197 if (result == -1.0 && PyErr_Occurred())
3198 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003199 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003200}
3201
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003202static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003203long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003204{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003205 return long_format(v, 8);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003206}
3207
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003208static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003209long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003210{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003211 return long_format(v, 16);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003212}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003213
3214static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003215long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003216
Tim Peters6d6c1a32001-08-02 04:15:00 +00003217static PyObject *
3218long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3219{
3220 PyObject *x = NULL;
3221 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003222 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003223
Guido van Rossumbef14172001-08-29 15:47:46 +00003224 if (type != &PyLong_Type)
3225 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003226 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3227 &x, &base))
3228 return NULL;
3229 if (x == NULL)
3230 return PyLong_FromLong(0L);
3231 if (base == -909)
3232 return PyNumber_Long(x);
3233 else if (PyString_Check(x))
3234 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003235#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003236 else if (PyUnicode_Check(x))
3237 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3238 PyUnicode_GET_SIZE(x),
3239 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003240#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003241 else {
3242 PyErr_SetString(PyExc_TypeError,
3243 "long() can't convert non-string with explicit base");
3244 return NULL;
3245 }
3246}
3247
Guido van Rossumbef14172001-08-29 15:47:46 +00003248/* Wimpy, slow approach to tp_new calls for subtypes of long:
3249 first create a regular long from whatever arguments we got,
3250 then allocate a subtype instance and initialize it from
3251 the regular long. The regular long is then thrown away.
3252*/
3253static PyObject *
3254long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3255{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003256 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003257 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003258
3259 assert(PyType_IsSubtype(type, &PyLong_Type));
3260 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3261 if (tmp == NULL)
3262 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003263 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003264 n = tmp->ob_size;
3265 if (n < 0)
3266 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003267 newobj = (PyLongObject *)type->tp_alloc(type, n);
3268 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003269 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003270 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003271 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003272 assert(PyLong_Check(newobj));
3273 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003274 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003275 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003276 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003277 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003278}
3279
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003280static PyObject *
3281long_getnewargs(PyLongObject *v)
3282{
3283 return Py_BuildValue("(N)", _PyLong_Copy(v));
3284}
3285
3286static PyMethodDef long_methods[] = {
3287 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3288 {NULL, NULL} /* sentinel */
3289};
3290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003291PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003292"long(x[, base]) -> integer\n\
3293\n\
3294Convert a string or number to a long integer, if possible. A floating\n\
3295point argument will be truncated towards zero (this does not include a\n\
3296string representation of a floating point number!) When converting a\n\
3297string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003298converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003299
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003300static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003301 (binaryfunc) long_add, /*nb_add*/
3302 (binaryfunc) long_sub, /*nb_subtract*/
3303 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003304 long_mod, /*nb_remainder*/
3305 long_divmod, /*nb_divmod*/
3306 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003307 (unaryfunc) long_neg, /*nb_negative*/
3308 (unaryfunc) long_pos, /*tp_positive*/
3309 (unaryfunc) long_abs, /*tp_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00003310 (inquiry) long_bool, /*tp_bool*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003311 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003312 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003313 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003314 long_and, /*nb_and*/
3315 long_xor, /*nb_xor*/
3316 long_or, /*nb_or*/
Neal Norwitzca810462006-08-29 07:57:59 +00003317 0, /*nb_coerce*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318 long_int, /*nb_int*/
3319 long_long, /*nb_long*/
3320 long_float, /*nb_float*/
3321 long_oct, /*nb_oct*/
3322 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003323 0, /* nb_inplace_add */
3324 0, /* nb_inplace_subtract */
3325 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003326 0, /* nb_inplace_remainder */
3327 0, /* nb_inplace_power */
3328 0, /* nb_inplace_lshift */
3329 0, /* nb_inplace_rshift */
3330 0, /* nb_inplace_and */
3331 0, /* nb_inplace_xor */
3332 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003333 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003334 long_true_divide, /* nb_true_divide */
3335 0, /* nb_inplace_floor_divide */
3336 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003337 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003338};
3339
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003340PyTypeObject PyLong_Type = {
3341 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003342 0, /* ob_size */
3343 "long", /* tp_name */
3344 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3345 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003346 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347 0, /* tp_print */
3348 0, /* tp_getattr */
3349 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003350 0, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003351 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003352 &long_as_number, /* tp_as_number */
3353 0, /* tp_as_sequence */
3354 0, /* tp_as_mapping */
3355 (hashfunc)long_hash, /* tp_hash */
3356 0, /* tp_call */
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003357 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003358 PyObject_GenericGetAttr, /* tp_getattro */
3359 0, /* tp_setattro */
3360 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003361 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 long_doc, /* tp_doc */
3363 0, /* tp_traverse */
3364 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003365 long_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003366 0, /* tp_weaklistoffset */
3367 0, /* tp_iter */
3368 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003369 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370 0, /* tp_members */
3371 0, /* tp_getset */
3372 0, /* tp_base */
3373 0, /* tp_dict */
3374 0, /* tp_descr_get */
3375 0, /* tp_descr_set */
3376 0, /* tp_dictoffset */
3377 0, /* tp_init */
3378 0, /* tp_alloc */
3379 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003380 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003381};