blob: a2690a26d198c4ab02f6343c5dec48f05bb7d806 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003/* Long (arbitrary precision) integer object implementation */
4
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00005/* XXX The functional organization of this file is terrible */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00008#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Tim Peters5af4e6c2002-08-12 02:31:19 +000012/* For long multiplication, use the O(N**2) school algorithm unless
13 * both operands contain more than KARATSUBA_CUTOFF digits (this
14 * being an internal Python long digit, in base BASE).
15 */
Tim Peters0973b992004-08-29 22:16:50 +000016#define KARATSUBA_CUTOFF 70
17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000018
Tim Peters47e52ee2004-08-30 02:44:38 +000019/* For exponentiation, use the binary left-to-right algorithm
20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
21 * In that case, do 5 bits at a time. The potential drawback is that
22 * a table of 2**5 intermediate results is computed.
23 */
24#define FIVEARY_CUTOFF 8
25
Guido van Rossume32e0141992-01-19 16:31:05 +000026#define ABS(x) ((x) < 0 ? -(x) : (x))
27
Tim Peters5af4e6c2002-08-12 02:31:19 +000028#undef MIN
29#undef MAX
30#define MAX(x, y) ((x) < (y) ? (y) : (x))
31#define MIN(x, y) ((x) > (y) ? (y) : (x))
32
Guido van Rossume32e0141992-01-19 16:31:05 +000033/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000034static PyLongObject *long_normalize(PyLongObject *);
35static PyLongObject *mul1(PyLongObject *, wdigit);
36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000037static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossumd2dbecb2006-08-18 16:29:54 +000038static PyObject *long_format(PyObject *aa, int base);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000043 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000044 }
45
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046/* Normalize (remove leading zeros from) a long int object.
47 Doesn't attempt to free the storage--in most cases, due to the nature
48 of the algorithms used, this could save at most be one word anyway. */
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000051long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052{
Martin v. Löwis18e16552006-02-15 17:27:45 +000053 Py_ssize_t j = ABS(v->ob_size);
54 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000055
Guido van Rossumedcc38a1991-05-05 20:09:44 +000056 while (i > 0 && v->ob_digit[i-1] == 0)
57 --i;
58 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000059 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060 return v;
61}
62
63/* Allocate a new long int object with size digits.
64 Return NULL and set exception if we run out of memory. */
65
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000067_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068{
Thomas Wouters477c8d52006-05-27 19:21:47 +000069 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000070 PyErr_NoMemory();
71 return NULL;
72 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000074}
75
Tim Peters64b5ce32001-09-10 20:52:51 +000076PyObject *
77_PyLong_Copy(PyLongObject *src)
78{
79 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000081
82 assert(src != NULL);
83 i = src->ob_size;
84 if (i < 0)
85 i = -(i);
86 result = _PyLong_New(i);
87 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000088 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000089 while (--i >= 0)
90 result->ob_digit[i] = src->ob_digit[i];
91 }
92 return (PyObject *)result;
93}
94
Guido van Rossumedcc38a1991-05-05 20:09:44 +000095/* Create a new long int object from a C long int */
96
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000098PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000099{
Tim Petersce9de2f2001-06-14 04:56:19 +0000100 PyLongObject *v;
101 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
102 int ndigits = 0;
103 int negative = 0;
104
105 if (ival < 0) {
106 ival = -ival;
107 negative = 1;
108 }
109
110 /* Count the number of Python digits.
111 We used to pick 5 ("big enough for anything"), but that's a
112 waste of time and space given that 5*15 = 75 bits are rarely
113 needed. */
114 t = (unsigned long)ival;
115 while (t) {
116 ++ndigits;
117 t >>= SHIFT;
118 }
119 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000120 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000121 digit *p = v->ob_digit;
122 v->ob_size = negative ? -ndigits : ndigits;
123 t = (unsigned long)ival;
124 while (t) {
125 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000126 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000127 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000130}
131
Guido van Rossum53756b11997-01-03 17:14:46 +0000132/* Create a new long int object from a C unsigned long int */
133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000135PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000136{
Tim Petersce9de2f2001-06-14 04:56:19 +0000137 PyLongObject *v;
138 unsigned long t;
139 int ndigits = 0;
140
141 /* Count the number of Python digits. */
142 t = (unsigned long)ival;
143 while (t) {
144 ++ndigits;
145 t >>= SHIFT;
146 }
147 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000148 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000149 digit *p = v->ob_digit;
150 v->ob_size = ndigits;
151 while (ival) {
152 *p++ = (digit)(ival & MASK);
153 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000154 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000157}
158
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000159/* Create a new long int object from a C double */
160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000165 double frac;
166 int i, ndig, expo, neg;
167 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000168 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000169 PyErr_SetString(PyExc_OverflowError,
170 "cannot convert float infinity to long");
171 return NULL;
172 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000173 if (dval < 0.0) {
174 neg = 1;
175 dval = -dval;
176 }
177 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
178 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000180 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000182 if (v == NULL)
183 return NULL;
184 frac = ldexp(frac, (expo-1) % SHIFT + 1);
185 for (i = ndig; --i >= 0; ) {
186 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000187 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000188 frac = frac - (double)bits;
189 frac = ldexp(frac, SHIFT);
190 }
191 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000192 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000194}
195
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000196/* Get a C long int from a long int object.
197 Returns -1 and sets an error condition if overflow occurs. */
198
199long
Tim Peters9f688bf2000-07-07 15:53:28 +0000200PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000201{
Guido van Rossumf7531811998-05-26 14:33:37 +0000202 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000204 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205 Py_ssize_t i;
206 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000207
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000209 if (vv != NULL && PyInt_Check(vv))
210 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000212 return -1;
213 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000215 i = v->ob_size;
216 sign = 1;
217 x = 0;
218 if (i < 0) {
219 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000220 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000221 }
222 while (--i >= 0) {
223 prev = x;
224 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000225 if ((x >> SHIFT) != prev)
226 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000227 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000228 /* Haven't lost any bits, but if the sign bit is set we're in
229 * trouble *unless* this is the min negative number. So,
230 * trouble iff sign bit set && (positive || some bit set other
231 * than the sign bit).
232 */
233 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
234 goto overflow;
235 return (long)x * sign;
236
237 overflow:
238 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000239 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000240 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000241}
242
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000243static Py_ssize_t
244_long_as_ssize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000245 register PyLongObject *v;
246 size_t x, prev;
247 Py_ssize_t i;
248 int sign;
249
250 if (vv == NULL || !PyLong_Check(vv)) {
251 PyErr_BadInternalCall();
252 return -1;
253 }
254 v = (PyLongObject *)vv;
255 i = v->ob_size;
256 sign = 1;
257 x = 0;
258 if (i < 0) {
259 sign = -1;
260 i = -(i);
261 }
262 while (--i >= 0) {
263 prev = x;
264 x = (x << SHIFT) + v->ob_digit[i];
265 if ((x >> SHIFT) != prev)
266 goto overflow;
267 }
268 /* Haven't lost any bits, but if the sign bit is set we're in
269 * trouble *unless* this is the min negative number. So,
270 * trouble iff sign bit set && (positive || some bit set other
271 * than the sign bit).
272 */
273 if ((Py_ssize_t)x < 0 && (sign > 0 || (x << 1) != 0))
274 goto overflow;
275 return (Py_ssize_t)x * sign;
276
277 overflow:
278 PyErr_SetString(PyExc_OverflowError,
279 "long int too large to convert to int");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280 if (sign > 0)
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000281 return PY_SSIZE_T_MAX;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000282 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283 return PY_SSIZE_T_MIN;
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000284}
285
286/* Get a Py_ssize_t from a long int object.
287 Returns -1 and sets an error condition if overflow occurs. */
288
289Py_ssize_t
290_PyLong_AsSsize_t(PyObject *vv)
291{
292 Py_ssize_t x;
293
294 x = _long_as_ssize_t(vv);
295 if (PyErr_Occurred()) return -1;
296 return x;
297}
298
299
300/* Get a Py_ssize_t from a long int object.
301 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
302 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 On error, return -1 with an exception set.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000304*/
305
306static Py_ssize_t
307long_index(PyObject *vv)
308{
309 Py_ssize_t x;
310
311 x = _long_as_ssize_t(vv);
312 if (PyErr_Occurred()) {
313 /* If overflow error, ignore the error */
314 if (x != -1) {
315 PyErr_Clear();
316 }
317 }
318 return x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319}
320
Guido van Rossumd8c80482002-08-13 00:24:58 +0000321/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000322 Returns -1 and sets an error condition if overflow occurs. */
323
324unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000325PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000326{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000328 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000329 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000332 if (vv != NULL && PyInt_Check(vv)) {
333 long val = PyInt_AsLong(vv);
334 if (val < 0) {
335 PyErr_SetString(PyExc_OverflowError,
336 "can't convert negative value to unsigned long");
337 return (unsigned long) -1;
338 }
339 return val;
340 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000342 return (unsigned long) -1;
343 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000345 i = v->ob_size;
346 x = 0;
347 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000349 "can't convert negative value to unsigned long");
350 return (unsigned long) -1;
351 }
352 while (--i >= 0) {
353 prev = x;
354 x = (x << SHIFT) + v->ob_digit[i];
355 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000357 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000358 return (unsigned long) -1;
359 }
360 }
361 return x;
362}
363
Thomas Hellera4ea6032003-04-17 18:55:45 +0000364/* Get a C unsigned long int from a long int object, ignoring the high bits.
365 Returns -1 and sets an error condition if an error occurs. */
366
367unsigned long
368PyLong_AsUnsignedLongMask(PyObject *vv)
369{
370 register PyLongObject *v;
371 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t i;
373 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000374
375 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000376 if (vv != NULL && PyInt_Check(vv))
377 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000378 PyErr_BadInternalCall();
379 return (unsigned long) -1;
380 }
381 v = (PyLongObject *)vv;
382 i = v->ob_size;
383 sign = 1;
384 x = 0;
385 if (i < 0) {
386 sign = -1;
387 i = -i;
388 }
389 while (--i >= 0) {
390 x = (x << SHIFT) + v->ob_digit[i];
391 }
392 return x * sign;
393}
394
Tim Peters5b8132f2003-01-31 15:52:05 +0000395int
396_PyLong_Sign(PyObject *vv)
397{
398 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000399
400 assert(v != NULL);
401 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000402
Tim Petersee1a53c2003-02-02 02:57:53 +0000403 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000404}
405
Tim Petersbaefd9e2003-01-28 20:37:45 +0000406size_t
407_PyLong_NumBits(PyObject *vv)
408{
409 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000410 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000411 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000412
413 assert(v != NULL);
414 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000415 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000416 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
417 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000418 digit msd = v->ob_digit[ndigits - 1];
419
Tim Peters5b8132f2003-01-31 15:52:05 +0000420 result = (ndigits - 1) * SHIFT;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000421 if (result / SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000422 goto Overflow;
423 do {
424 ++result;
425 if (result == 0)
426 goto Overflow;
427 msd >>= 1;
428 } while (msd);
429 }
430 return result;
431
432Overflow:
433 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
434 "to express in a platform size_t");
435 return (size_t)-1;
436}
437
Tim Peters2a9b3672001-06-11 21:23:58 +0000438PyObject *
439_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
440 int little_endian, int is_signed)
441{
442 const unsigned char* pstartbyte;/* LSB of bytes */
443 int incr; /* direction to move pstartbyte */
444 const unsigned char* pendbyte; /* MSB of bytes */
445 size_t numsignificantbytes; /* number of bytes that matter */
446 size_t ndigits; /* number of Python long digits */
447 PyLongObject* v; /* result */
448 int idigit = 0; /* next free index in v->ob_digit */
449
450 if (n == 0)
451 return PyLong_FromLong(0L);
452
453 if (little_endian) {
454 pstartbyte = bytes;
455 pendbyte = bytes + n - 1;
456 incr = 1;
457 }
458 else {
459 pstartbyte = bytes + n - 1;
460 pendbyte = bytes;
461 incr = -1;
462 }
463
464 if (is_signed)
465 is_signed = *pendbyte >= 0x80;
466
467 /* Compute numsignificantbytes. This consists of finding the most
468 significant byte. Leading 0 bytes are insignficant if the number
469 is positive, and leading 0xff bytes if negative. */
470 {
471 size_t i;
472 const unsigned char* p = pendbyte;
473 const int pincr = -incr; /* search MSB to LSB */
474 const unsigned char insignficant = is_signed ? 0xff : 0x00;
475
476 for (i = 0; i < n; ++i, p += pincr) {
477 if (*p != insignficant)
478 break;
479 }
480 numsignificantbytes = n - i;
481 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
482 actually has 2 significant bytes. OTOH, 0xff0001 ==
483 -0x00ffff, so we wouldn't *need* to bump it there; but we
484 do for 0xffff = -0x0001. To be safe without bothering to
485 check every case, bump it regardless. */
486 if (is_signed && numsignificantbytes < n)
487 ++numsignificantbytes;
488 }
489
490 /* How many Python long digits do we need? We have
491 8*numsignificantbytes bits, and each Python long digit has SHIFT
492 bits, so it's the ceiling of the quotient. */
493 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
494 if (ndigits > (size_t)INT_MAX)
495 return PyErr_NoMemory();
496 v = _PyLong_New((int)ndigits);
497 if (v == NULL)
498 return NULL;
499
500 /* Copy the bits over. The tricky parts are computing 2's-comp on
501 the fly for signed numbers, and dealing with the mismatch between
502 8-bit bytes and (probably) 15-bit Python digits.*/
503 {
504 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000505 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000506 twodigits accum = 0; /* sliding register */
507 unsigned int accumbits = 0; /* number of bits in accum */
508 const unsigned char* p = pstartbyte;
509
510 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000511 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000512 /* Compute correction for 2's comp, if needed. */
513 if (is_signed) {
514 thisbyte = (0xff ^ thisbyte) + carry;
515 carry = thisbyte >> 8;
516 thisbyte &= 0xff;
517 }
518 /* Because we're going LSB to MSB, thisbyte is
519 more significant than what's already in accum,
520 so needs to be prepended to accum. */
521 accum |= thisbyte << accumbits;
522 accumbits += 8;
523 if (accumbits >= SHIFT) {
524 /* There's enough to fill a Python digit. */
525 assert(idigit < (int)ndigits);
526 v->ob_digit[idigit] = (digit)(accum & MASK);
527 ++idigit;
528 accum >>= SHIFT;
529 accumbits -= SHIFT;
530 assert(accumbits < SHIFT);
531 }
532 }
533 assert(accumbits < SHIFT);
534 if (accumbits) {
535 assert(idigit < (int)ndigits);
536 v->ob_digit[idigit] = (digit)accum;
537 ++idigit;
538 }
539 }
540
541 v->ob_size = is_signed ? -idigit : idigit;
542 return (PyObject *)long_normalize(v);
543}
544
545int
546_PyLong_AsByteArray(PyLongObject* v,
547 unsigned char* bytes, size_t n,
548 int little_endian, int is_signed)
549{
550 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000551 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000552 twodigits accum; /* sliding register */
553 unsigned int accumbits; /* # bits in accum */
554 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
555 twodigits carry; /* for computing 2's-comp */
556 size_t j; /* # bytes filled */
557 unsigned char* p; /* pointer to next byte in bytes */
558 int pincr; /* direction to move p */
559
560 assert(v != NULL && PyLong_Check(v));
561
562 if (v->ob_size < 0) {
563 ndigits = -(v->ob_size);
564 if (!is_signed) {
565 PyErr_SetString(PyExc_TypeError,
566 "can't convert negative long to unsigned");
567 return -1;
568 }
569 do_twos_comp = 1;
570 }
571 else {
572 ndigits = v->ob_size;
573 do_twos_comp = 0;
574 }
575
576 if (little_endian) {
577 p = bytes;
578 pincr = 1;
579 }
580 else {
581 p = bytes + n - 1;
582 pincr = -1;
583 }
584
Tim Peters898cf852001-06-13 20:50:08 +0000585 /* Copy over all the Python digits.
586 It's crucial that every Python digit except for the MSD contribute
587 exactly SHIFT bits to the total, so first assert that the long is
588 normalized. */
589 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000590 j = 0;
591 accum = 0;
592 accumbits = 0;
593 carry = do_twos_comp ? 1 : 0;
594 for (i = 0; i < ndigits; ++i) {
595 twodigits thisdigit = v->ob_digit[i];
596 if (do_twos_comp) {
597 thisdigit = (thisdigit ^ MASK) + carry;
598 carry = thisdigit >> SHIFT;
599 thisdigit &= MASK;
600 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000601 /* Because we're going LSB to MSB, thisdigit is more
602 significant than what's already in accum, so needs to be
603 prepended to accum. */
604 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000605 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000606
Tim Petersede05092001-06-14 08:53:38 +0000607 /* The most-significant digit may be (probably is) at least
608 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000609 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000610 /* Count # of sign bits -- they needn't be stored,
611 * although for signed conversion we need later to
612 * make sure at least one sign bit gets stored.
613 * First shift conceptual sign bit to real sign bit.
614 */
615 stwodigits s = (stwodigits)(thisdigit <<
616 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000617 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000618 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000619 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000620 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000621 }
Tim Petersede05092001-06-14 08:53:38 +0000622 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000623 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000624
Tim Peters2a9b3672001-06-11 21:23:58 +0000625 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000626 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000627 if (j >= n)
628 goto Overflow;
629 ++j;
630 *p = (unsigned char)(accum & 0xff);
631 p += pincr;
632 accumbits -= 8;
633 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000634 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000635 }
636
637 /* Store the straggler (if any). */
638 assert(accumbits < 8);
639 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000640 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000641 if (j >= n)
642 goto Overflow;
643 ++j;
644 if (do_twos_comp) {
645 /* Fill leading bits of the byte with sign bits
646 (appropriately pretending that the long had an
647 infinite supply of sign bits). */
648 accum |= (~(twodigits)0) << accumbits;
649 }
650 *p = (unsigned char)(accum & 0xff);
651 p += pincr;
652 }
Tim Peters05607ad2001-06-13 21:01:27 +0000653 else if (j == n && n > 0 && is_signed) {
654 /* The main loop filled the byte array exactly, so the code
655 just above didn't get to ensure there's a sign bit, and the
656 loop below wouldn't add one either. Make sure a sign bit
657 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000658 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000659 int sign_bit_set = msb >= 0x80;
660 assert(accumbits == 0);
661 if (sign_bit_set == do_twos_comp)
662 return 0;
663 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000664 goto Overflow;
665 }
Tim Peters05607ad2001-06-13 21:01:27 +0000666
667 /* Fill remaining bytes with copies of the sign bit. */
668 {
669 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
670 for ( ; j < n; ++j, p += pincr)
671 *p = signbyte;
672 }
673
Tim Peters2a9b3672001-06-11 21:23:58 +0000674 return 0;
675
676Overflow:
677 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
678 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000679
Tim Peters2a9b3672001-06-11 21:23:58 +0000680}
681
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000682double
683_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
684{
685/* NBITS_WANTED should be > the number of bits in a double's precision,
686 but small enough so that 2**NBITS_WANTED is within the normal double
687 range. nbitsneeded is set to 1 less than that because the most-significant
688 Python digit contains at least 1 significant bit, but we don't want to
689 bother counting them (catering to the worst case cheaply).
690
691 57 is one more than VAX-D double precision; I (Tim) don't know of a double
692 format with more precision than that; it's 1 larger so that we add in at
693 least one round bit to stand in for the ignored least-significant bits.
694*/
695#define NBITS_WANTED 57
696 PyLongObject *v;
697 double x;
698 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699 Py_ssize_t i;
700 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000701 int nbitsneeded;
702
703 if (vv == NULL || !PyLong_Check(vv)) {
704 PyErr_BadInternalCall();
705 return -1;
706 }
707 v = (PyLongObject *)vv;
708 i = v->ob_size;
709 sign = 1;
710 if (i < 0) {
711 sign = -1;
712 i = -(i);
713 }
714 else if (i == 0) {
715 *exponent = 0;
716 return 0.0;
717 }
718 --i;
719 x = (double)v->ob_digit[i];
720 nbitsneeded = NBITS_WANTED - 1;
721 /* Invariant: i Python digits remain unaccounted for. */
722 while (i > 0 && nbitsneeded > 0) {
723 --i;
724 x = x * multiplier + (double)v->ob_digit[i];
725 nbitsneeded -= SHIFT;
726 }
727 /* There are i digits we didn't shift in. Pretending they're all
728 zeroes, the true value is x * 2**(i*SHIFT). */
729 *exponent = i;
730 assert(x > 0.0);
731 return x * sign;
732#undef NBITS_WANTED
733}
734
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000735/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000736
737double
Tim Peters9f688bf2000-07-07 15:53:28 +0000738PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000739{
Thomas Wouters553489a2006-02-01 21:32:04 +0000740 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000741 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000742
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 if (vv == NULL || !PyLong_Check(vv)) {
744 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000745 return -1;
746 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000747 x = _PyLong_AsScaledDouble(vv, &e);
748 if (x == -1.0 && PyErr_Occurred())
749 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000750 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
751 set correctly after a successful _PyLong_AsScaledDouble() call */
752 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000753 if (e > INT_MAX / SHIFT)
754 goto overflow;
755 errno = 0;
756 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000757 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000758 goto overflow;
759 return x;
760
761overflow:
762 PyErr_SetString(PyExc_OverflowError,
763 "long int too large to convert to float");
764 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000765}
766
Guido van Rossum78694d91998-09-18 14:14:13 +0000767/* Create a new long (or int) object from a C pointer */
768
769PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000770PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000771{
Tim Peters70128a12001-06-16 08:48:40 +0000772#if SIZEOF_VOID_P <= SIZEOF_LONG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 if ((long)p < 0)
774 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000775 return PyInt_FromLong((long)p);
776#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000777
Tim Peters70128a12001-06-16 08:48:40 +0000778#ifndef HAVE_LONG_LONG
779# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
780#endif
781#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000782# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000783#endif
784 /* optimize null pointers */
785 if (p == NULL)
786 return PyInt_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000788
789#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000790}
791
792/* Get a C pointer from a long object (or an int object in some cases) */
793
794void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000795PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000796{
797 /* This function will allow int or long objects. If vv is neither,
798 then the PyLong_AsLong*() functions will raise the exception:
799 PyExc_SystemError, "bad argument to internal function"
800 */
Tim Peters70128a12001-06-16 08:48:40 +0000801#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000802 long x;
803
Tim Peters70128a12001-06-16 08:48:40 +0000804 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000805 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000807 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808 else
809 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000810#else
Tim Peters70128a12001-06-16 08:48:40 +0000811
812#ifndef HAVE_LONG_LONG
813# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
814#endif
815#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000816# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000817#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000818 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000819
Tim Peters70128a12001-06-16 08:48:40 +0000820 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000821 x = PyInt_AS_LONG(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000823 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 else
825 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000826
827#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000828
829 if (x == -1 && PyErr_Occurred())
830 return NULL;
831 return (void *)x;
832}
833
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000834#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000835
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000836/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000837 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000838 */
839
Tim Peterscf37dfc2001-06-14 18:42:50 +0000840#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000841
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000842/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000843
844PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000845PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000846{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 PyLongObject *v;
848 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
849 int ndigits = 0;
850 int negative = 0;
851
852 if (ival < 0) {
853 ival = -ival;
854 negative = 1;
855 }
856
857 /* Count the number of Python digits.
858 We used to pick 5 ("big enough for anything"), but that's a
859 waste of time and space given that 5*15 = 75 bits are rarely
860 needed. */
861 t = (unsigned PY_LONG_LONG)ival;
862 while (t) {
863 ++ndigits;
864 t >>= SHIFT;
865 }
866 v = _PyLong_New(ndigits);
867 if (v != NULL) {
868 digit *p = v->ob_digit;
869 v->ob_size = negative ? -ndigits : ndigits;
870 t = (unsigned PY_LONG_LONG)ival;
871 while (t) {
872 *p++ = (digit)(t & MASK);
873 t >>= SHIFT;
874 }
875 }
876 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000877}
878
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000879/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000880
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000881PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000882PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000883{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884 PyLongObject *v;
885 unsigned PY_LONG_LONG t;
886 int ndigits = 0;
887
888 /* Count the number of Python digits. */
889 t = (unsigned PY_LONG_LONG)ival;
890 while (t) {
891 ++ndigits;
892 t >>= SHIFT;
893 }
894 v = _PyLong_New(ndigits);
895 if (v != NULL) {
896 digit *p = v->ob_digit;
897 v->ob_size = ndigits;
898 while (ival) {
899 *p++ = (digit)(ival & MASK);
900 ival >>= SHIFT;
901 }
902 }
903 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000904}
905
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906/* Create a new long int object from a C Py_ssize_t. */
907
908PyObject *
909_PyLong_FromSsize_t(Py_ssize_t ival)
910{
911 Py_ssize_t bytes = ival;
912 int one = 1;
913 return _PyLong_FromByteArray(
914 (unsigned char *)&bytes,
915 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
916}
917
918/* Create a new long int object from a C size_t. */
919
920PyObject *
921_PyLong_FromSize_t(size_t ival)
922{
923 size_t bytes = ival;
924 int one = 1;
925 return _PyLong_FromByteArray(
926 (unsigned char *)&bytes,
927 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
928}
929
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000930/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000931 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000932
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000933PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000934PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000935{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000936 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000937 int one = 1;
938 int res;
939
Tim Petersd38b1c72001-09-30 05:09:37 +0000940 if (vv == NULL) {
941 PyErr_BadInternalCall();
942 return -1;
943 }
944 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000945 PyNumberMethods *nb;
946 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000947 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000948 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000949 if ((nb = vv->ob_type->tp_as_number) == NULL ||
950 nb->nb_int == NULL) {
951 PyErr_SetString(PyExc_TypeError, "an integer is required");
952 return -1;
953 }
954 io = (*nb->nb_int) (vv);
955 if (io == NULL)
956 return -1;
957 if (PyInt_Check(io)) {
958 bytes = PyInt_AsLong(io);
959 Py_DECREF(io);
960 return bytes;
961 }
962 if (PyLong_Check(io)) {
963 bytes = PyLong_AsLongLong(io);
964 Py_DECREF(io);
965 return bytes;
966 }
967 Py_DECREF(io);
968 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000969 return -1;
970 }
971
Tim Petersd1a7da62001-06-13 00:35:57 +0000972 res = _PyLong_AsByteArray(
973 (PyLongObject *)vv, (unsigned char *)&bytes,
974 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000975
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000976 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000977 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000978 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000979 else
980 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000981}
982
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000983/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000984 Return -1 and set an error if overflow occurs. */
985
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000986unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000987PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000988{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000989 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000990 int one = 1;
991 int res;
992
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000993 if (vv == NULL || !PyLong_Check(vv)) {
994 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000996 }
997
Tim Petersd1a7da62001-06-13 00:35:57 +0000998 res = _PyLong_AsByteArray(
999 (PyLongObject *)vv, (unsigned char *)&bytes,
1000 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001001
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001002 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001003 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001004 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001005 else
1006 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001007}
Tim Petersd1a7da62001-06-13 00:35:57 +00001008
Thomas Hellera4ea6032003-04-17 18:55:45 +00001009/* Get a C unsigned long int from a long int object, ignoring the high bits.
1010 Returns -1 and sets an error condition if an error occurs. */
1011
1012unsigned PY_LONG_LONG
1013PyLong_AsUnsignedLongLongMask(PyObject *vv)
1014{
1015 register PyLongObject *v;
1016 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001017 Py_ssize_t i;
1018 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001019
1020 if (vv == NULL || !PyLong_Check(vv)) {
1021 PyErr_BadInternalCall();
1022 return (unsigned long) -1;
1023 }
1024 v = (PyLongObject *)vv;
1025 i = v->ob_size;
1026 sign = 1;
1027 x = 0;
1028 if (i < 0) {
1029 sign = -1;
1030 i = -i;
1031 }
1032 while (--i >= 0) {
1033 x = (x << SHIFT) + v->ob_digit[i];
1034 }
1035 return x * sign;
1036}
Tim Petersd1a7da62001-06-13 00:35:57 +00001037#undef IS_LITTLE_ENDIAN
1038
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001039#endif /* HAVE_LONG_LONG */
1040
Neil Schemenauerba872e22001-01-04 01:46:03 +00001041
1042static int
1043convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001044 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001045 *a = (PyLongObject *) v;
1046 Py_INCREF(v);
1047 }
1048 else if (PyInt_Check(v)) {
1049 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1050 }
1051 else {
1052 return 0;
1053 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001054 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001055 *b = (PyLongObject *) w;
1056 Py_INCREF(w);
1057 }
1058 else if (PyInt_Check(w)) {
1059 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1060 }
1061 else {
1062 Py_DECREF(*a);
1063 return 0;
1064 }
1065 return 1;
1066}
1067
1068#define CONVERT_BINOP(v, w, a, b) \
1069 if (!convert_binop(v, w, a, b)) { \
1070 Py_INCREF(Py_NotImplemented); \
1071 return Py_NotImplemented; \
1072 }
1073
Tim Peters877a2122002-08-12 05:09:36 +00001074/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1075 * is modified in place, by adding y to it. Carries are propagated as far as
1076 * x[m-1], and the remaining carry (0 or 1) is returned.
1077 */
1078static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001079v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001080{
1081 int i;
1082 digit carry = 0;
1083
1084 assert(m >= n);
1085 for (i = 0; i < n; ++i) {
1086 carry += x[i] + y[i];
1087 x[i] = carry & MASK;
1088 carry >>= SHIFT;
1089 assert((carry & 1) == carry);
1090 }
1091 for (; carry && i < m; ++i) {
1092 carry += x[i];
1093 x[i] = carry & MASK;
1094 carry >>= SHIFT;
1095 assert((carry & 1) == carry);
1096 }
1097 return carry;
1098}
1099
1100/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1101 * is modified in place, by subtracting y from it. Borrows are propagated as
1102 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1103 */
1104static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001105v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001106{
1107 int i;
1108 digit borrow = 0;
1109
1110 assert(m >= n);
1111 for (i = 0; i < n; ++i) {
1112 borrow = x[i] - y[i] - borrow;
1113 x[i] = borrow & MASK;
1114 borrow >>= SHIFT;
1115 borrow &= 1; /* keep only 1 sign bit */
1116 }
1117 for (; borrow && i < m; ++i) {
1118 borrow = x[i] - borrow;
1119 x[i] = borrow & MASK;
1120 borrow >>= SHIFT;
1121 borrow &= 1;
1122 }
1123 return borrow;
1124}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001125
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001126/* Multiply by a single digit, ignoring the sign. */
1127
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001128static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001129mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001130{
1131 return muladd1(a, n, (digit)0);
1132}
1133
1134/* Multiply by a single digit and add a single digit, ignoring the sign. */
1135
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001137muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001138{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001139 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001140 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001141 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001142 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001143
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001144 if (z == NULL)
1145 return NULL;
1146 for (i = 0; i < size_a; ++i) {
1147 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001148 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001149 carry >>= SHIFT;
1150 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001151 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001152 return long_normalize(z);
1153}
1154
Tim Peters212e6142001-07-14 12:23:19 +00001155/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1156 in pout, and returning the remainder. pin and pout point at the LSD.
1157 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1158 long_format, but that should be done with great care since longs are
1159 immutable. */
1160
1161static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001162inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001163{
1164 twodigits rem = 0;
1165
1166 assert(n > 0 && n <= MASK);
1167 pin += size;
1168 pout += size;
1169 while (--size >= 0) {
1170 digit hi;
1171 rem = (rem << SHIFT) + *--pin;
1172 *--pout = hi = (digit)(rem / n);
1173 rem -= hi * n;
1174 }
1175 return (digit)rem;
1176}
1177
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178/* Divide a long integer by a digit, returning both the quotient
1179 (as function result) and the remainder (through *prem).
1180 The sign of a is ignored; n should not be zero. */
1181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001183divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001184{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001185 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001186 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001187
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001188 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001190 if (z == NULL)
1191 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001192 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001193 return long_normalize(z);
1194}
1195
1196/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001197 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001198 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001199
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001200static PyObject *
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001201long_format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001203 register PyLongObject *a = (PyLongObject *)aa;
1204 PyStringObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001205 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001206 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001207 char *p;
1208 int bits;
1209 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001210
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001211 if (a == NULL || !PyLong_Check(a)) {
1212 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001213 return NULL;
1214 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001215 assert(base >= 2 && base <= 36);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001216 size_a = ABS(a->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001217
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001218 /* Compute a rough upper bound for the length of the string */
1219 i = base;
1220 bits = 0;
1221 while (i > 1) {
1222 ++bits;
1223 i >>= 1;
1224 }
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001225 i = 5 + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001227 if (str == NULL)
1228 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001230 *p = '\0';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001231 if (a->ob_size < 0)
1232 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001233
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001234 if (a->ob_size == 0) {
1235 *--p = '0';
1236 }
1237 else if ((base & (base - 1)) == 0) {
1238 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001239 twodigits accum = 0;
1240 int accumbits = 0; /* # of bits in accum */
1241 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001242 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001243 while ((i >>= 1) > 1)
1244 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001245
1246 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001247 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001248 accumbits += SHIFT;
1249 assert(accumbits >= basebits);
1250 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001251 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001252 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001253 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001254 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001255 accumbits -= basebits;
1256 accum >>= basebits;
1257 } while (i < size_a-1 ? accumbits >= basebits :
1258 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001259 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001260 }
1261 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001262 /* Not 0, and base not a power of 2. Divide repeatedly by
1263 base, but for speed use the highest power of base that
1264 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001265 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001266 digit *pin = a->ob_digit;
1267 PyLongObject *scratch;
1268 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001269 digit powbase = base; /* powbase == base ** power */
1270 int power = 1;
1271 for (;;) {
1272 unsigned long newpow = powbase * (unsigned long)base;
1273 if (newpow >> SHIFT) /* doesn't fit in a digit */
1274 break;
1275 powbase = (digit)newpow;
1276 ++power;
1277 }
Tim Peters212e6142001-07-14 12:23:19 +00001278
1279 /* Get a scratch area for repeated division. */
1280 scratch = _PyLong_New(size);
1281 if (scratch == NULL) {
1282 Py_DECREF(str);
1283 return NULL;
1284 }
1285
1286 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001287 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001288 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001289 digit rem = inplace_divrem1(scratch->ob_digit,
1290 pin, size, powbase);
1291 pin = scratch->ob_digit; /* no need to use a again */
1292 if (pin[size - 1] == 0)
1293 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001294 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001295 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001296 Py_DECREF(str);
1297 return NULL;
1298 })
Tim Peters212e6142001-07-14 12:23:19 +00001299
1300 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001301 assert(ntostore > 0);
1302 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001303 digit nextrem = (digit)(rem / base);
1304 char c = (char)(rem - nextrem * base);
1305 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001306 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001307 *--p = c;
1308 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001309 --ntostore;
1310 /* Termination is a bit delicate: must not
1311 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001312 remaining quotient and rem are both 0. */
1313 } while (ntostore && (size || rem));
1314 } while (size != 0);
1315 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001316 }
1317
Guido van Rossum2c475421992-08-14 15:13:07 +00001318 if (base == 8) {
1319 if (size_a != 0)
1320 *--p = '0';
1321 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001322 else if (base == 16) {
1323 *--p = 'x';
1324 *--p = '0';
1325 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001326 else if (base != 10) {
1327 *--p = '#';
1328 *--p = '0' + base%10;
1329 if (base > 10)
1330 *--p = '0' + base/10;
1331 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001332 if (sign)
1333 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 if (p != PyString_AS_STRING(str)) {
1335 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001336 assert(p > q);
1337 do {
1338 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001339 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340 _PyString_Resize((PyObject **)&str,
1341 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001342 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001344}
1345
Thomas Wouters477c8d52006-05-27 19:21:47 +00001346/* Table of digit values for 8-bit string -> integer conversion.
1347 * '0' maps to 0, ..., '9' maps to 9.
1348 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1349 * All other indices map to 37.
1350 * Note that when converting a base B string, a char c is a legitimate
1351 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1352 */
1353int _PyLong_DigitValue[256] = {
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 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1357 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1358 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1359 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1360 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1361 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1362 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1363 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1364 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1365 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1366 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1367 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1368 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1369 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1370};
1371
1372/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001373 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1374 * non-digit (which may be *str!). A normalized long is returned.
1375 * The point to this routine is that it takes time linear in the number of
1376 * string characters.
1377 */
1378static PyLongObject *
1379long_from_binary_base(char **str, int base)
1380{
1381 char *p = *str;
1382 char *start = p;
1383 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001384 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001385 PyLongObject *z;
1386 twodigits accum;
1387 int bits_in_accum;
1388 digit *pdigit;
1389
1390 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1391 n = base;
1392 for (bits_per_char = -1; n; ++bits_per_char)
1393 n >>= 1;
1394 /* n <- total # of bits needed, while setting p to end-of-string */
1395 n = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001396 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001397 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001398 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001399 n = (p - start) * bits_per_char;
1400 if (n / bits_per_char != p - start) {
1401 PyErr_SetString(PyExc_ValueError,
1402 "long string too large to convert");
1403 return NULL;
1404 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001405 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1406 n = (n + SHIFT - 1) / SHIFT;
1407 z = _PyLong_New(n);
1408 if (z == NULL)
1409 return NULL;
1410 /* Read string from right, and fill in long from left; i.e.,
1411 * from least to most significant in both.
1412 */
1413 accum = 0;
1414 bits_in_accum = 0;
1415 pdigit = z->ob_digit;
1416 while (--p >= start) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001418 assert(k >= 0 && k < base);
1419 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001420 bits_in_accum += bits_per_char;
1421 if (bits_in_accum >= SHIFT) {
1422 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001423 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001424 accum >>= SHIFT;
1425 bits_in_accum -= SHIFT;
1426 assert(bits_in_accum < SHIFT);
1427 }
1428 }
1429 if (bits_in_accum) {
1430 assert(bits_in_accum <= SHIFT);
1431 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001432 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001433 }
1434 while (pdigit - z->ob_digit < n)
1435 *pdigit++ = 0;
1436 return long_normalize(z);
1437}
1438
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001439PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001440PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001441{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001442 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001443 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 PyLongObject *z;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001445 PyObject *strobj, *strrepr;
1446 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001447
Guido van Rossum472c04f1996-12-05 21:57:21 +00001448 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001449 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001450 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001451 return NULL;
1452 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001453 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001454 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001455 if (*str == '+')
1456 ++str;
1457 else if (*str == '-') {
1458 ++str;
1459 sign = -1;
1460 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001461 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001462 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001463 if (base == 0) {
1464 if (str[0] != '0')
1465 base = 10;
1466 else if (str[1] == 'x' || str[1] == 'X')
1467 base = 16;
1468 else
1469 base = 8;
1470 }
1471 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1472 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001473
Guido van Rossume6762971998-06-22 03:54:46 +00001474 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001475 if ((base & (base - 1)) == 0)
1476 z = long_from_binary_base(&str, base);
1477 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001478/***
1479Binary bases can be converted in time linear in the number of digits, because
1480Python's representation base is binary. Other bases (including decimal!) use
1481the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001482
Thomas Wouters477c8d52006-05-27 19:21:47 +00001483First some math: the largest integer that can be expressed in N base-B digits
1484is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1485case number of Python digits needed to hold it is the smallest integer n s.t.
1486
1487 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1488 BASE**n >= B**N [taking logs to base BASE]
1489 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1490
1491The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1492this quickly. A Python long with that much space is reserved near the start,
1493and the result is computed into it.
1494
1495The input string is actually treated as being in base base**i (i.e., i digits
1496are processed at a time), where two more static arrays hold:
1497
1498 convwidth_base[base] = the largest integer i such that base**i <= BASE
1499 convmultmax_base[base] = base ** convwidth_base[base]
1500
1501The first of these is the largest i such that i consecutive input digits
1502must fit in a single Python digit. The second is effectively the input
1503base we're really using.
1504
1505Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1506convmultmax_base[base], the result is "simply"
1507
1508 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1509
1510where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001511
1512Error analysis: as above, the number of Python digits `n` needed is worst-
1513case
1514
1515 n >= N * log(B)/log(BASE)
1516
1517where `N` is the number of input digits in base `B`. This is computed via
1518
1519 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1520
1521below. Two numeric concerns are how much space this can waste, and whether
1522the computed result can be too small. To be concrete, assume BASE = 2**15,
1523which is the default (and it's unlikely anyone changes that).
1524
1525Waste isn't a problem: provided the first input digit isn't 0, the difference
1526between the worst-case input with N digits and the smallest input with N
1527digits is about a factor of B, but B is small compared to BASE so at most
1528one allocated Python digit can remain unused on that count. If
1529N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1530and adding 1 returns a result 1 larger than necessary. However, that can't
1531happen: whenever B is a power of 2, long_from_binary_base() is called
1532instead, and it's impossible for B**i to be an integer power of 2**15 when
1533B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1534an exact integer when B is not a power of 2, since B**i has a prime factor
1535other than 2 in that case, but (2**15)**j's only prime factor is 2).
1536
1537The computed result can be too small if the true value of N*log(B)/log(BASE)
1538is a little bit larger than an exact integer, but due to roundoff errors (in
1539computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1540yields a numeric result a little less than that integer. Unfortunately, "how
1541close can a transcendental function get to an integer over some range?"
1542questions are generally theoretically intractable. Computer analysis via
1543continued fractions is practical: expand log(B)/log(BASE) via continued
1544fractions, giving a sequence i/j of "the best" rational approximations. Then
1545j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1546we can get very close to being in trouble, but very rarely. For example,
154776573 is a denominator in one of the continued-fraction approximations to
1548log(10)/log(2**15), and indeed:
1549
1550 >>> log(10)/log(2**15)*76573
1551 16958.000000654003
1552
1553is very close to an integer. If we were working with IEEE single-precision,
1554rounding errors could kill us. Finding worst cases in IEEE double-precision
1555requires better-than-double-precision log() functions, and Tim didn't bother.
1556Instead the code checks to see whether the allocated space is enough as each
1557new Python digit is added, and copies the whole thing to a larger long if not.
1558This should happen extremely rarely, and in fact I don't have a test case
1559that triggers it(!). Instead the code was tested by artificially allocating
1560just 1 digit at the start, so that the copying code was exercised for every
1561digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562***/
1563 register twodigits c; /* current input character */
1564 Py_ssize_t size_z;
1565 int i;
1566 int convwidth;
1567 twodigits convmultmax, convmult;
1568 digit *pz, *pzstop;
1569 char* scan;
1570
1571 static double log_base_BASE[37] = {0.0e0,};
1572 static int convwidth_base[37] = {0,};
1573 static twodigits convmultmax_base[37] = {0,};
1574
1575 if (log_base_BASE[base] == 0.0) {
1576 twodigits convmax = base;
1577 int i = 1;
1578
1579 log_base_BASE[base] = log((double)base) /
1580 log((double)BASE);
1581 for (;;) {
1582 twodigits next = convmax * base;
1583 if (next > BASE)
1584 break;
1585 convmax = next;
1586 ++i;
1587 }
1588 convmultmax_base[base] = convmax;
1589 assert(i > 0);
1590 convwidth_base[base] = i;
1591 }
1592
1593 /* Find length of the string of numeric characters. */
1594 scan = str;
1595 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
1596 ++scan;
1597
1598 /* Create a long object that can contain the largest possible
1599 * integer with this base and length. Note that there's no
1600 * need to initialize z->ob_digit -- no slot is read up before
1601 * being stored into.
1602 */
1603 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001604 /* Uncomment next line to test exceedingly rare copy code */
1605 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606 assert(size_z > 0);
1607 z = _PyLong_New(size_z);
1608 if (z == NULL)
1609 return NULL;
1610 z->ob_size = 0;
1611
1612 /* `convwidth` consecutive input digits are treated as a single
1613 * digit in base `convmultmax`.
1614 */
1615 convwidth = convwidth_base[base];
1616 convmultmax = convmultmax_base[base];
1617
1618 /* Work ;-) */
1619 while (str < scan) {
1620 /* grab up to convwidth digits from the input string */
1621 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
1622 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1623 c = (twodigits)(c * base +
1624 _PyLong_DigitValue[Py_CHARMASK(*str)]);
1625 assert(c < BASE);
1626 }
1627
1628 convmult = convmultmax;
1629 /* Calculate the shift only if we couldn't get
1630 * convwidth digits.
1631 */
1632 if (i != convwidth) {
1633 convmult = base;
1634 for ( ; i > 1; --i)
1635 convmult *= base;
1636 }
1637
1638 /* Multiply z by convmult, and add c. */
1639 pz = z->ob_digit;
1640 pzstop = pz + z->ob_size;
1641 for (; pz < pzstop; ++pz) {
1642 c += (twodigits)*pz * convmult;
1643 *pz = (digit)(c & MASK);
1644 c >>= SHIFT;
1645 }
1646 /* carry off the current end? */
1647 if (c) {
1648 assert(c < BASE);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001649 if (z->ob_size < size_z) {
1650 *pz = (digit)c;
1651 ++z->ob_size;
1652 }
1653 else {
1654 PyLongObject *tmp;
1655 /* Extremely rare. Get more space. */
1656 assert(z->ob_size == size_z);
1657 tmp = _PyLong_New(size_z + 1);
1658 if (tmp == NULL) {
1659 Py_DECREF(z);
1660 return NULL;
1661 }
1662 memcpy(tmp->ob_digit,
1663 z->ob_digit,
1664 sizeof(digit) * size_z);
1665 Py_DECREF(z);
1666 z = tmp;
1667 z->ob_digit[size_z] = (digit)c;
1668 ++size_z;
1669 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001670 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001671 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001672 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001673 if (z == NULL)
1674 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001675 if (str == start)
1676 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001677 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001678 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001679 if (*str == 'L' || *str == 'l')
1680 str++;
1681 while (*str && isspace(Py_CHARMASK(*str)))
1682 str++;
1683 if (*str != '\0')
1684 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001685 if (pend)
1686 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001687 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001688
1689 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001690 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001691 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1692 strobj = PyString_FromStringAndSize(orig_str, slen);
1693 if (strobj == NULL)
1694 return NULL;
1695 strrepr = PyObject_Repr(strobj);
1696 Py_DECREF(strobj);
1697 if (strrepr == NULL)
1698 return NULL;
1699 PyErr_Format(PyExc_ValueError,
1700 "invalid literal for long() with base %d: %s",
1701 base, PyString_AS_STRING(strrepr));
1702 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001703 return NULL;
1704}
1705
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001706#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001707PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001708PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001709{
Walter Dörwald07e14762002-11-06 16:15:14 +00001710 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001711 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001712
Walter Dörwald07e14762002-11-06 16:15:14 +00001713 if (buffer == NULL)
1714 return NULL;
1715
1716 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1717 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001718 return NULL;
1719 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001720 result = PyLong_FromString(buffer, NULL, base);
1721 PyMem_FREE(buffer);
1722 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001723}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001724#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001725
Tim Peters9f688bf2000-07-07 15:53:28 +00001726/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001727static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001728 (PyLongObject *, PyLongObject *, PyLongObject **);
1729static PyObject *long_pos(PyLongObject *);
1730static int long_divrem(PyLongObject *, PyLongObject *,
1731 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001732
1733/* Long division with remainder, top-level routine */
1734
Guido van Rossume32e0141992-01-19 16:31:05 +00001735static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001736long_divrem(PyLongObject *a, PyLongObject *b,
1737 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001738{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001739 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001740 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001741
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001743 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001744 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001745 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001746 }
1747 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001748 (size_a == size_b &&
1749 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001750 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001751 *pdiv = _PyLong_New(0);
1752 Py_INCREF(a);
1753 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001754 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001755 }
1756 if (size_b == 1) {
1757 digit rem = 0;
1758 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001759 if (z == NULL)
1760 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001761 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001762 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001763 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001764 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001765 if (z == NULL)
1766 return -1;
1767 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001768 /* Set the signs.
1769 The quotient z has the sign of a*b;
1770 the remainder r has the sign of a,
1771 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001772 if ((a->ob_size < 0) != (b->ob_size < 0))
1773 z->ob_size = -(z->ob_size);
1774 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1775 (*prem)->ob_size = -((*prem)->ob_size);
1776 *pdiv = z;
1777 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001778}
1779
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001780/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001781
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001782static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001783x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001784{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001785 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001786 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001787 PyLongObject *v = mul1(v1, d);
1788 PyLongObject *w = mul1(w1, d);
1789 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001790 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001791
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001792 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001793 Py_XDECREF(v);
1794 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001795 return NULL;
1796 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001797
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001798 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001799 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001800 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001801
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001802 size_v = ABS(v->ob_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001803 k = size_v - size_w;
1804 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001805
Thomas Wouters477c8d52006-05-27 19:21:47 +00001806 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001807 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1808 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001809 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001810 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001811
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001812 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001813 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001814 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001816 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001817 if (vj == w->ob_digit[size_w-1])
1818 q = MASK;
1819 else
1820 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1821 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001822
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001823 while (w->ob_digit[size_w-2]*q >
1824 ((
1825 ((twodigits)vj << SHIFT)
1826 + v->ob_digit[j-1]
1827 - q*w->ob_digit[size_w-1]
1828 ) << SHIFT)
1829 + v->ob_digit[j-2])
1830 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001831
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001832 for (i = 0; i < size_w && i+k < size_v; ++i) {
1833 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001834 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001835 carry += v->ob_digit[i+k] - z
1836 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001837 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001838 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1839 carry, SHIFT);
1840 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001841 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001842
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001843 if (i+k < size_v) {
1844 carry += v->ob_digit[i+k];
1845 v->ob_digit[i+k] = 0;
1846 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001847
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001848 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001849 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001850 else {
1851 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001852 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001853 carry = 0;
1854 for (i = 0; i < size_w && i+k < size_v; ++i) {
1855 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001856 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001857 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1858 BASE_TWODIGITS_TYPE,
1859 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001860 }
1861 }
1862 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001863
Guido van Rossumc206c761995-01-10 15:23:19 +00001864 if (a == NULL)
1865 *prem = NULL;
1866 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001867 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001868 *prem = divrem1(v, d, &d);
1869 /* d receives the (unused) remainder */
1870 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001871 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001872 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001873 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001874 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875 Py_DECREF(v);
1876 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001877 return a;
1878}
1879
1880/* Methods */
1881
1882static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001883long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001884{
Guido van Rossum9475a232001-10-05 20:51:39 +00001885 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001886}
1887
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001888static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001889long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001890{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00001891 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001892}
1893
1894static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001895long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001896{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001897 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001898
Guido van Rossumc6913e71991-11-19 20:26:46 +00001899 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001900 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001901 sign = 0;
1902 else
1903 sign = a->ob_size - b->ob_size;
1904 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001905 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001906 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001907 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1908 ;
1909 if (i < 0)
1910 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001911 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001912 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001913 if (a->ob_size < 0)
1914 sign = -sign;
1915 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001916 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001917 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001918}
1919
Guido van Rossum9bfef441993-03-29 10:43:31 +00001920static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001921long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001922{
1923 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001924 Py_ssize_t i;
1925 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001926
1927 /* This is designed so that Python ints and longs with the
1928 same value hash to the same value, otherwise comparisons
1929 of mapping keys will turn out weird */
1930 i = v->ob_size;
1931 sign = 1;
1932 x = 0;
1933 if (i < 0) {
1934 sign = -1;
1935 i = -(i);
1936 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001937#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001938 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001939 /* Force a native long #-bits (32 or 64) circular shift */
1940 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001941 x += v->ob_digit[i];
1942 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001943#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001944 x = x * sign;
1945 if (x == -1)
1946 x = -2;
1947 return x;
1948}
1949
1950
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001951/* Add the absolute values of two long integers. */
1952
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001953static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001954x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001955{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001956 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001957 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001958 int i;
1959 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001960
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001961 /* Ensure a is the larger of the two: */
1962 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001963 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001964 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001965 size_a = size_b;
1966 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001967 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001968 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001969 if (z == NULL)
1970 return NULL;
1971 for (i = 0; i < size_b; ++i) {
1972 carry += a->ob_digit[i] + b->ob_digit[i];
1973 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001974 carry >>= SHIFT;
1975 }
1976 for (; i < size_a; ++i) {
1977 carry += a->ob_digit[i];
1978 z->ob_digit[i] = carry & MASK;
1979 carry >>= SHIFT;
1980 }
1981 z->ob_digit[i] = carry;
1982 return long_normalize(z);
1983}
1984
1985/* Subtract the absolute values of two integers. */
1986
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001987static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001988x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001989{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001990 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001992 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001993 int sign = 1;
1994 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001995
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001996 /* Ensure a is the larger of the two: */
1997 if (size_a < size_b) {
1998 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001999 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002000 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002001 size_a = size_b;
2002 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002003 }
2004 else if (size_a == size_b) {
2005 /* Find highest digit where a and b differ: */
2006 i = size_a;
2007 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2008 ;
2009 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002010 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002011 if (a->ob_digit[i] < b->ob_digit[i]) {
2012 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002013 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002014 }
2015 size_a = size_b = i+1;
2016 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002018 if (z == NULL)
2019 return NULL;
2020 for (i = 0; i < size_b; ++i) {
2021 /* The following assumes unsigned arithmetic
2022 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002023 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002024 z->ob_digit[i] = borrow & MASK;
2025 borrow >>= SHIFT;
2026 borrow &= 1; /* Keep only one sign bit */
2027 }
2028 for (; i < size_a; ++i) {
2029 borrow = a->ob_digit[i] - borrow;
2030 z->ob_digit[i] = borrow & MASK;
2031 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002032 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002033 }
2034 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002035 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002036 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002037 return long_normalize(z);
2038}
2039
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002040static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002041long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002042{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002043 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002044
Neil Schemenauerba872e22001-01-04 01:46:03 +00002045 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2046
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002047 if (a->ob_size < 0) {
2048 if (b->ob_size < 0) {
2049 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002050 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002051 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002052 }
2053 else
2054 z = x_sub(b, a);
2055 }
2056 else {
2057 if (b->ob_size < 0)
2058 z = x_sub(a, b);
2059 else
2060 z = x_add(a, b);
2061 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002062 Py_DECREF(a);
2063 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002064 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002065}
2066
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002067static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002068long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002069{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002070 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002071
Neil Schemenauerba872e22001-01-04 01:46:03 +00002072 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2073
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002074 if (a->ob_size < 0) {
2075 if (b->ob_size < 0)
2076 z = x_sub(a, b);
2077 else
2078 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002079 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002080 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002081 }
2082 else {
2083 if (b->ob_size < 0)
2084 z = x_add(a, b);
2085 else
2086 z = x_sub(a, b);
2087 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002088 Py_DECREF(a);
2089 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002090 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002091}
2092
Tim Peters5af4e6c2002-08-12 02:31:19 +00002093/* Grade school multiplication, ignoring the signs.
2094 * Returns the absolute value of the product, or NULL if error.
2095 */
2096static PyLongObject *
2097x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002098{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002099 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002100 Py_ssize_t size_a = ABS(a->ob_size);
2101 Py_ssize_t size_b = ABS(b->ob_size);
2102 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002103
Tim Peters5af4e6c2002-08-12 02:31:19 +00002104 z = _PyLong_New(size_a + size_b);
2105 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002106 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002107
2108 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002109 if (a == b) {
2110 /* Efficient squaring per HAC, Algorithm 14.16:
2111 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2112 * Gives slightly less than a 2x speedup when a == b,
2113 * via exploiting that each entry in the multiplication
2114 * pyramid appears twice (except for the size_a squares).
2115 */
2116 for (i = 0; i < size_a; ++i) {
2117 twodigits carry;
2118 twodigits f = a->ob_digit[i];
2119 digit *pz = z->ob_digit + (i << 1);
2120 digit *pa = a->ob_digit + i + 1;
2121 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002122
Tim Peters0973b992004-08-29 22:16:50 +00002123 SIGCHECK({
2124 Py_DECREF(z);
2125 return NULL;
2126 })
2127
2128 carry = *pz + f * f;
2129 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002130 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002131 assert(carry <= MASK);
2132
2133 /* Now f is added in twice in each column of the
2134 * pyramid it appears. Same as adding f<<1 once.
2135 */
2136 f <<= 1;
2137 while (pa < paend) {
2138 carry += *pz + *pa++ * f;
2139 *pz++ = (digit)(carry & MASK);
2140 carry >>= SHIFT;
2141 assert(carry <= (MASK << 1));
2142 }
2143 if (carry) {
2144 carry += *pz;
2145 *pz++ = (digit)(carry & MASK);
2146 carry >>= SHIFT;
2147 }
2148 if (carry)
2149 *pz += (digit)(carry & MASK);
2150 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002151 }
Tim Peters0973b992004-08-29 22:16:50 +00002152 }
2153 else { /* a is not the same as b -- gradeschool long mult */
2154 for (i = 0; i < size_a; ++i) {
2155 twodigits carry = 0;
2156 twodigits f = a->ob_digit[i];
2157 digit *pz = z->ob_digit + i;
2158 digit *pb = b->ob_digit;
2159 digit *pbend = b->ob_digit + size_b;
2160
2161 SIGCHECK({
2162 Py_DECREF(z);
2163 return NULL;
2164 })
2165
2166 while (pb < pbend) {
2167 carry += *pz + *pb++ * f;
2168 *pz++ = (digit)(carry & MASK);
2169 carry >>= SHIFT;
2170 assert(carry <= MASK);
2171 }
2172 if (carry)
2173 *pz += (digit)(carry & MASK);
2174 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002175 }
2176 }
Tim Peters44121a62002-08-12 06:17:58 +00002177 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002178}
2179
2180/* A helper for Karatsuba multiplication (k_mul).
2181 Takes a long "n" and an integer "size" representing the place to
2182 split, and sets low and high such that abs(n) == (high << size) + low,
2183 viewing the shift as being by digits. The sign bit is ignored, and
2184 the return values are >= 0.
2185 Returns 0 on success, -1 on failure.
2186*/
2187static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002188kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002189{
2190 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002191 Py_ssize_t size_lo, size_hi;
2192 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002193
2194 size_lo = MIN(size_n, size);
2195 size_hi = size_n - size_lo;
2196
2197 if ((hi = _PyLong_New(size_hi)) == NULL)
2198 return -1;
2199 if ((lo = _PyLong_New(size_lo)) == NULL) {
2200 Py_DECREF(hi);
2201 return -1;
2202 }
2203
2204 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2205 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2206
2207 *high = long_normalize(hi);
2208 *low = long_normalize(lo);
2209 return 0;
2210}
2211
Tim Peters60004642002-08-12 22:01:34 +00002212static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2213
Tim Peters5af4e6c2002-08-12 02:31:19 +00002214/* Karatsuba multiplication. Ignores the input signs, and returns the
2215 * absolute value of the product (or NULL if error).
2216 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2217 */
2218static PyLongObject *
2219k_mul(PyLongObject *a, PyLongObject *b)
2220{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002221 Py_ssize_t asize = ABS(a->ob_size);
2222 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002223 PyLongObject *ah = NULL;
2224 PyLongObject *al = NULL;
2225 PyLongObject *bh = NULL;
2226 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002227 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002228 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002229 Py_ssize_t shift; /* the number of digits we split off */
2230 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002231
Tim Peters5af4e6c2002-08-12 02:31:19 +00002232 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2233 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2234 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002235 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002236 * By picking X to be a power of 2, "*X" is just shifting, and it's
2237 * been reduced to 3 multiplies on numbers half the size.
2238 */
2239
Tim Petersfc07e562002-08-12 02:54:10 +00002240 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002241 * is largest.
2242 */
Tim Peters738eda72002-08-12 15:08:20 +00002243 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002244 t1 = a;
2245 a = b;
2246 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002247
2248 i = asize;
2249 asize = bsize;
2250 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002251 }
2252
2253 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002254 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2255 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002256 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002257 return _PyLong_New(0);
2258 else
2259 return x_mul(a, b);
2260 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002261
Tim Peters60004642002-08-12 22:01:34 +00002262 /* If a is small compared to b, splitting on b gives a degenerate
2263 * case with ah==0, and Karatsuba may be (even much) less efficient
2264 * than "grade school" then. However, we can still win, by viewing
2265 * b as a string of "big digits", each of width a->ob_size. That
2266 * leads to a sequence of balanced calls to k_mul.
2267 */
2268 if (2 * asize <= bsize)
2269 return k_lopsided_mul(a, b);
2270
Tim Petersd6974a52002-08-13 20:37:51 +00002271 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002272 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002273 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002274 assert(ah->ob_size > 0); /* the split isn't degenerate */
2275
Tim Peters0973b992004-08-29 22:16:50 +00002276 if (a == b) {
2277 bh = ah;
2278 bl = al;
2279 Py_INCREF(bh);
2280 Py_INCREF(bl);
2281 }
2282 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002283
Tim Petersd64c1de2002-08-12 17:36:03 +00002284 /* The plan:
2285 * 1. Allocate result space (asize + bsize digits: that's always
2286 * enough).
2287 * 2. Compute ah*bh, and copy into result at 2*shift.
2288 * 3. Compute al*bl, and copy into result at 0. Note that this
2289 * can't overlap with #2.
2290 * 4. Subtract al*bl from the result, starting at shift. This may
2291 * underflow (borrow out of the high digit), but we don't care:
2292 * we're effectively doing unsigned arithmetic mod
2293 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2294 * borrows and carries out of the high digit can be ignored.
2295 * 5. Subtract ah*bh from the result, starting at shift.
2296 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2297 * at shift.
2298 */
2299
2300 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002301 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002302 if (ret == NULL) goto fail;
2303#ifdef Py_DEBUG
2304 /* Fill with trash, to catch reference to uninitialized digits. */
2305 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2306#endif
Tim Peters44121a62002-08-12 06:17:58 +00002307
Tim Petersd64c1de2002-08-12 17:36:03 +00002308 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002309 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2310 assert(t1->ob_size >= 0);
2311 assert(2*shift + t1->ob_size <= ret->ob_size);
2312 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2313 t1->ob_size * sizeof(digit));
2314
2315 /* Zero-out the digits higher than the ah*bh copy. */
2316 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002317 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002318 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002319 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002320
Tim Petersd64c1de2002-08-12 17:36:03 +00002321 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002322 if ((t2 = k_mul(al, bl)) == NULL) {
2323 Py_DECREF(t1);
2324 goto fail;
2325 }
2326 assert(t2->ob_size >= 0);
2327 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2328 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002329
2330 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002331 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002332 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002333 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002334
Tim Petersd64c1de2002-08-12 17:36:03 +00002335 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2336 * because it's fresher in cache.
2337 */
Tim Peters738eda72002-08-12 15:08:20 +00002338 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002339 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002340 Py_DECREF(t2);
2341
Tim Petersd64c1de2002-08-12 17:36:03 +00002342 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002343 Py_DECREF(t1);
2344
Tim Petersd64c1de2002-08-12 17:36:03 +00002345 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002346 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2347 Py_DECREF(ah);
2348 Py_DECREF(al);
2349 ah = al = NULL;
2350
Tim Peters0973b992004-08-29 22:16:50 +00002351 if (a == b) {
2352 t2 = t1;
2353 Py_INCREF(t2);
2354 }
2355 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002356 Py_DECREF(t1);
2357 goto fail;
2358 }
2359 Py_DECREF(bh);
2360 Py_DECREF(bl);
2361 bh = bl = NULL;
2362
Tim Peters738eda72002-08-12 15:08:20 +00002363 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002364 Py_DECREF(t1);
2365 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002366 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002367 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002368
Tim Petersd6974a52002-08-13 20:37:51 +00002369 /* Add t3. It's not obvious why we can't run out of room here.
2370 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002371 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002372 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002373 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002374
Tim Peters5af4e6c2002-08-12 02:31:19 +00002375 return long_normalize(ret);
2376
2377 fail:
2378 Py_XDECREF(ret);
2379 Py_XDECREF(ah);
2380 Py_XDECREF(al);
2381 Py_XDECREF(bh);
2382 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002383 return NULL;
2384}
2385
Tim Petersd6974a52002-08-13 20:37:51 +00002386/* (*) Why adding t3 can't "run out of room" above.
2387
Tim Petersab86c2b2002-08-15 20:06:00 +00002388Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2389to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002390
Tim Petersab86c2b2002-08-15 20:06:00 +000023911. For any integer i, i = c(i/2) + f(i/2). In particular,
2392 bsize = c(bsize/2) + f(bsize/2).
23932. shift = f(bsize/2)
23943. asize <= bsize
23954. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2396 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002397
Tim Petersab86c2b2002-08-15 20:06:00 +00002398We allocated asize + bsize result digits, and add t3 into them at an offset
2399of shift. This leaves asize+bsize-shift allocated digit positions for t3
2400to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2401asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002402
Tim Petersab86c2b2002-08-15 20:06:00 +00002403bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2404at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002405
Tim Petersab86c2b2002-08-15 20:06:00 +00002406If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2407digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2408most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002409
Tim Petersab86c2b2002-08-15 20:06:00 +00002410The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002411
Tim Petersab86c2b2002-08-15 20:06:00 +00002412 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002413
Tim Petersab86c2b2002-08-15 20:06:00 +00002414and we have asize + c(bsize/2) available digit positions. We need to show
2415this is always enough. An instance of c(bsize/2) cancels out in both, so
2416the question reduces to whether asize digits is enough to hold
2417(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2418then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2419asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2420digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2421asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002422c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2423is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2424bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002425
Tim Peters48d52c02002-08-14 17:07:32 +00002426Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2427clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2428ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002429*/
2430
Tim Peters60004642002-08-12 22:01:34 +00002431/* b has at least twice the digits of a, and a is big enough that Karatsuba
2432 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2433 * of slices, each with a->ob_size digits, and multiply the slices by a,
2434 * one at a time. This gives k_mul balanced inputs to work with, and is
2435 * also cache-friendly (we compute one double-width slice of the result
2436 * at a time, then move on, never bactracking except for the helpful
2437 * single-width slice overlap between successive partial sums).
2438 */
2439static PyLongObject *
2440k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2441{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002442 const Py_ssize_t asize = ABS(a->ob_size);
2443 Py_ssize_t bsize = ABS(b->ob_size);
2444 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002445 PyLongObject *ret;
2446 PyLongObject *bslice = NULL;
2447
2448 assert(asize > KARATSUBA_CUTOFF);
2449 assert(2 * asize <= bsize);
2450
2451 /* Allocate result space, and zero it out. */
2452 ret = _PyLong_New(asize + bsize);
2453 if (ret == NULL)
2454 return NULL;
2455 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2456
2457 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002458 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002459 if (bslice == NULL)
2460 goto fail;
2461
2462 nbdone = 0;
2463 while (bsize > 0) {
2464 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002465 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002466
2467 /* Multiply the next slice of b by a. */
2468 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2469 nbtouse * sizeof(digit));
2470 bslice->ob_size = nbtouse;
2471 product = k_mul(a, bslice);
2472 if (product == NULL)
2473 goto fail;
2474
2475 /* Add into result. */
2476 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2477 product->ob_digit, product->ob_size);
2478 Py_DECREF(product);
2479
2480 bsize -= nbtouse;
2481 nbdone += nbtouse;
2482 }
2483
2484 Py_DECREF(bslice);
2485 return long_normalize(ret);
2486
2487 fail:
2488 Py_DECREF(ret);
2489 Py_XDECREF(bslice);
2490 return NULL;
2491}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002492
2493static PyObject *
2494long_mul(PyLongObject *v, PyLongObject *w)
2495{
2496 PyLongObject *a, *b, *z;
2497
2498 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002499 Py_INCREF(Py_NotImplemented);
2500 return Py_NotImplemented;
2501 }
2502
Tim Petersd64c1de2002-08-12 17:36:03 +00002503 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002504 /* Negate if exactly one of the inputs is negative. */
2505 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002506 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002507 Py_DECREF(a);
2508 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002509 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002510}
2511
Guido van Rossume32e0141992-01-19 16:31:05 +00002512/* The / and % operators are now defined in terms of divmod().
2513 The expression a mod b has the value a - b*floor(a/b).
2514 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002515 |a| by |b|, with the sign of a. This is also expressed
2516 as a - b*trunc(a/b), if trunc truncates towards zero.
2517 Some examples:
2518 a b a rem b a mod b
2519 13 10 3 3
2520 -13 10 -3 7
2521 13 -10 3 -7
2522 -13 -10 -3 -3
2523 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002524 have different signs. We then subtract one from the 'div'
2525 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002526
Tim Peters47e52ee2004-08-30 02:44:38 +00002527/* Compute
2528 * *pdiv, *pmod = divmod(v, w)
2529 * NULL can be passed for pdiv or pmod, in which case that part of
2530 * the result is simply thrown away. The caller owns a reference to
2531 * each of these it requests (does not pass NULL for).
2532 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002533static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002534l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002535 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002537 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002538
Guido van Rossume32e0141992-01-19 16:31:05 +00002539 if (long_divrem(v, w, &div, &mod) < 0)
2540 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002541 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2542 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002543 PyLongObject *temp;
2544 PyLongObject *one;
2545 temp = (PyLongObject *) long_add(mod, w);
2546 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002547 mod = temp;
2548 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002549 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002550 return -1;
2551 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002552 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002553 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002554 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2555 Py_DECREF(mod);
2556 Py_DECREF(div);
2557 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002558 return -1;
2559 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002560 Py_DECREF(one);
2561 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002562 div = temp;
2563 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002564 if (pdiv != NULL)
2565 *pdiv = div;
2566 else
2567 Py_DECREF(div);
2568
2569 if (pmod != NULL)
2570 *pmod = mod;
2571 else
2572 Py_DECREF(mod);
2573
Guido van Rossume32e0141992-01-19 16:31:05 +00002574 return 0;
2575}
2576
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002577static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002578long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002579{
Tim Peters47e52ee2004-08-30 02:44:38 +00002580 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002581
2582 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002583 if (l_divmod(a, b, &div, NULL) < 0)
2584 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002585 Py_DECREF(a);
2586 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002587 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002588}
2589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002590static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002591long_true_divide(PyObject *v, PyObject *w)
2592{
Tim Peterse2a60002001-09-04 06:17:36 +00002593 PyLongObject *a, *b;
2594 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002595 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002596
2597 CONVERT_BINOP(v, w, &a, &b);
2598 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2599 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002600 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2601 Py_DECREF(a);
2602 Py_DECREF(b);
2603 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002604 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002605 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2606 but should really be set correctly after sucessful calls to
2607 _PyLong_AsScaledDouble() */
2608 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002609
2610 if (bd == 0.0) {
2611 PyErr_SetString(PyExc_ZeroDivisionError,
2612 "long division or modulo by zero");
2613 return NULL;
2614 }
2615
2616 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2617 ad /= bd; /* overflow/underflow impossible here */
2618 aexp -= bexp;
2619 if (aexp > INT_MAX / SHIFT)
2620 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002621 else if (aexp < -(INT_MAX / SHIFT))
2622 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002623 errno = 0;
2624 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002625 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002626 goto overflow;
2627 return PyFloat_FromDouble(ad);
2628
2629overflow:
2630 PyErr_SetString(PyExc_OverflowError,
2631 "long/long too large for a float");
2632 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002633
Tim Peters20dab9f2001-09-04 05:31:47 +00002634}
2635
2636static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002637long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002638{
Tim Peters47e52ee2004-08-30 02:44:38 +00002639 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002640
2641 CONVERT_BINOP(v, w, &a, &b);
2642
Tim Peters47e52ee2004-08-30 02:44:38 +00002643 if (l_divmod(a, b, NULL, &mod) < 0)
2644 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002645 Py_DECREF(a);
2646 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002647 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002648}
2649
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002650static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002651long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002652{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002653 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002654 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002655
2656 CONVERT_BINOP(v, w, &a, &b);
2657
2658 if (l_divmod(a, b, &div, &mod) < 0) {
2659 Py_DECREF(a);
2660 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002661 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002662 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002663 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002664 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002665 PyTuple_SetItem(z, 0, (PyObject *) div);
2666 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002667 }
2668 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002669 Py_DECREF(div);
2670 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002671 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002672 Py_DECREF(a);
2673 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002674 return z;
2675}
2676
Tim Peters47e52ee2004-08-30 02:44:38 +00002677/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002678static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002679long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002680{
Tim Peters47e52ee2004-08-30 02:44:38 +00002681 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2682 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002683
Tim Peters47e52ee2004-08-30 02:44:38 +00002684 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002685 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002686 PyLongObject *temp = NULL;
2687
2688 /* 5-ary values. If the exponent is large enough, table is
2689 * precomputed so that table[i] == a**i % c for i in range(32).
2690 */
2691 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2692 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2693
2694 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002695 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002696 if (PyLong_Check(x)) {
2697 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002698 Py_INCREF(x);
2699 }
Tim Petersde7990b2005-07-17 23:45:23 +00002700 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002701 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002702 if (c == NULL)
2703 goto Error;
2704 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002705 else if (x == Py_None)
2706 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002707 else {
2708 Py_DECREF(a);
2709 Py_DECREF(b);
2710 Py_INCREF(Py_NotImplemented);
2711 return Py_NotImplemented;
2712 }
Tim Peters4c483c42001-09-05 06:24:58 +00002713
Tim Peters47e52ee2004-08-30 02:44:38 +00002714 if (b->ob_size < 0) { /* if exponent is negative */
2715 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002716 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002717 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002718 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002719 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002720 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002721 /* else return a float. This works because we know
2722 that this calls float_pow() which converts its
2723 arguments to double. */
2724 Py_DECREF(a);
2725 Py_DECREF(b);
2726 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002727 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002728 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002729
2730 if (c) {
2731 /* if modulus == 0:
2732 raise ValueError() */
2733 if (c->ob_size == 0) {
2734 PyErr_SetString(PyExc_ValueError,
2735 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002736 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002737 }
2738
2739 /* if modulus < 0:
2740 negativeOutput = True
2741 modulus = -modulus */
2742 if (c->ob_size < 0) {
2743 negativeOutput = 1;
2744 temp = (PyLongObject *)_PyLong_Copy(c);
2745 if (temp == NULL)
2746 goto Error;
2747 Py_DECREF(c);
2748 c = temp;
2749 temp = NULL;
2750 c->ob_size = - c->ob_size;
2751 }
2752
2753 /* if modulus == 1:
2754 return 0 */
2755 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2756 z = (PyLongObject *)PyLong_FromLong(0L);
2757 goto Done;
2758 }
2759
2760 /* if base < 0:
2761 base = base % modulus
2762 Having the base positive just makes things easier. */
2763 if (a->ob_size < 0) {
2764 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002765 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002766 Py_DECREF(a);
2767 a = temp;
2768 temp = NULL;
2769 }
2770 }
2771
2772 /* At this point a, b, and c are guaranteed non-negative UNLESS
2773 c is NULL, in which case a may be negative. */
2774
2775 z = (PyLongObject *)PyLong_FromLong(1L);
2776 if (z == NULL)
2777 goto Error;
2778
2779 /* Perform a modular reduction, X = X % c, but leave X alone if c
2780 * is NULL.
2781 */
2782#define REDUCE(X) \
2783 if (c != NULL) { \
2784 if (l_divmod(X, c, NULL, &temp) < 0) \
2785 goto Error; \
2786 Py_XDECREF(X); \
2787 X = temp; \
2788 temp = NULL; \
2789 }
2790
2791 /* Multiply two values, then reduce the result:
2792 result = X*Y % c. If c is NULL, skip the mod. */
2793#define MULT(X, Y, result) \
2794{ \
2795 temp = (PyLongObject *)long_mul(X, Y); \
2796 if (temp == NULL) \
2797 goto Error; \
2798 Py_XDECREF(result); \
2799 result = temp; \
2800 temp = NULL; \
2801 REDUCE(result) \
2802}
2803
2804 if (b->ob_size <= FIVEARY_CUTOFF) {
2805 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2806 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2807 for (i = b->ob_size - 1; i >= 0; --i) {
2808 digit bi = b->ob_digit[i];
2809
2810 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2811 MULT(z, z, z)
2812 if (bi & j)
2813 MULT(z, a, z)
2814 }
2815 }
2816 }
2817 else {
2818 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2819 Py_INCREF(z); /* still holds 1L */
2820 table[0] = z;
2821 for (i = 1; i < 32; ++i)
2822 MULT(table[i-1], a, table[i])
2823
2824 for (i = b->ob_size - 1; i >= 0; --i) {
2825 const digit bi = b->ob_digit[i];
2826
2827 for (j = SHIFT - 5; j >= 0; j -= 5) {
2828 const int index = (bi >> j) & 0x1f;
2829 for (k = 0; k < 5; ++k)
2830 MULT(z, z, z)
2831 if (index)
2832 MULT(z, table[index], z)
2833 }
2834 }
2835 }
2836
2837 if (negativeOutput && (z->ob_size != 0)) {
2838 temp = (PyLongObject *)long_sub(z, c);
2839 if (temp == NULL)
2840 goto Error;
2841 Py_DECREF(z);
2842 z = temp;
2843 temp = NULL;
2844 }
2845 goto Done;
2846
2847 Error:
2848 if (z != NULL) {
2849 Py_DECREF(z);
2850 z = NULL;
2851 }
2852 /* fall through */
2853 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002854 if (b->ob_size > FIVEARY_CUTOFF) {
2855 for (i = 0; i < 32; ++i)
2856 Py_XDECREF(table[i]);
2857 }
Tim Petersde7990b2005-07-17 23:45:23 +00002858 Py_DECREF(a);
2859 Py_DECREF(b);
2860 Py_XDECREF(c);
2861 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002862 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002863}
2864
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002865static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002866long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002867{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002868 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002869 PyLongObject *x;
2870 PyLongObject *w;
2871 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002872 if (w == NULL)
2873 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002874 x = (PyLongObject *) long_add(v, w);
2875 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002876 if (x == NULL)
2877 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002878 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002879 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002880}
2881
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002882static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002883long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002884{
Tim Peters69c2de32001-09-11 22:31:33 +00002885 if (PyLong_CheckExact(v)) {
2886 Py_INCREF(v);
2887 return (PyObject *)v;
2888 }
2889 else
2890 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002891}
2892
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002893static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002894long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002895{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002896 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002897 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002898 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002899 Py_INCREF(v);
2900 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002901 }
Tim Peters69c2de32001-09-11 22:31:33 +00002902 z = (PyLongObject *)_PyLong_Copy(v);
2903 if (z != NULL)
2904 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002905 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002906}
2907
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002908static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002909long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002910{
2911 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002912 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002913 else
2914 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002915}
2916
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002917static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002918long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002919{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002920 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002921}
2922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002923static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002924long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002925{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002926 PyLongObject *a, *b;
2927 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002928 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002929 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002930 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002931
Neil Schemenauerba872e22001-01-04 01:46:03 +00002932 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2933
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002934 if (a->ob_size < 0) {
2935 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002936 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002937 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002938 if (a1 == NULL)
2939 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002940 a2 = (PyLongObject *) long_rshift(a1, b);
2941 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002942 if (a2 == NULL)
2943 goto rshift_error;
2944 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002945 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002946 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002947 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002948
Neil Schemenauerba872e22001-01-04 01:46:03 +00002949 shiftby = PyLong_AsLong((PyObject *)b);
2950 if (shiftby == -1L && PyErr_Occurred())
2951 goto rshift_error;
2952 if (shiftby < 0) {
2953 PyErr_SetString(PyExc_ValueError,
2954 "negative shift count");
2955 goto rshift_error;
2956 }
2957 wordshift = shiftby / SHIFT;
2958 newsize = ABS(a->ob_size) - wordshift;
2959 if (newsize <= 0) {
2960 z = _PyLong_New(0);
2961 Py_DECREF(a);
2962 Py_DECREF(b);
2963 return (PyObject *)z;
2964 }
2965 loshift = shiftby % SHIFT;
2966 hishift = SHIFT - loshift;
2967 lomask = ((digit)1 << hishift) - 1;
2968 himask = MASK ^ lomask;
2969 z = _PyLong_New(newsize);
2970 if (z == NULL)
2971 goto rshift_error;
2972 if (a->ob_size < 0)
2973 z->ob_size = -(z->ob_size);
2974 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2975 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2976 if (i+1 < newsize)
2977 z->ob_digit[i] |=
2978 (a->ob_digit[j+1] << hishift) & himask;
2979 }
2980 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002981 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002982rshift_error:
2983 Py_DECREF(a);
2984 Py_DECREF(b);
2985 return (PyObject *) z;
2986
Guido van Rossumc6913e71991-11-19 20:26:46 +00002987}
2988
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002989static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002990long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002991{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002992 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002993 PyLongObject *a, *b;
2994 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002995 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002996 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002997 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002998
Neil Schemenauerba872e22001-01-04 01:46:03 +00002999 CONVERT_BINOP(v, w, &a, &b);
3000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003001 shiftby = PyLong_AsLong((PyObject *)b);
3002 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003003 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003004 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003005 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003006 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003007 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003008 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003009 PyErr_SetString(PyExc_ValueError,
3010 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003011 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003012 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003013 /* wordshift, remshift = divmod(shiftby, SHIFT) */
3014 wordshift = (int)shiftby / SHIFT;
3015 remshift = (int)shiftby - wordshift * SHIFT;
3016
3017 oldsize = ABS(a->ob_size);
3018 newsize = oldsize + wordshift;
3019 if (remshift)
3020 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003021 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003022 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003023 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003024 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003025 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003026 for (i = 0; i < wordshift; i++)
3027 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003028 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003029 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003030 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003031 z->ob_digit[i] = (digit)(accum & MASK);
3032 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003033 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003034 if (remshift)
3035 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003036 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003037 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003038 z = long_normalize(z);
3039lshift_error:
3040 Py_DECREF(a);
3041 Py_DECREF(b);
3042 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003043}
3044
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003045
3046/* Bitwise and/xor/or operations */
3047
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003048static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003049long_bitwise(PyLongObject *a,
3050 int op, /* '&', '|', '^' */
3051 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003052{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003053 digit maska, maskb; /* 0 or MASK */
3054 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003055 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003056 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003057 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003058 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003059 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003060
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003061 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003062 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003063 if (a == NULL)
3064 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003065 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003066 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003067 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003068 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003069 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003070 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003071 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003072 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003073 if (b == NULL) {
3074 Py_DECREF(a);
3075 return NULL;
3076 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003077 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003078 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003079 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003080 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003081 maskb = 0;
3082 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003083
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003084 negz = 0;
3085 switch (op) {
3086 case '^':
3087 if (maska != maskb) {
3088 maska ^= MASK;
3089 negz = -1;
3090 }
3091 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003092 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003093 if (maska && maskb) {
3094 op = '|';
3095 maska ^= MASK;
3096 maskb ^= MASK;
3097 negz = -1;
3098 }
3099 break;
3100 case '|':
3101 if (maska || maskb) {
3102 op = '&';
3103 maska ^= MASK;
3104 maskb ^= MASK;
3105 negz = -1;
3106 }
3107 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003108 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003109
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003110 /* JRH: The original logic here was to allocate the result value (z)
3111 as the longer of the two operands. However, there are some cases
3112 where the result is guaranteed to be shorter than that: AND of two
3113 positives, OR of two negatives: use the shorter number. AND with
3114 mixed signs: use the positive number. OR with mixed signs: use the
3115 negative number. After the transformations above, op will be '&'
3116 iff one of these cases applies, and mask will be non-0 for operands
3117 whose length should be ignored.
3118 */
3119
3120 size_a = a->ob_size;
3121 size_b = b->ob_size;
3122 size_z = op == '&'
3123 ? (maska
3124 ? size_b
3125 : (maskb ? size_a : MIN(size_a, size_b)))
3126 : MAX(size_a, size_b);
3127 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003128 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003129 Py_DECREF(a);
3130 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003131 return NULL;
3132 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003133
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003134 for (i = 0; i < size_z; ++i) {
3135 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3136 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3137 switch (op) {
3138 case '&': z->ob_digit[i] = diga & digb; break;
3139 case '|': z->ob_digit[i] = diga | digb; break;
3140 case '^': z->ob_digit[i] = diga ^ digb; break;
3141 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003142 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003143
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003144 Py_DECREF(a);
3145 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003146 z = long_normalize(z);
3147 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003148 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003149 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003150 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003151 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003152}
3153
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003154static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003155long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003156{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003157 PyLongObject *a, *b;
3158 PyObject *c;
3159 CONVERT_BINOP(v, w, &a, &b);
3160 c = long_bitwise(a, '&', b);
3161 Py_DECREF(a);
3162 Py_DECREF(b);
3163 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003164}
3165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003166static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003167long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003168{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003169 PyLongObject *a, *b;
3170 PyObject *c;
3171 CONVERT_BINOP(v, w, &a, &b);
3172 c = long_bitwise(a, '^', b);
3173 Py_DECREF(a);
3174 Py_DECREF(b);
3175 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003176}
3177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003178static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003179long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003180{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003181 PyLongObject *a, *b;
3182 PyObject *c;
3183 CONVERT_BINOP(v, w, &a, &b);
3184 c = long_bitwise(a, '|', b);
3185 Py_DECREF(a);
3186 Py_DECREF(b);
3187 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003188}
3189
Guido van Rossum234f9421993-06-17 12:35:49 +00003190static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003191long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003192{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003193 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003194 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003195 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003196 return 0;
3197 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003198 else if (PyLong_Check(*pw)) {
3199 Py_INCREF(*pv);
3200 Py_INCREF(*pw);
3201 return 0;
3202 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003203 return 1; /* Can't do it */
3204}
3205
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003206static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003207long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003208{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003209 if (PyLong_CheckExact(v))
3210 Py_INCREF(v);
3211 else
3212 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003213 return v;
3214}
3215
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003216static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003217long_int(PyObject *v)
3218{
3219 long x;
3220 x = PyLong_AsLong(v);
3221 if (PyErr_Occurred()) {
3222 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3223 PyErr_Clear();
3224 if (PyLong_CheckExact(v)) {
3225 Py_INCREF(v);
3226 return v;
3227 }
3228 else
3229 return _PyLong_Copy((PyLongObject *)v);
3230 }
3231 else
3232 return NULL;
3233 }
3234 return PyInt_FromLong(x);
3235}
3236
3237static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003238long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003239{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003240 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003241 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003242 if (result == -1.0 && PyErr_Occurred())
3243 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003244 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003245}
3246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003247static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003248long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003249{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003250 return long_format(v, 8);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003251}
3252
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003253static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003254long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003255{
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003256 return long_format(v, 16);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003257}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003258
3259static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003260long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003261
Tim Peters6d6c1a32001-08-02 04:15:00 +00003262static PyObject *
3263long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3264{
3265 PyObject *x = NULL;
3266 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003267 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003268
Guido van Rossumbef14172001-08-29 15:47:46 +00003269 if (type != &PyLong_Type)
3270 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003271 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3272 &x, &base))
3273 return NULL;
3274 if (x == NULL)
3275 return PyLong_FromLong(0L);
3276 if (base == -909)
3277 return PyNumber_Long(x);
3278 else if (PyString_Check(x))
3279 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003280#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003281 else if (PyUnicode_Check(x))
3282 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3283 PyUnicode_GET_SIZE(x),
3284 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003285#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003286 else {
3287 PyErr_SetString(PyExc_TypeError,
3288 "long() can't convert non-string with explicit base");
3289 return NULL;
3290 }
3291}
3292
Guido van Rossumbef14172001-08-29 15:47:46 +00003293/* Wimpy, slow approach to tp_new calls for subtypes of long:
3294 first create a regular long from whatever arguments we got,
3295 then allocate a subtype instance and initialize it from
3296 the regular long. The regular long is then thrown away.
3297*/
3298static PyObject *
3299long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3300{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003301 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003302 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003303
3304 assert(PyType_IsSubtype(type, &PyLong_Type));
3305 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3306 if (tmp == NULL)
3307 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003308 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003309 n = tmp->ob_size;
3310 if (n < 0)
3311 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003312 newobj = (PyLongObject *)type->tp_alloc(type, n);
3313 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003314 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003315 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003316 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317 assert(PyLong_Check(newobj));
3318 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003319 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003320 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003321 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003322 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003323}
3324
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003325static PyObject *
3326long_getnewargs(PyLongObject *v)
3327{
3328 return Py_BuildValue("(N)", _PyLong_Copy(v));
3329}
3330
3331static PyMethodDef long_methods[] = {
3332 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3333 {NULL, NULL} /* sentinel */
3334};
3335
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003336PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003337"long(x[, base]) -> integer\n\
3338\n\
3339Convert a string or number to a long integer, if possible. A floating\n\
3340point argument will be truncated towards zero (this does not include a\n\
3341string representation of a floating point number!) When converting a\n\
3342string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003343converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003344
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003345static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003346 (binaryfunc) long_add, /*nb_add*/
3347 (binaryfunc) long_sub, /*nb_subtract*/
3348 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003349 long_mod, /*nb_remainder*/
3350 long_divmod, /*nb_divmod*/
3351 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003352 (unaryfunc) long_neg, /*nb_negative*/
3353 (unaryfunc) long_pos, /*tp_positive*/
3354 (unaryfunc) long_abs, /*tp_absolute*/
3355 (inquiry) long_nonzero, /*tp_nonzero*/
3356 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003357 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003358 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003359 long_and, /*nb_and*/
3360 long_xor, /*nb_xor*/
3361 long_or, /*nb_or*/
3362 long_coerce, /*nb_coerce*/
3363 long_int, /*nb_int*/
3364 long_long, /*nb_long*/
3365 long_float, /*nb_float*/
3366 long_oct, /*nb_oct*/
3367 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003368 0, /* nb_inplace_add */
3369 0, /* nb_inplace_subtract */
3370 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003371 0, /* nb_inplace_remainder */
3372 0, /* nb_inplace_power */
3373 0, /* nb_inplace_lshift */
3374 0, /* nb_inplace_rshift */
3375 0, /* nb_inplace_and */
3376 0, /* nb_inplace_xor */
3377 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003378 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003379 long_true_divide, /* nb_true_divide */
3380 0, /* nb_inplace_floor_divide */
3381 0, /* nb_inplace_true_divide */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003382 long_index, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003383};
3384
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003385PyTypeObject PyLong_Type = {
3386 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003387 0, /* ob_size */
3388 "long", /* tp_name */
3389 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3390 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003391 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392 0, /* tp_print */
3393 0, /* tp_getattr */
3394 0, /* tp_setattr */
3395 (cmpfunc)long_compare, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003396 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003397 &long_as_number, /* tp_as_number */
3398 0, /* tp_as_sequence */
3399 0, /* tp_as_mapping */
3400 (hashfunc)long_hash, /* tp_hash */
3401 0, /* tp_call */
Guido van Rossumd2dbecb2006-08-18 16:29:54 +00003402 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003403 PyObject_GenericGetAttr, /* tp_getattro */
3404 0, /* tp_setattro */
3405 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003406 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003407 long_doc, /* tp_doc */
3408 0, /* tp_traverse */
3409 0, /* tp_clear */
3410 0, /* tp_richcompare */
3411 0, /* tp_weaklistoffset */
3412 0, /* tp_iter */
3413 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003414 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003415 0, /* tp_members */
3416 0, /* tp_getset */
3417 0, /* tp_base */
3418 0, /* tp_dict */
3419 0, /* tp_descr_get */
3420 0, /* tp_descr_set */
3421 0, /* tp_dictoffset */
3422 0, /* tp_init */
3423 0, /* tp_alloc */
3424 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003425 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003426};