blob: f68656bb5e9e1e1168728ad2b3af6823a50e31aa [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/* Long (arbitrary precision) integer object implementation */
2
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003/* XXX The functional organization of this file is terrible */
4
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00006#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00008#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +00009
Tim Peters5af4e6c2002-08-12 02:31:19 +000010/* For long multiplication, use the O(N**2) school algorithm unless
11 * both operands contain more than KARATSUBA_CUTOFF digits (this
12 * being an internal Python long digit, in base BASE).
13 */
Tim Peters0973b992004-08-29 22:16:50 +000014#define KARATSUBA_CUTOFF 70
15#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000016
Tim Peters47e52ee2004-08-30 02:44:38 +000017/* For exponentiation, use the binary left-to-right algorithm
18 * unless the exponent contains more than FIVEARY_CUTOFF digits.
19 * In that case, do 5 bits at a time. The potential drawback is that
20 * a table of 2**5 intermediate results is computed.
21 */
22#define FIVEARY_CUTOFF 8
23
Guido van Rossume32e0141992-01-19 16:31:05 +000024#define ABS(x) ((x) < 0 ? -(x) : (x))
25
Tim Peters5af4e6c2002-08-12 02:31:19 +000026#undef MIN
27#undef MAX
28#define MAX(x, y) ((x) < (y) ? (y) : (x))
29#define MIN(x, y) ((x) > (y) ? (y) : (x))
30
Guido van Rossume32e0141992-01-19 16:31:05 +000031/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000032static PyLongObject *long_normalize(PyLongObject *);
33static PyLongObject *mul1(PyLongObject *, wdigit);
34static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000035static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossumd2dbecb2006-08-18 16:29:54 +000036static PyObject *long_format(PyObject *aa, int base);
Guido van Rossume32e0141992-01-19 16:31:05 +000037
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000039 if (--_Py_Ticker < 0) { \
40 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000041 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000042 }
43
Guido van Rossumedcc38a1991-05-05 20:09:44 +000044/* Normalize (remove leading zeros from) a long int object.
45 Doesn't attempt to free the storage--in most cases, due to the nature
46 of the algorithms used, this could save at most be one word anyway. */
47
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000049long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000050{
Martin v. Löwis18e16552006-02-15 17:27:45 +000051 Py_ssize_t j = ABS(v->ob_size);
52 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000053
Guido van Rossumedcc38a1991-05-05 20:09:44 +000054 while (i > 0 && v->ob_digit[i-1] == 0)
55 --i;
56 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000057 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000058 return v;
59}
60
61/* Allocate a new long int object with size digits.
62 Return NULL and set exception if we run out of memory. */
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000065_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000066{
Thomas Wouters477c8d52006-05-27 19:21:47 +000067 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 PyErr_NoMemory();
69 return NULL;
70 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000072}
73
Tim Peters64b5ce32001-09-10 20:52:51 +000074PyObject *
75_PyLong_Copy(PyLongObject *src)
76{
77 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000079
80 assert(src != NULL);
81 i = src->ob_size;
82 if (i < 0)
83 i = -(i);
84 result = _PyLong_New(i);
85 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000086 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000087 while (--i >= 0)
88 result->ob_digit[i] = src->ob_digit[i];
89 }
90 return (PyObject *)result;
91}
92
Guido van Rossumedcc38a1991-05-05 20:09:44 +000093/* Create a new long int object from a C long int */
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000096PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000097{
Tim Petersce9de2f2001-06-14 04:56:19 +000098 PyLongObject *v;
99 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
100 int ndigits = 0;
101 int negative = 0;
102
103 if (ival < 0) {
104 ival = -ival;
105 negative = 1;
106 }
107
108 /* Count the number of Python digits.
109 We used to pick 5 ("big enough for anything"), but that's a
110 waste of time and space given that 5*15 = 75 bits are rarely
111 needed. */
112 t = (unsigned long)ival;
113 while (t) {
114 ++ndigits;
115 t >>= SHIFT;
116 }
117 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000118 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000119 digit *p = v->ob_digit;
120 v->ob_size = negative ? -ndigits : ndigits;
121 t = (unsigned long)ival;
122 while (t) {
123 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000124 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000125 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000126 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128}
129
Guido van Rossum53756b11997-01-03 17:14:46 +0000130/* Create a new long int object from a C unsigned long int */
131
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000133PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000134{
Tim Petersce9de2f2001-06-14 04:56:19 +0000135 PyLongObject *v;
136 unsigned long t;
137 int ndigits = 0;
138
139 /* Count the number of Python digits. */
140 t = (unsigned long)ival;
141 while (t) {
142 ++ndigits;
143 t >>= SHIFT;
144 }
145 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000146 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000147 digit *p = v->ob_digit;
148 v->ob_size = ndigits;
149 while (ival) {
150 *p++ = (digit)(ival & MASK);
151 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000152 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000153 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000155}
156
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000157/* Create a new long int object from a C double */
158
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000161{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163 double frac;
164 int i, ndig, expo, neg;
165 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000166 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000167 PyErr_SetString(PyExc_OverflowError,
168 "cannot convert float infinity to long");
169 return NULL;
170 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000171 if (dval < 0.0) {
172 neg = 1;
173 dval = -dval;
174 }
175 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
176 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000178 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000180 if (v == NULL)
181 return NULL;
182 frac = ldexp(frac, (expo-1) % SHIFT + 1);
183 for (i = ndig; --i >= 0; ) {
184 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000185 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000186 frac = frac - (double)bits;
187 frac = ldexp(frac, SHIFT);
188 }
189 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000190 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000192}
193
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
195 * anything about what happens when a signed integer operation overflows,
196 * and some compilers think they're doing you a favor by being "clever"
197 * then. The bit pattern for the largest postive signed long is
198 * (unsigned long)LONG_MAX, and for the smallest negative signed long
199 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
200 * However, some other compilers warn about applying unary minus to an
201 * unsigned operand. Hence the weird "0-".
202 */
203#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
204#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
205
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000206/* Get a C long int from a long int object.
207 Returns -1 and sets an error condition if overflow occurs. */
208
209long
Tim Peters9f688bf2000-07-07 15:53:28 +0000210PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000211{
Guido van Rossumf7531811998-05-26 14:33:37 +0000212 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000214 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215 Py_ssize_t i;
216 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000217
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000219 if (vv != NULL && PyInt_Check(vv))
220 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000222 return -1;
223 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000225 i = v->ob_size;
226 sign = 1;
227 x = 0;
228 if (i < 0) {
229 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000230 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000231 }
232 while (--i >= 0) {
233 prev = x;
234 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000235 if ((x >> SHIFT) != prev)
236 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000237 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000238 /* Haven't lost any bits, but casting to long requires extra care
239 * (see comment above).
240 */
241 if (x <= (unsigned long)LONG_MAX) {
242 return (long)x * sign;
243 }
244 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
245 return LONG_MIN;
246 }
247 /* else overflow */
Guido van Rossumf7531811998-05-26 14:33:37 +0000248
249 overflow:
250 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000251 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000252 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000253}
254
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000255/* Get a Py_ssize_t from a long int object.
256 Returns -1 and sets an error condition if overflow occurs. */
257
258Py_ssize_t
259_PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000260 register PyLongObject *v;
261 size_t x, prev;
262 Py_ssize_t i;
263 int sign;
264
265 if (vv == NULL || !PyLong_Check(vv)) {
266 PyErr_BadInternalCall();
267 return -1;
268 }
269 v = (PyLongObject *)vv;
270 i = v->ob_size;
271 sign = 1;
272 x = 0;
273 if (i < 0) {
274 sign = -1;
275 i = -(i);
276 }
277 while (--i >= 0) {
278 prev = x;
279 x = (x << SHIFT) + v->ob_digit[i];
280 if ((x >> SHIFT) != prev)
281 goto overflow;
282 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000283 /* Haven't lost any bits, but casting to a signed type requires
284 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285 */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000286 if (x <= (size_t)PY_SSIZE_T_MAX) {
287 return (Py_ssize_t)x * sign;
288 }
289 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
290 return PY_SSIZE_T_MIN;
291 }
292 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000293
294 overflow:
295 PyErr_SetString(PyExc_OverflowError,
296 "long int too large to convert to int");
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000297 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000298}
299
Guido van Rossumd8c80482002-08-13 00:24:58 +0000300/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000301 Returns -1 and sets an error condition if overflow occurs. */
302
303unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000304PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000305{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000307 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000308 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000309
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000311 if (vv != NULL && PyInt_Check(vv)) {
312 long val = PyInt_AsLong(vv);
313 if (val < 0) {
314 PyErr_SetString(PyExc_OverflowError,
315 "can't convert negative value to unsigned long");
316 return (unsigned long) -1;
317 }
318 return val;
319 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000321 return (unsigned long) -1;
322 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000324 i = v->ob_size;
325 x = 0;
326 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000328 "can't convert negative value to unsigned long");
329 return (unsigned long) -1;
330 }
331 while (--i >= 0) {
332 prev = x;
333 x = (x << SHIFT) + v->ob_digit[i];
334 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000336 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000337 return (unsigned long) -1;
338 }
339 }
340 return x;
341}
342
Thomas Hellera4ea6032003-04-17 18:55:45 +0000343/* Get a C unsigned long int from a long int object, ignoring the high bits.
344 Returns -1 and sets an error condition if an error occurs. */
345
346unsigned long
347PyLong_AsUnsignedLongMask(PyObject *vv)
348{
349 register PyLongObject *v;
350 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000351 Py_ssize_t i;
352 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000353
354 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000355 if (vv != NULL && PyInt_Check(vv))
356 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000357 PyErr_BadInternalCall();
358 return (unsigned long) -1;
359 }
360 v = (PyLongObject *)vv;
361 i = v->ob_size;
362 sign = 1;
363 x = 0;
364 if (i < 0) {
365 sign = -1;
366 i = -i;
367 }
368 while (--i >= 0) {
369 x = (x << SHIFT) + v->ob_digit[i];
370 }
371 return x * sign;
372}
373
Tim Peters5b8132f2003-01-31 15:52:05 +0000374int
375_PyLong_Sign(PyObject *vv)
376{
377 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000378
379 assert(v != NULL);
380 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000381
Tim Petersee1a53c2003-02-02 02:57:53 +0000382 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000383}
384
Tim Petersbaefd9e2003-01-28 20:37:45 +0000385size_t
386_PyLong_NumBits(PyObject *vv)
387{
388 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000389 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000391
392 assert(v != NULL);
393 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000394 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000395 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
396 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000397 digit msd = v->ob_digit[ndigits - 1];
398
Tim Peters5b8132f2003-01-31 15:52:05 +0000399 result = (ndigits - 1) * SHIFT;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 if (result / SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000401 goto Overflow;
402 do {
403 ++result;
404 if (result == 0)
405 goto Overflow;
406 msd >>= 1;
407 } while (msd);
408 }
409 return result;
410
411Overflow:
412 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
413 "to express in a platform size_t");
414 return (size_t)-1;
415}
416
Tim Peters2a9b3672001-06-11 21:23:58 +0000417PyObject *
418_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
419 int little_endian, int is_signed)
420{
421 const unsigned char* pstartbyte;/* LSB of bytes */
422 int incr; /* direction to move pstartbyte */
423 const unsigned char* pendbyte; /* MSB of bytes */
424 size_t numsignificantbytes; /* number of bytes that matter */
425 size_t ndigits; /* number of Python long digits */
426 PyLongObject* v; /* result */
427 int idigit = 0; /* next free index in v->ob_digit */
428
429 if (n == 0)
430 return PyLong_FromLong(0L);
431
432 if (little_endian) {
433 pstartbyte = bytes;
434 pendbyte = bytes + n - 1;
435 incr = 1;
436 }
437 else {
438 pstartbyte = bytes + n - 1;
439 pendbyte = bytes;
440 incr = -1;
441 }
442
443 if (is_signed)
444 is_signed = *pendbyte >= 0x80;
445
446 /* Compute numsignificantbytes. This consists of finding the most
447 significant byte. Leading 0 bytes are insignficant if the number
448 is positive, and leading 0xff bytes if negative. */
449 {
450 size_t i;
451 const unsigned char* p = pendbyte;
452 const int pincr = -incr; /* search MSB to LSB */
453 const unsigned char insignficant = is_signed ? 0xff : 0x00;
454
455 for (i = 0; i < n; ++i, p += pincr) {
456 if (*p != insignficant)
457 break;
458 }
459 numsignificantbytes = n - i;
460 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
461 actually has 2 significant bytes. OTOH, 0xff0001 ==
462 -0x00ffff, so we wouldn't *need* to bump it there; but we
463 do for 0xffff = -0x0001. To be safe without bothering to
464 check every case, bump it regardless. */
465 if (is_signed && numsignificantbytes < n)
466 ++numsignificantbytes;
467 }
468
469 /* How many Python long digits do we need? We have
470 8*numsignificantbytes bits, and each Python long digit has SHIFT
471 bits, so it's the ceiling of the quotient. */
472 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
473 if (ndigits > (size_t)INT_MAX)
474 return PyErr_NoMemory();
475 v = _PyLong_New((int)ndigits);
476 if (v == NULL)
477 return NULL;
478
479 /* Copy the bits over. The tricky parts are computing 2's-comp on
480 the fly for signed numbers, and dealing with the mismatch between
481 8-bit bytes and (probably) 15-bit Python digits.*/
482 {
483 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000484 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000485 twodigits accum = 0; /* sliding register */
486 unsigned int accumbits = 0; /* number of bits in accum */
487 const unsigned char* p = pstartbyte;
488
489 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000490 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000491 /* Compute correction for 2's comp, if needed. */
492 if (is_signed) {
493 thisbyte = (0xff ^ thisbyte) + carry;
494 carry = thisbyte >> 8;
495 thisbyte &= 0xff;
496 }
497 /* Because we're going LSB to MSB, thisbyte is
498 more significant than what's already in accum,
499 so needs to be prepended to accum. */
500 accum |= thisbyte << accumbits;
501 accumbits += 8;
502 if (accumbits >= SHIFT) {
503 /* There's enough to fill a Python digit. */
504 assert(idigit < (int)ndigits);
505 v->ob_digit[idigit] = (digit)(accum & MASK);
506 ++idigit;
507 accum >>= SHIFT;
508 accumbits -= SHIFT;
509 assert(accumbits < SHIFT);
510 }
511 }
512 assert(accumbits < SHIFT);
513 if (accumbits) {
514 assert(idigit < (int)ndigits);
515 v->ob_digit[idigit] = (digit)accum;
516 ++idigit;
517 }
518 }
519
520 v->ob_size = is_signed ? -idigit : idigit;
521 return (PyObject *)long_normalize(v);
522}
523
524int
525_PyLong_AsByteArray(PyLongObject* v,
526 unsigned char* bytes, size_t n,
527 int little_endian, int is_signed)
528{
529 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000531 twodigits accum; /* sliding register */
532 unsigned int accumbits; /* # bits in accum */
533 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
534 twodigits carry; /* for computing 2's-comp */
535 size_t j; /* # bytes filled */
536 unsigned char* p; /* pointer to next byte in bytes */
537 int pincr; /* direction to move p */
538
539 assert(v != NULL && PyLong_Check(v));
540
541 if (v->ob_size < 0) {
542 ndigits = -(v->ob_size);
543 if (!is_signed) {
544 PyErr_SetString(PyExc_TypeError,
545 "can't convert negative long to unsigned");
546 return -1;
547 }
548 do_twos_comp = 1;
549 }
550 else {
551 ndigits = v->ob_size;
552 do_twos_comp = 0;
553 }
554
555 if (little_endian) {
556 p = bytes;
557 pincr = 1;
558 }
559 else {
560 p = bytes + n - 1;
561 pincr = -1;
562 }
563
Tim Peters898cf852001-06-13 20:50:08 +0000564 /* Copy over all the Python digits.
565 It's crucial that every Python digit except for the MSD contribute
566 exactly SHIFT bits to the total, so first assert that the long is
567 normalized. */
568 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000569 j = 0;
570 accum = 0;
571 accumbits = 0;
572 carry = do_twos_comp ? 1 : 0;
573 for (i = 0; i < ndigits; ++i) {
574 twodigits thisdigit = v->ob_digit[i];
575 if (do_twos_comp) {
576 thisdigit = (thisdigit ^ MASK) + carry;
577 carry = thisdigit >> SHIFT;
578 thisdigit &= MASK;
579 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000580 /* Because we're going LSB to MSB, thisdigit is more
581 significant than what's already in accum, so needs to be
582 prepended to accum. */
583 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000584 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000585
Tim Petersede05092001-06-14 08:53:38 +0000586 /* The most-significant digit may be (probably is) at least
587 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000588 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000589 /* Count # of sign bits -- they needn't be stored,
590 * although for signed conversion we need later to
591 * make sure at least one sign bit gets stored.
592 * First shift conceptual sign bit to real sign bit.
593 */
594 stwodigits s = (stwodigits)(thisdigit <<
595 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000596 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000597 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000598 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000599 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000600 }
Tim Petersede05092001-06-14 08:53:38 +0000601 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000602 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000603
Tim Peters2a9b3672001-06-11 21:23:58 +0000604 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000605 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000606 if (j >= n)
607 goto Overflow;
608 ++j;
609 *p = (unsigned char)(accum & 0xff);
610 p += pincr;
611 accumbits -= 8;
612 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000613 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000614 }
615
616 /* Store the straggler (if any). */
617 assert(accumbits < 8);
618 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000619 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000620 if (j >= n)
621 goto Overflow;
622 ++j;
623 if (do_twos_comp) {
624 /* Fill leading bits of the byte with sign bits
625 (appropriately pretending that the long had an
626 infinite supply of sign bits). */
627 accum |= (~(twodigits)0) << accumbits;
628 }
629 *p = (unsigned char)(accum & 0xff);
630 p += pincr;
631 }
Tim Peters05607ad2001-06-13 21:01:27 +0000632 else if (j == n && n > 0 && is_signed) {
633 /* The main loop filled the byte array exactly, so the code
634 just above didn't get to ensure there's a sign bit, and the
635 loop below wouldn't add one either. Make sure a sign bit
636 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000637 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000638 int sign_bit_set = msb >= 0x80;
639 assert(accumbits == 0);
640 if (sign_bit_set == do_twos_comp)
641 return 0;
642 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000643 goto Overflow;
644 }
Tim Peters05607ad2001-06-13 21:01:27 +0000645
646 /* Fill remaining bytes with copies of the sign bit. */
647 {
648 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
649 for ( ; j < n; ++j, p += pincr)
650 *p = signbyte;
651 }
652
Tim Peters2a9b3672001-06-11 21:23:58 +0000653 return 0;
654
655Overflow:
656 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
657 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000658
Tim Peters2a9b3672001-06-11 21:23:58 +0000659}
660
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000661double
662_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
663{
664/* NBITS_WANTED should be > the number of bits in a double's precision,
665 but small enough so that 2**NBITS_WANTED is within the normal double
666 range. nbitsneeded is set to 1 less than that because the most-significant
667 Python digit contains at least 1 significant bit, but we don't want to
668 bother counting them (catering to the worst case cheaply).
669
670 57 is one more than VAX-D double precision; I (Tim) don't know of a double
671 format with more precision than that; it's 1 larger so that we add in at
672 least one round bit to stand in for the ignored least-significant bits.
673*/
674#define NBITS_WANTED 57
675 PyLongObject *v;
676 double x;
677 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678 Py_ssize_t i;
679 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000680 int nbitsneeded;
681
682 if (vv == NULL || !PyLong_Check(vv)) {
683 PyErr_BadInternalCall();
684 return -1;
685 }
686 v = (PyLongObject *)vv;
687 i = v->ob_size;
688 sign = 1;
689 if (i < 0) {
690 sign = -1;
691 i = -(i);
692 }
693 else if (i == 0) {
694 *exponent = 0;
695 return 0.0;
696 }
697 --i;
698 x = (double)v->ob_digit[i];
699 nbitsneeded = NBITS_WANTED - 1;
700 /* Invariant: i Python digits remain unaccounted for. */
701 while (i > 0 && nbitsneeded > 0) {
702 --i;
703 x = x * multiplier + (double)v->ob_digit[i];
704 nbitsneeded -= SHIFT;
705 }
706 /* There are i digits we didn't shift in. Pretending they're all
707 zeroes, the true value is x * 2**(i*SHIFT). */
708 *exponent = i;
709 assert(x > 0.0);
710 return x * sign;
711#undef NBITS_WANTED
712}
713
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000714/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000715
716double
Tim Peters9f688bf2000-07-07 15:53:28 +0000717PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000718{
Thomas Wouters553489a2006-02-01 21:32:04 +0000719 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000720 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000721
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 if (vv == NULL || !PyLong_Check(vv)) {
723 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000724 return -1;
725 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000726 x = _PyLong_AsScaledDouble(vv, &e);
727 if (x == -1.0 && PyErr_Occurred())
728 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000729 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
730 set correctly after a successful _PyLong_AsScaledDouble() call */
731 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000732 if (e > INT_MAX / SHIFT)
733 goto overflow;
734 errno = 0;
735 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000736 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000737 goto overflow;
738 return x;
739
740overflow:
741 PyErr_SetString(PyExc_OverflowError,
742 "long int too large to convert to float");
743 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000744}
745
Guido van Rossum78694d91998-09-18 14:14:13 +0000746/* Create a new long (or int) object from a C pointer */
747
748PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000749PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000750{
Tim Peters70128a12001-06-16 08:48:40 +0000751#if SIZEOF_VOID_P <= SIZEOF_LONG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752 if ((long)p < 0)
753 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000754 return PyInt_FromLong((long)p);
755#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000756
Tim Peters70128a12001-06-16 08:48:40 +0000757#ifndef HAVE_LONG_LONG
758# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
759#endif
760#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000761# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000762#endif
763 /* optimize null pointers */
764 if (p == NULL)
765 return PyInt_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000767
768#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000769}
770
771/* Get a C pointer from a long object (or an int object in some cases) */
772
773void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000774PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000775{
776 /* This function will allow int or long objects. If vv is neither,
777 then the PyLong_AsLong*() functions will raise the exception:
778 PyExc_SystemError, "bad argument to internal function"
779 */
Tim Peters70128a12001-06-16 08:48:40 +0000780#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000781 long x;
782
Tim Peters70128a12001-06-16 08:48:40 +0000783 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000784 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000786 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 else
788 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000789#else
Tim Peters70128a12001-06-16 08:48:40 +0000790
791#ifndef HAVE_LONG_LONG
792# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
793#endif
794#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000795# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000796#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000797 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000798
Tim Peters70128a12001-06-16 08:48:40 +0000799 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000800 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000802 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803 else
804 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000805
806#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000807
808 if (x == -1 && PyErr_Occurred())
809 return NULL;
810 return (void *)x;
811}
812
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000813#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000814
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000815/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000816 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000817 */
818
Tim Peterscf37dfc2001-06-14 18:42:50 +0000819#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000820
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000821/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000822
823PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000824PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000825{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 PyLongObject *v;
827 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
828 int ndigits = 0;
829 int negative = 0;
830
831 if (ival < 0) {
832 ival = -ival;
833 negative = 1;
834 }
835
836 /* Count the number of Python digits.
837 We used to pick 5 ("big enough for anything"), but that's a
838 waste of time and space given that 5*15 = 75 bits are rarely
839 needed. */
840 t = (unsigned PY_LONG_LONG)ival;
841 while (t) {
842 ++ndigits;
843 t >>= SHIFT;
844 }
845 v = _PyLong_New(ndigits);
846 if (v != NULL) {
847 digit *p = v->ob_digit;
848 v->ob_size = negative ? -ndigits : ndigits;
849 t = (unsigned PY_LONG_LONG)ival;
850 while (t) {
851 *p++ = (digit)(t & MASK);
852 t >>= SHIFT;
853 }
854 }
855 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000856}
857
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000858/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000859
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000860PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000861PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000862{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 PyLongObject *v;
864 unsigned PY_LONG_LONG t;
865 int ndigits = 0;
866
867 /* Count the number of Python digits. */
868 t = (unsigned PY_LONG_LONG)ival;
869 while (t) {
870 ++ndigits;
871 t >>= SHIFT;
872 }
873 v = _PyLong_New(ndigits);
874 if (v != NULL) {
875 digit *p = v->ob_digit;
876 v->ob_size = ndigits;
877 while (ival) {
878 *p++ = (digit)(ival & MASK);
879 ival >>= SHIFT;
880 }
881 }
882 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000883}
884
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885/* Create a new long int object from a C Py_ssize_t. */
886
887PyObject *
888_PyLong_FromSsize_t(Py_ssize_t ival)
889{
890 Py_ssize_t bytes = ival;
891 int one = 1;
892 return _PyLong_FromByteArray(
893 (unsigned char *)&bytes,
894 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
895}
896
897/* Create a new long int object from a C size_t. */
898
899PyObject *
900_PyLong_FromSize_t(size_t ival)
901{
902 size_t bytes = ival;
903 int one = 1;
904 return _PyLong_FromByteArray(
905 (unsigned char *)&bytes,
906 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
907}
908
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000909/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000910 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000911
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000912PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000913PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000914{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000915 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000916 int one = 1;
917 int res;
918
Tim Petersd38b1c72001-09-30 05:09:37 +0000919 if (vv == NULL) {
920 PyErr_BadInternalCall();
921 return -1;
922 }
923 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000924 PyNumberMethods *nb;
925 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000926 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000927 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000928 if ((nb = vv->ob_type->tp_as_number) == NULL ||
929 nb->nb_int == NULL) {
930 PyErr_SetString(PyExc_TypeError, "an integer is required");
931 return -1;
932 }
933 io = (*nb->nb_int) (vv);
934 if (io == NULL)
935 return -1;
936 if (PyInt_Check(io)) {
937 bytes = PyInt_AsLong(io);
938 Py_DECREF(io);
939 return bytes;
940 }
941 if (PyLong_Check(io)) {
942 bytes = PyLong_AsLongLong(io);
943 Py_DECREF(io);
944 return bytes;
945 }
946 Py_DECREF(io);
947 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000948 return -1;
949 }
950
Tim Petersd1a7da62001-06-13 00:35:57 +0000951 res = _PyLong_AsByteArray(
952 (PyLongObject *)vv, (unsigned char *)&bytes,
953 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000954
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000955 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000956 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000957 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000958 else
959 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000960}
961
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000962/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000963 Return -1 and set an error if overflow occurs. */
964
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000965unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000966PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000967{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000968 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000969 int one = 1;
970 int res;
971
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000972 if (vv == NULL || !PyLong_Check(vv)) {
973 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000975 }
976
Tim Petersd1a7da62001-06-13 00:35:57 +0000977 res = _PyLong_AsByteArray(
978 (PyLongObject *)vv, (unsigned char *)&bytes,
979 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000980
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000981 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000982 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000983 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000984 else
985 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000986}
Tim Petersd1a7da62001-06-13 00:35:57 +0000987
Thomas Hellera4ea6032003-04-17 18:55:45 +0000988/* Get a C unsigned long int from a long int object, ignoring the high bits.
989 Returns -1 and sets an error condition if an error occurs. */
990
991unsigned PY_LONG_LONG
992PyLong_AsUnsignedLongLongMask(PyObject *vv)
993{
994 register PyLongObject *v;
995 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000996 Py_ssize_t i;
997 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000998
999 if (vv == NULL || !PyLong_Check(vv)) {
1000 PyErr_BadInternalCall();
1001 return (unsigned long) -1;
1002 }
1003 v = (PyLongObject *)vv;
1004 i = v->ob_size;
1005 sign = 1;
1006 x = 0;
1007 if (i < 0) {
1008 sign = -1;
1009 i = -i;
1010 }
1011 while (--i >= 0) {
1012 x = (x << SHIFT) + v->ob_digit[i];
1013 }
1014 return x * sign;
1015}
Tim Petersd1a7da62001-06-13 00:35:57 +00001016#undef IS_LITTLE_ENDIAN
1017
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001018#endif /* HAVE_LONG_LONG */
1019
Neil Schemenauerba872e22001-01-04 01:46:03 +00001020
1021static int
1022convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001023 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001024 *a = (PyLongObject *) v;
1025 Py_INCREF(v);
1026 }
1027 else if (PyInt_Check(v)) {
1028 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1029 }
1030 else {
1031 return 0;
1032 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001033 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001034 *b = (PyLongObject *) w;
1035 Py_INCREF(w);
1036 }
1037 else if (PyInt_Check(w)) {
1038 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1039 }
1040 else {
1041 Py_DECREF(*a);
1042 return 0;
1043 }
1044 return 1;
1045}
1046
1047#define CONVERT_BINOP(v, w, a, b) \
1048 if (!convert_binop(v, w, a, b)) { \
1049 Py_INCREF(Py_NotImplemented); \
1050 return Py_NotImplemented; \
1051 }
1052
Tim Peters877a2122002-08-12 05:09:36 +00001053/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1054 * is modified in place, by adding y to it. Carries are propagated as far as
1055 * x[m-1], and the remaining carry (0 or 1) is returned.
1056 */
1057static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001059{
1060 int i;
1061 digit carry = 0;
1062
1063 assert(m >= n);
1064 for (i = 0; i < n; ++i) {
1065 carry += x[i] + y[i];
1066 x[i] = carry & MASK;
1067 carry >>= SHIFT;
1068 assert((carry & 1) == carry);
1069 }
1070 for (; carry && i < m; ++i) {
1071 carry += x[i];
1072 x[i] = carry & MASK;
1073 carry >>= SHIFT;
1074 assert((carry & 1) == carry);
1075 }
1076 return carry;
1077}
1078
1079/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1080 * is modified in place, by subtracting y from it. Borrows are propagated as
1081 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1082 */
1083static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001084v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001085{
1086 int i;
1087 digit borrow = 0;
1088
1089 assert(m >= n);
1090 for (i = 0; i < n; ++i) {
1091 borrow = x[i] - y[i] - borrow;
1092 x[i] = borrow & MASK;
1093 borrow >>= SHIFT;
1094 borrow &= 1; /* keep only 1 sign bit */
1095 }
1096 for (; borrow && i < m; ++i) {
1097 borrow = x[i] - borrow;
1098 x[i] = borrow & MASK;
1099 borrow >>= SHIFT;
1100 borrow &= 1;
1101 }
1102 return borrow;
1103}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001104
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001105/* Multiply by a single digit, ignoring the sign. */
1106
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001107static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001108mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001109{
1110 return muladd1(a, n, (digit)0);
1111}
1112
1113/* Multiply by a single digit and add a single digit, ignoring the sign. */
1114
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001116muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001117{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001118 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001119 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001120 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001121 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001122
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001123 if (z == NULL)
1124 return NULL;
1125 for (i = 0; i < size_a; ++i) {
1126 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001127 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001128 carry >>= SHIFT;
1129 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001130 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001131 return long_normalize(z);
1132}
1133
Tim Peters212e6142001-07-14 12:23:19 +00001134/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1135 in pout, and returning the remainder. pin and pout point at the LSD.
1136 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1137 long_format, but that should be done with great care since longs are
1138 immutable. */
1139
1140static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001141inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001142{
1143 twodigits rem = 0;
1144
1145 assert(n > 0 && n <= MASK);
1146 pin += size;
1147 pout += size;
1148 while (--size >= 0) {
1149 digit hi;
1150 rem = (rem << SHIFT) + *--pin;
1151 *--pout = hi = (digit)(rem / n);
1152 rem -= hi * n;
1153 }
1154 return (digit)rem;
1155}
1156
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001157/* Divide a long integer by a digit, returning both the quotient
1158 (as function result) and the remainder (through *prem).
1159 The sign of a is ignored; n should not be zero. */
1160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001162divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001163{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001164 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001166
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001167 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001169 if (z == NULL)
1170 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001171 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001172 return long_normalize(z);
1173}
1174
1175/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001176 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001177 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179static PyObject *
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001180long_format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001181{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 register PyLongObject *a = (PyLongObject *)aa;
1183 PyStringObject *str;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001184 Py_ssize_t i, j, sz;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001185 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001186 char *p;
1187 int bits;
1188 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001189
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001190 if (a == NULL || !PyLong_Check(a)) {
1191 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001192 return NULL;
1193 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001194 assert(base >= 2 && base <= 36);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001195 size_a = ABS(a->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001196
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001197 /* Compute a rough upper bound for the length of the string */
1198 i = base;
1199 bits = 0;
1200 while (i > 1) {
1201 ++bits;
1202 i >>= 1;
1203 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001204 i = 5;
1205 j = size_a*SHIFT + bits-1;
1206 sz = i + j / bits;
1207 if (j / SHIFT < size_a || sz < i) {
1208 PyErr_SetString(PyExc_OverflowError,
1209 "long is too large to format");
1210 return NULL;
1211 }
1212 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001213 if (str == NULL)
1214 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001215 p = PyString_AS_STRING(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001216 *p = '\0';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001217 if (a->ob_size < 0)
1218 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001219
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001220 if (a->ob_size == 0) {
1221 *--p = '0';
1222 }
1223 else if ((base & (base - 1)) == 0) {
1224 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001225 twodigits accum = 0;
1226 int accumbits = 0; /* # of bits in accum */
1227 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001228 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001229 while ((i >>= 1) > 1)
1230 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001231
1232 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001233 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001234 accumbits += SHIFT;
1235 assert(accumbits >= basebits);
1236 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001237 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001238 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001239 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001240 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001241 accumbits -= basebits;
1242 accum >>= basebits;
1243 } while (i < size_a-1 ? accumbits >= basebits :
1244 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001245 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001246 }
1247 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001248 /* Not 0, and base not a power of 2. Divide repeatedly by
1249 base, but for speed use the highest power of base that
1250 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001251 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001252 digit *pin = a->ob_digit;
1253 PyLongObject *scratch;
1254 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001255 digit powbase = base; /* powbase == base ** power */
1256 int power = 1;
1257 for (;;) {
1258 unsigned long newpow = powbase * (unsigned long)base;
1259 if (newpow >> SHIFT) /* doesn't fit in a digit */
1260 break;
1261 powbase = (digit)newpow;
1262 ++power;
1263 }
Tim Peters212e6142001-07-14 12:23:19 +00001264
1265 /* Get a scratch area for repeated division. */
1266 scratch = _PyLong_New(size);
1267 if (scratch == NULL) {
1268 Py_DECREF(str);
1269 return NULL;
1270 }
1271
1272 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001273 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001274 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001275 digit rem = inplace_divrem1(scratch->ob_digit,
1276 pin, size, powbase);
1277 pin = scratch->ob_digit; /* no need to use a again */
1278 if (pin[size - 1] == 0)
1279 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001280 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001281 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001282 Py_DECREF(str);
1283 return NULL;
1284 })
Tim Peters212e6142001-07-14 12:23:19 +00001285
1286 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001287 assert(ntostore > 0);
1288 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001289 digit nextrem = (digit)(rem / base);
1290 char c = (char)(rem - nextrem * base);
1291 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001292 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001293 *--p = c;
1294 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001295 --ntostore;
1296 /* Termination is a bit delicate: must not
1297 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001298 remaining quotient and rem are both 0. */
1299 } while (ntostore && (size || rem));
1300 } while (size != 0);
1301 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001302 }
1303
Guido van Rossum2c475421992-08-14 15:13:07 +00001304 if (base == 8) {
1305 if (size_a != 0)
1306 *--p = '0';
1307 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001308 else if (base == 16) {
1309 *--p = 'x';
1310 *--p = '0';
1311 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001312 else if (base != 10) {
1313 *--p = '#';
1314 *--p = '0' + base%10;
1315 if (base > 10)
1316 *--p = '0' + base/10;
1317 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001318 if (sign)
1319 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 if (p != PyString_AS_STRING(str)) {
1321 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001322 assert(p > q);
1323 do {
1324 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001325 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 _PyString_Resize((PyObject **)&str,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001327 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001328 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001330}
1331
Thomas Wouters477c8d52006-05-27 19:21:47 +00001332/* Table of digit values for 8-bit string -> integer conversion.
1333 * '0' maps to 0, ..., '9' maps to 9.
1334 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1335 * All other indices map to 37.
1336 * Note that when converting a base B string, a char c is a legitimate
1337 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1338 */
1339int _PyLong_DigitValue[256] = {
1340 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1341 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1342 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1343 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1344 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1345 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1346 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1347 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1348 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1349 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1350 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1351 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1352 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1353 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1354 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1355 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1356};
1357
1358/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001359 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1360 * non-digit (which may be *str!). A normalized long is returned.
1361 * The point to this routine is that it takes time linear in the number of
1362 * string characters.
1363 */
1364static PyLongObject *
1365long_from_binary_base(char **str, int base)
1366{
1367 char *p = *str;
1368 char *start = p;
1369 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001370 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001371 PyLongObject *z;
1372 twodigits accum;
1373 int bits_in_accum;
1374 digit *pdigit;
1375
1376 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1377 n = base;
1378 for (bits_per_char = -1; n; ++bits_per_char)
1379 n >>= 1;
1380 /* n <- total # of bits needed, while setting p to end-of-string */
1381 n = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001383 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001384 *str = p;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001385 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1386 n = (p - start) * bits_per_char + SHIFT - 1;
1387 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001388 PyErr_SetString(PyExc_ValueError,
1389 "long string too large to convert");
1390 return NULL;
1391 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001392 n = n / SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001393 z = _PyLong_New(n);
1394 if (z == NULL)
1395 return NULL;
1396 /* Read string from right, and fill in long from left; i.e.,
1397 * from least to most significant in both.
1398 */
1399 accum = 0;
1400 bits_in_accum = 0;
1401 pdigit = z->ob_digit;
1402 while (--p >= start) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001404 assert(k >= 0 && k < base);
1405 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001406 bits_in_accum += bits_per_char;
1407 if (bits_in_accum >= SHIFT) {
1408 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001409 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001410 accum >>= SHIFT;
1411 bits_in_accum -= SHIFT;
1412 assert(bits_in_accum < SHIFT);
1413 }
1414 }
1415 if (bits_in_accum) {
1416 assert(bits_in_accum <= SHIFT);
1417 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001418 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001419 }
1420 while (pdigit - z->ob_digit < n)
1421 *pdigit++ = 0;
1422 return long_normalize(z);
1423}
1424
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001425PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001426PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001427{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001428 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001429 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001430 PyLongObject *z;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431 PyObject *strobj, *strrepr;
1432 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001433
Guido van Rossum472c04f1996-12-05 21:57:21 +00001434 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001435 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001436 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001437 return NULL;
1438 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001439 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001440 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001441 if (*str == '+')
1442 ++str;
1443 else if (*str == '-') {
1444 ++str;
1445 sign = -1;
1446 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001447 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001448 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001449 if (base == 0) {
1450 if (str[0] != '0')
1451 base = 10;
1452 else if (str[1] == 'x' || str[1] == 'X')
1453 base = 16;
1454 else
1455 base = 8;
1456 }
1457 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1458 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001459
Guido van Rossume6762971998-06-22 03:54:46 +00001460 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001461 if ((base & (base - 1)) == 0)
1462 z = long_from_binary_base(&str, base);
1463 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001464/***
1465Binary bases can be converted in time linear in the number of digits, because
1466Python's representation base is binary. Other bases (including decimal!) use
1467the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001468
Thomas Wouters477c8d52006-05-27 19:21:47 +00001469First some math: the largest integer that can be expressed in N base-B digits
1470is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1471case number of Python digits needed to hold it is the smallest integer n s.t.
1472
1473 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1474 BASE**n >= B**N [taking logs to base BASE]
1475 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1476
1477The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1478this quickly. A Python long with that much space is reserved near the start,
1479and the result is computed into it.
1480
1481The input string is actually treated as being in base base**i (i.e., i digits
1482are processed at a time), where two more static arrays hold:
1483
1484 convwidth_base[base] = the largest integer i such that base**i <= BASE
1485 convmultmax_base[base] = base ** convwidth_base[base]
1486
1487The first of these is the largest i such that i consecutive input digits
1488must fit in a single Python digit. The second is effectively the input
1489base we're really using.
1490
1491Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1492convmultmax_base[base], the result is "simply"
1493
1494 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1495
1496where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001497
1498Error analysis: as above, the number of Python digits `n` needed is worst-
1499case
1500
1501 n >= N * log(B)/log(BASE)
1502
1503where `N` is the number of input digits in base `B`. This is computed via
1504
1505 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1506
1507below. Two numeric concerns are how much space this can waste, and whether
1508the computed result can be too small. To be concrete, assume BASE = 2**15,
1509which is the default (and it's unlikely anyone changes that).
1510
1511Waste isn't a problem: provided the first input digit isn't 0, the difference
1512between the worst-case input with N digits and the smallest input with N
1513digits is about a factor of B, but B is small compared to BASE so at most
1514one allocated Python digit can remain unused on that count. If
1515N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1516and adding 1 returns a result 1 larger than necessary. However, that can't
1517happen: whenever B is a power of 2, long_from_binary_base() is called
1518instead, and it's impossible for B**i to be an integer power of 2**15 when
1519B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1520an exact integer when B is not a power of 2, since B**i has a prime factor
1521other than 2 in that case, but (2**15)**j's only prime factor is 2).
1522
1523The computed result can be too small if the true value of N*log(B)/log(BASE)
1524is a little bit larger than an exact integer, but due to roundoff errors (in
1525computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1526yields a numeric result a little less than that integer. Unfortunately, "how
1527close can a transcendental function get to an integer over some range?"
1528questions are generally theoretically intractable. Computer analysis via
1529continued fractions is practical: expand log(B)/log(BASE) via continued
1530fractions, giving a sequence i/j of "the best" rational approximations. Then
1531j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1532we can get very close to being in trouble, but very rarely. For example,
153376573 is a denominator in one of the continued-fraction approximations to
1534log(10)/log(2**15), and indeed:
1535
1536 >>> log(10)/log(2**15)*76573
1537 16958.000000654003
1538
1539is very close to an integer. If we were working with IEEE single-precision,
1540rounding errors could kill us. Finding worst cases in IEEE double-precision
1541requires better-than-double-precision log() functions, and Tim didn't bother.
1542Instead the code checks to see whether the allocated space is enough as each
1543new Python digit is added, and copies the whole thing to a larger long if not.
1544This should happen extremely rarely, and in fact I don't have a test case
1545that triggers it(!). Instead the code was tested by artificially allocating
1546just 1 digit at the start, so that the copying code was exercised for every
1547digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001548***/
1549 register twodigits c; /* current input character */
1550 Py_ssize_t size_z;
1551 int i;
1552 int convwidth;
1553 twodigits convmultmax, convmult;
1554 digit *pz, *pzstop;
1555 char* scan;
1556
1557 static double log_base_BASE[37] = {0.0e0,};
1558 static int convwidth_base[37] = {0,};
1559 static twodigits convmultmax_base[37] = {0,};
1560
1561 if (log_base_BASE[base] == 0.0) {
1562 twodigits convmax = base;
1563 int i = 1;
1564
1565 log_base_BASE[base] = log((double)base) /
1566 log((double)BASE);
1567 for (;;) {
1568 twodigits next = convmax * base;
1569 if (next > BASE)
1570 break;
1571 convmax = next;
1572 ++i;
1573 }
1574 convmultmax_base[base] = convmax;
1575 assert(i > 0);
1576 convwidth_base[base] = i;
1577 }
1578
1579 /* Find length of the string of numeric characters. */
1580 scan = str;
1581 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
1582 ++scan;
1583
1584 /* Create a long object that can contain the largest possible
1585 * integer with this base and length. Note that there's no
1586 * need to initialize z->ob_digit -- no slot is read up before
1587 * being stored into.
1588 */
1589 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001590 /* Uncomment next line to test exceedingly rare copy code */
1591 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001592 assert(size_z > 0);
1593 z = _PyLong_New(size_z);
1594 if (z == NULL)
1595 return NULL;
1596 z->ob_size = 0;
1597
1598 /* `convwidth` consecutive input digits are treated as a single
1599 * digit in base `convmultmax`.
1600 */
1601 convwidth = convwidth_base[base];
1602 convmultmax = convmultmax_base[base];
1603
1604 /* Work ;-) */
1605 while (str < scan) {
1606 /* grab up to convwidth digits from the input string */
1607 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
1608 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1609 c = (twodigits)(c * base +
1610 _PyLong_DigitValue[Py_CHARMASK(*str)]);
1611 assert(c < BASE);
1612 }
1613
1614 convmult = convmultmax;
1615 /* Calculate the shift only if we couldn't get
1616 * convwidth digits.
1617 */
1618 if (i != convwidth) {
1619 convmult = base;
1620 for ( ; i > 1; --i)
1621 convmult *= base;
1622 }
1623
1624 /* Multiply z by convmult, and add c. */
1625 pz = z->ob_digit;
1626 pzstop = pz + z->ob_size;
1627 for (; pz < pzstop; ++pz) {
1628 c += (twodigits)*pz * convmult;
1629 *pz = (digit)(c & MASK);
1630 c >>= SHIFT;
1631 }
1632 /* carry off the current end? */
1633 if (c) {
1634 assert(c < BASE);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001635 if (z->ob_size < size_z) {
1636 *pz = (digit)c;
1637 ++z->ob_size;
1638 }
1639 else {
1640 PyLongObject *tmp;
1641 /* Extremely rare. Get more space. */
1642 assert(z->ob_size == size_z);
1643 tmp = _PyLong_New(size_z + 1);
1644 if (tmp == NULL) {
1645 Py_DECREF(z);
1646 return NULL;
1647 }
1648 memcpy(tmp->ob_digit,
1649 z->ob_digit,
1650 sizeof(digit) * size_z);
1651 Py_DECREF(z);
1652 z = tmp;
1653 z->ob_digit[size_z] = (digit)c;
1654 ++size_z;
1655 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001657 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001658 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001659 if (z == NULL)
1660 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001661 if (str == start)
1662 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001663 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001664 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001665 if (*str == 'L' || *str == 'l')
1666 str++;
1667 while (*str && isspace(Py_CHARMASK(*str)))
1668 str++;
1669 if (*str != '\0')
1670 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001671 if (pend)
1672 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001673 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001674
1675 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001676 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1678 strobj = PyString_FromStringAndSize(orig_str, slen);
1679 if (strobj == NULL)
1680 return NULL;
1681 strrepr = PyObject_Repr(strobj);
1682 Py_DECREF(strobj);
1683 if (strrepr == NULL)
1684 return NULL;
1685 PyErr_Format(PyExc_ValueError,
1686 "invalid literal for long() with base %d: %s",
1687 base, PyString_AS_STRING(strrepr));
1688 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001689 return NULL;
1690}
1691
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001692#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001693PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001694PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001695{
Walter Dörwald07e14762002-11-06 16:15:14 +00001696 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001698
Walter Dörwald07e14762002-11-06 16:15:14 +00001699 if (buffer == NULL)
1700 return NULL;
1701
1702 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1703 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001704 return NULL;
1705 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001706 result = PyLong_FromString(buffer, NULL, base);
1707 PyMem_FREE(buffer);
1708 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001709}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001710#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001711
Tim Peters9f688bf2000-07-07 15:53:28 +00001712/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001713static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001714 (PyLongObject *, PyLongObject *, PyLongObject **);
1715static PyObject *long_pos(PyLongObject *);
1716static int long_divrem(PyLongObject *, PyLongObject *,
1717 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001718
1719/* Long division with remainder, top-level routine */
1720
Guido van Rossume32e0141992-01-19 16:31:05 +00001721static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001722long_divrem(PyLongObject *a, PyLongObject *b,
1723 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001724{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001725 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001726 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001727
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001728 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001729 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001730 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001731 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001732 }
1733 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001734 (size_a == size_b &&
1735 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001737 *pdiv = _PyLong_New(0);
1738 Py_INCREF(a);
1739 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001740 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001741 }
1742 if (size_b == 1) {
1743 digit rem = 0;
1744 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001745 if (z == NULL)
1746 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001747 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001748 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001749 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001750 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001751 if (z == NULL)
1752 return -1;
1753 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001754 /* Set the signs.
1755 The quotient z has the sign of a*b;
1756 the remainder r has the sign of a,
1757 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001758 if ((a->ob_size < 0) != (b->ob_size < 0))
1759 z->ob_size = -(z->ob_size);
1760 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1761 (*prem)->ob_size = -((*prem)->ob_size);
1762 *pdiv = z;
1763 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001764}
1765
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001766/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001767
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001768static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001769x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001770{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001771 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001772 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001773 PyLongObject *v = mul1(v1, d);
1774 PyLongObject *w = mul1(w1, d);
1775 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001776 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001777
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001778 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001779 Py_XDECREF(v);
1780 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001781 return NULL;
1782 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001783
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001784 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001785 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001786 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001787
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001788 size_v = ABS(v->ob_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001789 k = size_v - size_w;
1790 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001791
Thomas Wouters477c8d52006-05-27 19:21:47 +00001792 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001793 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1794 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001795 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001796 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001797
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001798 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001799 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001800 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001801 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001802 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001803 if (vj == w->ob_digit[size_w-1])
1804 q = MASK;
1805 else
1806 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1807 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001808
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001809 while (w->ob_digit[size_w-2]*q >
1810 ((
1811 ((twodigits)vj << SHIFT)
1812 + v->ob_digit[j-1]
1813 - q*w->ob_digit[size_w-1]
1814 ) << SHIFT)
1815 + v->ob_digit[j-2])
1816 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001817
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001818 for (i = 0; i < size_w && i+k < size_v; ++i) {
1819 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001820 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001821 carry += v->ob_digit[i+k] - z
1822 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001823 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001824 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1825 carry, SHIFT);
1826 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001827 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001828
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001829 if (i+k < size_v) {
1830 carry += v->ob_digit[i+k];
1831 v->ob_digit[i+k] = 0;
1832 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001833
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001834 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001835 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001836 else {
1837 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001838 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001839 carry = 0;
1840 for (i = 0; i < size_w && i+k < size_v; ++i) {
1841 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001842 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001843 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1844 BASE_TWODIGITS_TYPE,
1845 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001846 }
1847 }
1848 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001849
Guido van Rossumc206c761995-01-10 15:23:19 +00001850 if (a == NULL)
1851 *prem = NULL;
1852 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001853 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001854 *prem = divrem1(v, d, &d);
1855 /* d receives the (unused) remainder */
1856 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001857 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001858 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001859 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001860 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001861 Py_DECREF(v);
1862 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001863 return a;
1864}
1865
1866/* Methods */
1867
1868static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001869long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001870{
Guido van Rossum9475a232001-10-05 20:51:39 +00001871 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001872}
1873
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001874static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001875long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001876{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001877 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001878}
1879
1880static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001881long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001882{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001883 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001884
Guido van Rossumc6913e71991-11-19 20:26:46 +00001885 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001886 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001887 sign = 0;
1888 else
1889 sign = a->ob_size - b->ob_size;
1890 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001891 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001892 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001893 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1894 ;
1895 if (i < 0)
1896 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001897 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001898 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001899 if (a->ob_size < 0)
1900 sign = -sign;
1901 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001902 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001903 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001904}
1905
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001906static PyObject *
1907long_richcompare(PyObject *self, PyObject *other, int op)
1908{
1909 PyLongObject *a, *b;
1910 CONVERT_BINOP((PyObject *)self, (PyObject *)other, &a, &b);
1911 return Py_CmpToRich(op, long_compare(a, b));
1912}
1913
Guido van Rossum9bfef441993-03-29 10:43:31 +00001914static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001915long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001916{
1917 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001918 Py_ssize_t i;
1919 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001920
1921 /* This is designed so that Python ints and longs with the
1922 same value hash to the same value, otherwise comparisons
1923 of mapping keys will turn out weird */
1924 i = v->ob_size;
1925 sign = 1;
1926 x = 0;
1927 if (i < 0) {
1928 sign = -1;
1929 i = -(i);
1930 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001931#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001932 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001933 /* Force a native long #-bits (32 or 64) circular shift */
1934 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001935 x += v->ob_digit[i];
1936 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001937#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001938 x = x * sign;
1939 if (x == -1)
1940 x = -2;
1941 return x;
1942}
1943
1944
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001945/* Add the absolute values of two long integers. */
1946
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001947static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001948x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001949{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001950 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001951 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001952 int i;
1953 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001954
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001955 /* Ensure a is the larger of the two: */
1956 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001957 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001958 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001959 size_a = size_b;
1960 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001961 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001962 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001963 if (z == NULL)
1964 return NULL;
1965 for (i = 0; i < size_b; ++i) {
1966 carry += a->ob_digit[i] + b->ob_digit[i];
1967 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001968 carry >>= SHIFT;
1969 }
1970 for (; i < size_a; ++i) {
1971 carry += a->ob_digit[i];
1972 z->ob_digit[i] = carry & MASK;
1973 carry >>= SHIFT;
1974 }
1975 z->ob_digit[i] = carry;
1976 return long_normalize(z);
1977}
1978
1979/* Subtract the absolute values of two integers. */
1980
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001981static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001982x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001983{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001984 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001985 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001986 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001987 int sign = 1;
1988 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001989
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001990 /* Ensure a is the larger of the two: */
1991 if (size_a < size_b) {
1992 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001993 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001994 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001995 size_a = size_b;
1996 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001997 }
1998 else if (size_a == size_b) {
1999 /* Find highest digit where a and b differ: */
2000 i = size_a;
2001 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2002 ;
2003 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002004 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002005 if (a->ob_digit[i] < b->ob_digit[i]) {
2006 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002007 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002008 }
2009 size_a = size_b = i+1;
2010 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002012 if (z == NULL)
2013 return NULL;
2014 for (i = 0; i < size_b; ++i) {
2015 /* The following assumes unsigned arithmetic
2016 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002017 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002018 z->ob_digit[i] = borrow & MASK;
2019 borrow >>= SHIFT;
2020 borrow &= 1; /* Keep only one sign bit */
2021 }
2022 for (; i < size_a; ++i) {
2023 borrow = a->ob_digit[i] - borrow;
2024 z->ob_digit[i] = borrow & MASK;
2025 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002026 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002027 }
2028 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002029 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002030 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002031 return long_normalize(z);
2032}
2033
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002034static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002035long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002036{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002037 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002038
Neil Schemenauerba872e22001-01-04 01:46:03 +00002039 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2040
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002041 if (a->ob_size < 0) {
2042 if (b->ob_size < 0) {
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 z = x_sub(b, a);
2049 }
2050 else {
2051 if (b->ob_size < 0)
2052 z = x_sub(a, b);
2053 else
2054 z = x_add(a, b);
2055 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002056 Py_DECREF(a);
2057 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002058 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002059}
2060
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002062long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002063{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002064 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002065
Neil Schemenauerba872e22001-01-04 01:46:03 +00002066 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2067
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002068 if (a->ob_size < 0) {
2069 if (b->ob_size < 0)
2070 z = x_sub(a, b);
2071 else
2072 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002073 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002074 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002075 }
2076 else {
2077 if (b->ob_size < 0)
2078 z = x_add(a, b);
2079 else
2080 z = x_sub(a, b);
2081 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002082 Py_DECREF(a);
2083 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002084 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002085}
2086
Tim Peters5af4e6c2002-08-12 02:31:19 +00002087/* Grade school multiplication, ignoring the signs.
2088 * Returns the absolute value of the product, or NULL if error.
2089 */
2090static PyLongObject *
2091x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002092{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002093 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002094 Py_ssize_t size_a = ABS(a->ob_size);
2095 Py_ssize_t size_b = ABS(b->ob_size);
2096 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002097
Tim Peters5af4e6c2002-08-12 02:31:19 +00002098 z = _PyLong_New(size_a + size_b);
2099 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002100 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002101
2102 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002103 if (a == b) {
2104 /* Efficient squaring per HAC, Algorithm 14.16:
2105 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2106 * Gives slightly less than a 2x speedup when a == b,
2107 * via exploiting that each entry in the multiplication
2108 * pyramid appears twice (except for the size_a squares).
2109 */
2110 for (i = 0; i < size_a; ++i) {
2111 twodigits carry;
2112 twodigits f = a->ob_digit[i];
2113 digit *pz = z->ob_digit + (i << 1);
2114 digit *pa = a->ob_digit + i + 1;
2115 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002116
Tim Peters0973b992004-08-29 22:16:50 +00002117 SIGCHECK({
2118 Py_DECREF(z);
2119 return NULL;
2120 })
2121
2122 carry = *pz + f * f;
2123 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002124 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002125 assert(carry <= MASK);
2126
2127 /* Now f is added in twice in each column of the
2128 * pyramid it appears. Same as adding f<<1 once.
2129 */
2130 f <<= 1;
2131 while (pa < paend) {
2132 carry += *pz + *pa++ * f;
2133 *pz++ = (digit)(carry & MASK);
2134 carry >>= SHIFT;
2135 assert(carry <= (MASK << 1));
2136 }
2137 if (carry) {
2138 carry += *pz;
2139 *pz++ = (digit)(carry & MASK);
2140 carry >>= SHIFT;
2141 }
2142 if (carry)
2143 *pz += (digit)(carry & MASK);
2144 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002145 }
Tim Peters0973b992004-08-29 22:16:50 +00002146 }
2147 else { /* a is not the same as b -- gradeschool long mult */
2148 for (i = 0; i < size_a; ++i) {
2149 twodigits carry = 0;
2150 twodigits f = a->ob_digit[i];
2151 digit *pz = z->ob_digit + i;
2152 digit *pb = b->ob_digit;
2153 digit *pbend = b->ob_digit + size_b;
2154
2155 SIGCHECK({
2156 Py_DECREF(z);
2157 return NULL;
2158 })
2159
2160 while (pb < pbend) {
2161 carry += *pz + *pb++ * f;
2162 *pz++ = (digit)(carry & MASK);
2163 carry >>= SHIFT;
2164 assert(carry <= MASK);
2165 }
2166 if (carry)
2167 *pz += (digit)(carry & MASK);
2168 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002169 }
2170 }
Tim Peters44121a62002-08-12 06:17:58 +00002171 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002172}
2173
2174/* A helper for Karatsuba multiplication (k_mul).
2175 Takes a long "n" and an integer "size" representing the place to
2176 split, and sets low and high such that abs(n) == (high << size) + low,
2177 viewing the shift as being by digits. The sign bit is ignored, and
2178 the return values are >= 0.
2179 Returns 0 on success, -1 on failure.
2180*/
2181static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002182kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002183{
2184 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002185 Py_ssize_t size_lo, size_hi;
2186 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002187
2188 size_lo = MIN(size_n, size);
2189 size_hi = size_n - size_lo;
2190
2191 if ((hi = _PyLong_New(size_hi)) == NULL)
2192 return -1;
2193 if ((lo = _PyLong_New(size_lo)) == NULL) {
2194 Py_DECREF(hi);
2195 return -1;
2196 }
2197
2198 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2199 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2200
2201 *high = long_normalize(hi);
2202 *low = long_normalize(lo);
2203 return 0;
2204}
2205
Tim Peters60004642002-08-12 22:01:34 +00002206static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2207
Tim Peters5af4e6c2002-08-12 02:31:19 +00002208/* Karatsuba multiplication. Ignores the input signs, and returns the
2209 * absolute value of the product (or NULL if error).
2210 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2211 */
2212static PyLongObject *
2213k_mul(PyLongObject *a, PyLongObject *b)
2214{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002215 Py_ssize_t asize = ABS(a->ob_size);
2216 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002217 PyLongObject *ah = NULL;
2218 PyLongObject *al = NULL;
2219 PyLongObject *bh = NULL;
2220 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002221 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002222 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002223 Py_ssize_t shift; /* the number of digits we split off */
2224 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002225
Tim Peters5af4e6c2002-08-12 02:31:19 +00002226 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2227 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2228 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002229 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002230 * By picking X to be a power of 2, "*X" is just shifting, and it's
2231 * been reduced to 3 multiplies on numbers half the size.
2232 */
2233
Tim Petersfc07e562002-08-12 02:54:10 +00002234 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002235 * is largest.
2236 */
Tim Peters738eda72002-08-12 15:08:20 +00002237 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002238 t1 = a;
2239 a = b;
2240 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002241
2242 i = asize;
2243 asize = bsize;
2244 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002245 }
2246
2247 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002248 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2249 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002250 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002251 return _PyLong_New(0);
2252 else
2253 return x_mul(a, b);
2254 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002255
Tim Peters60004642002-08-12 22:01:34 +00002256 /* If a is small compared to b, splitting on b gives a degenerate
2257 * case with ah==0, and Karatsuba may be (even much) less efficient
2258 * than "grade school" then. However, we can still win, by viewing
2259 * b as a string of "big digits", each of width a->ob_size. That
2260 * leads to a sequence of balanced calls to k_mul.
2261 */
2262 if (2 * asize <= bsize)
2263 return k_lopsided_mul(a, b);
2264
Tim Petersd6974a52002-08-13 20:37:51 +00002265 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002266 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002267 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002268 assert(ah->ob_size > 0); /* the split isn't degenerate */
2269
Tim Peters0973b992004-08-29 22:16:50 +00002270 if (a == b) {
2271 bh = ah;
2272 bl = al;
2273 Py_INCREF(bh);
2274 Py_INCREF(bl);
2275 }
2276 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002277
Tim Petersd64c1de2002-08-12 17:36:03 +00002278 /* The plan:
2279 * 1. Allocate result space (asize + bsize digits: that's always
2280 * enough).
2281 * 2. Compute ah*bh, and copy into result at 2*shift.
2282 * 3. Compute al*bl, and copy into result at 0. Note that this
2283 * can't overlap with #2.
2284 * 4. Subtract al*bl from the result, starting at shift. This may
2285 * underflow (borrow out of the high digit), but we don't care:
2286 * we're effectively doing unsigned arithmetic mod
2287 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2288 * borrows and carries out of the high digit can be ignored.
2289 * 5. Subtract ah*bh from the result, starting at shift.
2290 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2291 * at shift.
2292 */
2293
2294 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002295 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002296 if (ret == NULL) goto fail;
2297#ifdef Py_DEBUG
2298 /* Fill with trash, to catch reference to uninitialized digits. */
2299 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2300#endif
Tim Peters44121a62002-08-12 06:17:58 +00002301
Tim Petersd64c1de2002-08-12 17:36:03 +00002302 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002303 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2304 assert(t1->ob_size >= 0);
2305 assert(2*shift + t1->ob_size <= ret->ob_size);
2306 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2307 t1->ob_size * sizeof(digit));
2308
2309 /* Zero-out the digits higher than the ah*bh copy. */
2310 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002311 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002312 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002313 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002314
Tim Petersd64c1de2002-08-12 17:36:03 +00002315 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002316 if ((t2 = k_mul(al, bl)) == NULL) {
2317 Py_DECREF(t1);
2318 goto fail;
2319 }
2320 assert(t2->ob_size >= 0);
2321 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2322 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002323
2324 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002325 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002326 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002327 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002328
Tim Petersd64c1de2002-08-12 17:36:03 +00002329 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2330 * because it's fresher in cache.
2331 */
Tim Peters738eda72002-08-12 15:08:20 +00002332 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002333 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002334 Py_DECREF(t2);
2335
Tim Petersd64c1de2002-08-12 17:36:03 +00002336 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002337 Py_DECREF(t1);
2338
Tim Petersd64c1de2002-08-12 17:36:03 +00002339 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002340 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2341 Py_DECREF(ah);
2342 Py_DECREF(al);
2343 ah = al = NULL;
2344
Tim Peters0973b992004-08-29 22:16:50 +00002345 if (a == b) {
2346 t2 = t1;
2347 Py_INCREF(t2);
2348 }
2349 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002350 Py_DECREF(t1);
2351 goto fail;
2352 }
2353 Py_DECREF(bh);
2354 Py_DECREF(bl);
2355 bh = bl = NULL;
2356
Tim Peters738eda72002-08-12 15:08:20 +00002357 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002358 Py_DECREF(t1);
2359 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002360 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002361 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002362
Tim Petersd6974a52002-08-13 20:37:51 +00002363 /* Add t3. It's not obvious why we can't run out of room here.
2364 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002365 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002366 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002367 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002368
Tim Peters5af4e6c2002-08-12 02:31:19 +00002369 return long_normalize(ret);
2370
2371 fail:
2372 Py_XDECREF(ret);
2373 Py_XDECREF(ah);
2374 Py_XDECREF(al);
2375 Py_XDECREF(bh);
2376 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002377 return NULL;
2378}
2379
Tim Petersd6974a52002-08-13 20:37:51 +00002380/* (*) Why adding t3 can't "run out of room" above.
2381
Tim Petersab86c2b2002-08-15 20:06:00 +00002382Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2383to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002384
Tim Petersab86c2b2002-08-15 20:06:00 +000023851. For any integer i, i = c(i/2) + f(i/2). In particular,
2386 bsize = c(bsize/2) + f(bsize/2).
23872. shift = f(bsize/2)
23883. asize <= bsize
23894. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2390 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002391
Tim Petersab86c2b2002-08-15 20:06:00 +00002392We allocated asize + bsize result digits, and add t3 into them at an offset
2393of shift. This leaves asize+bsize-shift allocated digit positions for t3
2394to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2395asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002396
Tim Petersab86c2b2002-08-15 20:06:00 +00002397bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2398at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002399
Tim Petersab86c2b2002-08-15 20:06:00 +00002400If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2401digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2402most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002403
Tim Petersab86c2b2002-08-15 20:06:00 +00002404The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002405
Tim Petersab86c2b2002-08-15 20:06:00 +00002406 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002407
Tim Petersab86c2b2002-08-15 20:06:00 +00002408and we have asize + c(bsize/2) available digit positions. We need to show
2409this is always enough. An instance of c(bsize/2) cancels out in both, so
2410the question reduces to whether asize digits is enough to hold
2411(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2412then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2413asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2414digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2415asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002416c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2417is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2418bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002419
Tim Peters48d52c02002-08-14 17:07:32 +00002420Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2421clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2422ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002423*/
2424
Tim Peters60004642002-08-12 22:01:34 +00002425/* b has at least twice the digits of a, and a is big enough that Karatsuba
2426 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2427 * of slices, each with a->ob_size digits, and multiply the slices by a,
2428 * one at a time. This gives k_mul balanced inputs to work with, and is
2429 * also cache-friendly (we compute one double-width slice of the result
2430 * at a time, then move on, never bactracking except for the helpful
2431 * single-width slice overlap between successive partial sums).
2432 */
2433static PyLongObject *
2434k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2435{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002436 const Py_ssize_t asize = ABS(a->ob_size);
2437 Py_ssize_t bsize = ABS(b->ob_size);
2438 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002439 PyLongObject *ret;
2440 PyLongObject *bslice = NULL;
2441
2442 assert(asize > KARATSUBA_CUTOFF);
2443 assert(2 * asize <= bsize);
2444
2445 /* Allocate result space, and zero it out. */
2446 ret = _PyLong_New(asize + bsize);
2447 if (ret == NULL)
2448 return NULL;
2449 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2450
2451 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002452 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002453 if (bslice == NULL)
2454 goto fail;
2455
2456 nbdone = 0;
2457 while (bsize > 0) {
2458 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002459 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002460
2461 /* Multiply the next slice of b by a. */
2462 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2463 nbtouse * sizeof(digit));
2464 bslice->ob_size = nbtouse;
2465 product = k_mul(a, bslice);
2466 if (product == NULL)
2467 goto fail;
2468
2469 /* Add into result. */
2470 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2471 product->ob_digit, product->ob_size);
2472 Py_DECREF(product);
2473
2474 bsize -= nbtouse;
2475 nbdone += nbtouse;
2476 }
2477
2478 Py_DECREF(bslice);
2479 return long_normalize(ret);
2480
2481 fail:
2482 Py_DECREF(ret);
2483 Py_XDECREF(bslice);
2484 return NULL;
2485}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002486
2487static PyObject *
2488long_mul(PyLongObject *v, PyLongObject *w)
2489{
2490 PyLongObject *a, *b, *z;
2491
2492 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002493 Py_INCREF(Py_NotImplemented);
2494 return Py_NotImplemented;
2495 }
2496
Tim Petersd64c1de2002-08-12 17:36:03 +00002497 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002498 /* Negate if exactly one of the inputs is negative. */
2499 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002500 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002501 Py_DECREF(a);
2502 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002503 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002504}
2505
Guido van Rossume32e0141992-01-19 16:31:05 +00002506/* The / and % operators are now defined in terms of divmod().
2507 The expression a mod b has the value a - b*floor(a/b).
2508 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002509 |a| by |b|, with the sign of a. This is also expressed
2510 as a - b*trunc(a/b), if trunc truncates towards zero.
2511 Some examples:
2512 a b a rem b a mod b
2513 13 10 3 3
2514 -13 10 -3 7
2515 13 -10 3 -7
2516 -13 -10 -3 -3
2517 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002518 have different signs. We then subtract one from the 'div'
2519 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002520
Tim Peters47e52ee2004-08-30 02:44:38 +00002521/* Compute
2522 * *pdiv, *pmod = divmod(v, w)
2523 * NULL can be passed for pdiv or pmod, in which case that part of
2524 * the result is simply thrown away. The caller owns a reference to
2525 * each of these it requests (does not pass NULL for).
2526 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002527static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002528l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002529 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002530{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002531 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002532
Guido van Rossume32e0141992-01-19 16:31:05 +00002533 if (long_divrem(v, w, &div, &mod) < 0)
2534 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002535 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2536 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002537 PyLongObject *temp;
2538 PyLongObject *one;
2539 temp = (PyLongObject *) long_add(mod, w);
2540 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002541 mod = temp;
2542 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002543 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002544 return -1;
2545 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002546 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002547 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002548 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2549 Py_DECREF(mod);
2550 Py_DECREF(div);
2551 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002552 return -1;
2553 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002554 Py_DECREF(one);
2555 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002556 div = temp;
2557 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002558 if (pdiv != NULL)
2559 *pdiv = div;
2560 else
2561 Py_DECREF(div);
2562
2563 if (pmod != NULL)
2564 *pmod = mod;
2565 else
2566 Py_DECREF(mod);
2567
Guido van Rossume32e0141992-01-19 16:31:05 +00002568 return 0;
2569}
2570
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002571static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002572long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002573{
Tim Peters47e52ee2004-08-30 02:44:38 +00002574 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002575
2576 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002577 if (l_divmod(a, b, &div, NULL) < 0)
2578 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002579 Py_DECREF(a);
2580 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002581 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002582}
2583
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002584static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002585long_true_divide(PyObject *v, PyObject *w)
2586{
Tim Peterse2a60002001-09-04 06:17:36 +00002587 PyLongObject *a, *b;
2588 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002589 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002590
2591 CONVERT_BINOP(v, w, &a, &b);
2592 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2593 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002594 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2595 Py_DECREF(a);
2596 Py_DECREF(b);
2597 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002598 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002599 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2600 but should really be set correctly after sucessful calls to
2601 _PyLong_AsScaledDouble() */
2602 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002603
2604 if (bd == 0.0) {
2605 PyErr_SetString(PyExc_ZeroDivisionError,
2606 "long division or modulo by zero");
2607 return NULL;
2608 }
2609
2610 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2611 ad /= bd; /* overflow/underflow impossible here */
2612 aexp -= bexp;
2613 if (aexp > INT_MAX / SHIFT)
2614 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002615 else if (aexp < -(INT_MAX / SHIFT))
2616 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002617 errno = 0;
2618 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002619 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002620 goto overflow;
2621 return PyFloat_FromDouble(ad);
2622
2623overflow:
2624 PyErr_SetString(PyExc_OverflowError,
2625 "long/long too large for a float");
2626 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002627
Tim Peters20dab9f2001-09-04 05:31:47 +00002628}
2629
2630static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002631long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002632{
Tim Peters47e52ee2004-08-30 02:44:38 +00002633 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002634
2635 CONVERT_BINOP(v, w, &a, &b);
2636
Tim Peters47e52ee2004-08-30 02:44:38 +00002637 if (l_divmod(a, b, NULL, &mod) < 0)
2638 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002639 Py_DECREF(a);
2640 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002641 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002642}
2643
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002644static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002645long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002646{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002647 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002648 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002649
2650 CONVERT_BINOP(v, w, &a, &b);
2651
2652 if (l_divmod(a, b, &div, &mod) < 0) {
2653 Py_DECREF(a);
2654 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002655 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002656 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002657 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002658 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002659 PyTuple_SetItem(z, 0, (PyObject *) div);
2660 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002661 }
2662 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002663 Py_DECREF(div);
2664 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002665 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002666 Py_DECREF(a);
2667 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002668 return z;
2669}
2670
Tim Peters47e52ee2004-08-30 02:44:38 +00002671/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002672static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002673long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002674{
Tim Peters47e52ee2004-08-30 02:44:38 +00002675 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2676 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002677
Tim Peters47e52ee2004-08-30 02:44:38 +00002678 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002679 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002680 PyLongObject *temp = NULL;
2681
2682 /* 5-ary values. If the exponent is large enough, table is
2683 * precomputed so that table[i] == a**i % c for i in range(32).
2684 */
2685 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2686 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2687
2688 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002689 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002690 if (PyLong_Check(x)) {
2691 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002692 Py_INCREF(x);
2693 }
Tim Petersde7990b2005-07-17 23:45:23 +00002694 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002695 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002696 if (c == NULL)
2697 goto Error;
2698 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002699 else if (x == Py_None)
2700 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002701 else {
2702 Py_DECREF(a);
2703 Py_DECREF(b);
2704 Py_INCREF(Py_NotImplemented);
2705 return Py_NotImplemented;
2706 }
Tim Peters4c483c42001-09-05 06:24:58 +00002707
Tim Peters47e52ee2004-08-30 02:44:38 +00002708 if (b->ob_size < 0) { /* if exponent is negative */
2709 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002710 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002711 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002712 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002713 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002714 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002715 /* else return a float. This works because we know
2716 that this calls float_pow() which converts its
2717 arguments to double. */
2718 Py_DECREF(a);
2719 Py_DECREF(b);
2720 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002721 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002722 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002723
2724 if (c) {
2725 /* if modulus == 0:
2726 raise ValueError() */
2727 if (c->ob_size == 0) {
2728 PyErr_SetString(PyExc_ValueError,
2729 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002730 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002731 }
2732
2733 /* if modulus < 0:
2734 negativeOutput = True
2735 modulus = -modulus */
2736 if (c->ob_size < 0) {
2737 negativeOutput = 1;
2738 temp = (PyLongObject *)_PyLong_Copy(c);
2739 if (temp == NULL)
2740 goto Error;
2741 Py_DECREF(c);
2742 c = temp;
2743 temp = NULL;
2744 c->ob_size = - c->ob_size;
2745 }
2746
2747 /* if modulus == 1:
2748 return 0 */
2749 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2750 z = (PyLongObject *)PyLong_FromLong(0L);
2751 goto Done;
2752 }
2753
2754 /* if base < 0:
2755 base = base % modulus
2756 Having the base positive just makes things easier. */
2757 if (a->ob_size < 0) {
2758 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002759 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002760 Py_DECREF(a);
2761 a = temp;
2762 temp = NULL;
2763 }
2764 }
2765
2766 /* At this point a, b, and c are guaranteed non-negative UNLESS
2767 c is NULL, in which case a may be negative. */
2768
2769 z = (PyLongObject *)PyLong_FromLong(1L);
2770 if (z == NULL)
2771 goto Error;
2772
2773 /* Perform a modular reduction, X = X % c, but leave X alone if c
2774 * is NULL.
2775 */
2776#define REDUCE(X) \
2777 if (c != NULL) { \
2778 if (l_divmod(X, c, NULL, &temp) < 0) \
2779 goto Error; \
2780 Py_XDECREF(X); \
2781 X = temp; \
2782 temp = NULL; \
2783 }
2784
2785 /* Multiply two values, then reduce the result:
2786 result = X*Y % c. If c is NULL, skip the mod. */
2787#define MULT(X, Y, result) \
2788{ \
2789 temp = (PyLongObject *)long_mul(X, Y); \
2790 if (temp == NULL) \
2791 goto Error; \
2792 Py_XDECREF(result); \
2793 result = temp; \
2794 temp = NULL; \
2795 REDUCE(result) \
2796}
2797
2798 if (b->ob_size <= FIVEARY_CUTOFF) {
2799 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2800 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2801 for (i = b->ob_size - 1; i >= 0; --i) {
2802 digit bi = b->ob_digit[i];
2803
2804 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2805 MULT(z, z, z)
2806 if (bi & j)
2807 MULT(z, a, z)
2808 }
2809 }
2810 }
2811 else {
2812 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2813 Py_INCREF(z); /* still holds 1L */
2814 table[0] = z;
2815 for (i = 1; i < 32; ++i)
2816 MULT(table[i-1], a, table[i])
2817
2818 for (i = b->ob_size - 1; i >= 0; --i) {
2819 const digit bi = b->ob_digit[i];
2820
2821 for (j = SHIFT - 5; j >= 0; j -= 5) {
2822 const int index = (bi >> j) & 0x1f;
2823 for (k = 0; k < 5; ++k)
2824 MULT(z, z, z)
2825 if (index)
2826 MULT(z, table[index], z)
2827 }
2828 }
2829 }
2830
2831 if (negativeOutput && (z->ob_size != 0)) {
2832 temp = (PyLongObject *)long_sub(z, c);
2833 if (temp == NULL)
2834 goto Error;
2835 Py_DECREF(z);
2836 z = temp;
2837 temp = NULL;
2838 }
2839 goto Done;
2840
2841 Error:
2842 if (z != NULL) {
2843 Py_DECREF(z);
2844 z = NULL;
2845 }
2846 /* fall through */
2847 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002848 if (b->ob_size > FIVEARY_CUTOFF) {
2849 for (i = 0; i < 32; ++i)
2850 Py_XDECREF(table[i]);
2851 }
Tim Petersde7990b2005-07-17 23:45:23 +00002852 Py_DECREF(a);
2853 Py_DECREF(b);
2854 Py_XDECREF(c);
2855 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002856 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002857}
2858
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002859static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002860long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002861{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002862 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002863 PyLongObject *x;
2864 PyLongObject *w;
2865 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002866 if (w == NULL)
2867 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002868 x = (PyLongObject *) long_add(v, w);
2869 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002870 if (x == NULL)
2871 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002872 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002873 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002874}
2875
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002876static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002877long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002878{
Tim Peters69c2de32001-09-11 22:31:33 +00002879 if (PyLong_CheckExact(v)) {
2880 Py_INCREF(v);
2881 return (PyObject *)v;
2882 }
2883 else
2884 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002885}
2886
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002887static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002888long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002889{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002890 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002891 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002892 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002893 Py_INCREF(v);
2894 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002895 }
Tim Peters69c2de32001-09-11 22:31:33 +00002896 z = (PyLongObject *)_PyLong_Copy(v);
2897 if (z != NULL)
2898 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002899 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002900}
2901
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002902static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002903long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002904{
2905 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002906 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002907 else
2908 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002909}
2910
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002911static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00002912long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002913{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002914 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002915}
2916
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002917static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002918long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002919{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002920 PyLongObject *a, *b;
2921 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002922 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002923 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002924 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002925
Neil Schemenauerba872e22001-01-04 01:46:03 +00002926 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2927
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002928 if (a->ob_size < 0) {
2929 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002930 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002931 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002932 if (a1 == NULL)
2933 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002934 a2 = (PyLongObject *) long_rshift(a1, b);
2935 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002936 if (a2 == NULL)
2937 goto rshift_error;
2938 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002939 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002940 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002941 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002942
Neil Schemenauerba872e22001-01-04 01:46:03 +00002943 shiftby = PyLong_AsLong((PyObject *)b);
2944 if (shiftby == -1L && PyErr_Occurred())
2945 goto rshift_error;
2946 if (shiftby < 0) {
2947 PyErr_SetString(PyExc_ValueError,
2948 "negative shift count");
2949 goto rshift_error;
2950 }
2951 wordshift = shiftby / SHIFT;
2952 newsize = ABS(a->ob_size) - wordshift;
2953 if (newsize <= 0) {
2954 z = _PyLong_New(0);
2955 Py_DECREF(a);
2956 Py_DECREF(b);
2957 return (PyObject *)z;
2958 }
2959 loshift = shiftby % SHIFT;
2960 hishift = SHIFT - loshift;
2961 lomask = ((digit)1 << hishift) - 1;
2962 himask = MASK ^ lomask;
2963 z = _PyLong_New(newsize);
2964 if (z == NULL)
2965 goto rshift_error;
2966 if (a->ob_size < 0)
2967 z->ob_size = -(z->ob_size);
2968 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2969 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2970 if (i+1 < newsize)
2971 z->ob_digit[i] |=
2972 (a->ob_digit[j+1] << hishift) & himask;
2973 }
2974 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002975 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002976rshift_error:
2977 Py_DECREF(a);
2978 Py_DECREF(b);
2979 return (PyObject *) z;
2980
Guido van Rossumc6913e71991-11-19 20:26:46 +00002981}
2982
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002983static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002984long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002985{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002986 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002987 PyLongObject *a, *b;
2988 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002989 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002990 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002991 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002992
Neil Schemenauerba872e22001-01-04 01:46:03 +00002993 CONVERT_BINOP(v, w, &a, &b);
2994
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002995 shiftby = PyLong_AsLong((PyObject *)b);
2996 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002997 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002998 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002999 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003000 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003001 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003002 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003003 PyErr_SetString(PyExc_ValueError,
3004 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003005 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003006 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003007 /* wordshift, remshift = divmod(shiftby, SHIFT) */
3008 wordshift = (int)shiftby / SHIFT;
3009 remshift = (int)shiftby - wordshift * SHIFT;
3010
3011 oldsize = ABS(a->ob_size);
3012 newsize = oldsize + wordshift;
3013 if (remshift)
3014 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003015 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003016 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003017 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003018 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003019 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003020 for (i = 0; i < wordshift; i++)
3021 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003022 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003023 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003024 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003025 z->ob_digit[i] = (digit)(accum & MASK);
3026 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003027 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003028 if (remshift)
3029 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003030 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003031 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003032 z = long_normalize(z);
3033lshift_error:
3034 Py_DECREF(a);
3035 Py_DECREF(b);
3036 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003037}
3038
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003039
3040/* Bitwise and/xor/or operations */
3041
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003042static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003043long_bitwise(PyLongObject *a,
3044 int op, /* '&', '|', '^' */
3045 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003046{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003047 digit maska, maskb; /* 0 or MASK */
3048 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003049 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003050 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003051 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003052 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003053 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003054
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003055 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003056 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003057 if (a == NULL)
3058 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003059 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003060 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003061 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003062 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003063 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003064 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003065 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003066 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003067 if (b == NULL) {
3068 Py_DECREF(a);
3069 return NULL;
3070 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003071 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003072 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003073 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003074 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003075 maskb = 0;
3076 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003077
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003078 negz = 0;
3079 switch (op) {
3080 case '^':
3081 if (maska != maskb) {
3082 maska ^= MASK;
3083 negz = -1;
3084 }
3085 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003086 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003087 if (maska && maskb) {
3088 op = '|';
3089 maska ^= MASK;
3090 maskb ^= MASK;
3091 negz = -1;
3092 }
3093 break;
3094 case '|':
3095 if (maska || maskb) {
3096 op = '&';
3097 maska ^= MASK;
3098 maskb ^= MASK;
3099 negz = -1;
3100 }
3101 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003102 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003103
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003104 /* JRH: The original logic here was to allocate the result value (z)
3105 as the longer of the two operands. However, there are some cases
3106 where the result is guaranteed to be shorter than that: AND of two
3107 positives, OR of two negatives: use the shorter number. AND with
3108 mixed signs: use the positive number. OR with mixed signs: use the
3109 negative number. After the transformations above, op will be '&'
3110 iff one of these cases applies, and mask will be non-0 for operands
3111 whose length should be ignored.
3112 */
3113
3114 size_a = a->ob_size;
3115 size_b = b->ob_size;
3116 size_z = op == '&'
3117 ? (maska
3118 ? size_b
3119 : (maskb ? size_a : MIN(size_a, size_b)))
3120 : MAX(size_a, size_b);
3121 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003122 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003123 Py_DECREF(a);
3124 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003125 return NULL;
3126 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003127
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003128 for (i = 0; i < size_z; ++i) {
3129 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3130 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3131 switch (op) {
3132 case '&': z->ob_digit[i] = diga & digb; break;
3133 case '|': z->ob_digit[i] = diga | digb; break;
3134 case '^': z->ob_digit[i] = diga ^ digb; break;
3135 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003136 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003137
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003138 Py_DECREF(a);
3139 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003140 z = long_normalize(z);
3141 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003142 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003143 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003144 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003145 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003146}
3147
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003148static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003149long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003150{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003151 PyLongObject *a, *b;
3152 PyObject *c;
3153 CONVERT_BINOP(v, w, &a, &b);
3154 c = long_bitwise(a, '&', b);
3155 Py_DECREF(a);
3156 Py_DECREF(b);
3157 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003158}
3159
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003160static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003161long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003162{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003163 PyLongObject *a, *b;
3164 PyObject *c;
3165 CONVERT_BINOP(v, w, &a, &b);
3166 c = long_bitwise(a, '^', b);
3167 Py_DECREF(a);
3168 Py_DECREF(b);
3169 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003170}
3171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003172static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003173long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003174{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003175 PyLongObject *a, *b;
3176 PyObject *c;
3177 CONVERT_BINOP(v, w, &a, &b);
3178 c = long_bitwise(a, '|', b);
3179 Py_DECREF(a);
3180 Py_DECREF(b);
3181 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003182}
3183
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003184static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003185long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003186{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003187 if (PyLong_CheckExact(v))
3188 Py_INCREF(v);
3189 else
3190 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003191 return v;
3192}
3193
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003194static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003195long_int(PyObject *v)
3196{
3197 long x;
3198 x = PyLong_AsLong(v);
3199 if (PyErr_Occurred()) {
3200 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3201 PyErr_Clear();
3202 if (PyLong_CheckExact(v)) {
3203 Py_INCREF(v);
3204 return v;
3205 }
3206 else
3207 return _PyLong_Copy((PyLongObject *)v);
3208 }
3209 else
3210 return NULL;
3211 }
3212 return PyInt_FromLong(x);
3213}
3214
3215static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003216long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003217{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003218 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003219 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003220 if (result == -1.0 && PyErr_Occurred())
3221 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003222 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003223}
3224
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003225static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003226long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003227{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003228 return long_format(v, 8);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003229}
3230
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003231static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003232long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003233{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003234 return long_format(v, 16);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003235}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003236
3237static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003238long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003239
Tim Peters6d6c1a32001-08-02 04:15:00 +00003240static PyObject *
3241long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3242{
3243 PyObject *x = NULL;
3244 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003245 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003246
Guido van Rossumbef14172001-08-29 15:47:46 +00003247 if (type != &PyLong_Type)
3248 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003249 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3250 &x, &base))
3251 return NULL;
3252 if (x == NULL)
3253 return PyLong_FromLong(0L);
3254 if (base == -909)
3255 return PyNumber_Long(x);
3256 else if (PyString_Check(x))
3257 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003258#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003259 else if (PyUnicode_Check(x))
3260 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3261 PyUnicode_GET_SIZE(x),
3262 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003263#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003264 else {
3265 PyErr_SetString(PyExc_TypeError,
3266 "long() can't convert non-string with explicit base");
3267 return NULL;
3268 }
3269}
3270
Guido van Rossumbef14172001-08-29 15:47:46 +00003271/* Wimpy, slow approach to tp_new calls for subtypes of long:
3272 first create a regular long from whatever arguments we got,
3273 then allocate a subtype instance and initialize it from
3274 the regular long. The regular long is then thrown away.
3275*/
3276static PyObject *
3277long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3278{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003279 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003280 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003281
3282 assert(PyType_IsSubtype(type, &PyLong_Type));
3283 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3284 if (tmp == NULL)
3285 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003286 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003287 n = tmp->ob_size;
3288 if (n < 0)
3289 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290 newobj = (PyLongObject *)type->tp_alloc(type, n);
3291 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003292 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003293 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003294 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003295 assert(PyLong_Check(newobj));
3296 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003297 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003298 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003299 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003300 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003301}
3302
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003303static PyObject *
3304long_getnewargs(PyLongObject *v)
3305{
3306 return Py_BuildValue("(N)", _PyLong_Copy(v));
3307}
3308
3309static PyMethodDef long_methods[] = {
3310 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3311 {NULL, NULL} /* sentinel */
3312};
3313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003314PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003315"long(x[, base]) -> integer\n\
3316\n\
3317Convert a string or number to a long integer, if possible. A floating\n\
3318point argument will be truncated towards zero (this does not include a\n\
3319string representation of a floating point number!) When converting a\n\
3320string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003322
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003323static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003324 (binaryfunc) long_add, /*nb_add*/
3325 (binaryfunc) long_sub, /*nb_subtract*/
3326 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003327 long_mod, /*nb_remainder*/
3328 long_divmod, /*nb_divmod*/
3329 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003330 (unaryfunc) long_neg, /*nb_negative*/
3331 (unaryfunc) long_pos, /*tp_positive*/
3332 (unaryfunc) long_abs, /*tp_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00003333 (inquiry) long_bool, /*tp_bool*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003334 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003335 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003336 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003337 long_and, /*nb_and*/
3338 long_xor, /*nb_xor*/
3339 long_or, /*nb_or*/
Neal Norwitzca810462006-08-29 07:57:59 +00003340 0, /*nb_coerce*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003341 long_int, /*nb_int*/
3342 long_long, /*nb_long*/
3343 long_float, /*nb_float*/
3344 long_oct, /*nb_oct*/
3345 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003346 0, /* nb_inplace_add */
3347 0, /* nb_inplace_subtract */
3348 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003349 0, /* nb_inplace_remainder */
3350 0, /* nb_inplace_power */
3351 0, /* nb_inplace_lshift */
3352 0, /* nb_inplace_rshift */
3353 0, /* nb_inplace_and */
3354 0, /* nb_inplace_xor */
3355 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003356 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003357 long_true_divide, /* nb_true_divide */
3358 0, /* nb_inplace_floor_divide */
3359 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003360 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003361};
3362
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003363PyTypeObject PyLong_Type = {
3364 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003365 0, /* ob_size */
3366 "long", /* tp_name */
3367 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3368 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003369 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370 0, /* tp_print */
3371 0, /* tp_getattr */
3372 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003373 0, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003374 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003375 &long_as_number, /* tp_as_number */
3376 0, /* tp_as_sequence */
3377 0, /* tp_as_mapping */
3378 (hashfunc)long_hash, /* tp_hash */
3379 0, /* tp_call */
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003380 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003381 PyObject_GenericGetAttr, /* tp_getattro */
3382 0, /* tp_setattro */
3383 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003385 long_doc, /* tp_doc */
3386 0, /* tp_traverse */
3387 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003388 long_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003389 0, /* tp_weaklistoffset */
3390 0, /* tp_iter */
3391 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003392 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003393 0, /* tp_members */
3394 0, /* tp_getset */
3395 0, /* tp_base */
3396 0, /* tp_dict */
3397 0, /* tp_descr_get */
3398 0, /* tp_descr_set */
3399 0, /* tp_dictoffset */
3400 0, /* tp_init */
3401 0, /* tp_alloc */
3402 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003403 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003404};