blob: 16c70435a168b6e19b6b9fbd2f4561b44c796867 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003/* Long (arbitrary precision) integer object implementation */
4
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00005/* XXX The functional organization of this file is terrible */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00008#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Tim Peters5af4e6c2002-08-12 02:31:19 +000012/* For long multiplication, use the O(N**2) school algorithm unless
13 * both operands contain more than KARATSUBA_CUTOFF digits (this
14 * being an internal Python long digit, in base BASE).
15 */
Tim Peters0973b992004-08-29 22:16:50 +000016#define KARATSUBA_CUTOFF 70
17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000018
Tim Peters47e52ee2004-08-30 02:44:38 +000019/* For exponentiation, use the binary left-to-right algorithm
20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
21 * In that case, do 5 bits at a time. The potential drawback is that
22 * a table of 2**5 intermediate results is computed.
23 */
24#define FIVEARY_CUTOFF 8
25
Guido van Rossume32e0141992-01-19 16:31:05 +000026#define ABS(x) ((x) < 0 ? -(x) : (x))
27
Tim Peters5af4e6c2002-08-12 02:31:19 +000028#undef MIN
29#undef MAX
30#define MAX(x, y) ((x) < (y) ? (y) : (x))
31#define MIN(x, y) ((x) > (y) ? (y) : (x))
32
Guido van Rossume32e0141992-01-19 16:31:05 +000033/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000034static PyLongObject *long_normalize(PyLongObject *);
35static PyLongObject *mul1(PyLongObject *, wdigit);
36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000037static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossumd2dbecb2006-08-18 16:29:54 +000038static PyObject *long_format(PyObject *aa, int base);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000043 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000044 }
45
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046/* Normalize (remove leading zeros from) a long int object.
47 Doesn't attempt to free the storage--in most cases, due to the nature
48 of the algorithms used, this could save at most be one word anyway. */
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000051long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052{
Martin v. Löwis18e16552006-02-15 17:27:45 +000053 Py_ssize_t j = ABS(v->ob_size);
54 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000055
Guido van Rossumedcc38a1991-05-05 20:09:44 +000056 while (i > 0 && v->ob_digit[i-1] == 0)
57 --i;
58 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000059 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060 return v;
61}
62
63/* Allocate a new long int object with size digits.
64 Return NULL and set exception if we run out of memory. */
65
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000067_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068{
Thomas Wouters477c8d52006-05-27 19:21:47 +000069 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000070 PyErr_NoMemory();
71 return NULL;
72 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000074}
75
Tim Peters64b5ce32001-09-10 20:52:51 +000076PyObject *
77_PyLong_Copy(PyLongObject *src)
78{
79 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000081
82 assert(src != NULL);
83 i = src->ob_size;
84 if (i < 0)
85 i = -(i);
86 result = _PyLong_New(i);
87 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000088 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000089 while (--i >= 0)
90 result->ob_digit[i] = src->ob_digit[i];
91 }
92 return (PyObject *)result;
93}
94
Guido van Rossumedcc38a1991-05-05 20:09:44 +000095/* Create a new long int object from a C long int */
96
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000098PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000099{
Tim Petersce9de2f2001-06-14 04:56:19 +0000100 PyLongObject *v;
101 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
102 int ndigits = 0;
103 int negative = 0;
104
105 if (ival < 0) {
106 ival = -ival;
107 negative = 1;
108 }
109
110 /* Count the number of Python digits.
111 We used to pick 5 ("big enough for anything"), but that's a
112 waste of time and space given that 5*15 = 75 bits are rarely
113 needed. */
114 t = (unsigned long)ival;
115 while (t) {
116 ++ndigits;
117 t >>= SHIFT;
118 }
119 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000120 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000121 digit *p = v->ob_digit;
122 v->ob_size = negative ? -ndigits : ndigits;
123 t = (unsigned long)ival;
124 while (t) {
125 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000126 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000127 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000130}
131
Guido van Rossum53756b11997-01-03 17:14:46 +0000132/* Create a new long int object from a C unsigned long int */
133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000135PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000136{
Tim Petersce9de2f2001-06-14 04:56:19 +0000137 PyLongObject *v;
138 unsigned long t;
139 int ndigits = 0;
140
141 /* Count the number of Python digits. */
142 t = (unsigned long)ival;
143 while (t) {
144 ++ndigits;
145 t >>= SHIFT;
146 }
147 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000148 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000149 digit *p = v->ob_digit;
150 v->ob_size = ndigits;
151 while (ival) {
152 *p++ = (digit)(ival & MASK);
153 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000154 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000157}
158
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000159/* Create a new long int object from a C double */
160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000165 double frac;
166 int i, ndig, expo, neg;
167 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000168 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000169 PyErr_SetString(PyExc_OverflowError,
170 "cannot convert float infinity to long");
171 return NULL;
172 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000173 if (dval < 0.0) {
174 neg = 1;
175 dval = -dval;
176 }
177 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
178 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000180 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000182 if (v == NULL)
183 return NULL;
184 frac = ldexp(frac, (expo-1) % SHIFT + 1);
185 for (i = ndig; --i >= 0; ) {
186 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000187 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000188 frac = frac - (double)bits;
189 frac = ldexp(frac, SHIFT);
190 }
191 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000192 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000194}
195
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000196/* Get a C long int from a long int object.
197 Returns -1 and sets an error condition if overflow occurs. */
198
199long
Tim Peters9f688bf2000-07-07 15:53:28 +0000200PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000201{
Guido van Rossumf7531811998-05-26 14:33:37 +0000202 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000204 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205 Py_ssize_t i;
206 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000207
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000209 if (vv != NULL && PyInt_Check(vv))
210 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000212 return -1;
213 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000215 i = v->ob_size;
216 sign = 1;
217 x = 0;
218 if (i < 0) {
219 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000220 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000221 }
222 while (--i >= 0) {
223 prev = x;
224 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000225 if ((x >> SHIFT) != prev)
226 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000227 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000228 /* Haven't lost any bits, but if the sign bit is set we're in
229 * trouble *unless* this is the min negative number. So,
230 * trouble iff sign bit set && (positive || some bit set other
231 * than the sign bit).
232 */
233 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
234 goto overflow;
235 return (long)x * sign;
236
237 overflow:
238 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000239 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000240 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000241}
242
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000243/* Get a Py_ssize_t from a long int object.
244 Returns -1 and sets an error condition if overflow occurs. */
245
246Py_ssize_t
247_PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000248 register PyLongObject *v;
249 size_t x, prev;
250 Py_ssize_t i;
251 int sign;
252
253 if (vv == NULL || !PyLong_Check(vv)) {
254 PyErr_BadInternalCall();
255 return -1;
256 }
257 v = (PyLongObject *)vv;
258 i = v->ob_size;
259 sign = 1;
260 x = 0;
261 if (i < 0) {
262 sign = -1;
263 i = -(i);
264 }
265 while (--i >= 0) {
266 prev = x;
267 x = (x << SHIFT) + v->ob_digit[i];
268 if ((x >> SHIFT) != prev)
269 goto overflow;
270 }
271 /* Haven't lost any bits, but if the sign bit is set we're in
272 * trouble *unless* this is the min negative number. So,
273 * trouble iff sign bit set && (positive || some bit set other
274 * than the sign bit).
275 */
276 if ((Py_ssize_t)x < 0 && (sign > 0 || (x << 1) != 0))
277 goto overflow;
278 return (Py_ssize_t)x * sign;
279
280 overflow:
281 PyErr_SetString(PyExc_OverflowError,
282 "long int too large to convert to int");
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000283 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000284}
285
Guido van Rossumd8c80482002-08-13 00:24:58 +0000286/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000287 Returns -1 and sets an error condition if overflow occurs. */
288
289unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000290PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000291{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000293 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000294 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000295
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000297 if (vv != NULL && PyInt_Check(vv)) {
298 long val = PyInt_AsLong(vv);
299 if (val < 0) {
300 PyErr_SetString(PyExc_OverflowError,
301 "can't convert negative value to unsigned long");
302 return (unsigned long) -1;
303 }
304 return val;
305 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000307 return (unsigned long) -1;
308 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000310 i = v->ob_size;
311 x = 0;
312 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000314 "can't convert negative value to unsigned long");
315 return (unsigned long) -1;
316 }
317 while (--i >= 0) {
318 prev = x;
319 x = (x << SHIFT) + v->ob_digit[i];
320 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000322 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000323 return (unsigned long) -1;
324 }
325 }
326 return x;
327}
328
Thomas Hellera4ea6032003-04-17 18:55:45 +0000329/* Get a C unsigned long int from a long int object, ignoring the high bits.
330 Returns -1 and sets an error condition if an error occurs. */
331
332unsigned long
333PyLong_AsUnsignedLongMask(PyObject *vv)
334{
335 register PyLongObject *v;
336 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000337 Py_ssize_t i;
338 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000339
340 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000341 if (vv != NULL && PyInt_Check(vv))
342 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000343 PyErr_BadInternalCall();
344 return (unsigned long) -1;
345 }
346 v = (PyLongObject *)vv;
347 i = v->ob_size;
348 sign = 1;
349 x = 0;
350 if (i < 0) {
351 sign = -1;
352 i = -i;
353 }
354 while (--i >= 0) {
355 x = (x << SHIFT) + v->ob_digit[i];
356 }
357 return x * sign;
358}
359
Tim Peters5b8132f2003-01-31 15:52:05 +0000360int
361_PyLong_Sign(PyObject *vv)
362{
363 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000364
365 assert(v != NULL);
366 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000367
Tim Petersee1a53c2003-02-02 02:57:53 +0000368 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000369}
370
Tim Petersbaefd9e2003-01-28 20:37:45 +0000371size_t
372_PyLong_NumBits(PyObject *vv)
373{
374 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000375 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000377
378 assert(v != NULL);
379 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000380 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000381 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
382 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000383 digit msd = v->ob_digit[ndigits - 1];
384
Tim Peters5b8132f2003-01-31 15:52:05 +0000385 result = (ndigits - 1) * SHIFT;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 if (result / SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000387 goto Overflow;
388 do {
389 ++result;
390 if (result == 0)
391 goto Overflow;
392 msd >>= 1;
393 } while (msd);
394 }
395 return result;
396
397Overflow:
398 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
399 "to express in a platform size_t");
400 return (size_t)-1;
401}
402
Tim Peters2a9b3672001-06-11 21:23:58 +0000403PyObject *
404_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
405 int little_endian, int is_signed)
406{
407 const unsigned char* pstartbyte;/* LSB of bytes */
408 int incr; /* direction to move pstartbyte */
409 const unsigned char* pendbyte; /* MSB of bytes */
410 size_t numsignificantbytes; /* number of bytes that matter */
411 size_t ndigits; /* number of Python long digits */
412 PyLongObject* v; /* result */
413 int idigit = 0; /* next free index in v->ob_digit */
414
415 if (n == 0)
416 return PyLong_FromLong(0L);
417
418 if (little_endian) {
419 pstartbyte = bytes;
420 pendbyte = bytes + n - 1;
421 incr = 1;
422 }
423 else {
424 pstartbyte = bytes + n - 1;
425 pendbyte = bytes;
426 incr = -1;
427 }
428
429 if (is_signed)
430 is_signed = *pendbyte >= 0x80;
431
432 /* Compute numsignificantbytes. This consists of finding the most
433 significant byte. Leading 0 bytes are insignficant if the number
434 is positive, and leading 0xff bytes if negative. */
435 {
436 size_t i;
437 const unsigned char* p = pendbyte;
438 const int pincr = -incr; /* search MSB to LSB */
439 const unsigned char insignficant = is_signed ? 0xff : 0x00;
440
441 for (i = 0; i < n; ++i, p += pincr) {
442 if (*p != insignficant)
443 break;
444 }
445 numsignificantbytes = n - i;
446 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
447 actually has 2 significant bytes. OTOH, 0xff0001 ==
448 -0x00ffff, so we wouldn't *need* to bump it there; but we
449 do for 0xffff = -0x0001. To be safe without bothering to
450 check every case, bump it regardless. */
451 if (is_signed && numsignificantbytes < n)
452 ++numsignificantbytes;
453 }
454
455 /* How many Python long digits do we need? We have
456 8*numsignificantbytes bits, and each Python long digit has SHIFT
457 bits, so it's the ceiling of the quotient. */
458 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
459 if (ndigits > (size_t)INT_MAX)
460 return PyErr_NoMemory();
461 v = _PyLong_New((int)ndigits);
462 if (v == NULL)
463 return NULL;
464
465 /* Copy the bits over. The tricky parts are computing 2's-comp on
466 the fly for signed numbers, and dealing with the mismatch between
467 8-bit bytes and (probably) 15-bit Python digits.*/
468 {
469 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000470 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000471 twodigits accum = 0; /* sliding register */
472 unsigned int accumbits = 0; /* number of bits in accum */
473 const unsigned char* p = pstartbyte;
474
475 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000476 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000477 /* Compute correction for 2's comp, if needed. */
478 if (is_signed) {
479 thisbyte = (0xff ^ thisbyte) + carry;
480 carry = thisbyte >> 8;
481 thisbyte &= 0xff;
482 }
483 /* Because we're going LSB to MSB, thisbyte is
484 more significant than what's already in accum,
485 so needs to be prepended to accum. */
486 accum |= thisbyte << accumbits;
487 accumbits += 8;
488 if (accumbits >= SHIFT) {
489 /* There's enough to fill a Python digit. */
490 assert(idigit < (int)ndigits);
491 v->ob_digit[idigit] = (digit)(accum & MASK);
492 ++idigit;
493 accum >>= SHIFT;
494 accumbits -= SHIFT;
495 assert(accumbits < SHIFT);
496 }
497 }
498 assert(accumbits < SHIFT);
499 if (accumbits) {
500 assert(idigit < (int)ndigits);
501 v->ob_digit[idigit] = (digit)accum;
502 ++idigit;
503 }
504 }
505
506 v->ob_size = is_signed ? -idigit : idigit;
507 return (PyObject *)long_normalize(v);
508}
509
510int
511_PyLong_AsByteArray(PyLongObject* v,
512 unsigned char* bytes, size_t n,
513 int little_endian, int is_signed)
514{
515 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000516 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000517 twodigits accum; /* sliding register */
518 unsigned int accumbits; /* # bits in accum */
519 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
520 twodigits carry; /* for computing 2's-comp */
521 size_t j; /* # bytes filled */
522 unsigned char* p; /* pointer to next byte in bytes */
523 int pincr; /* direction to move p */
524
525 assert(v != NULL && PyLong_Check(v));
526
527 if (v->ob_size < 0) {
528 ndigits = -(v->ob_size);
529 if (!is_signed) {
530 PyErr_SetString(PyExc_TypeError,
531 "can't convert negative long to unsigned");
532 return -1;
533 }
534 do_twos_comp = 1;
535 }
536 else {
537 ndigits = v->ob_size;
538 do_twos_comp = 0;
539 }
540
541 if (little_endian) {
542 p = bytes;
543 pincr = 1;
544 }
545 else {
546 p = bytes + n - 1;
547 pincr = -1;
548 }
549
Tim Peters898cf852001-06-13 20:50:08 +0000550 /* Copy over all the Python digits.
551 It's crucial that every Python digit except for the MSD contribute
552 exactly SHIFT bits to the total, so first assert that the long is
553 normalized. */
554 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000555 j = 0;
556 accum = 0;
557 accumbits = 0;
558 carry = do_twos_comp ? 1 : 0;
559 for (i = 0; i < ndigits; ++i) {
560 twodigits thisdigit = v->ob_digit[i];
561 if (do_twos_comp) {
562 thisdigit = (thisdigit ^ MASK) + carry;
563 carry = thisdigit >> SHIFT;
564 thisdigit &= MASK;
565 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000566 /* Because we're going LSB to MSB, thisdigit is more
567 significant than what's already in accum, so needs to be
568 prepended to accum. */
569 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000570 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000571
Tim Petersede05092001-06-14 08:53:38 +0000572 /* The most-significant digit may be (probably is) at least
573 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000574 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000575 /* Count # of sign bits -- they needn't be stored,
576 * although for signed conversion we need later to
577 * make sure at least one sign bit gets stored.
578 * First shift conceptual sign bit to real sign bit.
579 */
580 stwodigits s = (stwodigits)(thisdigit <<
581 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000582 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000583 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000584 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000585 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000586 }
Tim Petersede05092001-06-14 08:53:38 +0000587 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000588 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000589
Tim Peters2a9b3672001-06-11 21:23:58 +0000590 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000591 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000592 if (j >= n)
593 goto Overflow;
594 ++j;
595 *p = (unsigned char)(accum & 0xff);
596 p += pincr;
597 accumbits -= 8;
598 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000599 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000600 }
601
602 /* Store the straggler (if any). */
603 assert(accumbits < 8);
604 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000605 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000606 if (j >= n)
607 goto Overflow;
608 ++j;
609 if (do_twos_comp) {
610 /* Fill leading bits of the byte with sign bits
611 (appropriately pretending that the long had an
612 infinite supply of sign bits). */
613 accum |= (~(twodigits)0) << accumbits;
614 }
615 *p = (unsigned char)(accum & 0xff);
616 p += pincr;
617 }
Tim Peters05607ad2001-06-13 21:01:27 +0000618 else if (j == n && n > 0 && is_signed) {
619 /* The main loop filled the byte array exactly, so the code
620 just above didn't get to ensure there's a sign bit, and the
621 loop below wouldn't add one either. Make sure a sign bit
622 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000623 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000624 int sign_bit_set = msb >= 0x80;
625 assert(accumbits == 0);
626 if (sign_bit_set == do_twos_comp)
627 return 0;
628 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000629 goto Overflow;
630 }
Tim Peters05607ad2001-06-13 21:01:27 +0000631
632 /* Fill remaining bytes with copies of the sign bit. */
633 {
634 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
635 for ( ; j < n; ++j, p += pincr)
636 *p = signbyte;
637 }
638
Tim Peters2a9b3672001-06-11 21:23:58 +0000639 return 0;
640
641Overflow:
642 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
643 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000644
Tim Peters2a9b3672001-06-11 21:23:58 +0000645}
646
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000647double
648_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
649{
650/* NBITS_WANTED should be > the number of bits in a double's precision,
651 but small enough so that 2**NBITS_WANTED is within the normal double
652 range. nbitsneeded is set to 1 less than that because the most-significant
653 Python digit contains at least 1 significant bit, but we don't want to
654 bother counting them (catering to the worst case cheaply).
655
656 57 is one more than VAX-D double precision; I (Tim) don't know of a double
657 format with more precision than that; it's 1 larger so that we add in at
658 least one round bit to stand in for the ignored least-significant bits.
659*/
660#define NBITS_WANTED 57
661 PyLongObject *v;
662 double x;
663 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000664 Py_ssize_t i;
665 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000666 int nbitsneeded;
667
668 if (vv == NULL || !PyLong_Check(vv)) {
669 PyErr_BadInternalCall();
670 return -1;
671 }
672 v = (PyLongObject *)vv;
673 i = v->ob_size;
674 sign = 1;
675 if (i < 0) {
676 sign = -1;
677 i = -(i);
678 }
679 else if (i == 0) {
680 *exponent = 0;
681 return 0.0;
682 }
683 --i;
684 x = (double)v->ob_digit[i];
685 nbitsneeded = NBITS_WANTED - 1;
686 /* Invariant: i Python digits remain unaccounted for. */
687 while (i > 0 && nbitsneeded > 0) {
688 --i;
689 x = x * multiplier + (double)v->ob_digit[i];
690 nbitsneeded -= SHIFT;
691 }
692 /* There are i digits we didn't shift in. Pretending they're all
693 zeroes, the true value is x * 2**(i*SHIFT). */
694 *exponent = i;
695 assert(x > 0.0);
696 return x * sign;
697#undef NBITS_WANTED
698}
699
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000700/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000701
702double
Tim Peters9f688bf2000-07-07 15:53:28 +0000703PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000704{
Thomas Wouters553489a2006-02-01 21:32:04 +0000705 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000706 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000707
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 if (vv == NULL || !PyLong_Check(vv)) {
709 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000710 return -1;
711 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000712 x = _PyLong_AsScaledDouble(vv, &e);
713 if (x == -1.0 && PyErr_Occurred())
714 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000715 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
716 set correctly after a successful _PyLong_AsScaledDouble() call */
717 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000718 if (e > INT_MAX / SHIFT)
719 goto overflow;
720 errno = 0;
721 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000722 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000723 goto overflow;
724 return x;
725
726overflow:
727 PyErr_SetString(PyExc_OverflowError,
728 "long int too large to convert to float");
729 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000730}
731
Guido van Rossum78694d91998-09-18 14:14:13 +0000732/* Create a new long (or int) object from a C pointer */
733
734PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000735PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000736{
Tim Peters70128a12001-06-16 08:48:40 +0000737#if SIZEOF_VOID_P <= SIZEOF_LONG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738 if ((long)p < 0)
739 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000740 return PyInt_FromLong((long)p);
741#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000742
Tim Peters70128a12001-06-16 08:48:40 +0000743#ifndef HAVE_LONG_LONG
744# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
745#endif
746#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000747# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000748#endif
749 /* optimize null pointers */
750 if (p == NULL)
751 return PyInt_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000753
754#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000755}
756
757/* Get a C pointer from a long object (or an int object in some cases) */
758
759void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000760PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000761{
762 /* This function will allow int or long objects. If vv is neither,
763 then the PyLong_AsLong*() functions will raise the exception:
764 PyExc_SystemError, "bad argument to internal function"
765 */
Tim Peters70128a12001-06-16 08:48:40 +0000766#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000767 long x;
768
Tim Peters70128a12001-06-16 08:48:40 +0000769 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000770 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000772 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 else
774 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000775#else
Tim Peters70128a12001-06-16 08:48:40 +0000776
777#ifndef HAVE_LONG_LONG
778# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
779#endif
780#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000781# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000782#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000783 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000784
Tim Peters70128a12001-06-16 08:48:40 +0000785 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000786 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000788 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789 else
790 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000791
792#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000793
794 if (x == -1 && PyErr_Occurred())
795 return NULL;
796 return (void *)x;
797}
798
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000799#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000800
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000801/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000802 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000803 */
804
Tim Peterscf37dfc2001-06-14 18:42:50 +0000805#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000806
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000807/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000808
809PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000810PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000811{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812 PyLongObject *v;
813 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
814 int ndigits = 0;
815 int negative = 0;
816
817 if (ival < 0) {
818 ival = -ival;
819 negative = 1;
820 }
821
822 /* Count the number of Python digits.
823 We used to pick 5 ("big enough for anything"), but that's a
824 waste of time and space given that 5*15 = 75 bits are rarely
825 needed. */
826 t = (unsigned PY_LONG_LONG)ival;
827 while (t) {
828 ++ndigits;
829 t >>= SHIFT;
830 }
831 v = _PyLong_New(ndigits);
832 if (v != NULL) {
833 digit *p = v->ob_digit;
834 v->ob_size = negative ? -ndigits : ndigits;
835 t = (unsigned PY_LONG_LONG)ival;
836 while (t) {
837 *p++ = (digit)(t & MASK);
838 t >>= SHIFT;
839 }
840 }
841 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000842}
843
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000844/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000845
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000846PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000847PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000848{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000849 PyLongObject *v;
850 unsigned PY_LONG_LONG t;
851 int ndigits = 0;
852
853 /* Count the number of Python digits. */
854 t = (unsigned PY_LONG_LONG)ival;
855 while (t) {
856 ++ndigits;
857 t >>= SHIFT;
858 }
859 v = _PyLong_New(ndigits);
860 if (v != NULL) {
861 digit *p = v->ob_digit;
862 v->ob_size = ndigits;
863 while (ival) {
864 *p++ = (digit)(ival & MASK);
865 ival >>= SHIFT;
866 }
867 }
868 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000869}
870
Martin v. Löwis18e16552006-02-15 17:27:45 +0000871/* Create a new long int object from a C Py_ssize_t. */
872
873PyObject *
874_PyLong_FromSsize_t(Py_ssize_t ival)
875{
876 Py_ssize_t bytes = ival;
877 int one = 1;
878 return _PyLong_FromByteArray(
879 (unsigned char *)&bytes,
880 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
881}
882
883/* Create a new long int object from a C size_t. */
884
885PyObject *
886_PyLong_FromSize_t(size_t ival)
887{
888 size_t bytes = ival;
889 int one = 1;
890 return _PyLong_FromByteArray(
891 (unsigned char *)&bytes,
892 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
893}
894
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000895/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000896 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000897
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000898PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000899PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000900{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000901 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000902 int one = 1;
903 int res;
904
Tim Petersd38b1c72001-09-30 05:09:37 +0000905 if (vv == NULL) {
906 PyErr_BadInternalCall();
907 return -1;
908 }
909 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000910 PyNumberMethods *nb;
911 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000912 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000913 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000914 if ((nb = vv->ob_type->tp_as_number) == NULL ||
915 nb->nb_int == NULL) {
916 PyErr_SetString(PyExc_TypeError, "an integer is required");
917 return -1;
918 }
919 io = (*nb->nb_int) (vv);
920 if (io == NULL)
921 return -1;
922 if (PyInt_Check(io)) {
923 bytes = PyInt_AsLong(io);
924 Py_DECREF(io);
925 return bytes;
926 }
927 if (PyLong_Check(io)) {
928 bytes = PyLong_AsLongLong(io);
929 Py_DECREF(io);
930 return bytes;
931 }
932 Py_DECREF(io);
933 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000934 return -1;
935 }
936
Tim Petersd1a7da62001-06-13 00:35:57 +0000937 res = _PyLong_AsByteArray(
938 (PyLongObject *)vv, (unsigned char *)&bytes,
939 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000940
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000941 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000942 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000943 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000944 else
945 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000946}
947
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000948/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000949 Return -1 and set an error if overflow occurs. */
950
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000951unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000952PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000953{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000954 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000955 int one = 1;
956 int res;
957
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000958 if (vv == NULL || !PyLong_Check(vv)) {
959 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000961 }
962
Tim Petersd1a7da62001-06-13 00:35:57 +0000963 res = _PyLong_AsByteArray(
964 (PyLongObject *)vv, (unsigned char *)&bytes,
965 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000966
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000967 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000968 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000969 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000970 else
971 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000972}
Tim Petersd1a7da62001-06-13 00:35:57 +0000973
Thomas Hellera4ea6032003-04-17 18:55:45 +0000974/* Get a C unsigned long int from a long int object, ignoring the high bits.
975 Returns -1 and sets an error condition if an error occurs. */
976
977unsigned PY_LONG_LONG
978PyLong_AsUnsignedLongLongMask(PyObject *vv)
979{
980 register PyLongObject *v;
981 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000982 Py_ssize_t i;
983 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000984
985 if (vv == NULL || !PyLong_Check(vv)) {
986 PyErr_BadInternalCall();
987 return (unsigned long) -1;
988 }
989 v = (PyLongObject *)vv;
990 i = v->ob_size;
991 sign = 1;
992 x = 0;
993 if (i < 0) {
994 sign = -1;
995 i = -i;
996 }
997 while (--i >= 0) {
998 x = (x << SHIFT) + v->ob_digit[i];
999 }
1000 return x * sign;
1001}
Tim Petersd1a7da62001-06-13 00:35:57 +00001002#undef IS_LITTLE_ENDIAN
1003
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001004#endif /* HAVE_LONG_LONG */
1005
Neil Schemenauerba872e22001-01-04 01:46:03 +00001006
1007static int
1008convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001009 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001010 *a = (PyLongObject *) v;
1011 Py_INCREF(v);
1012 }
1013 else if (PyInt_Check(v)) {
1014 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1015 }
1016 else {
1017 return 0;
1018 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001019 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001020 *b = (PyLongObject *) w;
1021 Py_INCREF(w);
1022 }
1023 else if (PyInt_Check(w)) {
1024 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1025 }
1026 else {
1027 Py_DECREF(*a);
1028 return 0;
1029 }
1030 return 1;
1031}
1032
1033#define CONVERT_BINOP(v, w, a, b) \
1034 if (!convert_binop(v, w, a, b)) { \
1035 Py_INCREF(Py_NotImplemented); \
1036 return Py_NotImplemented; \
1037 }
1038
Tim Peters877a2122002-08-12 05:09:36 +00001039/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1040 * is modified in place, by adding y to it. Carries are propagated as far as
1041 * x[m-1], and the remaining carry (0 or 1) is returned.
1042 */
1043static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001045{
1046 int i;
1047 digit carry = 0;
1048
1049 assert(m >= n);
1050 for (i = 0; i < n; ++i) {
1051 carry += x[i] + y[i];
1052 x[i] = carry & MASK;
1053 carry >>= SHIFT;
1054 assert((carry & 1) == carry);
1055 }
1056 for (; carry && i < m; ++i) {
1057 carry += x[i];
1058 x[i] = carry & MASK;
1059 carry >>= SHIFT;
1060 assert((carry & 1) == carry);
1061 }
1062 return carry;
1063}
1064
1065/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1066 * is modified in place, by subtracting y from it. Borrows are propagated as
1067 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1068 */
1069static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001070v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001071{
1072 int i;
1073 digit borrow = 0;
1074
1075 assert(m >= n);
1076 for (i = 0; i < n; ++i) {
1077 borrow = x[i] - y[i] - borrow;
1078 x[i] = borrow & MASK;
1079 borrow >>= SHIFT;
1080 borrow &= 1; /* keep only 1 sign bit */
1081 }
1082 for (; borrow && i < m; ++i) {
1083 borrow = x[i] - borrow;
1084 x[i] = borrow & MASK;
1085 borrow >>= SHIFT;
1086 borrow &= 1;
1087 }
1088 return borrow;
1089}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001090
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001091/* Multiply by a single digit, ignoring the sign. */
1092
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001093static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001094mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001095{
1096 return muladd1(a, n, (digit)0);
1097}
1098
1099/* Multiply by a single digit and add a single digit, ignoring the sign. */
1100
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001102muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001103{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001104 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001105 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001106 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001107 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001108
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001109 if (z == NULL)
1110 return NULL;
1111 for (i = 0; i < size_a; ++i) {
1112 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001113 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001114 carry >>= SHIFT;
1115 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001116 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001117 return long_normalize(z);
1118}
1119
Tim Peters212e6142001-07-14 12:23:19 +00001120/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1121 in pout, and returning the remainder. pin and pout point at the LSD.
1122 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1123 long_format, but that should be done with great care since longs are
1124 immutable. */
1125
1126static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001128{
1129 twodigits rem = 0;
1130
1131 assert(n > 0 && n <= MASK);
1132 pin += size;
1133 pout += size;
1134 while (--size >= 0) {
1135 digit hi;
1136 rem = (rem << SHIFT) + *--pin;
1137 *--pout = hi = (digit)(rem / n);
1138 rem -= hi * n;
1139 }
1140 return (digit)rem;
1141}
1142
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001143/* Divide a long integer by a digit, returning both the quotient
1144 (as function result) and the remainder (through *prem).
1145 The sign of a is ignored; n should not be zero. */
1146
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001148divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001149{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001152
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001153 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001155 if (z == NULL)
1156 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001157 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001158 return long_normalize(z);
1159}
1160
1161/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001162 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001163 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001164
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165static PyObject *
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001166long_format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001167{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168 register PyLongObject *a = (PyLongObject *)aa;
1169 PyStringObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001170 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001171 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001172 char *p;
1173 int bits;
1174 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001175
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 if (a == NULL || !PyLong_Check(a)) {
1177 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001178 return NULL;
1179 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001180 assert(base >= 2 && base <= 36);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001181 size_a = ABS(a->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001182
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001183 /* Compute a rough upper bound for the length of the string */
1184 i = base;
1185 bits = 0;
1186 while (i > 1) {
1187 ++bits;
1188 i >>= 1;
1189 }
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001190 i = 5 + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001192 if (str == NULL)
1193 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001195 *p = '\0';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001196 if (a->ob_size < 0)
1197 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001198
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001199 if (a->ob_size == 0) {
1200 *--p = '0';
1201 }
1202 else if ((base & (base - 1)) == 0) {
1203 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001204 twodigits accum = 0;
1205 int accumbits = 0; /* # of bits in accum */
1206 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001207 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001208 while ((i >>= 1) > 1)
1209 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001210
1211 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001212 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001213 accumbits += SHIFT;
1214 assert(accumbits >= basebits);
1215 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001216 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001217 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001218 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001219 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001220 accumbits -= basebits;
1221 accum >>= basebits;
1222 } while (i < size_a-1 ? accumbits >= basebits :
1223 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001224 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001225 }
1226 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001227 /* Not 0, and base not a power of 2. Divide repeatedly by
1228 base, but for speed use the highest power of base that
1229 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001230 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001231 digit *pin = a->ob_digit;
1232 PyLongObject *scratch;
1233 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001234 digit powbase = base; /* powbase == base ** power */
1235 int power = 1;
1236 for (;;) {
1237 unsigned long newpow = powbase * (unsigned long)base;
1238 if (newpow >> SHIFT) /* doesn't fit in a digit */
1239 break;
1240 powbase = (digit)newpow;
1241 ++power;
1242 }
Tim Peters212e6142001-07-14 12:23:19 +00001243
1244 /* Get a scratch area for repeated division. */
1245 scratch = _PyLong_New(size);
1246 if (scratch == NULL) {
1247 Py_DECREF(str);
1248 return NULL;
1249 }
1250
1251 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001252 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001253 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001254 digit rem = inplace_divrem1(scratch->ob_digit,
1255 pin, size, powbase);
1256 pin = scratch->ob_digit; /* no need to use a again */
1257 if (pin[size - 1] == 0)
1258 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001259 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001260 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001261 Py_DECREF(str);
1262 return NULL;
1263 })
Tim Peters212e6142001-07-14 12:23:19 +00001264
1265 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001266 assert(ntostore > 0);
1267 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001268 digit nextrem = (digit)(rem / base);
1269 char c = (char)(rem - nextrem * base);
1270 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001271 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001272 *--p = c;
1273 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001274 --ntostore;
1275 /* Termination is a bit delicate: must not
1276 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001277 remaining quotient and rem are both 0. */
1278 } while (ntostore && (size || rem));
1279 } while (size != 0);
1280 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001281 }
1282
Guido van Rossum2c475421992-08-14 15:13:07 +00001283 if (base == 8) {
1284 if (size_a != 0)
1285 *--p = '0';
1286 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001287 else if (base == 16) {
1288 *--p = 'x';
1289 *--p = '0';
1290 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001291 else if (base != 10) {
1292 *--p = '#';
1293 *--p = '0' + base%10;
1294 if (base > 10)
1295 *--p = '0' + base/10;
1296 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001297 if (sign)
1298 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 if (p != PyString_AS_STRING(str)) {
1300 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001301 assert(p > q);
1302 do {
1303 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001304 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 _PyString_Resize((PyObject **)&str,
1306 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001307 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001309}
1310
Thomas Wouters477c8d52006-05-27 19:21:47 +00001311/* Table of digit values for 8-bit string -> integer conversion.
1312 * '0' maps to 0, ..., '9' maps to 9.
1313 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1314 * All other indices map to 37.
1315 * Note that when converting a base B string, a char c is a legitimate
1316 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1317 */
1318int _PyLong_DigitValue[256] = {
1319 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1320 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1321 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1322 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 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, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1326 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 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 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1334 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1335};
1336
1337/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001338 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1339 * non-digit (which may be *str!). A normalized long is returned.
1340 * The point to this routine is that it takes time linear in the number of
1341 * string characters.
1342 */
1343static PyLongObject *
1344long_from_binary_base(char **str, int base)
1345{
1346 char *p = *str;
1347 char *start = p;
1348 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001349 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001350 PyLongObject *z;
1351 twodigits accum;
1352 int bits_in_accum;
1353 digit *pdigit;
1354
1355 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1356 n = base;
1357 for (bits_per_char = -1; n; ++bits_per_char)
1358 n >>= 1;
1359 /* n <- total # of bits needed, while setting p to end-of-string */
1360 n = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001361 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001362 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001363 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001364 n = (p - start) * bits_per_char;
1365 if (n / bits_per_char != p - start) {
1366 PyErr_SetString(PyExc_ValueError,
1367 "long string too large to convert");
1368 return NULL;
1369 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001370 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1371 n = (n + SHIFT - 1) / SHIFT;
1372 z = _PyLong_New(n);
1373 if (z == NULL)
1374 return NULL;
1375 /* Read string from right, and fill in long from left; i.e.,
1376 * from least to most significant in both.
1377 */
1378 accum = 0;
1379 bits_in_accum = 0;
1380 pdigit = z->ob_digit;
1381 while (--p >= start) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001383 assert(k >= 0 && k < base);
1384 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001385 bits_in_accum += bits_per_char;
1386 if (bits_in_accum >= SHIFT) {
1387 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001389 accum >>= SHIFT;
1390 bits_in_accum -= SHIFT;
1391 assert(bits_in_accum < SHIFT);
1392 }
1393 }
1394 if (bits_in_accum) {
1395 assert(bits_in_accum <= SHIFT);
1396 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001397 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001398 }
1399 while (pdigit - z->ob_digit < n)
1400 *pdigit++ = 0;
1401 return long_normalize(z);
1402}
1403
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001404PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001405PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001406{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001407 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001408 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409 PyLongObject *z;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001410 PyObject *strobj, *strrepr;
1411 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001412
Guido van Rossum472c04f1996-12-05 21:57:21 +00001413 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001414 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001415 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001416 return NULL;
1417 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001418 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001419 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001420 if (*str == '+')
1421 ++str;
1422 else if (*str == '-') {
1423 ++str;
1424 sign = -1;
1425 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001426 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001427 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001428 if (base == 0) {
1429 if (str[0] != '0')
1430 base = 10;
1431 else if (str[1] == 'x' || str[1] == 'X')
1432 base = 16;
1433 else
1434 base = 8;
1435 }
1436 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1437 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438
Guido van Rossume6762971998-06-22 03:54:46 +00001439 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001440 if ((base & (base - 1)) == 0)
1441 z = long_from_binary_base(&str, base);
1442 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001443/***
1444Binary bases can be converted in time linear in the number of digits, because
1445Python's representation base is binary. Other bases (including decimal!) use
1446the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001447
Thomas Wouters477c8d52006-05-27 19:21:47 +00001448First some math: the largest integer that can be expressed in N base-B digits
1449is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1450case number of Python digits needed to hold it is the smallest integer n s.t.
1451
1452 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1453 BASE**n >= B**N [taking logs to base BASE]
1454 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1455
1456The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1457this quickly. A Python long with that much space is reserved near the start,
1458and the result is computed into it.
1459
1460The input string is actually treated as being in base base**i (i.e., i digits
1461are processed at a time), where two more static arrays hold:
1462
1463 convwidth_base[base] = the largest integer i such that base**i <= BASE
1464 convmultmax_base[base] = base ** convwidth_base[base]
1465
1466The first of these is the largest i such that i consecutive input digits
1467must fit in a single Python digit. The second is effectively the input
1468base we're really using.
1469
1470Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1471convmultmax_base[base], the result is "simply"
1472
1473 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1474
1475where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001476
1477Error analysis: as above, the number of Python digits `n` needed is worst-
1478case
1479
1480 n >= N * log(B)/log(BASE)
1481
1482where `N` is the number of input digits in base `B`. This is computed via
1483
1484 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1485
1486below. Two numeric concerns are how much space this can waste, and whether
1487the computed result can be too small. To be concrete, assume BASE = 2**15,
1488which is the default (and it's unlikely anyone changes that).
1489
1490Waste isn't a problem: provided the first input digit isn't 0, the difference
1491between the worst-case input with N digits and the smallest input with N
1492digits is about a factor of B, but B is small compared to BASE so at most
1493one allocated Python digit can remain unused on that count. If
1494N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1495and adding 1 returns a result 1 larger than necessary. However, that can't
1496happen: whenever B is a power of 2, long_from_binary_base() is called
1497instead, and it's impossible for B**i to be an integer power of 2**15 when
1498B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1499an exact integer when B is not a power of 2, since B**i has a prime factor
1500other than 2 in that case, but (2**15)**j's only prime factor is 2).
1501
1502The computed result can be too small if the true value of N*log(B)/log(BASE)
1503is a little bit larger than an exact integer, but due to roundoff errors (in
1504computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1505yields a numeric result a little less than that integer. Unfortunately, "how
1506close can a transcendental function get to an integer over some range?"
1507questions are generally theoretically intractable. Computer analysis via
1508continued fractions is practical: expand log(B)/log(BASE) via continued
1509fractions, giving a sequence i/j of "the best" rational approximations. Then
1510j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1511we can get very close to being in trouble, but very rarely. For example,
151276573 is a denominator in one of the continued-fraction approximations to
1513log(10)/log(2**15), and indeed:
1514
1515 >>> log(10)/log(2**15)*76573
1516 16958.000000654003
1517
1518is very close to an integer. If we were working with IEEE single-precision,
1519rounding errors could kill us. Finding worst cases in IEEE double-precision
1520requires better-than-double-precision log() functions, and Tim didn't bother.
1521Instead the code checks to see whether the allocated space is enough as each
1522new Python digit is added, and copies the whole thing to a larger long if not.
1523This should happen extremely rarely, and in fact I don't have a test case
1524that triggers it(!). Instead the code was tested by artificially allocating
1525just 1 digit at the start, so that the copying code was exercised for every
1526digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001527***/
1528 register twodigits c; /* current input character */
1529 Py_ssize_t size_z;
1530 int i;
1531 int convwidth;
1532 twodigits convmultmax, convmult;
1533 digit *pz, *pzstop;
1534 char* scan;
1535
1536 static double log_base_BASE[37] = {0.0e0,};
1537 static int convwidth_base[37] = {0,};
1538 static twodigits convmultmax_base[37] = {0,};
1539
1540 if (log_base_BASE[base] == 0.0) {
1541 twodigits convmax = base;
1542 int i = 1;
1543
1544 log_base_BASE[base] = log((double)base) /
1545 log((double)BASE);
1546 for (;;) {
1547 twodigits next = convmax * base;
1548 if (next > BASE)
1549 break;
1550 convmax = next;
1551 ++i;
1552 }
1553 convmultmax_base[base] = convmax;
1554 assert(i > 0);
1555 convwidth_base[base] = i;
1556 }
1557
1558 /* Find length of the string of numeric characters. */
1559 scan = str;
1560 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
1561 ++scan;
1562
1563 /* Create a long object that can contain the largest possible
1564 * integer with this base and length. Note that there's no
1565 * need to initialize z->ob_digit -- no slot is read up before
1566 * being stored into.
1567 */
1568 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001569 /* Uncomment next line to test exceedingly rare copy code */
1570 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571 assert(size_z > 0);
1572 z = _PyLong_New(size_z);
1573 if (z == NULL)
1574 return NULL;
1575 z->ob_size = 0;
1576
1577 /* `convwidth` consecutive input digits are treated as a single
1578 * digit in base `convmultmax`.
1579 */
1580 convwidth = convwidth_base[base];
1581 convmultmax = convmultmax_base[base];
1582
1583 /* Work ;-) */
1584 while (str < scan) {
1585 /* grab up to convwidth digits from the input string */
1586 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
1587 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1588 c = (twodigits)(c * base +
1589 _PyLong_DigitValue[Py_CHARMASK(*str)]);
1590 assert(c < BASE);
1591 }
1592
1593 convmult = convmultmax;
1594 /* Calculate the shift only if we couldn't get
1595 * convwidth digits.
1596 */
1597 if (i != convwidth) {
1598 convmult = base;
1599 for ( ; i > 1; --i)
1600 convmult *= base;
1601 }
1602
1603 /* Multiply z by convmult, and add c. */
1604 pz = z->ob_digit;
1605 pzstop = pz + z->ob_size;
1606 for (; pz < pzstop; ++pz) {
1607 c += (twodigits)*pz * convmult;
1608 *pz = (digit)(c & MASK);
1609 c >>= SHIFT;
1610 }
1611 /* carry off the current end? */
1612 if (c) {
1613 assert(c < BASE);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001614 if (z->ob_size < size_z) {
1615 *pz = (digit)c;
1616 ++z->ob_size;
1617 }
1618 else {
1619 PyLongObject *tmp;
1620 /* Extremely rare. Get more space. */
1621 assert(z->ob_size == size_z);
1622 tmp = _PyLong_New(size_z + 1);
1623 if (tmp == NULL) {
1624 Py_DECREF(z);
1625 return NULL;
1626 }
1627 memcpy(tmp->ob_digit,
1628 z->ob_digit,
1629 sizeof(digit) * size_z);
1630 Py_DECREF(z);
1631 z = tmp;
1632 z->ob_digit[size_z] = (digit)c;
1633 ++size_z;
1634 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001635 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001636 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001637 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001638 if (z == NULL)
1639 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001640 if (str == start)
1641 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001642 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001643 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001644 if (*str == 'L' || *str == 'l')
1645 str++;
1646 while (*str && isspace(Py_CHARMASK(*str)))
1647 str++;
1648 if (*str != '\0')
1649 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001650 if (pend)
1651 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001652 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001653
1654 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001655 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1657 strobj = PyString_FromStringAndSize(orig_str, slen);
1658 if (strobj == NULL)
1659 return NULL;
1660 strrepr = PyObject_Repr(strobj);
1661 Py_DECREF(strobj);
1662 if (strrepr == NULL)
1663 return NULL;
1664 PyErr_Format(PyExc_ValueError,
1665 "invalid literal for long() with base %d: %s",
1666 base, PyString_AS_STRING(strrepr));
1667 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001668 return NULL;
1669}
1670
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001671#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001672PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001673PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001674{
Walter Dörwald07e14762002-11-06 16:15:14 +00001675 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001677
Walter Dörwald07e14762002-11-06 16:15:14 +00001678 if (buffer == NULL)
1679 return NULL;
1680
1681 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1682 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001683 return NULL;
1684 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001685 result = PyLong_FromString(buffer, NULL, base);
1686 PyMem_FREE(buffer);
1687 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001688}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001689#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001690
Tim Peters9f688bf2000-07-07 15:53:28 +00001691/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001692static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001693 (PyLongObject *, PyLongObject *, PyLongObject **);
1694static PyObject *long_pos(PyLongObject *);
1695static int long_divrem(PyLongObject *, PyLongObject *,
1696 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001697
1698/* Long division with remainder, top-level routine */
1699
Guido van Rossume32e0141992-01-19 16:31:05 +00001700static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001701long_divrem(PyLongObject *a, PyLongObject *b,
1702 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001703{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001704 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001705 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001706
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001707 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001708 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001709 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001710 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001711 }
1712 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001713 (size_a == size_b &&
1714 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001715 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001716 *pdiv = _PyLong_New(0);
1717 Py_INCREF(a);
1718 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001719 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001720 }
1721 if (size_b == 1) {
1722 digit rem = 0;
1723 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001724 if (z == NULL)
1725 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001726 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001727 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001728 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001729 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001730 if (z == NULL)
1731 return -1;
1732 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001733 /* Set the signs.
1734 The quotient z has the sign of a*b;
1735 the remainder r has the sign of a,
1736 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001737 if ((a->ob_size < 0) != (b->ob_size < 0))
1738 z->ob_size = -(z->ob_size);
1739 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1740 (*prem)->ob_size = -((*prem)->ob_size);
1741 *pdiv = z;
1742 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001743}
1744
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001745/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001746
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001747static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001748x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001749{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001750 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001751 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001752 PyLongObject *v = mul1(v1, d);
1753 PyLongObject *w = mul1(w1, d);
1754 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001755 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001756
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001757 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001758 Py_XDECREF(v);
1759 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001760 return NULL;
1761 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001762
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001763 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001764 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001765 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001766
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001767 size_v = ABS(v->ob_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001768 k = size_v - size_w;
1769 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001770
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001772 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1773 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001774 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001775 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001776
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001777 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001778 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001779 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001780 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001781 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001782 if (vj == w->ob_digit[size_w-1])
1783 q = MASK;
1784 else
1785 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1786 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001787
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001788 while (w->ob_digit[size_w-2]*q >
1789 ((
1790 ((twodigits)vj << SHIFT)
1791 + v->ob_digit[j-1]
1792 - q*w->ob_digit[size_w-1]
1793 ) << SHIFT)
1794 + v->ob_digit[j-2])
1795 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001796
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001797 for (i = 0; i < size_w && i+k < size_v; ++i) {
1798 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001799 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001800 carry += v->ob_digit[i+k] - z
1801 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001802 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001803 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1804 carry, SHIFT);
1805 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001806 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001807
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001808 if (i+k < size_v) {
1809 carry += v->ob_digit[i+k];
1810 v->ob_digit[i+k] = 0;
1811 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001812
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001813 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001814 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 else {
1816 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001817 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001818 carry = 0;
1819 for (i = 0; i < size_w && i+k < size_v; ++i) {
1820 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001821 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001822 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1823 BASE_TWODIGITS_TYPE,
1824 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001825 }
1826 }
1827 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001828
Guido van Rossumc206c761995-01-10 15:23:19 +00001829 if (a == NULL)
1830 *prem = NULL;
1831 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001832 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001833 *prem = divrem1(v, d, &d);
1834 /* d receives the (unused) remainder */
1835 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001836 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001837 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001838 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001839 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001840 Py_DECREF(v);
1841 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001842 return a;
1843}
1844
1845/* Methods */
1846
1847static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001848long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001849{
Guido van Rossum9475a232001-10-05 20:51:39 +00001850 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001851}
1852
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001853static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001854long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001855{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001856 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001857}
1858
1859static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001860long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001861{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001862 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001863
Guido van Rossumc6913e71991-11-19 20:26:46 +00001864 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001865 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001866 sign = 0;
1867 else
1868 sign = a->ob_size - b->ob_size;
1869 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001870 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001871 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001872 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1873 ;
1874 if (i < 0)
1875 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001876 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001877 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001878 if (a->ob_size < 0)
1879 sign = -sign;
1880 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001881 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001882 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001883}
1884
Guido van Rossum9bfef441993-03-29 10:43:31 +00001885static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001886long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001887{
1888 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001889 Py_ssize_t i;
1890 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001891
1892 /* This is designed so that Python ints and longs with the
1893 same value hash to the same value, otherwise comparisons
1894 of mapping keys will turn out weird */
1895 i = v->ob_size;
1896 sign = 1;
1897 x = 0;
1898 if (i < 0) {
1899 sign = -1;
1900 i = -(i);
1901 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001902#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001903 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001904 /* Force a native long #-bits (32 or 64) circular shift */
1905 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001906 x += v->ob_digit[i];
1907 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001908#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001909 x = x * sign;
1910 if (x == -1)
1911 x = -2;
1912 return x;
1913}
1914
1915
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001916/* Add the absolute values of two long integers. */
1917
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001918static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001919x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001920{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001921 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001922 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001923 int i;
1924 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001925
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001926 /* Ensure a is the larger of the two: */
1927 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001928 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001929 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001930 size_a = size_b;
1931 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001932 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001933 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001934 if (z == NULL)
1935 return NULL;
1936 for (i = 0; i < size_b; ++i) {
1937 carry += a->ob_digit[i] + b->ob_digit[i];
1938 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001939 carry >>= SHIFT;
1940 }
1941 for (; i < size_a; ++i) {
1942 carry += a->ob_digit[i];
1943 z->ob_digit[i] = carry & MASK;
1944 carry >>= SHIFT;
1945 }
1946 z->ob_digit[i] = carry;
1947 return long_normalize(z);
1948}
1949
1950/* Subtract the absolute values of two integers. */
1951
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001952static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001953x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001954{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001955 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001956 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001957 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001958 int sign = 1;
1959 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001960
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001961 /* Ensure a is the larger of the two: */
1962 if (size_a < size_b) {
1963 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001964 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001965 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001966 size_a = size_b;
1967 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001968 }
1969 else if (size_a == size_b) {
1970 /* Find highest digit where a and b differ: */
1971 i = size_a;
1972 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1973 ;
1974 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001975 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001976 if (a->ob_digit[i] < b->ob_digit[i]) {
1977 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001978 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001979 }
1980 size_a = size_b = i+1;
1981 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001982 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001983 if (z == NULL)
1984 return NULL;
1985 for (i = 0; i < size_b; ++i) {
1986 /* The following assumes unsigned arithmetic
1987 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001988 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001989 z->ob_digit[i] = borrow & MASK;
1990 borrow >>= SHIFT;
1991 borrow &= 1; /* Keep only one sign bit */
1992 }
1993 for (; i < size_a; ++i) {
1994 borrow = a->ob_digit[i] - borrow;
1995 z->ob_digit[i] = borrow & MASK;
1996 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001997 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001998 }
1999 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002000 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002001 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002002 return long_normalize(z);
2003}
2004
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002006long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002007{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002008 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002009
Neil Schemenauerba872e22001-01-04 01:46:03 +00002010 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2011
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002012 if (a->ob_size < 0) {
2013 if (b->ob_size < 0) {
2014 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002015 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002016 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002017 }
2018 else
2019 z = x_sub(b, a);
2020 }
2021 else {
2022 if (b->ob_size < 0)
2023 z = x_sub(a, b);
2024 else
2025 z = x_add(a, b);
2026 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002027 Py_DECREF(a);
2028 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002029 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002030}
2031
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002032static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002033long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002034{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002035 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002036
Neil Schemenauerba872e22001-01-04 01:46:03 +00002037 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2038
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002039 if (a->ob_size < 0) {
2040 if (b->ob_size < 0)
2041 z = x_sub(a, b);
2042 else
2043 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002044 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002045 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002046 }
2047 else {
2048 if (b->ob_size < 0)
2049 z = x_add(a, b);
2050 else
2051 z = x_sub(a, b);
2052 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002053 Py_DECREF(a);
2054 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002055 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002056}
2057
Tim Peters5af4e6c2002-08-12 02:31:19 +00002058/* Grade school multiplication, ignoring the signs.
2059 * Returns the absolute value of the product, or NULL if error.
2060 */
2061static PyLongObject *
2062x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002063{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002064 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002065 Py_ssize_t size_a = ABS(a->ob_size);
2066 Py_ssize_t size_b = ABS(b->ob_size);
2067 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002068
Tim Peters5af4e6c2002-08-12 02:31:19 +00002069 z = _PyLong_New(size_a + size_b);
2070 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002071 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002072
2073 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002074 if (a == b) {
2075 /* Efficient squaring per HAC, Algorithm 14.16:
2076 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2077 * Gives slightly less than a 2x speedup when a == b,
2078 * via exploiting that each entry in the multiplication
2079 * pyramid appears twice (except for the size_a squares).
2080 */
2081 for (i = 0; i < size_a; ++i) {
2082 twodigits carry;
2083 twodigits f = a->ob_digit[i];
2084 digit *pz = z->ob_digit + (i << 1);
2085 digit *pa = a->ob_digit + i + 1;
2086 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002087
Tim Peters0973b992004-08-29 22:16:50 +00002088 SIGCHECK({
2089 Py_DECREF(z);
2090 return NULL;
2091 })
2092
2093 carry = *pz + f * f;
2094 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002095 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002096 assert(carry <= MASK);
2097
2098 /* Now f is added in twice in each column of the
2099 * pyramid it appears. Same as adding f<<1 once.
2100 */
2101 f <<= 1;
2102 while (pa < paend) {
2103 carry += *pz + *pa++ * f;
2104 *pz++ = (digit)(carry & MASK);
2105 carry >>= SHIFT;
2106 assert(carry <= (MASK << 1));
2107 }
2108 if (carry) {
2109 carry += *pz;
2110 *pz++ = (digit)(carry & MASK);
2111 carry >>= SHIFT;
2112 }
2113 if (carry)
2114 *pz += (digit)(carry & MASK);
2115 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002116 }
Tim Peters0973b992004-08-29 22:16:50 +00002117 }
2118 else { /* a is not the same as b -- gradeschool long mult */
2119 for (i = 0; i < size_a; ++i) {
2120 twodigits carry = 0;
2121 twodigits f = a->ob_digit[i];
2122 digit *pz = z->ob_digit + i;
2123 digit *pb = b->ob_digit;
2124 digit *pbend = b->ob_digit + size_b;
2125
2126 SIGCHECK({
2127 Py_DECREF(z);
2128 return NULL;
2129 })
2130
2131 while (pb < pbend) {
2132 carry += *pz + *pb++ * f;
2133 *pz++ = (digit)(carry & MASK);
2134 carry >>= SHIFT;
2135 assert(carry <= MASK);
2136 }
2137 if (carry)
2138 *pz += (digit)(carry & MASK);
2139 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002140 }
2141 }
Tim Peters44121a62002-08-12 06:17:58 +00002142 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002143}
2144
2145/* A helper for Karatsuba multiplication (k_mul).
2146 Takes a long "n" and an integer "size" representing the place to
2147 split, and sets low and high such that abs(n) == (high << size) + low,
2148 viewing the shift as being by digits. The sign bit is ignored, and
2149 the return values are >= 0.
2150 Returns 0 on success, -1 on failure.
2151*/
2152static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002153kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002154{
2155 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002156 Py_ssize_t size_lo, size_hi;
2157 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002158
2159 size_lo = MIN(size_n, size);
2160 size_hi = size_n - size_lo;
2161
2162 if ((hi = _PyLong_New(size_hi)) == NULL)
2163 return -1;
2164 if ((lo = _PyLong_New(size_lo)) == NULL) {
2165 Py_DECREF(hi);
2166 return -1;
2167 }
2168
2169 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2170 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2171
2172 *high = long_normalize(hi);
2173 *low = long_normalize(lo);
2174 return 0;
2175}
2176
Tim Peters60004642002-08-12 22:01:34 +00002177static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2178
Tim Peters5af4e6c2002-08-12 02:31:19 +00002179/* Karatsuba multiplication. Ignores the input signs, and returns the
2180 * absolute value of the product (or NULL if error).
2181 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2182 */
2183static PyLongObject *
2184k_mul(PyLongObject *a, PyLongObject *b)
2185{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002186 Py_ssize_t asize = ABS(a->ob_size);
2187 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002188 PyLongObject *ah = NULL;
2189 PyLongObject *al = NULL;
2190 PyLongObject *bh = NULL;
2191 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002192 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002193 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002194 Py_ssize_t shift; /* the number of digits we split off */
2195 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002196
Tim Peters5af4e6c2002-08-12 02:31:19 +00002197 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2198 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2199 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002200 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002201 * By picking X to be a power of 2, "*X" is just shifting, and it's
2202 * been reduced to 3 multiplies on numbers half the size.
2203 */
2204
Tim Petersfc07e562002-08-12 02:54:10 +00002205 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002206 * is largest.
2207 */
Tim Peters738eda72002-08-12 15:08:20 +00002208 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002209 t1 = a;
2210 a = b;
2211 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002212
2213 i = asize;
2214 asize = bsize;
2215 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002216 }
2217
2218 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002219 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2220 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002221 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002222 return _PyLong_New(0);
2223 else
2224 return x_mul(a, b);
2225 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002226
Tim Peters60004642002-08-12 22:01:34 +00002227 /* If a is small compared to b, splitting on b gives a degenerate
2228 * case with ah==0, and Karatsuba may be (even much) less efficient
2229 * than "grade school" then. However, we can still win, by viewing
2230 * b as a string of "big digits", each of width a->ob_size. That
2231 * leads to a sequence of balanced calls to k_mul.
2232 */
2233 if (2 * asize <= bsize)
2234 return k_lopsided_mul(a, b);
2235
Tim Petersd6974a52002-08-13 20:37:51 +00002236 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002237 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002238 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002239 assert(ah->ob_size > 0); /* the split isn't degenerate */
2240
Tim Peters0973b992004-08-29 22:16:50 +00002241 if (a == b) {
2242 bh = ah;
2243 bl = al;
2244 Py_INCREF(bh);
2245 Py_INCREF(bl);
2246 }
2247 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002248
Tim Petersd64c1de2002-08-12 17:36:03 +00002249 /* The plan:
2250 * 1. Allocate result space (asize + bsize digits: that's always
2251 * enough).
2252 * 2. Compute ah*bh, and copy into result at 2*shift.
2253 * 3. Compute al*bl, and copy into result at 0. Note that this
2254 * can't overlap with #2.
2255 * 4. Subtract al*bl from the result, starting at shift. This may
2256 * underflow (borrow out of the high digit), but we don't care:
2257 * we're effectively doing unsigned arithmetic mod
2258 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2259 * borrows and carries out of the high digit can be ignored.
2260 * 5. Subtract ah*bh from the result, starting at shift.
2261 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2262 * at shift.
2263 */
2264
2265 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002266 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002267 if (ret == NULL) goto fail;
2268#ifdef Py_DEBUG
2269 /* Fill with trash, to catch reference to uninitialized digits. */
2270 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2271#endif
Tim Peters44121a62002-08-12 06:17:58 +00002272
Tim Petersd64c1de2002-08-12 17:36:03 +00002273 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002274 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2275 assert(t1->ob_size >= 0);
2276 assert(2*shift + t1->ob_size <= ret->ob_size);
2277 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2278 t1->ob_size * sizeof(digit));
2279
2280 /* Zero-out the digits higher than the ah*bh copy. */
2281 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002282 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002283 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002284 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002285
Tim Petersd64c1de2002-08-12 17:36:03 +00002286 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002287 if ((t2 = k_mul(al, bl)) == NULL) {
2288 Py_DECREF(t1);
2289 goto fail;
2290 }
2291 assert(t2->ob_size >= 0);
2292 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2293 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002294
2295 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002296 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002297 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002298 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002299
Tim Petersd64c1de2002-08-12 17:36:03 +00002300 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2301 * because it's fresher in cache.
2302 */
Tim Peters738eda72002-08-12 15:08:20 +00002303 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002304 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002305 Py_DECREF(t2);
2306
Tim Petersd64c1de2002-08-12 17:36:03 +00002307 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002308 Py_DECREF(t1);
2309
Tim Petersd64c1de2002-08-12 17:36:03 +00002310 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002311 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2312 Py_DECREF(ah);
2313 Py_DECREF(al);
2314 ah = al = NULL;
2315
Tim Peters0973b992004-08-29 22:16:50 +00002316 if (a == b) {
2317 t2 = t1;
2318 Py_INCREF(t2);
2319 }
2320 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002321 Py_DECREF(t1);
2322 goto fail;
2323 }
2324 Py_DECREF(bh);
2325 Py_DECREF(bl);
2326 bh = bl = NULL;
2327
Tim Peters738eda72002-08-12 15:08:20 +00002328 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002329 Py_DECREF(t1);
2330 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002331 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002332 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002333
Tim Petersd6974a52002-08-13 20:37:51 +00002334 /* Add t3. It's not obvious why we can't run out of room here.
2335 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002336 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002337 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002338 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002339
Tim Peters5af4e6c2002-08-12 02:31:19 +00002340 return long_normalize(ret);
2341
2342 fail:
2343 Py_XDECREF(ret);
2344 Py_XDECREF(ah);
2345 Py_XDECREF(al);
2346 Py_XDECREF(bh);
2347 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002348 return NULL;
2349}
2350
Tim Petersd6974a52002-08-13 20:37:51 +00002351/* (*) Why adding t3 can't "run out of room" above.
2352
Tim Petersab86c2b2002-08-15 20:06:00 +00002353Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2354to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002355
Tim Petersab86c2b2002-08-15 20:06:00 +000023561. For any integer i, i = c(i/2) + f(i/2). In particular,
2357 bsize = c(bsize/2) + f(bsize/2).
23582. shift = f(bsize/2)
23593. asize <= bsize
23604. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2361 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002362
Tim Petersab86c2b2002-08-15 20:06:00 +00002363We allocated asize + bsize result digits, and add t3 into them at an offset
2364of shift. This leaves asize+bsize-shift allocated digit positions for t3
2365to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2366asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002367
Tim Petersab86c2b2002-08-15 20:06:00 +00002368bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2369at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002370
Tim Petersab86c2b2002-08-15 20:06:00 +00002371If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2372digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2373most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002374
Tim Petersab86c2b2002-08-15 20:06:00 +00002375The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002376
Tim Petersab86c2b2002-08-15 20:06:00 +00002377 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002378
Tim Petersab86c2b2002-08-15 20:06:00 +00002379and we have asize + c(bsize/2) available digit positions. We need to show
2380this is always enough. An instance of c(bsize/2) cancels out in both, so
2381the question reduces to whether asize digits is enough to hold
2382(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2383then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2384asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2385digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2386asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002387c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2388is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2389bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002390
Tim Peters48d52c02002-08-14 17:07:32 +00002391Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2392clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2393ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002394*/
2395
Tim Peters60004642002-08-12 22:01:34 +00002396/* b has at least twice the digits of a, and a is big enough that Karatsuba
2397 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2398 * of slices, each with a->ob_size digits, and multiply the slices by a,
2399 * one at a time. This gives k_mul balanced inputs to work with, and is
2400 * also cache-friendly (we compute one double-width slice of the result
2401 * at a time, then move on, never bactracking except for the helpful
2402 * single-width slice overlap between successive partial sums).
2403 */
2404static PyLongObject *
2405k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2406{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002407 const Py_ssize_t asize = ABS(a->ob_size);
2408 Py_ssize_t bsize = ABS(b->ob_size);
2409 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002410 PyLongObject *ret;
2411 PyLongObject *bslice = NULL;
2412
2413 assert(asize > KARATSUBA_CUTOFF);
2414 assert(2 * asize <= bsize);
2415
2416 /* Allocate result space, and zero it out. */
2417 ret = _PyLong_New(asize + bsize);
2418 if (ret == NULL)
2419 return NULL;
2420 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2421
2422 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002423 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002424 if (bslice == NULL)
2425 goto fail;
2426
2427 nbdone = 0;
2428 while (bsize > 0) {
2429 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002430 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002431
2432 /* Multiply the next slice of b by a. */
2433 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2434 nbtouse * sizeof(digit));
2435 bslice->ob_size = nbtouse;
2436 product = k_mul(a, bslice);
2437 if (product == NULL)
2438 goto fail;
2439
2440 /* Add into result. */
2441 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2442 product->ob_digit, product->ob_size);
2443 Py_DECREF(product);
2444
2445 bsize -= nbtouse;
2446 nbdone += nbtouse;
2447 }
2448
2449 Py_DECREF(bslice);
2450 return long_normalize(ret);
2451
2452 fail:
2453 Py_DECREF(ret);
2454 Py_XDECREF(bslice);
2455 return NULL;
2456}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002457
2458static PyObject *
2459long_mul(PyLongObject *v, PyLongObject *w)
2460{
2461 PyLongObject *a, *b, *z;
2462
2463 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002464 Py_INCREF(Py_NotImplemented);
2465 return Py_NotImplemented;
2466 }
2467
Tim Petersd64c1de2002-08-12 17:36:03 +00002468 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002469 /* Negate if exactly one of the inputs is negative. */
2470 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002471 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002472 Py_DECREF(a);
2473 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002474 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002475}
2476
Guido van Rossume32e0141992-01-19 16:31:05 +00002477/* The / and % operators are now defined in terms of divmod().
2478 The expression a mod b has the value a - b*floor(a/b).
2479 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002480 |a| by |b|, with the sign of a. This is also expressed
2481 as a - b*trunc(a/b), if trunc truncates towards zero.
2482 Some examples:
2483 a b a rem b a mod b
2484 13 10 3 3
2485 -13 10 -3 7
2486 13 -10 3 -7
2487 -13 -10 -3 -3
2488 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002489 have different signs. We then subtract one from the 'div'
2490 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002491
Tim Peters47e52ee2004-08-30 02:44:38 +00002492/* Compute
2493 * *pdiv, *pmod = divmod(v, w)
2494 * NULL can be passed for pdiv or pmod, in which case that part of
2495 * the result is simply thrown away. The caller owns a reference to
2496 * each of these it requests (does not pass NULL for).
2497 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002498static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002499l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002500 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002501{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002502 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002503
Guido van Rossume32e0141992-01-19 16:31:05 +00002504 if (long_divrem(v, w, &div, &mod) < 0)
2505 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002506 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2507 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002508 PyLongObject *temp;
2509 PyLongObject *one;
2510 temp = (PyLongObject *) long_add(mod, w);
2511 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002512 mod = temp;
2513 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002514 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002515 return -1;
2516 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002517 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002518 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002519 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2520 Py_DECREF(mod);
2521 Py_DECREF(div);
2522 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002523 return -1;
2524 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002525 Py_DECREF(one);
2526 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002527 div = temp;
2528 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002529 if (pdiv != NULL)
2530 *pdiv = div;
2531 else
2532 Py_DECREF(div);
2533
2534 if (pmod != NULL)
2535 *pmod = mod;
2536 else
2537 Py_DECREF(mod);
2538
Guido van Rossume32e0141992-01-19 16:31:05 +00002539 return 0;
2540}
2541
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002542static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002543long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002544{
Tim Peters47e52ee2004-08-30 02:44:38 +00002545 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002546
2547 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002548 if (l_divmod(a, b, &div, NULL) < 0)
2549 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002550 Py_DECREF(a);
2551 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002552 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002553}
2554
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002555static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002556long_true_divide(PyObject *v, PyObject *w)
2557{
Tim Peterse2a60002001-09-04 06:17:36 +00002558 PyLongObject *a, *b;
2559 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002560 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002561
2562 CONVERT_BINOP(v, w, &a, &b);
2563 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2564 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002565 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2566 Py_DECREF(a);
2567 Py_DECREF(b);
2568 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002569 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002570 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2571 but should really be set correctly after sucessful calls to
2572 _PyLong_AsScaledDouble() */
2573 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002574
2575 if (bd == 0.0) {
2576 PyErr_SetString(PyExc_ZeroDivisionError,
2577 "long division or modulo by zero");
2578 return NULL;
2579 }
2580
2581 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2582 ad /= bd; /* overflow/underflow impossible here */
2583 aexp -= bexp;
2584 if (aexp > INT_MAX / SHIFT)
2585 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002586 else if (aexp < -(INT_MAX / SHIFT))
2587 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002588 errno = 0;
2589 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002590 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002591 goto overflow;
2592 return PyFloat_FromDouble(ad);
2593
2594overflow:
2595 PyErr_SetString(PyExc_OverflowError,
2596 "long/long too large for a float");
2597 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002598
Tim Peters20dab9f2001-09-04 05:31:47 +00002599}
2600
2601static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002602long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002603{
Tim Peters47e52ee2004-08-30 02:44:38 +00002604 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002605
2606 CONVERT_BINOP(v, w, &a, &b);
2607
Tim Peters47e52ee2004-08-30 02:44:38 +00002608 if (l_divmod(a, b, NULL, &mod) < 0)
2609 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002610 Py_DECREF(a);
2611 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002612 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002613}
2614
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002615static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002616long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002617{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002618 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002619 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002620
2621 CONVERT_BINOP(v, w, &a, &b);
2622
2623 if (l_divmod(a, b, &div, &mod) < 0) {
2624 Py_DECREF(a);
2625 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002626 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002627 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002628 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002629 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002630 PyTuple_SetItem(z, 0, (PyObject *) div);
2631 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002632 }
2633 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002634 Py_DECREF(div);
2635 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002636 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002637 Py_DECREF(a);
2638 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002639 return z;
2640}
2641
Tim Peters47e52ee2004-08-30 02:44:38 +00002642/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002643static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002644long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002645{
Tim Peters47e52ee2004-08-30 02:44:38 +00002646 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2647 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002648
Tim Peters47e52ee2004-08-30 02:44:38 +00002649 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002650 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002651 PyLongObject *temp = NULL;
2652
2653 /* 5-ary values. If the exponent is large enough, table is
2654 * precomputed so that table[i] == a**i % c for i in range(32).
2655 */
2656 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2657 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2658
2659 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002660 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002661 if (PyLong_Check(x)) {
2662 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002663 Py_INCREF(x);
2664 }
Tim Petersde7990b2005-07-17 23:45:23 +00002665 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002666 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002667 if (c == NULL)
2668 goto Error;
2669 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002670 else if (x == Py_None)
2671 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002672 else {
2673 Py_DECREF(a);
2674 Py_DECREF(b);
2675 Py_INCREF(Py_NotImplemented);
2676 return Py_NotImplemented;
2677 }
Tim Peters4c483c42001-09-05 06:24:58 +00002678
Tim Peters47e52ee2004-08-30 02:44:38 +00002679 if (b->ob_size < 0) { /* if exponent is negative */
2680 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002681 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002682 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002683 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002684 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002685 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002686 /* else return a float. This works because we know
2687 that this calls float_pow() which converts its
2688 arguments to double. */
2689 Py_DECREF(a);
2690 Py_DECREF(b);
2691 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002692 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002693 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002694
2695 if (c) {
2696 /* if modulus == 0:
2697 raise ValueError() */
2698 if (c->ob_size == 0) {
2699 PyErr_SetString(PyExc_ValueError,
2700 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002701 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002702 }
2703
2704 /* if modulus < 0:
2705 negativeOutput = True
2706 modulus = -modulus */
2707 if (c->ob_size < 0) {
2708 negativeOutput = 1;
2709 temp = (PyLongObject *)_PyLong_Copy(c);
2710 if (temp == NULL)
2711 goto Error;
2712 Py_DECREF(c);
2713 c = temp;
2714 temp = NULL;
2715 c->ob_size = - c->ob_size;
2716 }
2717
2718 /* if modulus == 1:
2719 return 0 */
2720 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2721 z = (PyLongObject *)PyLong_FromLong(0L);
2722 goto Done;
2723 }
2724
2725 /* if base < 0:
2726 base = base % modulus
2727 Having the base positive just makes things easier. */
2728 if (a->ob_size < 0) {
2729 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002730 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002731 Py_DECREF(a);
2732 a = temp;
2733 temp = NULL;
2734 }
2735 }
2736
2737 /* At this point a, b, and c are guaranteed non-negative UNLESS
2738 c is NULL, in which case a may be negative. */
2739
2740 z = (PyLongObject *)PyLong_FromLong(1L);
2741 if (z == NULL)
2742 goto Error;
2743
2744 /* Perform a modular reduction, X = X % c, but leave X alone if c
2745 * is NULL.
2746 */
2747#define REDUCE(X) \
2748 if (c != NULL) { \
2749 if (l_divmod(X, c, NULL, &temp) < 0) \
2750 goto Error; \
2751 Py_XDECREF(X); \
2752 X = temp; \
2753 temp = NULL; \
2754 }
2755
2756 /* Multiply two values, then reduce the result:
2757 result = X*Y % c. If c is NULL, skip the mod. */
2758#define MULT(X, Y, result) \
2759{ \
2760 temp = (PyLongObject *)long_mul(X, Y); \
2761 if (temp == NULL) \
2762 goto Error; \
2763 Py_XDECREF(result); \
2764 result = temp; \
2765 temp = NULL; \
2766 REDUCE(result) \
2767}
2768
2769 if (b->ob_size <= FIVEARY_CUTOFF) {
2770 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2771 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2772 for (i = b->ob_size - 1; i >= 0; --i) {
2773 digit bi = b->ob_digit[i];
2774
2775 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2776 MULT(z, z, z)
2777 if (bi & j)
2778 MULT(z, a, z)
2779 }
2780 }
2781 }
2782 else {
2783 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2784 Py_INCREF(z); /* still holds 1L */
2785 table[0] = z;
2786 for (i = 1; i < 32; ++i)
2787 MULT(table[i-1], a, table[i])
2788
2789 for (i = b->ob_size - 1; i >= 0; --i) {
2790 const digit bi = b->ob_digit[i];
2791
2792 for (j = SHIFT - 5; j >= 0; j -= 5) {
2793 const int index = (bi >> j) & 0x1f;
2794 for (k = 0; k < 5; ++k)
2795 MULT(z, z, z)
2796 if (index)
2797 MULT(z, table[index], z)
2798 }
2799 }
2800 }
2801
2802 if (negativeOutput && (z->ob_size != 0)) {
2803 temp = (PyLongObject *)long_sub(z, c);
2804 if (temp == NULL)
2805 goto Error;
2806 Py_DECREF(z);
2807 z = temp;
2808 temp = NULL;
2809 }
2810 goto Done;
2811
2812 Error:
2813 if (z != NULL) {
2814 Py_DECREF(z);
2815 z = NULL;
2816 }
2817 /* fall through */
2818 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002819 if (b->ob_size > FIVEARY_CUTOFF) {
2820 for (i = 0; i < 32; ++i)
2821 Py_XDECREF(table[i]);
2822 }
Tim Petersde7990b2005-07-17 23:45:23 +00002823 Py_DECREF(a);
2824 Py_DECREF(b);
2825 Py_XDECREF(c);
2826 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002827 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002828}
2829
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002830static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002831long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002832{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002833 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002834 PyLongObject *x;
2835 PyLongObject *w;
2836 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002837 if (w == NULL)
2838 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002839 x = (PyLongObject *) long_add(v, w);
2840 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002841 if (x == NULL)
2842 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002843 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002844 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002845}
2846
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002847static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002848long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002849{
Tim Peters69c2de32001-09-11 22:31:33 +00002850 if (PyLong_CheckExact(v)) {
2851 Py_INCREF(v);
2852 return (PyObject *)v;
2853 }
2854 else
2855 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002856}
2857
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002858static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002859long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002860{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002861 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002862 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002863 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002864 Py_INCREF(v);
2865 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002866 }
Tim Peters69c2de32001-09-11 22:31:33 +00002867 z = (PyLongObject *)_PyLong_Copy(v);
2868 if (z != NULL)
2869 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002870 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002871}
2872
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002873static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002874long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002875{
2876 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002877 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002878 else
2879 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002880}
2881
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002882static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002883long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002884{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002885 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002886}
2887
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002888static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002889long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002890{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002891 PyLongObject *a, *b;
2892 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002893 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002894 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002895 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002896
Neil Schemenauerba872e22001-01-04 01:46:03 +00002897 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2898
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002899 if (a->ob_size < 0) {
2900 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002901 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002902 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002903 if (a1 == NULL)
2904 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002905 a2 = (PyLongObject *) long_rshift(a1, b);
2906 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002907 if (a2 == NULL)
2908 goto rshift_error;
2909 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002910 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002911 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002912 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002913
Neil Schemenauerba872e22001-01-04 01:46:03 +00002914 shiftby = PyLong_AsLong((PyObject *)b);
2915 if (shiftby == -1L && PyErr_Occurred())
2916 goto rshift_error;
2917 if (shiftby < 0) {
2918 PyErr_SetString(PyExc_ValueError,
2919 "negative shift count");
2920 goto rshift_error;
2921 }
2922 wordshift = shiftby / SHIFT;
2923 newsize = ABS(a->ob_size) - wordshift;
2924 if (newsize <= 0) {
2925 z = _PyLong_New(0);
2926 Py_DECREF(a);
2927 Py_DECREF(b);
2928 return (PyObject *)z;
2929 }
2930 loshift = shiftby % SHIFT;
2931 hishift = SHIFT - loshift;
2932 lomask = ((digit)1 << hishift) - 1;
2933 himask = MASK ^ lomask;
2934 z = _PyLong_New(newsize);
2935 if (z == NULL)
2936 goto rshift_error;
2937 if (a->ob_size < 0)
2938 z->ob_size = -(z->ob_size);
2939 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2940 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2941 if (i+1 < newsize)
2942 z->ob_digit[i] |=
2943 (a->ob_digit[j+1] << hishift) & himask;
2944 }
2945 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002946 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002947rshift_error:
2948 Py_DECREF(a);
2949 Py_DECREF(b);
2950 return (PyObject *) z;
2951
Guido van Rossumc6913e71991-11-19 20:26:46 +00002952}
2953
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002954static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002955long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002956{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002957 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002958 PyLongObject *a, *b;
2959 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002960 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002961 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002962 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002963
Neil Schemenauerba872e22001-01-04 01:46:03 +00002964 CONVERT_BINOP(v, w, &a, &b);
2965
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002966 shiftby = PyLong_AsLong((PyObject *)b);
2967 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002968 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002969 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002970 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002971 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002972 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002973 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002974 PyErr_SetString(PyExc_ValueError,
2975 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002976 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002977 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002978 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2979 wordshift = (int)shiftby / SHIFT;
2980 remshift = (int)shiftby - wordshift * SHIFT;
2981
2982 oldsize = ABS(a->ob_size);
2983 newsize = oldsize + wordshift;
2984 if (remshift)
2985 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002986 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002987 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002988 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002989 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002990 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002991 for (i = 0; i < wordshift; i++)
2992 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002993 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002994 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002995 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002996 z->ob_digit[i] = (digit)(accum & MASK);
2997 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002998 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002999 if (remshift)
3000 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003001 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003002 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003003 z = long_normalize(z);
3004lshift_error:
3005 Py_DECREF(a);
3006 Py_DECREF(b);
3007 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003008}
3009
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003010
3011/* Bitwise and/xor/or operations */
3012
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003013static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003014long_bitwise(PyLongObject *a,
3015 int op, /* '&', '|', '^' */
3016 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003017{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003018 digit maska, maskb; /* 0 or MASK */
3019 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003020 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003021 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003022 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003023 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003024 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003025
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003026 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003027 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003028 if (a == NULL)
3029 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003030 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003031 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003032 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003033 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003034 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003035 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003036 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003037 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003038 if (b == NULL) {
3039 Py_DECREF(a);
3040 return NULL;
3041 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003042 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003043 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003044 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003045 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003046 maskb = 0;
3047 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003048
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003049 negz = 0;
3050 switch (op) {
3051 case '^':
3052 if (maska != maskb) {
3053 maska ^= MASK;
3054 negz = -1;
3055 }
3056 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003057 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003058 if (maska && maskb) {
3059 op = '|';
3060 maska ^= MASK;
3061 maskb ^= MASK;
3062 negz = -1;
3063 }
3064 break;
3065 case '|':
3066 if (maska || maskb) {
3067 op = '&';
3068 maska ^= MASK;
3069 maskb ^= MASK;
3070 negz = -1;
3071 }
3072 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003073 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003074
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003075 /* JRH: The original logic here was to allocate the result value (z)
3076 as the longer of the two operands. However, there are some cases
3077 where the result is guaranteed to be shorter than that: AND of two
3078 positives, OR of two negatives: use the shorter number. AND with
3079 mixed signs: use the positive number. OR with mixed signs: use the
3080 negative number. After the transformations above, op will be '&'
3081 iff one of these cases applies, and mask will be non-0 for operands
3082 whose length should be ignored.
3083 */
3084
3085 size_a = a->ob_size;
3086 size_b = b->ob_size;
3087 size_z = op == '&'
3088 ? (maska
3089 ? size_b
3090 : (maskb ? size_a : MIN(size_a, size_b)))
3091 : MAX(size_a, size_b);
3092 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003093 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003094 Py_DECREF(a);
3095 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003096 return NULL;
3097 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003098
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003099 for (i = 0; i < size_z; ++i) {
3100 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3101 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3102 switch (op) {
3103 case '&': z->ob_digit[i] = diga & digb; break;
3104 case '|': z->ob_digit[i] = diga | digb; break;
3105 case '^': z->ob_digit[i] = diga ^ digb; break;
3106 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003107 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003108
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003109 Py_DECREF(a);
3110 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003111 z = long_normalize(z);
3112 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003113 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003114 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003115 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003116 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003117}
3118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003119static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003120long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003121{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003122 PyLongObject *a, *b;
3123 PyObject *c;
3124 CONVERT_BINOP(v, w, &a, &b);
3125 c = long_bitwise(a, '&', b);
3126 Py_DECREF(a);
3127 Py_DECREF(b);
3128 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003129}
3130
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003131static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003132long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003133{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003134 PyLongObject *a, *b;
3135 PyObject *c;
3136 CONVERT_BINOP(v, w, &a, &b);
3137 c = long_bitwise(a, '^', b);
3138 Py_DECREF(a);
3139 Py_DECREF(b);
3140 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003141}
3142
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003143static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003144long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003145{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003146 PyLongObject *a, *b;
3147 PyObject *c;
3148 CONVERT_BINOP(v, w, &a, &b);
3149 c = long_bitwise(a, '|', b);
3150 Py_DECREF(a);
3151 Py_DECREF(b);
3152 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003153}
3154
Guido van Rossum234f9421993-06-17 12:35:49 +00003155static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003156long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003157{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003158 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003159 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003160 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003161 return 0;
3162 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003163 else if (PyLong_Check(*pw)) {
3164 Py_INCREF(*pv);
3165 Py_INCREF(*pw);
3166 return 0;
3167 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003168 return 1; /* Can't do it */
3169}
3170
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003171static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003172long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003173{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003174 if (PyLong_CheckExact(v))
3175 Py_INCREF(v);
3176 else
3177 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003178 return v;
3179}
3180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003181static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003182long_int(PyObject *v)
3183{
3184 long x;
3185 x = PyLong_AsLong(v);
3186 if (PyErr_Occurred()) {
3187 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3188 PyErr_Clear();
3189 if (PyLong_CheckExact(v)) {
3190 Py_INCREF(v);
3191 return v;
3192 }
3193 else
3194 return _PyLong_Copy((PyLongObject *)v);
3195 }
3196 else
3197 return NULL;
3198 }
3199 return PyInt_FromLong(x);
3200}
3201
3202static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003203long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003204{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003205 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003206 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003207 if (result == -1.0 && PyErr_Occurred())
3208 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003209 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003210}
3211
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003212static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003213long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003214{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003215 return long_format(v, 8);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003216}
3217
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003218static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003219long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003220{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003221 return long_format(v, 16);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003222}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003223
3224static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003225long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003226
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227static PyObject *
3228long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3229{
3230 PyObject *x = NULL;
3231 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003232 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003233
Guido van Rossumbef14172001-08-29 15:47:46 +00003234 if (type != &PyLong_Type)
3235 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003236 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3237 &x, &base))
3238 return NULL;
3239 if (x == NULL)
3240 return PyLong_FromLong(0L);
3241 if (base == -909)
3242 return PyNumber_Long(x);
3243 else if (PyString_Check(x))
3244 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003245#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003246 else if (PyUnicode_Check(x))
3247 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3248 PyUnicode_GET_SIZE(x),
3249 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003250#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003251 else {
3252 PyErr_SetString(PyExc_TypeError,
3253 "long() can't convert non-string with explicit base");
3254 return NULL;
3255 }
3256}
3257
Guido van Rossumbef14172001-08-29 15:47:46 +00003258/* Wimpy, slow approach to tp_new calls for subtypes of long:
3259 first create a regular long from whatever arguments we got,
3260 then allocate a subtype instance and initialize it from
3261 the regular long. The regular long is then thrown away.
3262*/
3263static PyObject *
3264long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3265{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003266 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003267 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003268
3269 assert(PyType_IsSubtype(type, &PyLong_Type));
3270 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3271 if (tmp == NULL)
3272 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003273 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003274 n = tmp->ob_size;
3275 if (n < 0)
3276 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003277 newobj = (PyLongObject *)type->tp_alloc(type, n);
3278 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003279 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003280 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003281 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003282 assert(PyLong_Check(newobj));
3283 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003284 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003285 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003286 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003287 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003288}
3289
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003290static PyObject *
3291long_getnewargs(PyLongObject *v)
3292{
3293 return Py_BuildValue("(N)", _PyLong_Copy(v));
3294}
3295
3296static PyMethodDef long_methods[] = {
3297 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3298 {NULL, NULL} /* sentinel */
3299};
3300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003301PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003302"long(x[, base]) -> integer\n\
3303\n\
3304Convert a string or number to a long integer, if possible. A floating\n\
3305point argument will be truncated towards zero (this does not include a\n\
3306string representation of a floating point number!) When converting a\n\
3307string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003308converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003309
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003310static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003311 (binaryfunc) long_add, /*nb_add*/
3312 (binaryfunc) long_sub, /*nb_subtract*/
3313 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003314 long_mod, /*nb_remainder*/
3315 long_divmod, /*nb_divmod*/
3316 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003317 (unaryfunc) long_neg, /*nb_negative*/
3318 (unaryfunc) long_pos, /*tp_positive*/
3319 (unaryfunc) long_abs, /*tp_absolute*/
3320 (inquiry) long_nonzero, /*tp_nonzero*/
3321 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003322 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003323 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003324 long_and, /*nb_and*/
3325 long_xor, /*nb_xor*/
3326 long_or, /*nb_or*/
3327 long_coerce, /*nb_coerce*/
3328 long_int, /*nb_int*/
3329 long_long, /*nb_long*/
3330 long_float, /*nb_float*/
3331 long_oct, /*nb_oct*/
3332 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003333 0, /* nb_inplace_add */
3334 0, /* nb_inplace_subtract */
3335 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003336 0, /* nb_inplace_remainder */
3337 0, /* nb_inplace_power */
3338 0, /* nb_inplace_lshift */
3339 0, /* nb_inplace_rshift */
3340 0, /* nb_inplace_and */
3341 0, /* nb_inplace_xor */
3342 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003343 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003344 long_true_divide, /* nb_true_divide */
3345 0, /* nb_inplace_floor_divide */
3346 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003347 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003348};
3349
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003350PyTypeObject PyLong_Type = {
3351 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003352 0, /* ob_size */
3353 "long", /* tp_name */
3354 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3355 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003356 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003357 0, /* tp_print */
3358 0, /* tp_getattr */
3359 0, /* tp_setattr */
3360 (cmpfunc)long_compare, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003361 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 &long_as_number, /* tp_as_number */
3363 0, /* tp_as_sequence */
3364 0, /* tp_as_mapping */
3365 (hashfunc)long_hash, /* tp_hash */
3366 0, /* tp_call */
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003367 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 PyObject_GenericGetAttr, /* tp_getattro */
3369 0, /* tp_setattro */
3370 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003372 long_doc, /* tp_doc */
3373 0, /* tp_traverse */
3374 0, /* tp_clear */
3375 0, /* tp_richcompare */
3376 0, /* tp_weaklistoffset */
3377 0, /* tp_iter */
3378 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003379 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003380 0, /* tp_members */
3381 0, /* tp_getset */
3382 0, /* tp_base */
3383 0, /* tp_dict */
3384 0, /* tp_descr_get */
3385 0, /* tp_descr_set */
3386 0, /* tp_dictoffset */
3387 0, /* tp_init */
3388 0, /* tp_alloc */
3389 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003390 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003391};