blob: b6cb61a02b59782be4871c5e8e3fdb1851c81ed0 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003/* Long (arbitrary precision) integer object implementation */
4
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00005/* XXX The functional organization of this file is terrible */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00008#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Tim Peters5af4e6c2002-08-12 02:31:19 +000012/* For long multiplication, use the O(N**2) school algorithm unless
13 * both operands contain more than KARATSUBA_CUTOFF digits (this
14 * being an internal Python long digit, in base BASE).
15 */
Tim Peters0973b992004-08-29 22:16:50 +000016#define KARATSUBA_CUTOFF 70
17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000018
Tim Peters47e52ee2004-08-30 02:44:38 +000019/* For exponentiation, use the binary left-to-right algorithm
20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
21 * In that case, do 5 bits at a time. The potential drawback is that
22 * a table of 2**5 intermediate results is computed.
23 */
24#define FIVEARY_CUTOFF 8
25
Guido van Rossume32e0141992-01-19 16:31:05 +000026#define ABS(x) ((x) < 0 ? -(x) : (x))
27
Tim Peters5af4e6c2002-08-12 02:31:19 +000028#undef MIN
29#undef MAX
30#define MAX(x, y) ((x) < (y) ? (y) : (x))
31#define MIN(x, y) ((x) > (y) ? (y) : (x))
32
Guido van Rossume32e0141992-01-19 16:31:05 +000033/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000034static PyLongObject *long_normalize(PyLongObject *);
35static PyLongObject *mul1(PyLongObject *, wdigit);
36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000037static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Tim Peters9f688bf2000-07-07 15:53:28 +000038static PyObject *long_format(PyObject *aa, int base, int addL);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Tim Petersd89fc222006-05-25 22:28:46 +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{
Neal Norwitzc6a989a2006-05-10 06:57:58 +000069 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000070 PyErr_NoMemory();
71 return NULL;
72 }
Neal Norwitz4f3be8a2008-07-31 17:08:14 +000073 /* XXX(nnorwitz): This can overflow --
74 PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000076}
77
Tim Peters64b5ce32001-09-10 20:52:51 +000078PyObject *
79_PyLong_Copy(PyLongObject *src)
80{
81 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000082 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000083
84 assert(src != NULL);
85 i = src->ob_size;
86 if (i < 0)
87 i = -(i);
88 result = _PyLong_New(i);
89 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000090 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000091 while (--i >= 0)
92 result->ob_digit[i] = src->ob_digit[i];
93 }
94 return (PyObject *)result;
95}
96
Guido van Rossumedcc38a1991-05-05 20:09:44 +000097/* Create a new long int object from a C long int */
98
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000100PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000101{
Tim Petersce9de2f2001-06-14 04:56:19 +0000102 PyLongObject *v;
103 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
104 int ndigits = 0;
105 int negative = 0;
106
107 if (ival < 0) {
108 ival = -ival;
109 negative = 1;
110 }
111
112 /* Count the number of Python digits.
113 We used to pick 5 ("big enough for anything"), but that's a
114 waste of time and space given that 5*15 = 75 bits are rarely
115 needed. */
116 t = (unsigned long)ival;
117 while (t) {
118 ++ndigits;
119 t >>= SHIFT;
120 }
121 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000122 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000123 digit *p = v->ob_digit;
124 v->ob_size = negative ? -ndigits : ndigits;
125 t = (unsigned long)ival;
126 while (t) {
127 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000128 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000129 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000130 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000132}
133
Guido van Rossum53756b11997-01-03 17:14:46 +0000134/* Create a new long int object from a C unsigned long int */
135
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000137PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000138{
Tim Petersce9de2f2001-06-14 04:56:19 +0000139 PyLongObject *v;
140 unsigned long t;
141 int ndigits = 0;
142
143 /* Count the number of Python digits. */
144 t = (unsigned long)ival;
145 while (t) {
146 ++ndigits;
147 t >>= SHIFT;
148 }
149 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000150 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000151 digit *p = v->ob_digit;
152 v->ob_size = ndigits;
153 while (ival) {
154 *p++ = (digit)(ival & MASK);
155 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000156 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000157 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000159}
160
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000161/* Create a new long int object from a C double */
162
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000165{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000167 double frac;
168 int i, ndig, expo, neg;
169 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000170 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000171 PyErr_SetString(PyExc_OverflowError,
172 "cannot convert float infinity to long");
173 return NULL;
174 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000175 if (dval < 0.0) {
176 neg = 1;
177 dval = -dval;
178 }
179 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
180 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000182 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000184 if (v == NULL)
185 return NULL;
186 frac = ldexp(frac, (expo-1) % SHIFT + 1);
187 for (i = ndig; --i >= 0; ) {
188 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000189 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000190 frac = frac - (double)bits;
191 frac = ldexp(frac, SHIFT);
192 }
193 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000194 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000196}
197
Armin Rigo4b63c212006-10-04 11:44:06 +0000198/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
199 * anything about what happens when a signed integer operation overflows,
200 * and some compilers think they're doing you a favor by being "clever"
201 * then. The bit pattern for the largest postive signed long is
202 * (unsigned long)LONG_MAX, and for the smallest negative signed long
203 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
204 * However, some other compilers warn about applying unary minus to an
205 * unsigned operand. Hence the weird "0-".
206 */
207#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
208#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
209
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000210/* Get a C long int from a long int object.
211 Returns -1 and sets an error condition if overflow occurs. */
212
213long
Tim Peters9f688bf2000-07-07 15:53:28 +0000214PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000215{
Guido van Rossumf7531811998-05-26 14:33:37 +0000216 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000218 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219 Py_ssize_t i;
220 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000221
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000223 if (vv != NULL && PyInt_Check(vv))
224 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000226 return -1;
227 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000229 i = v->ob_size;
230 sign = 1;
231 x = 0;
232 if (i < 0) {
233 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000234 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000235 }
236 while (--i >= 0) {
237 prev = x;
238 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000239 if ((x >> SHIFT) != prev)
240 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000241 }
Armin Rigo4b63c212006-10-04 11:44:06 +0000242 /* Haven't lost any bits, but casting to long requires extra care
243 * (see comment above).
244 */
245 if (x <= (unsigned long)LONG_MAX) {
246 return (long)x * sign;
247 }
248 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
249 return LONG_MIN;
250 }
251 /* else overflow */
Guido van Rossumf7531811998-05-26 14:33:37 +0000252
253 overflow:
254 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000255 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000256 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000257}
258
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000259/* Get a Py_ssize_t from a long int object.
260 Returns -1 and sets an error condition if overflow occurs. */
261
262Py_ssize_t
263_PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000264 register PyLongObject *v;
265 size_t x, prev;
266 Py_ssize_t i;
267 int sign;
268
269 if (vv == NULL || !PyLong_Check(vv)) {
270 PyErr_BadInternalCall();
271 return -1;
272 }
273 v = (PyLongObject *)vv;
274 i = v->ob_size;
275 sign = 1;
276 x = 0;
277 if (i < 0) {
278 sign = -1;
279 i = -(i);
280 }
281 while (--i >= 0) {
282 prev = x;
283 x = (x << SHIFT) + v->ob_digit[i];
284 if ((x >> SHIFT) != prev)
285 goto overflow;
286 }
Armin Rigo4b63c212006-10-04 11:44:06 +0000287 /* Haven't lost any bits, but casting to a signed type requires
288 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000289 */
Armin Rigo4b63c212006-10-04 11:44:06 +0000290 if (x <= (size_t)PY_SSIZE_T_MAX) {
291 return (Py_ssize_t)x * sign;
292 }
293 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
294 return PY_SSIZE_T_MIN;
295 }
296 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000297
298 overflow:
299 PyErr_SetString(PyExc_OverflowError,
300 "long int too large to convert to int");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000301 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000302}
303
Guido van Rossumd8c80482002-08-13 00:24:58 +0000304/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000305 Returns -1 and sets an error condition if overflow occurs. */
306
307unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000308PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000309{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000311 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000312 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000313
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000315 if (vv != NULL && PyInt_Check(vv)) {
316 long val = PyInt_AsLong(vv);
317 if (val < 0) {
318 PyErr_SetString(PyExc_OverflowError,
319 "can't convert negative value to unsigned long");
320 return (unsigned long) -1;
321 }
322 return val;
323 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000325 return (unsigned long) -1;
326 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000328 i = v->ob_size;
329 x = 0;
330 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000332 "can't convert negative value to unsigned long");
333 return (unsigned long) -1;
334 }
335 while (--i >= 0) {
336 prev = x;
337 x = (x << SHIFT) + v->ob_digit[i];
338 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000340 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000341 return (unsigned long) -1;
342 }
343 }
344 return x;
345}
346
Thomas Hellera4ea6032003-04-17 18:55:45 +0000347/* Get a C unsigned long int from a long int object, ignoring the high bits.
348 Returns -1 and sets an error condition if an error occurs. */
349
350unsigned long
351PyLong_AsUnsignedLongMask(PyObject *vv)
352{
353 register PyLongObject *v;
354 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355 Py_ssize_t i;
356 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000357
358 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000359 if (vv != NULL && PyInt_Check(vv))
360 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000361 PyErr_BadInternalCall();
362 return (unsigned long) -1;
363 }
364 v = (PyLongObject *)vv;
365 i = v->ob_size;
366 sign = 1;
367 x = 0;
368 if (i < 0) {
369 sign = -1;
370 i = -i;
371 }
372 while (--i >= 0) {
373 x = (x << SHIFT) + v->ob_digit[i];
374 }
375 return x * sign;
376}
377
Tim Peters5b8132f2003-01-31 15:52:05 +0000378int
379_PyLong_Sign(PyObject *vv)
380{
381 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000382
383 assert(v != NULL);
384 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000385
Tim Petersee1a53c2003-02-02 02:57:53 +0000386 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000387}
388
Tim Petersbaefd9e2003-01-28 20:37:45 +0000389size_t
390_PyLong_NumBits(PyObject *vv)
391{
392 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000393 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000395
396 assert(v != NULL);
397 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000398 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000399 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
400 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000401 digit msd = v->ob_digit[ndigits - 1];
402
Tim Peters5b8132f2003-01-31 15:52:05 +0000403 result = (ndigits - 1) * SHIFT;
Skip Montanaro429433b2006-04-18 00:35:43 +0000404 if (result / SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000405 goto Overflow;
406 do {
407 ++result;
408 if (result == 0)
409 goto Overflow;
410 msd >>= 1;
411 } while (msd);
412 }
413 return result;
414
415Overflow:
416 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
417 "to express in a platform size_t");
418 return (size_t)-1;
419}
420
Tim Peters2a9b3672001-06-11 21:23:58 +0000421PyObject *
422_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
423 int little_endian, int is_signed)
424{
425 const unsigned char* pstartbyte;/* LSB of bytes */
426 int incr; /* direction to move pstartbyte */
427 const unsigned char* pendbyte; /* MSB of bytes */
428 size_t numsignificantbytes; /* number of bytes that matter */
429 size_t ndigits; /* number of Python long digits */
430 PyLongObject* v; /* result */
431 int idigit = 0; /* next free index in v->ob_digit */
432
433 if (n == 0)
434 return PyLong_FromLong(0L);
435
436 if (little_endian) {
437 pstartbyte = bytes;
438 pendbyte = bytes + n - 1;
439 incr = 1;
440 }
441 else {
442 pstartbyte = bytes + n - 1;
443 pendbyte = bytes;
444 incr = -1;
445 }
446
447 if (is_signed)
448 is_signed = *pendbyte >= 0x80;
449
450 /* Compute numsignificantbytes. This consists of finding the most
451 significant byte. Leading 0 bytes are insignficant if the number
452 is positive, and leading 0xff bytes if negative. */
453 {
454 size_t i;
455 const unsigned char* p = pendbyte;
456 const int pincr = -incr; /* search MSB to LSB */
457 const unsigned char insignficant = is_signed ? 0xff : 0x00;
458
459 for (i = 0; i < n; ++i, p += pincr) {
460 if (*p != insignficant)
461 break;
462 }
463 numsignificantbytes = n - i;
464 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
465 actually has 2 significant bytes. OTOH, 0xff0001 ==
466 -0x00ffff, so we wouldn't *need* to bump it there; but we
467 do for 0xffff = -0x0001. To be safe without bothering to
468 check every case, bump it regardless. */
469 if (is_signed && numsignificantbytes < n)
470 ++numsignificantbytes;
471 }
472
473 /* How many Python long digits do we need? We have
474 8*numsignificantbytes bits, and each Python long digit has SHIFT
475 bits, so it's the ceiling of the quotient. */
476 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
477 if (ndigits > (size_t)INT_MAX)
478 return PyErr_NoMemory();
479 v = _PyLong_New((int)ndigits);
480 if (v == NULL)
481 return NULL;
482
483 /* Copy the bits over. The tricky parts are computing 2's-comp on
484 the fly for signed numbers, and dealing with the mismatch between
485 8-bit bytes and (probably) 15-bit Python digits.*/
486 {
487 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000488 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000489 twodigits accum = 0; /* sliding register */
490 unsigned int accumbits = 0; /* number of bits in accum */
491 const unsigned char* p = pstartbyte;
492
493 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000494 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000495 /* Compute correction for 2's comp, if needed. */
496 if (is_signed) {
497 thisbyte = (0xff ^ thisbyte) + carry;
498 carry = thisbyte >> 8;
499 thisbyte &= 0xff;
500 }
501 /* Because we're going LSB to MSB, thisbyte is
502 more significant than what's already in accum,
503 so needs to be prepended to accum. */
504 accum |= thisbyte << accumbits;
505 accumbits += 8;
506 if (accumbits >= SHIFT) {
507 /* There's enough to fill a Python digit. */
508 assert(idigit < (int)ndigits);
509 v->ob_digit[idigit] = (digit)(accum & MASK);
510 ++idigit;
511 accum >>= SHIFT;
512 accumbits -= SHIFT;
513 assert(accumbits < SHIFT);
514 }
515 }
516 assert(accumbits < SHIFT);
517 if (accumbits) {
518 assert(idigit < (int)ndigits);
519 v->ob_digit[idigit] = (digit)accum;
520 ++idigit;
521 }
522 }
523
524 v->ob_size = is_signed ? -idigit : idigit;
525 return (PyObject *)long_normalize(v);
526}
527
528int
529_PyLong_AsByteArray(PyLongObject* v,
530 unsigned char* bytes, size_t n,
531 int little_endian, int is_signed)
532{
533 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000535 twodigits accum; /* sliding register */
536 unsigned int accumbits; /* # bits in accum */
537 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
538 twodigits carry; /* for computing 2's-comp */
539 size_t j; /* # bytes filled */
540 unsigned char* p; /* pointer to next byte in bytes */
541 int pincr; /* direction to move p */
542
543 assert(v != NULL && PyLong_Check(v));
544
545 if (v->ob_size < 0) {
546 ndigits = -(v->ob_size);
547 if (!is_signed) {
548 PyErr_SetString(PyExc_TypeError,
549 "can't convert negative long to unsigned");
550 return -1;
551 }
552 do_twos_comp = 1;
553 }
554 else {
555 ndigits = v->ob_size;
556 do_twos_comp = 0;
557 }
558
559 if (little_endian) {
560 p = bytes;
561 pincr = 1;
562 }
563 else {
564 p = bytes + n - 1;
565 pincr = -1;
566 }
567
Tim Peters898cf852001-06-13 20:50:08 +0000568 /* Copy over all the Python digits.
569 It's crucial that every Python digit except for the MSD contribute
570 exactly SHIFT bits to the total, so first assert that the long is
571 normalized. */
572 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000573 j = 0;
574 accum = 0;
575 accumbits = 0;
576 carry = do_twos_comp ? 1 : 0;
577 for (i = 0; i < ndigits; ++i) {
578 twodigits thisdigit = v->ob_digit[i];
579 if (do_twos_comp) {
580 thisdigit = (thisdigit ^ MASK) + carry;
581 carry = thisdigit >> SHIFT;
582 thisdigit &= MASK;
583 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000584 /* Because we're going LSB to MSB, thisdigit is more
585 significant than what's already in accum, so needs to be
586 prepended to accum. */
587 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000588 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000589
Tim Petersede05092001-06-14 08:53:38 +0000590 /* The most-significant digit may be (probably is) at least
591 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000592 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000593 /* Count # of sign bits -- they needn't be stored,
594 * although for signed conversion we need later to
595 * make sure at least one sign bit gets stored.
596 * First shift conceptual sign bit to real sign bit.
597 */
598 stwodigits s = (stwodigits)(thisdigit <<
599 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000600 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000601 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000602 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000603 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000604 }
Tim Petersede05092001-06-14 08:53:38 +0000605 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000606 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000607
Tim Peters2a9b3672001-06-11 21:23:58 +0000608 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000609 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000610 if (j >= n)
611 goto Overflow;
612 ++j;
613 *p = (unsigned char)(accum & 0xff);
614 p += pincr;
615 accumbits -= 8;
616 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000617 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000618 }
619
620 /* Store the straggler (if any). */
621 assert(accumbits < 8);
622 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000623 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000624 if (j >= n)
625 goto Overflow;
626 ++j;
627 if (do_twos_comp) {
628 /* Fill leading bits of the byte with sign bits
629 (appropriately pretending that the long had an
630 infinite supply of sign bits). */
631 accum |= (~(twodigits)0) << accumbits;
632 }
633 *p = (unsigned char)(accum & 0xff);
634 p += pincr;
635 }
Tim Peters05607ad2001-06-13 21:01:27 +0000636 else if (j == n && n > 0 && is_signed) {
637 /* The main loop filled the byte array exactly, so the code
638 just above didn't get to ensure there's a sign bit, and the
639 loop below wouldn't add one either. Make sure a sign bit
640 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000641 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000642 int sign_bit_set = msb >= 0x80;
643 assert(accumbits == 0);
644 if (sign_bit_set == do_twos_comp)
645 return 0;
646 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000647 goto Overflow;
648 }
Tim Peters05607ad2001-06-13 21:01:27 +0000649
650 /* Fill remaining bytes with copies of the sign bit. */
651 {
652 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
653 for ( ; j < n; ++j, p += pincr)
654 *p = signbyte;
655 }
656
Tim Peters2a9b3672001-06-11 21:23:58 +0000657 return 0;
658
659Overflow:
660 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
661 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000662
Tim Peters2a9b3672001-06-11 21:23:58 +0000663}
664
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000665double
666_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
667{
668/* NBITS_WANTED should be > the number of bits in a double's precision,
669 but small enough so that 2**NBITS_WANTED is within the normal double
670 range. nbitsneeded is set to 1 less than that because the most-significant
671 Python digit contains at least 1 significant bit, but we don't want to
672 bother counting them (catering to the worst case cheaply).
673
674 57 is one more than VAX-D double precision; I (Tim) don't know of a double
675 format with more precision than that; it's 1 larger so that we add in at
676 least one round bit to stand in for the ignored least-significant bits.
677*/
678#define NBITS_WANTED 57
679 PyLongObject *v;
680 double x;
681 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000682 Py_ssize_t i;
683 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000684 int nbitsneeded;
685
686 if (vv == NULL || !PyLong_Check(vv)) {
687 PyErr_BadInternalCall();
688 return -1;
689 }
690 v = (PyLongObject *)vv;
691 i = v->ob_size;
692 sign = 1;
693 if (i < 0) {
694 sign = -1;
695 i = -(i);
696 }
697 else if (i == 0) {
698 *exponent = 0;
699 return 0.0;
700 }
701 --i;
702 x = (double)v->ob_digit[i];
703 nbitsneeded = NBITS_WANTED - 1;
704 /* Invariant: i Python digits remain unaccounted for. */
705 while (i > 0 && nbitsneeded > 0) {
706 --i;
707 x = x * multiplier + (double)v->ob_digit[i];
708 nbitsneeded -= SHIFT;
709 }
710 /* There are i digits we didn't shift in. Pretending they're all
711 zeroes, the true value is x * 2**(i*SHIFT). */
712 *exponent = i;
713 assert(x > 0.0);
714 return x * sign;
715#undef NBITS_WANTED
716}
717
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000718/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000719
720double
Tim Peters9f688bf2000-07-07 15:53:28 +0000721PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000722{
Thomas Wouters553489a2006-02-01 21:32:04 +0000723 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000724 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000725
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 if (vv == NULL || !PyLong_Check(vv)) {
727 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000728 return -1;
729 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000730 x = _PyLong_AsScaledDouble(vv, &e);
731 if (x == -1.0 && PyErr_Occurred())
732 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000733 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
734 set correctly after a successful _PyLong_AsScaledDouble() call */
735 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000736 if (e > INT_MAX / SHIFT)
737 goto overflow;
738 errno = 0;
739 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000740 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000741 goto overflow;
742 return x;
743
744overflow:
745 PyErr_SetString(PyExc_OverflowError,
746 "long int too large to convert to float");
747 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000748}
749
Guido van Rossum78694d91998-09-18 14:14:13 +0000750/* Create a new long (or int) object from a C pointer */
751
752PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000753PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000754{
Tim Peters70128a12001-06-16 08:48:40 +0000755#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000756 if ((long)p < 0)
757 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000758 return PyInt_FromLong((long)p);
759#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000760
Tim Peters70128a12001-06-16 08:48:40 +0000761#ifndef HAVE_LONG_LONG
762# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
763#endif
764#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000765# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000766#endif
767 /* optimize null pointers */
768 if (p == NULL)
769 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000770 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000771
772#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000773}
774
775/* Get a C pointer from a long object (or an int object in some cases) */
776
777void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000778PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000779{
780 /* This function will allow int or long objects. If vv is neither,
781 then the PyLong_AsLong*() functions will raise the exception:
782 PyExc_SystemError, "bad argument to internal function"
783 */
Tim Peters70128a12001-06-16 08:48:40 +0000784#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000785 long x;
786
Tim Peters70128a12001-06-16 08:48:40 +0000787 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000788 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000789 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000790 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000791 else
792 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000793#else
Tim Peters70128a12001-06-16 08:48:40 +0000794
795#ifndef HAVE_LONG_LONG
796# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
797#endif
798#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000799# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000800#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000801 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000802
Tim Peters70128a12001-06-16 08:48:40 +0000803 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000804 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000805 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000806 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000807 else
808 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000809
810#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000811
812 if (x == -1 && PyErr_Occurred())
813 return NULL;
814 return (void *)x;
815}
816
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000817#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000818
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000819/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000820 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000821 */
822
Tim Peterscf37dfc2001-06-14 18:42:50 +0000823#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000824
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000825/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000826
827PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000829{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000830 PyLongObject *v;
831 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
832 int ndigits = 0;
833 int negative = 0;
834
835 if (ival < 0) {
836 ival = -ival;
837 negative = 1;
838 }
839
840 /* Count the number of Python digits.
841 We used to pick 5 ("big enough for anything"), but that's a
842 waste of time and space given that 5*15 = 75 bits are rarely
843 needed. */
844 t = (unsigned PY_LONG_LONG)ival;
845 while (t) {
846 ++ndigits;
847 t >>= SHIFT;
848 }
849 v = _PyLong_New(ndigits);
850 if (v != NULL) {
851 digit *p = v->ob_digit;
852 v->ob_size = negative ? -ndigits : ndigits;
853 t = (unsigned PY_LONG_LONG)ival;
854 while (t) {
855 *p++ = (digit)(t & MASK);
856 t >>= SHIFT;
857 }
858 }
859 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000860}
861
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000862/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000863
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000864PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000865PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000866{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000867 PyLongObject *v;
868 unsigned PY_LONG_LONG t;
869 int ndigits = 0;
870
871 /* Count the number of Python digits. */
872 t = (unsigned PY_LONG_LONG)ival;
873 while (t) {
874 ++ndigits;
875 t >>= SHIFT;
876 }
877 v = _PyLong_New(ndigits);
878 if (v != NULL) {
879 digit *p = v->ob_digit;
880 v->ob_size = ndigits;
881 while (ival) {
882 *p++ = (digit)(ival & MASK);
883 ival >>= SHIFT;
884 }
885 }
886 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000887}
888
Martin v. Löwis18e16552006-02-15 17:27:45 +0000889/* Create a new long int object from a C Py_ssize_t. */
890
891PyObject *
892_PyLong_FromSsize_t(Py_ssize_t ival)
893{
894 Py_ssize_t bytes = ival;
895 int one = 1;
896 return _PyLong_FromByteArray(
897 (unsigned char *)&bytes,
Kristján Valur Jónssonf4601d82007-05-07 18:30:48 +0000898 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899}
900
901/* Create a new long int object from a C size_t. */
902
903PyObject *
904_PyLong_FromSize_t(size_t ival)
905{
906 size_t bytes = ival;
907 int one = 1;
908 return _PyLong_FromByteArray(
909 (unsigned char *)&bytes,
910 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
911}
912
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000913/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000914 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000915
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000916PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000917PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000918{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000919 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000920 int one = 1;
921 int res;
922
Tim Petersd38b1c72001-09-30 05:09:37 +0000923 if (vv == NULL) {
924 PyErr_BadInternalCall();
925 return -1;
926 }
927 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000928 PyNumberMethods *nb;
929 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000930 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000931 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000932 if ((nb = vv->ob_type->tp_as_number) == NULL ||
933 nb->nb_int == NULL) {
934 PyErr_SetString(PyExc_TypeError, "an integer is required");
935 return -1;
936 }
937 io = (*nb->nb_int) (vv);
938 if (io == NULL)
939 return -1;
940 if (PyInt_Check(io)) {
941 bytes = PyInt_AsLong(io);
942 Py_DECREF(io);
943 return bytes;
944 }
945 if (PyLong_Check(io)) {
946 bytes = PyLong_AsLongLong(io);
947 Py_DECREF(io);
948 return bytes;
949 }
950 Py_DECREF(io);
951 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000952 return -1;
953 }
954
Tim Petersd1a7da62001-06-13 00:35:57 +0000955 res = _PyLong_AsByteArray(
956 (PyLongObject *)vv, (unsigned char *)&bytes,
957 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000958
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000959 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000960 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000961 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000962 else
963 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000964}
965
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000966/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000967 Return -1 and set an error if overflow occurs. */
968
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000969unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000970PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000971{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000972 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000973 int one = 1;
974 int res;
975
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000976 if (vv == NULL || !PyLong_Check(vv)) {
977 PyErr_BadInternalCall();
Skip Montanaro429433b2006-04-18 00:35:43 +0000978 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000979 }
980
Tim Petersd1a7da62001-06-13 00:35:57 +0000981 res = _PyLong_AsByteArray(
982 (PyLongObject *)vv, (unsigned char *)&bytes,
983 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000984
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000985 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000986 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000987 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000988 else
989 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000990}
Tim Petersd1a7da62001-06-13 00:35:57 +0000991
Thomas Hellera4ea6032003-04-17 18:55:45 +0000992/* Get a C unsigned long int from a long int object, ignoring the high bits.
993 Returns -1 and sets an error condition if an error occurs. */
994
995unsigned PY_LONG_LONG
996PyLong_AsUnsignedLongLongMask(PyObject *vv)
997{
998 register PyLongObject *v;
999 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001000 Py_ssize_t i;
1001 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001002
1003 if (vv == NULL || !PyLong_Check(vv)) {
1004 PyErr_BadInternalCall();
1005 return (unsigned long) -1;
1006 }
1007 v = (PyLongObject *)vv;
1008 i = v->ob_size;
1009 sign = 1;
1010 x = 0;
1011 if (i < 0) {
1012 sign = -1;
1013 i = -i;
1014 }
1015 while (--i >= 0) {
1016 x = (x << SHIFT) + v->ob_digit[i];
1017 }
1018 return x * sign;
1019}
Tim Petersd1a7da62001-06-13 00:35:57 +00001020#undef IS_LITTLE_ENDIAN
1021
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001022#endif /* HAVE_LONG_LONG */
1023
Neil Schemenauerba872e22001-01-04 01:46:03 +00001024
1025static int
1026convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001027 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001028 *a = (PyLongObject *) v;
1029 Py_INCREF(v);
1030 }
1031 else if (PyInt_Check(v)) {
1032 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1033 }
1034 else {
1035 return 0;
1036 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001037 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001038 *b = (PyLongObject *) w;
1039 Py_INCREF(w);
1040 }
1041 else if (PyInt_Check(w)) {
1042 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1043 }
1044 else {
1045 Py_DECREF(*a);
1046 return 0;
1047 }
1048 return 1;
1049}
1050
1051#define CONVERT_BINOP(v, w, a, b) \
1052 if (!convert_binop(v, w, a, b)) { \
1053 Py_INCREF(Py_NotImplemented); \
1054 return Py_NotImplemented; \
1055 }
1056
Tim Peters877a2122002-08-12 05:09:36 +00001057/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1058 * is modified in place, by adding y to it. Carries are propagated as far as
1059 * x[m-1], and the remaining carry (0 or 1) is returned.
1060 */
1061static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001062v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001063{
1064 int i;
1065 digit carry = 0;
1066
1067 assert(m >= n);
1068 for (i = 0; i < n; ++i) {
1069 carry += x[i] + y[i];
1070 x[i] = carry & MASK;
1071 carry >>= SHIFT;
1072 assert((carry & 1) == carry);
1073 }
1074 for (; carry && i < m; ++i) {
1075 carry += x[i];
1076 x[i] = carry & MASK;
1077 carry >>= SHIFT;
1078 assert((carry & 1) == carry);
1079 }
1080 return carry;
1081}
1082
1083/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1084 * is modified in place, by subtracting y from it. Borrows are propagated as
1085 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1086 */
1087static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001088v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001089{
1090 int i;
1091 digit borrow = 0;
1092
1093 assert(m >= n);
1094 for (i = 0; i < n; ++i) {
1095 borrow = x[i] - y[i] - borrow;
1096 x[i] = borrow & MASK;
1097 borrow >>= SHIFT;
1098 borrow &= 1; /* keep only 1 sign bit */
1099 }
1100 for (; borrow && i < m; ++i) {
1101 borrow = x[i] - borrow;
1102 x[i] = borrow & MASK;
1103 borrow >>= SHIFT;
1104 borrow &= 1;
1105 }
1106 return borrow;
1107}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001108
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001109/* Multiply by a single digit, ignoring the sign. */
1110
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001112mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001113{
1114 return muladd1(a, n, (digit)0);
1115}
1116
1117/* Multiply by a single digit and add a single digit, ignoring the sign. */
1118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001119static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001120muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001121{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001122 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001124 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001125 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001126
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001127 if (z == NULL)
1128 return NULL;
1129 for (i = 0; i < size_a; ++i) {
1130 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001131 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001132 carry >>= SHIFT;
1133 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001134 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001135 return long_normalize(z);
1136}
1137
Tim Peters212e6142001-07-14 12:23:19 +00001138/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1139 in pout, and returning the remainder. pin and pout point at the LSD.
1140 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1141 long_format, but that should be done with great care since longs are
1142 immutable. */
1143
1144static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001145inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001146{
1147 twodigits rem = 0;
1148
1149 assert(n > 0 && n <= MASK);
1150 pin += size;
1151 pout += size;
1152 while (--size >= 0) {
1153 digit hi;
1154 rem = (rem << SHIFT) + *--pin;
1155 *--pout = hi = (digit)(rem / n);
1156 rem -= hi * n;
1157 }
1158 return (digit)rem;
1159}
1160
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001161/* Divide a long integer by a digit, returning both the quotient
1162 (as function result) and the remainder (through *prem).
1163 The sign of a is ignored; n should not be zero. */
1164
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001166divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001167{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001169 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001170
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001171 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001173 if (z == NULL)
1174 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001175 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001176 return long_normalize(z);
1177}
1178
1179/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001180 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001181 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001182
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001183static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001184long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001185{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001186 register PyLongObject *a = (PyLongObject *)aa;
1187 PyStringObject *str;
Armin Rigo4b63c212006-10-04 11:44:06 +00001188 Py_ssize_t i, j, sz;
Neal Norwitzc09efa82006-07-23 07:53:14 +00001189 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001190 char *p;
1191 int bits;
1192 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001193
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 if (a == NULL || !PyLong_Check(a)) {
1195 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001196 return NULL;
1197 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001198 assert(base >= 2 && base <= 36);
Neal Norwitzc09efa82006-07-23 07:53:14 +00001199 size_a = ABS(a->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001200
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001201 /* Compute a rough upper bound for the length of the string */
1202 i = base;
1203 bits = 0;
1204 while (i > 1) {
1205 ++bits;
1206 i >>= 1;
1207 }
Armin Rigo4b63c212006-10-04 11:44:06 +00001208 i = 5 + (addL ? 1 : 0);
1209 j = size_a*SHIFT + bits-1;
1210 sz = i + j / bits;
1211 if (j / SHIFT < size_a || sz < i) {
1212 PyErr_SetString(PyExc_OverflowError,
1213 "long is too large to format");
1214 return NULL;
1215 }
1216 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001217 if (str == NULL)
1218 return NULL;
Armin Rigo4b63c212006-10-04 11:44:06 +00001219 p = PyString_AS_STRING(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001220 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001221 if (addL)
1222 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001223 if (a->ob_size < 0)
1224 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001225
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001226 if (a->ob_size == 0) {
1227 *--p = '0';
1228 }
1229 else if ((base & (base - 1)) == 0) {
1230 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001231 twodigits accum = 0;
1232 int accumbits = 0; /* # of bits in accum */
1233 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001234 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001235 while ((i >>= 1) > 1)
1236 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001237
1238 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001239 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001240 accumbits += SHIFT;
1241 assert(accumbits >= basebits);
1242 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001243 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001244 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001245 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001246 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001247 accumbits -= basebits;
1248 accum >>= basebits;
1249 } while (i < size_a-1 ? accumbits >= basebits :
1250 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001251 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001252 }
1253 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001254 /* Not 0, and base not a power of 2. Divide repeatedly by
1255 base, but for speed use the highest power of base that
1256 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001257 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001258 digit *pin = a->ob_digit;
1259 PyLongObject *scratch;
1260 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001261 digit powbase = base; /* powbase == base ** power */
1262 int power = 1;
1263 for (;;) {
1264 unsigned long newpow = powbase * (unsigned long)base;
1265 if (newpow >> SHIFT) /* doesn't fit in a digit */
1266 break;
1267 powbase = (digit)newpow;
1268 ++power;
1269 }
Tim Peters212e6142001-07-14 12:23:19 +00001270
1271 /* Get a scratch area for repeated division. */
1272 scratch = _PyLong_New(size);
1273 if (scratch == NULL) {
1274 Py_DECREF(str);
1275 return NULL;
1276 }
1277
1278 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001279 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001280 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001281 digit rem = inplace_divrem1(scratch->ob_digit,
1282 pin, size, powbase);
1283 pin = scratch->ob_digit; /* no need to use a again */
1284 if (pin[size - 1] == 0)
1285 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001286 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001287 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001288 Py_DECREF(str);
1289 return NULL;
1290 })
Tim Peters212e6142001-07-14 12:23:19 +00001291
1292 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001293 assert(ntostore > 0);
1294 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001295 digit nextrem = (digit)(rem / base);
1296 char c = (char)(rem - nextrem * base);
1297 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001298 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001299 *--p = c;
1300 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001301 --ntostore;
1302 /* Termination is a bit delicate: must not
1303 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001304 remaining quotient and rem are both 0. */
1305 } while (ntostore && (size || rem));
1306 } while (size != 0);
1307 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001308 }
1309
Guido van Rossum2c475421992-08-14 15:13:07 +00001310 if (base == 8) {
1311 if (size_a != 0)
1312 *--p = '0';
1313 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001314 else if (base == 16) {
1315 *--p = 'x';
1316 *--p = '0';
1317 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001318 else if (base != 10) {
1319 *--p = '#';
1320 *--p = '0' + base%10;
1321 if (base > 10)
1322 *--p = '0' + base/10;
1323 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001324 if (sign)
1325 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 if (p != PyString_AS_STRING(str)) {
1327 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001328 assert(p > q);
1329 do {
1330 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001331 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001332 _PyString_Resize((PyObject **)&str,
Armin Rigo4b63c212006-10-04 11:44:06 +00001333 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001334 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001336}
1337
Tim Petersda53afa2006-05-25 17:34:03 +00001338/* Table of digit values for 8-bit string -> integer conversion.
1339 * '0' maps to 0, ..., '9' maps to 9.
1340 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1341 * All other indices map to 37.
1342 * Note that when converting a base B string, a char c is a legitimate
1343 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1344 */
1345int _PyLong_DigitValue[256] = {
Tim Peters696cf432006-05-24 21:10:40 +00001346 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1347 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1348 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1349 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1350 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1351 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1352 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1353 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1354 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1355 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1356 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1357 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1358 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1359 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1360 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1361 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Tim Peters696cf432006-05-24 21:10:40 +00001362};
1363
1364/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001365 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1366 * non-digit (which may be *str!). A normalized long is returned.
1367 * The point to this routine is that it takes time linear in the number of
1368 * string characters.
1369 */
1370static PyLongObject *
1371long_from_binary_base(char **str, int base)
1372{
1373 char *p = *str;
1374 char *start = p;
1375 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001376 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001377 PyLongObject *z;
1378 twodigits accum;
1379 int bits_in_accum;
1380 digit *pdigit;
1381
1382 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1383 n = base;
1384 for (bits_per_char = -1; n; ++bits_per_char)
1385 n >>= 1;
1386 /* n <- total # of bits needed, while setting p to end-of-string */
1387 n = 0;
Tim Petersda53afa2006-05-25 17:34:03 +00001388 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001389 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001390 *str = p;
Armin Rigo4b63c212006-10-04 11:44:06 +00001391 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1392 n = (p - start) * bits_per_char + SHIFT - 1;
1393 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001394 PyErr_SetString(PyExc_ValueError,
1395 "long string too large to convert");
1396 return NULL;
1397 }
Armin Rigo4b63c212006-10-04 11:44:06 +00001398 n = n / SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001399 z = _PyLong_New(n);
1400 if (z == NULL)
1401 return NULL;
1402 /* Read string from right, and fill in long from left; i.e.,
1403 * from least to most significant in both.
1404 */
1405 accum = 0;
1406 bits_in_accum = 0;
1407 pdigit = z->ob_digit;
1408 while (--p >= start) {
Tim Petersda53afa2006-05-25 17:34:03 +00001409 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001410 assert(k >= 0 && k < base);
1411 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001412 bits_in_accum += bits_per_char;
1413 if (bits_in_accum >= SHIFT) {
1414 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001415 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001416 accum >>= SHIFT;
1417 bits_in_accum -= SHIFT;
1418 assert(bits_in_accum < SHIFT);
1419 }
1420 }
1421 if (bits_in_accum) {
1422 assert(bits_in_accum <= SHIFT);
1423 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001424 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001425 }
1426 while (pdigit - z->ob_digit < n)
1427 *pdigit++ = 0;
1428 return long_normalize(z);
1429}
1430
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001431PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001432PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001433{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001434 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001435 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436 PyLongObject *z;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001437 PyObject *strobj, *strrepr;
1438 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001439
Guido van Rossum472c04f1996-12-05 21:57:21 +00001440 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001441 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001442 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001443 return NULL;
1444 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001445 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001446 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001447 if (*str == '+')
1448 ++str;
1449 else if (*str == '-') {
1450 ++str;
1451 sign = -1;
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 (base == 0) {
1456 if (str[0] != '0')
1457 base = 10;
1458 else if (str[1] == 'x' || str[1] == 'X')
1459 base = 16;
1460 else
1461 base = 8;
1462 }
1463 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1464 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001465
Guido van Rossume6762971998-06-22 03:54:46 +00001466 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001467 if ((base & (base - 1)) == 0)
1468 z = long_from_binary_base(&str, base);
1469 else {
Tim Peters696cf432006-05-24 21:10:40 +00001470/***
1471Binary bases can be converted in time linear in the number of digits, because
1472Python's representation base is binary. Other bases (including decimal!) use
1473the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001474
Tim Peters696cf432006-05-24 21:10:40 +00001475First some math: the largest integer that can be expressed in N base-B digits
1476is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1477case number of Python digits needed to hold it is the smallest integer n s.t.
1478
1479 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1480 BASE**n >= B**N [taking logs to base BASE]
1481 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1482
1483The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1484this quickly. A Python long with that much space is reserved near the start,
1485and the result is computed into it.
1486
1487The input string is actually treated as being in base base**i (i.e., i digits
1488are processed at a time), where two more static arrays hold:
1489
1490 convwidth_base[base] = the largest integer i such that base**i <= BASE
1491 convmultmax_base[base] = base ** convwidth_base[base]
1492
1493The first of these is the largest i such that i consecutive input digits
1494must fit in a single Python digit. The second is effectively the input
1495base we're really using.
1496
1497Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1498convmultmax_base[base], the result is "simply"
1499
1500 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1501
1502where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001503
1504Error analysis: as above, the number of Python digits `n` needed is worst-
1505case
1506
1507 n >= N * log(B)/log(BASE)
1508
1509where `N` is the number of input digits in base `B`. This is computed via
1510
1511 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1512
1513below. Two numeric concerns are how much space this can waste, and whether
1514the computed result can be too small. To be concrete, assume BASE = 2**15,
1515which is the default (and it's unlikely anyone changes that).
1516
1517Waste isn't a problem: provided the first input digit isn't 0, the difference
1518between the worst-case input with N digits and the smallest input with N
1519digits is about a factor of B, but B is small compared to BASE so at most
1520one allocated Python digit can remain unused on that count. If
1521N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1522and adding 1 returns a result 1 larger than necessary. However, that can't
1523happen: whenever B is a power of 2, long_from_binary_base() is called
1524instead, and it's impossible for B**i to be an integer power of 2**15 when
1525B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1526an exact integer when B is not a power of 2, since B**i has a prime factor
1527other than 2 in that case, but (2**15)**j's only prime factor is 2).
1528
1529The computed result can be too small if the true value of N*log(B)/log(BASE)
1530is a little bit larger than an exact integer, but due to roundoff errors (in
1531computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1532yields a numeric result a little less than that integer. Unfortunately, "how
1533close can a transcendental function get to an integer over some range?"
1534questions are generally theoretically intractable. Computer analysis via
1535continued fractions is practical: expand log(B)/log(BASE) via continued
1536fractions, giving a sequence i/j of "the best" rational approximations. Then
1537j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1538we can get very close to being in trouble, but very rarely. For example,
153976573 is a denominator in one of the continued-fraction approximations to
1540log(10)/log(2**15), and indeed:
1541
1542 >>> log(10)/log(2**15)*76573
1543 16958.000000654003
1544
1545is very close to an integer. If we were working with IEEE single-precision,
1546rounding errors could kill us. Finding worst cases in IEEE double-precision
1547requires better-than-double-precision log() functions, and Tim didn't bother.
1548Instead the code checks to see whether the allocated space is enough as each
1549new Python digit is added, and copies the whole thing to a larger long if not.
1550This should happen extremely rarely, and in fact I don't have a test case
1551that triggers it(!). Instead the code was tested by artificially allocating
1552just 1 digit at the start, so that the copying code was exercised for every
1553digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001554***/
1555 register twodigits c; /* current input character */
1556 Py_ssize_t size_z;
1557 int i;
1558 int convwidth;
1559 twodigits convmultmax, convmult;
1560 digit *pz, *pzstop;
1561 char* scan;
1562
1563 static double log_base_BASE[37] = {0.0e0,};
1564 static int convwidth_base[37] = {0,};
1565 static twodigits convmultmax_base[37] = {0,};
1566
1567 if (log_base_BASE[base] == 0.0) {
1568 twodigits convmax = base;
1569 int i = 1;
1570
1571 log_base_BASE[base] = log((double)base) /
1572 log((double)BASE);
1573 for (;;) {
1574 twodigits next = convmax * base;
1575 if (next > BASE)
1576 break;
1577 convmax = next;
1578 ++i;
1579 }
1580 convmultmax_base[base] = convmax;
1581 assert(i > 0);
1582 convwidth_base[base] = i;
1583 }
1584
1585 /* Find length of the string of numeric characters. */
1586 scan = str;
Tim Petersda53afa2006-05-25 17:34:03 +00001587 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001588 ++scan;
1589
1590 /* Create a long object that can contain the largest possible
1591 * integer with this base and length. Note that there's no
1592 * need to initialize z->ob_digit -- no slot is read up before
1593 * being stored into.
1594 */
1595 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001596 /* Uncomment next line to test exceedingly rare copy code */
1597 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001598 assert(size_z > 0);
1599 z = _PyLong_New(size_z);
1600 if (z == NULL)
1601 return NULL;
1602 z->ob_size = 0;
1603
1604 /* `convwidth` consecutive input digits are treated as a single
1605 * digit in base `convmultmax`.
1606 */
1607 convwidth = convwidth_base[base];
1608 convmultmax = convmultmax_base[base];
1609
1610 /* Work ;-) */
1611 while (str < scan) {
1612 /* grab up to convwidth digits from the input string */
Tim Petersda53afa2006-05-25 17:34:03 +00001613 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00001614 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1615 c = (twodigits)(c * base +
Tim Petersda53afa2006-05-25 17:34:03 +00001616 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Tim Peters696cf432006-05-24 21:10:40 +00001617 assert(c < BASE);
1618 }
1619
1620 convmult = convmultmax;
1621 /* Calculate the shift only if we couldn't get
1622 * convwidth digits.
1623 */
1624 if (i != convwidth) {
1625 convmult = base;
1626 for ( ; i > 1; --i)
1627 convmult *= base;
1628 }
1629
1630 /* Multiply z by convmult, and add c. */
1631 pz = z->ob_digit;
1632 pzstop = pz + z->ob_size;
1633 for (; pz < pzstop; ++pz) {
1634 c += (twodigits)*pz * convmult;
1635 *pz = (digit)(c & MASK);
1636 c >>= SHIFT;
1637 }
1638 /* carry off the current end? */
1639 if (c) {
1640 assert(c < BASE);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001641 if (z->ob_size < size_z) {
1642 *pz = (digit)c;
1643 ++z->ob_size;
1644 }
1645 else {
1646 PyLongObject *tmp;
1647 /* Extremely rare. Get more space. */
1648 assert(z->ob_size == size_z);
1649 tmp = _PyLong_New(size_z + 1);
1650 if (tmp == NULL) {
1651 Py_DECREF(z);
1652 return NULL;
1653 }
1654 memcpy(tmp->ob_digit,
1655 z->ob_digit,
1656 sizeof(digit) * size_z);
1657 Py_DECREF(z);
1658 z = tmp;
1659 z->ob_digit[size_z] = (digit)c;
1660 ++size_z;
1661 }
Tim Peters696cf432006-05-24 21:10:40 +00001662 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001663 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001664 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001665 if (z == NULL)
1666 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001667 if (str == start)
1668 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00001669 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001670 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001671 if (*str == 'L' || *str == 'l')
1672 str++;
1673 while (*str && isspace(Py_CHARMASK(*str)))
1674 str++;
1675 if (*str != '\0')
1676 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001677 if (pend)
1678 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001680
1681 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001682 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001683 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1684 strobj = PyString_FromStringAndSize(orig_str, slen);
1685 if (strobj == NULL)
1686 return NULL;
1687 strrepr = PyObject_Repr(strobj);
1688 Py_DECREF(strobj);
1689 if (strrepr == NULL)
1690 return NULL;
1691 PyErr_Format(PyExc_ValueError,
1692 "invalid literal for long() with base %d: %s",
1693 base, PyString_AS_STRING(strrepr));
1694 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001695 return NULL;
1696}
1697
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001698#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001699PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001700PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001701{
Walter Dörwald07e14762002-11-06 16:15:14 +00001702 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001703 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001704
Walter Dörwald07e14762002-11-06 16:15:14 +00001705 if (buffer == NULL)
1706 return NULL;
1707
1708 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1709 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001710 return NULL;
1711 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001712 result = PyLong_FromString(buffer, NULL, base);
1713 PyMem_FREE(buffer);
1714 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001715}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001716#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001717
Tim Peters9f688bf2000-07-07 15:53:28 +00001718/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001719static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001720 (PyLongObject *, PyLongObject *, PyLongObject **);
1721static PyObject *long_pos(PyLongObject *);
1722static int long_divrem(PyLongObject *, PyLongObject *,
1723 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001724
1725/* Long division with remainder, top-level routine */
1726
Guido van Rossume32e0141992-01-19 16:31:05 +00001727static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001728long_divrem(PyLongObject *a, PyLongObject *b,
1729 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001730{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001731 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001732 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001733
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001734 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001735 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001736 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001737 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001738 }
1739 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001740 (size_a == size_b &&
1741 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001743 *pdiv = _PyLong_New(0);
Georg Brandl1dfa8ac2007-04-21 07:22:57 +00001744 if (*pdiv == NULL)
1745 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001746 Py_INCREF(a);
1747 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001748 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001749 }
1750 if (size_b == 1) {
1751 digit rem = 0;
1752 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001753 if (z == NULL)
1754 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandl1dfa8ac2007-04-21 07:22:57 +00001756 if (*prem == NULL) {
1757 Py_DECREF(z);
1758 return -1;
1759 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001760 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001761 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001762 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001763 if (z == NULL)
1764 return -1;
1765 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001766 /* Set the signs.
1767 The quotient z has the sign of a*b;
1768 the remainder r has the sign of a,
1769 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001770 if ((a->ob_size < 0) != (b->ob_size < 0))
1771 z->ob_size = -(z->ob_size);
1772 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1773 (*prem)->ob_size = -((*prem)->ob_size);
1774 *pdiv = z;
1775 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001776}
1777
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001778/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001779
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001780static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001781x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001782{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001783 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001784 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001785 PyLongObject *v = mul1(v1, d);
1786 PyLongObject *w = mul1(w1, d);
1787 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001788 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001789
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001790 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001791 Py_XDECREF(v);
1792 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001793 return NULL;
1794 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001795
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001796 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001797 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001798 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001799
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001800 size_v = ABS(v->ob_size);
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001801 k = size_v - size_w;
1802 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001803
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001804 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001805 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1806 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001807 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001808 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001809
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001810 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001811 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001812 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001813 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001814 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 if (vj == w->ob_digit[size_w-1])
1816 q = MASK;
1817 else
1818 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1819 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001820
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001821 while (w->ob_digit[size_w-2]*q >
1822 ((
1823 ((twodigits)vj << SHIFT)
1824 + v->ob_digit[j-1]
1825 - q*w->ob_digit[size_w-1]
1826 ) << SHIFT)
1827 + v->ob_digit[j-2])
1828 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001829
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001830 for (i = 0; i < size_w && i+k < size_v; ++i) {
1831 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001832 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001833 carry += v->ob_digit[i+k] - z
1834 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001835 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001836 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1837 carry, SHIFT);
1838 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001839 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001840
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001841 if (i+k < size_v) {
1842 carry += v->ob_digit[i+k];
1843 v->ob_digit[i+k] = 0;
1844 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001845
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001846 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001847 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001848 else {
1849 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001850 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001851 carry = 0;
1852 for (i = 0; i < size_w && i+k < size_v; ++i) {
1853 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001854 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001855 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1856 BASE_TWODIGITS_TYPE,
1857 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001858 }
1859 }
1860 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001861
Guido van Rossumc206c761995-01-10 15:23:19 +00001862 if (a == NULL)
1863 *prem = NULL;
1864 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001865 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001866 *prem = divrem1(v, d, &d);
1867 /* d receives the (unused) remainder */
1868 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001869 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001870 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001871 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001872 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001873 Py_DECREF(v);
1874 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001875 return a;
1876}
1877
1878/* Methods */
1879
1880static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001881long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001882{
Guido van Rossum9475a232001-10-05 20:51:39 +00001883 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001884}
1885
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001886static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001887long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001888{
Fred Drake121ee271999-12-23 15:41:28 +00001889 return long_format(v, 10, 1);
1890}
1891
1892static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001893long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001894{
1895 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001896}
1897
1898static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001899long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001900{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001901 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001902
Guido van Rossumc6913e71991-11-19 20:26:46 +00001903 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001904 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001905 sign = 0;
1906 else
1907 sign = a->ob_size - b->ob_size;
1908 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001909 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001910 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001911 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1912 ;
1913 if (i < 0)
1914 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001915 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001916 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001917 if (a->ob_size < 0)
1918 sign = -sign;
1919 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001920 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001921 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001922}
1923
Guido van Rossum9bfef441993-03-29 10:43:31 +00001924static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001925long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001926{
1927 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001928 Py_ssize_t i;
1929 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001930
1931 /* This is designed so that Python ints and longs with the
1932 same value hash to the same value, otherwise comparisons
1933 of mapping keys will turn out weird */
1934 i = v->ob_size;
1935 sign = 1;
1936 x = 0;
1937 if (i < 0) {
1938 sign = -1;
1939 i = -(i);
1940 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001941#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001942 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001943 /* Force a native long #-bits (32 or 64) circular shift */
1944 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001945 x += v->ob_digit[i];
1946 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001947#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001948 x = x * sign;
1949 if (x == -1)
1950 x = -2;
1951 return x;
1952}
1953
1954
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001955/* Add the absolute values of two long integers. */
1956
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001957static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001958x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001959{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001960 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001961 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001962 int i;
1963 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001964
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001965 /* Ensure a is the larger of the two: */
1966 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001967 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001968 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001969 size_a = size_b;
1970 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001971 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001972 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001973 if (z == NULL)
1974 return NULL;
1975 for (i = 0; i < size_b; ++i) {
1976 carry += a->ob_digit[i] + b->ob_digit[i];
1977 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001978 carry >>= SHIFT;
1979 }
1980 for (; i < size_a; ++i) {
1981 carry += a->ob_digit[i];
1982 z->ob_digit[i] = carry & MASK;
1983 carry >>= SHIFT;
1984 }
1985 z->ob_digit[i] = carry;
1986 return long_normalize(z);
1987}
1988
1989/* Subtract the absolute values of two integers. */
1990
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001992x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001993{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001994 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001995 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001996 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001997 int sign = 1;
1998 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001999
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002000 /* Ensure a is the larger of the two: */
2001 if (size_a < size_b) {
2002 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002003 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002004 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005 size_a = size_b;
2006 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002007 }
2008 else if (size_a == size_b) {
2009 /* Find highest digit where a and b differ: */
2010 i = size_a;
2011 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2012 ;
2013 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002014 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002015 if (a->ob_digit[i] < b->ob_digit[i]) {
2016 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002018 }
2019 size_a = size_b = i+1;
2020 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002021 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002022 if (z == NULL)
2023 return NULL;
2024 for (i = 0; i < size_b; ++i) {
2025 /* The following assumes unsigned arithmetic
2026 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002027 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002028 z->ob_digit[i] = borrow & MASK;
2029 borrow >>= SHIFT;
2030 borrow &= 1; /* Keep only one sign bit */
2031 }
2032 for (; i < size_a; ++i) {
2033 borrow = a->ob_digit[i] - borrow;
2034 z->ob_digit[i] = borrow & MASK;
2035 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002036 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002037 }
2038 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002039 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002040 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002041 return long_normalize(z);
2042}
2043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002044static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002045long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002046{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002047 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002048
Neil Schemenauerba872e22001-01-04 01:46:03 +00002049 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2050
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002051 if (a->ob_size < 0) {
2052 if (b->ob_size < 0) {
2053 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002054 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002055 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002056 }
2057 else
2058 z = x_sub(b, a);
2059 }
2060 else {
2061 if (b->ob_size < 0)
2062 z = x_sub(a, b);
2063 else
2064 z = x_add(a, b);
2065 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002066 Py_DECREF(a);
2067 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002068 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002069}
2070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002072long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002073{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002074 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002075
Neil Schemenauerba872e22001-01-04 01:46:03 +00002076 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2077
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002078 if (a->ob_size < 0) {
2079 if (b->ob_size < 0)
2080 z = x_sub(a, b);
2081 else
2082 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002083 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002084 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002085 }
2086 else {
2087 if (b->ob_size < 0)
2088 z = x_add(a, b);
2089 else
2090 z = x_sub(a, b);
2091 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002092 Py_DECREF(a);
2093 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002094 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002095}
2096
Tim Peters5af4e6c2002-08-12 02:31:19 +00002097/* Grade school multiplication, ignoring the signs.
2098 * Returns the absolute value of the product, or NULL if error.
2099 */
2100static PyLongObject *
2101x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002102{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002103 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002104 Py_ssize_t size_a = ABS(a->ob_size);
2105 Py_ssize_t size_b = ABS(b->ob_size);
2106 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002107
Tim Peters5af4e6c2002-08-12 02:31:19 +00002108 z = _PyLong_New(size_a + size_b);
2109 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002110 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002111
2112 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002113 if (a == b) {
2114 /* Efficient squaring per HAC, Algorithm 14.16:
2115 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2116 * Gives slightly less than a 2x speedup when a == b,
2117 * via exploiting that each entry in the multiplication
2118 * pyramid appears twice (except for the size_a squares).
2119 */
2120 for (i = 0; i < size_a; ++i) {
2121 twodigits carry;
2122 twodigits f = a->ob_digit[i];
2123 digit *pz = z->ob_digit + (i << 1);
2124 digit *pa = a->ob_digit + i + 1;
2125 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002126
Tim Peters0973b992004-08-29 22:16:50 +00002127 SIGCHECK({
2128 Py_DECREF(z);
2129 return NULL;
2130 })
2131
2132 carry = *pz + f * f;
2133 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002134 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002135 assert(carry <= MASK);
2136
2137 /* Now f is added in twice in each column of the
2138 * pyramid it appears. Same as adding f<<1 once.
2139 */
2140 f <<= 1;
2141 while (pa < paend) {
2142 carry += *pz + *pa++ * f;
2143 *pz++ = (digit)(carry & MASK);
2144 carry >>= SHIFT;
2145 assert(carry <= (MASK << 1));
2146 }
2147 if (carry) {
2148 carry += *pz;
2149 *pz++ = (digit)(carry & MASK);
2150 carry >>= SHIFT;
2151 }
2152 if (carry)
2153 *pz += (digit)(carry & MASK);
2154 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002155 }
Tim Peters0973b992004-08-29 22:16:50 +00002156 }
2157 else { /* a is not the same as b -- gradeschool long mult */
2158 for (i = 0; i < size_a; ++i) {
2159 twodigits carry = 0;
2160 twodigits f = a->ob_digit[i];
2161 digit *pz = z->ob_digit + i;
2162 digit *pb = b->ob_digit;
2163 digit *pbend = b->ob_digit + size_b;
2164
2165 SIGCHECK({
2166 Py_DECREF(z);
2167 return NULL;
2168 })
2169
2170 while (pb < pbend) {
2171 carry += *pz + *pb++ * f;
2172 *pz++ = (digit)(carry & MASK);
2173 carry >>= SHIFT;
2174 assert(carry <= MASK);
2175 }
2176 if (carry)
2177 *pz += (digit)(carry & MASK);
2178 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002179 }
2180 }
Tim Peters44121a62002-08-12 06:17:58 +00002181 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002182}
2183
2184/* A helper for Karatsuba multiplication (k_mul).
2185 Takes a long "n" and an integer "size" representing the place to
2186 split, and sets low and high such that abs(n) == (high << size) + low,
2187 viewing the shift as being by digits. The sign bit is ignored, and
2188 the return values are >= 0.
2189 Returns 0 on success, -1 on failure.
2190*/
2191static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002192kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002193{
2194 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002195 Py_ssize_t size_lo, size_hi;
2196 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002197
2198 size_lo = MIN(size_n, size);
2199 size_hi = size_n - size_lo;
2200
2201 if ((hi = _PyLong_New(size_hi)) == NULL)
2202 return -1;
2203 if ((lo = _PyLong_New(size_lo)) == NULL) {
2204 Py_DECREF(hi);
2205 return -1;
2206 }
2207
2208 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2209 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2210
2211 *high = long_normalize(hi);
2212 *low = long_normalize(lo);
2213 return 0;
2214}
2215
Tim Peters60004642002-08-12 22:01:34 +00002216static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2217
Tim Peters5af4e6c2002-08-12 02:31:19 +00002218/* Karatsuba multiplication. Ignores the input signs, and returns the
2219 * absolute value of the product (or NULL if error).
2220 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2221 */
2222static PyLongObject *
2223k_mul(PyLongObject *a, PyLongObject *b)
2224{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002225 Py_ssize_t asize = ABS(a->ob_size);
2226 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002227 PyLongObject *ah = NULL;
2228 PyLongObject *al = NULL;
2229 PyLongObject *bh = NULL;
2230 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002231 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002232 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002233 Py_ssize_t shift; /* the number of digits we split off */
2234 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002235
Tim Peters5af4e6c2002-08-12 02:31:19 +00002236 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2237 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2238 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002239 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002240 * By picking X to be a power of 2, "*X" is just shifting, and it's
2241 * been reduced to 3 multiplies on numbers half the size.
2242 */
2243
Tim Petersfc07e562002-08-12 02:54:10 +00002244 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002245 * is largest.
2246 */
Tim Peters738eda72002-08-12 15:08:20 +00002247 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002248 t1 = a;
2249 a = b;
2250 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002251
2252 i = asize;
2253 asize = bsize;
2254 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002255 }
2256
2257 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002258 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2259 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002260 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002261 return _PyLong_New(0);
2262 else
2263 return x_mul(a, b);
2264 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002265
Tim Peters60004642002-08-12 22:01:34 +00002266 /* If a is small compared to b, splitting on b gives a degenerate
2267 * case with ah==0, and Karatsuba may be (even much) less efficient
2268 * than "grade school" then. However, we can still win, by viewing
2269 * b as a string of "big digits", each of width a->ob_size. That
2270 * leads to a sequence of balanced calls to k_mul.
2271 */
2272 if (2 * asize <= bsize)
2273 return k_lopsided_mul(a, b);
2274
Tim Petersd6974a52002-08-13 20:37:51 +00002275 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002276 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002277 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002278 assert(ah->ob_size > 0); /* the split isn't degenerate */
2279
Tim Peters0973b992004-08-29 22:16:50 +00002280 if (a == b) {
2281 bh = ah;
2282 bl = al;
2283 Py_INCREF(bh);
2284 Py_INCREF(bl);
2285 }
2286 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002287
Tim Petersd64c1de2002-08-12 17:36:03 +00002288 /* The plan:
2289 * 1. Allocate result space (asize + bsize digits: that's always
2290 * enough).
2291 * 2. Compute ah*bh, and copy into result at 2*shift.
2292 * 3. Compute al*bl, and copy into result at 0. Note that this
2293 * can't overlap with #2.
2294 * 4. Subtract al*bl from the result, starting at shift. This may
2295 * underflow (borrow out of the high digit), but we don't care:
2296 * we're effectively doing unsigned arithmetic mod
2297 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2298 * borrows and carries out of the high digit can be ignored.
2299 * 5. Subtract ah*bh from the result, starting at shift.
2300 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2301 * at shift.
2302 */
2303
2304 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002305 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002306 if (ret == NULL) goto fail;
2307#ifdef Py_DEBUG
2308 /* Fill with trash, to catch reference to uninitialized digits. */
2309 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2310#endif
Tim Peters44121a62002-08-12 06:17:58 +00002311
Tim Petersd64c1de2002-08-12 17:36:03 +00002312 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002313 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2314 assert(t1->ob_size >= 0);
2315 assert(2*shift + t1->ob_size <= ret->ob_size);
2316 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2317 t1->ob_size * sizeof(digit));
2318
2319 /* Zero-out the digits higher than the ah*bh copy. */
2320 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002321 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002322 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002323 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002324
Tim Petersd64c1de2002-08-12 17:36:03 +00002325 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002326 if ((t2 = k_mul(al, bl)) == NULL) {
2327 Py_DECREF(t1);
2328 goto fail;
2329 }
2330 assert(t2->ob_size >= 0);
2331 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2332 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002333
2334 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002335 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002336 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002337 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002338
Tim Petersd64c1de2002-08-12 17:36:03 +00002339 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2340 * because it's fresher in cache.
2341 */
Tim Peters738eda72002-08-12 15:08:20 +00002342 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002343 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002344 Py_DECREF(t2);
2345
Tim Petersd64c1de2002-08-12 17:36:03 +00002346 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002347 Py_DECREF(t1);
2348
Tim Petersd64c1de2002-08-12 17:36:03 +00002349 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002350 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2351 Py_DECREF(ah);
2352 Py_DECREF(al);
2353 ah = al = NULL;
2354
Tim Peters0973b992004-08-29 22:16:50 +00002355 if (a == b) {
2356 t2 = t1;
2357 Py_INCREF(t2);
2358 }
2359 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002360 Py_DECREF(t1);
2361 goto fail;
2362 }
2363 Py_DECREF(bh);
2364 Py_DECREF(bl);
2365 bh = bl = NULL;
2366
Tim Peters738eda72002-08-12 15:08:20 +00002367 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002368 Py_DECREF(t1);
2369 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002370 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002371 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002372
Tim Petersd6974a52002-08-13 20:37:51 +00002373 /* Add t3. It's not obvious why we can't run out of room here.
2374 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002375 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002376 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002377 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002378
Tim Peters5af4e6c2002-08-12 02:31:19 +00002379 return long_normalize(ret);
2380
2381 fail:
2382 Py_XDECREF(ret);
2383 Py_XDECREF(ah);
2384 Py_XDECREF(al);
2385 Py_XDECREF(bh);
2386 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002387 return NULL;
2388}
2389
Tim Petersd6974a52002-08-13 20:37:51 +00002390/* (*) Why adding t3 can't "run out of room" above.
2391
Tim Petersab86c2b2002-08-15 20:06:00 +00002392Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2393to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002394
Tim Petersab86c2b2002-08-15 20:06:00 +000023951. For any integer i, i = c(i/2) + f(i/2). In particular,
2396 bsize = c(bsize/2) + f(bsize/2).
23972. shift = f(bsize/2)
23983. asize <= bsize
23994. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2400 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002401
Tim Petersab86c2b2002-08-15 20:06:00 +00002402We allocated asize + bsize result digits, and add t3 into them at an offset
2403of shift. This leaves asize+bsize-shift allocated digit positions for t3
2404to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2405asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002406
Tim Petersab86c2b2002-08-15 20:06:00 +00002407bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2408at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002409
Tim Petersab86c2b2002-08-15 20:06:00 +00002410If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2411digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2412most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002413
Tim Petersab86c2b2002-08-15 20:06:00 +00002414The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002415
Tim Petersab86c2b2002-08-15 20:06:00 +00002416 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002417
Tim Petersab86c2b2002-08-15 20:06:00 +00002418and we have asize + c(bsize/2) available digit positions. We need to show
2419this is always enough. An instance of c(bsize/2) cancels out in both, so
2420the question reduces to whether asize digits is enough to hold
2421(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2422then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2423asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2424digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2425asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002426c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2427is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2428bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002429
Tim Peters48d52c02002-08-14 17:07:32 +00002430Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2431clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2432ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002433*/
2434
Tim Peters60004642002-08-12 22:01:34 +00002435/* b has at least twice the digits of a, and a is big enough that Karatsuba
2436 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2437 * of slices, each with a->ob_size digits, and multiply the slices by a,
2438 * one at a time. This gives k_mul balanced inputs to work with, and is
2439 * also cache-friendly (we compute one double-width slice of the result
2440 * at a time, then move on, never bactracking except for the helpful
2441 * single-width slice overlap between successive partial sums).
2442 */
2443static PyLongObject *
2444k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2445{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002446 const Py_ssize_t asize = ABS(a->ob_size);
2447 Py_ssize_t bsize = ABS(b->ob_size);
2448 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002449 PyLongObject *ret;
2450 PyLongObject *bslice = NULL;
2451
2452 assert(asize > KARATSUBA_CUTOFF);
2453 assert(2 * asize <= bsize);
2454
2455 /* Allocate result space, and zero it out. */
2456 ret = _PyLong_New(asize + bsize);
2457 if (ret == NULL)
2458 return NULL;
2459 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2460
2461 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002462 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002463 if (bslice == NULL)
2464 goto fail;
2465
2466 nbdone = 0;
2467 while (bsize > 0) {
2468 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002469 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002470
2471 /* Multiply the next slice of b by a. */
2472 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2473 nbtouse * sizeof(digit));
2474 bslice->ob_size = nbtouse;
2475 product = k_mul(a, bslice);
2476 if (product == NULL)
2477 goto fail;
2478
2479 /* Add into result. */
2480 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2481 product->ob_digit, product->ob_size);
2482 Py_DECREF(product);
2483
2484 bsize -= nbtouse;
2485 nbdone += nbtouse;
2486 }
2487
2488 Py_DECREF(bslice);
2489 return long_normalize(ret);
2490
2491 fail:
2492 Py_DECREF(ret);
2493 Py_XDECREF(bslice);
2494 return NULL;
2495}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002496
2497static PyObject *
2498long_mul(PyLongObject *v, PyLongObject *w)
2499{
2500 PyLongObject *a, *b, *z;
2501
2502 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002503 Py_INCREF(Py_NotImplemented);
2504 return Py_NotImplemented;
2505 }
2506
Tim Petersd64c1de2002-08-12 17:36:03 +00002507 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002508 /* Negate if exactly one of the inputs is negative. */
2509 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002510 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002511 Py_DECREF(a);
2512 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002513 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002514}
2515
Guido van Rossume32e0141992-01-19 16:31:05 +00002516/* The / and % operators are now defined in terms of divmod().
2517 The expression a mod b has the value a - b*floor(a/b).
2518 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002519 |a| by |b|, with the sign of a. This is also expressed
2520 as a - b*trunc(a/b), if trunc truncates towards zero.
2521 Some examples:
2522 a b a rem b a mod b
2523 13 10 3 3
2524 -13 10 -3 7
2525 13 -10 3 -7
2526 -13 -10 -3 -3
2527 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002528 have different signs. We then subtract one from the 'div'
2529 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002530
Tim Peters47e52ee2004-08-30 02:44:38 +00002531/* Compute
2532 * *pdiv, *pmod = divmod(v, w)
2533 * NULL can be passed for pdiv or pmod, in which case that part of
2534 * the result is simply thrown away. The caller owns a reference to
2535 * each of these it requests (does not pass NULL for).
2536 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002537static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002538l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002539 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002540{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002541 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002542
Guido van Rossume32e0141992-01-19 16:31:05 +00002543 if (long_divrem(v, w, &div, &mod) < 0)
2544 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002545 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2546 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002547 PyLongObject *temp;
2548 PyLongObject *one;
2549 temp = (PyLongObject *) long_add(mod, w);
2550 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002551 mod = temp;
2552 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002553 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002554 return -1;
2555 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002556 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002557 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002558 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2559 Py_DECREF(mod);
2560 Py_DECREF(div);
2561 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002562 return -1;
2563 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002564 Py_DECREF(one);
2565 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002566 div = temp;
2567 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002568 if (pdiv != NULL)
2569 *pdiv = div;
2570 else
2571 Py_DECREF(div);
2572
2573 if (pmod != NULL)
2574 *pmod = mod;
2575 else
2576 Py_DECREF(mod);
2577
Guido van Rossume32e0141992-01-19 16:31:05 +00002578 return 0;
2579}
2580
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002581static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002582long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002583{
Tim Peters47e52ee2004-08-30 02:44:38 +00002584 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002585
2586 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002587 if (l_divmod(a, b, &div, NULL) < 0)
2588 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002589 Py_DECREF(a);
2590 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002591 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002592}
2593
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002594static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002595long_classic_div(PyObject *v, PyObject *w)
2596{
Tim Peters47e52ee2004-08-30 02:44:38 +00002597 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002598
2599 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002600 if (Py_DivisionWarningFlag &&
2601 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2602 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002603 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002604 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002605 Py_DECREF(a);
2606 Py_DECREF(b);
2607 return (PyObject *)div;
2608}
2609
2610static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002611long_true_divide(PyObject *v, PyObject *w)
2612{
Tim Peterse2a60002001-09-04 06:17:36 +00002613 PyLongObject *a, *b;
2614 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002615 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002616
2617 CONVERT_BINOP(v, w, &a, &b);
2618 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2619 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002620 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2621 Py_DECREF(a);
2622 Py_DECREF(b);
2623 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002624 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002625 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2626 but should really be set correctly after sucessful calls to
2627 _PyLong_AsScaledDouble() */
2628 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002629
2630 if (bd == 0.0) {
2631 PyErr_SetString(PyExc_ZeroDivisionError,
2632 "long division or modulo by zero");
2633 return NULL;
2634 }
2635
2636 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2637 ad /= bd; /* overflow/underflow impossible here */
2638 aexp -= bexp;
2639 if (aexp > INT_MAX / SHIFT)
2640 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002641 else if (aexp < -(INT_MAX / SHIFT))
2642 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002643 errno = 0;
2644 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002645 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002646 goto overflow;
2647 return PyFloat_FromDouble(ad);
2648
2649overflow:
2650 PyErr_SetString(PyExc_OverflowError,
2651 "long/long too large for a float");
2652 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002653
Tim Peters20dab9f2001-09-04 05:31:47 +00002654}
2655
2656static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002657long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002658{
Tim Peters47e52ee2004-08-30 02:44:38 +00002659 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002660
2661 CONVERT_BINOP(v, w, &a, &b);
2662
Tim Peters47e52ee2004-08-30 02:44:38 +00002663 if (l_divmod(a, b, NULL, &mod) < 0)
2664 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002665 Py_DECREF(a);
2666 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002667 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002668}
2669
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002670static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002671long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002672{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002673 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002674 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002675
2676 CONVERT_BINOP(v, w, &a, &b);
2677
2678 if (l_divmod(a, b, &div, &mod) < 0) {
2679 Py_DECREF(a);
2680 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002681 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002682 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002683 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002684 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002685 PyTuple_SetItem(z, 0, (PyObject *) div);
2686 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002687 }
2688 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002689 Py_DECREF(div);
2690 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002691 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002692 Py_DECREF(a);
2693 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002694 return z;
2695}
2696
Tim Peters47e52ee2004-08-30 02:44:38 +00002697/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002698static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002699long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002700{
Tim Peters47e52ee2004-08-30 02:44:38 +00002701 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2702 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002703
Tim Peters47e52ee2004-08-30 02:44:38 +00002704 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002705 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002706 PyLongObject *temp = NULL;
2707
2708 /* 5-ary values. If the exponent is large enough, table is
2709 * precomputed so that table[i] == a**i % c for i in range(32).
2710 */
2711 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2712 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2713
2714 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002715 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002716 if (PyLong_Check(x)) {
2717 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002718 Py_INCREF(x);
2719 }
Tim Petersde7990b2005-07-17 23:45:23 +00002720 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002721 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002722 if (c == NULL)
2723 goto Error;
2724 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002725 else if (x == Py_None)
2726 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002727 else {
2728 Py_DECREF(a);
2729 Py_DECREF(b);
2730 Py_INCREF(Py_NotImplemented);
2731 return Py_NotImplemented;
2732 }
Tim Peters4c483c42001-09-05 06:24:58 +00002733
Tim Peters47e52ee2004-08-30 02:44:38 +00002734 if (b->ob_size < 0) { /* if exponent is negative */
2735 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002736 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002737 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002738 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002739 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002740 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002741 /* else return a float. This works because we know
2742 that this calls float_pow() which converts its
2743 arguments to double. */
2744 Py_DECREF(a);
2745 Py_DECREF(b);
2746 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002747 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002748 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002749
2750 if (c) {
2751 /* if modulus == 0:
2752 raise ValueError() */
2753 if (c->ob_size == 0) {
2754 PyErr_SetString(PyExc_ValueError,
2755 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002756 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002757 }
2758
2759 /* if modulus < 0:
2760 negativeOutput = True
2761 modulus = -modulus */
2762 if (c->ob_size < 0) {
2763 negativeOutput = 1;
2764 temp = (PyLongObject *)_PyLong_Copy(c);
2765 if (temp == NULL)
2766 goto Error;
2767 Py_DECREF(c);
2768 c = temp;
2769 temp = NULL;
2770 c->ob_size = - c->ob_size;
2771 }
2772
2773 /* if modulus == 1:
2774 return 0 */
2775 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2776 z = (PyLongObject *)PyLong_FromLong(0L);
2777 goto Done;
2778 }
2779
2780 /* if base < 0:
2781 base = base % modulus
2782 Having the base positive just makes things easier. */
2783 if (a->ob_size < 0) {
2784 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002785 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002786 Py_DECREF(a);
2787 a = temp;
2788 temp = NULL;
2789 }
2790 }
2791
2792 /* At this point a, b, and c are guaranteed non-negative UNLESS
2793 c is NULL, in which case a may be negative. */
2794
2795 z = (PyLongObject *)PyLong_FromLong(1L);
2796 if (z == NULL)
2797 goto Error;
2798
2799 /* Perform a modular reduction, X = X % c, but leave X alone if c
2800 * is NULL.
2801 */
2802#define REDUCE(X) \
2803 if (c != NULL) { \
2804 if (l_divmod(X, c, NULL, &temp) < 0) \
2805 goto Error; \
2806 Py_XDECREF(X); \
2807 X = temp; \
2808 temp = NULL; \
2809 }
2810
2811 /* Multiply two values, then reduce the result:
2812 result = X*Y % c. If c is NULL, skip the mod. */
2813#define MULT(X, Y, result) \
2814{ \
2815 temp = (PyLongObject *)long_mul(X, Y); \
2816 if (temp == NULL) \
2817 goto Error; \
2818 Py_XDECREF(result); \
2819 result = temp; \
2820 temp = NULL; \
2821 REDUCE(result) \
2822}
2823
2824 if (b->ob_size <= FIVEARY_CUTOFF) {
2825 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2826 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2827 for (i = b->ob_size - 1; i >= 0; --i) {
2828 digit bi = b->ob_digit[i];
2829
2830 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2831 MULT(z, z, z)
2832 if (bi & j)
2833 MULT(z, a, z)
2834 }
2835 }
2836 }
2837 else {
2838 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2839 Py_INCREF(z); /* still holds 1L */
2840 table[0] = z;
2841 for (i = 1; i < 32; ++i)
2842 MULT(table[i-1], a, table[i])
2843
2844 for (i = b->ob_size - 1; i >= 0; --i) {
2845 const digit bi = b->ob_digit[i];
2846
2847 for (j = SHIFT - 5; j >= 0; j -= 5) {
2848 const int index = (bi >> j) & 0x1f;
2849 for (k = 0; k < 5; ++k)
2850 MULT(z, z, z)
2851 if (index)
2852 MULT(z, table[index], z)
2853 }
2854 }
2855 }
2856
2857 if (negativeOutput && (z->ob_size != 0)) {
2858 temp = (PyLongObject *)long_sub(z, c);
2859 if (temp == NULL)
2860 goto Error;
2861 Py_DECREF(z);
2862 z = temp;
2863 temp = NULL;
2864 }
2865 goto Done;
2866
2867 Error:
2868 if (z != NULL) {
2869 Py_DECREF(z);
2870 z = NULL;
2871 }
2872 /* fall through */
2873 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002874 if (b->ob_size > FIVEARY_CUTOFF) {
2875 for (i = 0; i < 32; ++i)
2876 Py_XDECREF(table[i]);
2877 }
Tim Petersde7990b2005-07-17 23:45:23 +00002878 Py_DECREF(a);
2879 Py_DECREF(b);
2880 Py_XDECREF(c);
2881 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002882 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002883}
2884
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002885static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002886long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002887{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002888 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002889 PyLongObject *x;
2890 PyLongObject *w;
2891 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002892 if (w == NULL)
2893 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002894 x = (PyLongObject *) long_add(v, w);
2895 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002896 if (x == NULL)
2897 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002898 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002899 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002900}
2901
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002902static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002903long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002904{
Tim Peters69c2de32001-09-11 22:31:33 +00002905 if (PyLong_CheckExact(v)) {
2906 Py_INCREF(v);
2907 return (PyObject *)v;
2908 }
2909 else
2910 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002911}
2912
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002913static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002914long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002915{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002916 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002917 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002918 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002919 Py_INCREF(v);
2920 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002921 }
Tim Peters69c2de32001-09-11 22:31:33 +00002922 z = (PyLongObject *)_PyLong_Copy(v);
2923 if (z != NULL)
2924 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002925 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002926}
2927
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002928static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002929long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002930{
2931 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002932 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002933 else
2934 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002935}
2936
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002937static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002938long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002939{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002940 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002941}
2942
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002943static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002944long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002945{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002946 PyLongObject *a, *b;
2947 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002948 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002949 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002950 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002951
Neil Schemenauerba872e22001-01-04 01:46:03 +00002952 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2953
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002954 if (a->ob_size < 0) {
2955 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002956 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002957 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002958 if (a1 == NULL)
2959 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002960 a2 = (PyLongObject *) long_rshift(a1, b);
2961 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002962 if (a2 == NULL)
2963 goto rshift_error;
2964 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002965 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002966 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002967 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002968
Neil Schemenauerba872e22001-01-04 01:46:03 +00002969 shiftby = PyLong_AsLong((PyObject *)b);
2970 if (shiftby == -1L && PyErr_Occurred())
2971 goto rshift_error;
2972 if (shiftby < 0) {
2973 PyErr_SetString(PyExc_ValueError,
2974 "negative shift count");
2975 goto rshift_error;
2976 }
2977 wordshift = shiftby / SHIFT;
2978 newsize = ABS(a->ob_size) - wordshift;
2979 if (newsize <= 0) {
2980 z = _PyLong_New(0);
2981 Py_DECREF(a);
2982 Py_DECREF(b);
2983 return (PyObject *)z;
2984 }
2985 loshift = shiftby % SHIFT;
2986 hishift = SHIFT - loshift;
2987 lomask = ((digit)1 << hishift) - 1;
2988 himask = MASK ^ lomask;
2989 z = _PyLong_New(newsize);
2990 if (z == NULL)
2991 goto rshift_error;
2992 if (a->ob_size < 0)
2993 z->ob_size = -(z->ob_size);
2994 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2995 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2996 if (i+1 < newsize)
2997 z->ob_digit[i] |=
2998 (a->ob_digit[j+1] << hishift) & himask;
2999 }
3000 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003001 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003002rshift_error:
3003 Py_DECREF(a);
3004 Py_DECREF(b);
3005 return (PyObject *) z;
3006
Guido van Rossumc6913e71991-11-19 20:26:46 +00003007}
3008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003009static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003010long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003011{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003012 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003013 PyLongObject *a, *b;
3014 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003015 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003016 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003017 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003018
Neil Schemenauerba872e22001-01-04 01:46:03 +00003019 CONVERT_BINOP(v, w, &a, &b);
3020
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003021 shiftby = PyLong_AsLong((PyObject *)b);
3022 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003023 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003024 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003025 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003026 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003027 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003028 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003029 PyErr_SetString(PyExc_ValueError,
3030 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003031 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003032 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003033 /* wordshift, remshift = divmod(shiftby, SHIFT) */
3034 wordshift = (int)shiftby / SHIFT;
3035 remshift = (int)shiftby - wordshift * SHIFT;
3036
3037 oldsize = ABS(a->ob_size);
3038 newsize = oldsize + wordshift;
3039 if (remshift)
3040 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003041 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003042 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003043 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003044 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003045 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003046 for (i = 0; i < wordshift; i++)
3047 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003048 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003049 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003050 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003051 z->ob_digit[i] = (digit)(accum & MASK);
3052 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003053 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003054 if (remshift)
3055 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003056 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003057 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003058 z = long_normalize(z);
3059lshift_error:
3060 Py_DECREF(a);
3061 Py_DECREF(b);
3062 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003063}
3064
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003065
3066/* Bitwise and/xor/or operations */
3067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003068static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003069long_bitwise(PyLongObject *a,
3070 int op, /* '&', '|', '^' */
3071 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003072{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003073 digit maska, maskb; /* 0 or MASK */
3074 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003075 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003076 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003077 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003078 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003079 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003080
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003081 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003082 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003083 if (a == NULL)
3084 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003085 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003086 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003087 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003088 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003089 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003090 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003091 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003092 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003093 if (b == NULL) {
3094 Py_DECREF(a);
3095 return NULL;
3096 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003097 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003098 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003099 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003100 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003101 maskb = 0;
3102 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003103
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003104 negz = 0;
3105 switch (op) {
3106 case '^':
3107 if (maska != maskb) {
3108 maska ^= MASK;
3109 negz = -1;
3110 }
3111 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003112 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003113 if (maska && maskb) {
3114 op = '|';
3115 maska ^= MASK;
3116 maskb ^= MASK;
3117 negz = -1;
3118 }
3119 break;
3120 case '|':
3121 if (maska || maskb) {
3122 op = '&';
3123 maska ^= MASK;
3124 maskb ^= MASK;
3125 negz = -1;
3126 }
3127 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003128 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003129
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003130 /* JRH: The original logic here was to allocate the result value (z)
3131 as the longer of the two operands. However, there are some cases
3132 where the result is guaranteed to be shorter than that: AND of two
3133 positives, OR of two negatives: use the shorter number. AND with
3134 mixed signs: use the positive number. OR with mixed signs: use the
3135 negative number. After the transformations above, op will be '&'
3136 iff one of these cases applies, and mask will be non-0 for operands
3137 whose length should be ignored.
3138 */
3139
3140 size_a = a->ob_size;
3141 size_b = b->ob_size;
3142 size_z = op == '&'
3143 ? (maska
3144 ? size_b
3145 : (maskb ? size_a : MIN(size_a, size_b)))
3146 : MAX(size_a, size_b);
3147 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003148 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003149 Py_DECREF(a);
3150 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003151 return NULL;
3152 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003153
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003154 for (i = 0; i < size_z; ++i) {
3155 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3156 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3157 switch (op) {
3158 case '&': z->ob_digit[i] = diga & digb; break;
3159 case '|': z->ob_digit[i] = diga | digb; break;
3160 case '^': z->ob_digit[i] = diga ^ digb; break;
3161 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003162 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003163
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003164 Py_DECREF(a);
3165 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003166 z = long_normalize(z);
3167 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003168 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003169 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003170 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003171 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003172}
3173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003174static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003175long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003176{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003177 PyLongObject *a, *b;
3178 PyObject *c;
3179 CONVERT_BINOP(v, w, &a, &b);
3180 c = long_bitwise(a, '&', b);
3181 Py_DECREF(a);
3182 Py_DECREF(b);
3183 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003184}
3185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003186static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003187long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003188{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003189 PyLongObject *a, *b;
3190 PyObject *c;
3191 CONVERT_BINOP(v, w, &a, &b);
3192 c = long_bitwise(a, '^', b);
3193 Py_DECREF(a);
3194 Py_DECREF(b);
3195 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003196}
3197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003198static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003199long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003200{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003201 PyLongObject *a, *b;
3202 PyObject *c;
3203 CONVERT_BINOP(v, w, &a, &b);
3204 c = long_bitwise(a, '|', b);
3205 Py_DECREF(a);
3206 Py_DECREF(b);
3207 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003208}
3209
Guido van Rossum234f9421993-06-17 12:35:49 +00003210static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003211long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003212{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003213 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003214 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandl1dfa8ac2007-04-21 07:22:57 +00003215 if (*pw == NULL)
3216 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003217 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003218 return 0;
3219 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003220 else if (PyLong_Check(*pw)) {
3221 Py_INCREF(*pv);
3222 Py_INCREF(*pw);
3223 return 0;
3224 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003225 return 1; /* Can't do it */
3226}
3227
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003228static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003229long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003230{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003231 if (PyLong_CheckExact(v))
3232 Py_INCREF(v);
3233 else
3234 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003235 return v;
3236}
3237
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003238static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003239long_int(PyObject *v)
3240{
3241 long x;
3242 x = PyLong_AsLong(v);
3243 if (PyErr_Occurred()) {
3244 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3245 PyErr_Clear();
3246 if (PyLong_CheckExact(v)) {
3247 Py_INCREF(v);
3248 return v;
3249 }
3250 else
3251 return _PyLong_Copy((PyLongObject *)v);
3252 }
3253 else
3254 return NULL;
3255 }
3256 return PyInt_FromLong(x);
3257}
3258
3259static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003260long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003261{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003262 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003263 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003264 if (result == -1.0 && PyErr_Occurred())
3265 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003266 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003267}
3268
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003269static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003270long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003271{
Fred Drake121ee271999-12-23 15:41:28 +00003272 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003273}
3274
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003275static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003276long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003277{
Fred Drake121ee271999-12-23 15:41:28 +00003278 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003279}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003280
3281static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003282long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003283
Tim Peters6d6c1a32001-08-02 04:15:00 +00003284static PyObject *
3285long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3286{
3287 PyObject *x = NULL;
3288 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003289 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003290
Guido van Rossumbef14172001-08-29 15:47:46 +00003291 if (type != &PyLong_Type)
3292 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003293 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3294 &x, &base))
3295 return NULL;
3296 if (x == NULL)
3297 return PyLong_FromLong(0L);
3298 if (base == -909)
3299 return PyNumber_Long(x);
Georg Brandlffb0a802007-03-06 18:44:35 +00003300 else if (PyString_Check(x)) {
3301 /* Since PyLong_FromString doesn't have a length parameter,
3302 * check here for possible NULs in the string. */
3303 char *string = PyString_AS_STRING(x);
3304 if (strlen(string) != PyString_Size(x)) {
3305 /* create a repr() of the input string,
3306 * just like PyLong_FromString does. */
3307 PyObject *srepr;
3308 srepr = PyObject_Repr(x);
3309 if (srepr == NULL)
3310 return NULL;
3311 PyErr_Format(PyExc_ValueError,
3312 "invalid literal for long() with base %d: %s",
3313 base, PyString_AS_STRING(srepr));
3314 Py_DECREF(srepr);
3315 return NULL;
3316 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003317 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandlffb0a802007-03-06 18:44:35 +00003318 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003319#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003320 else if (PyUnicode_Check(x))
3321 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3322 PyUnicode_GET_SIZE(x),
3323 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003324#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003325 else {
3326 PyErr_SetString(PyExc_TypeError,
3327 "long() can't convert non-string with explicit base");
3328 return NULL;
3329 }
3330}
3331
Guido van Rossumbef14172001-08-29 15:47:46 +00003332/* Wimpy, slow approach to tp_new calls for subtypes of long:
3333 first create a regular long from whatever arguments we got,
3334 then allocate a subtype instance and initialize it from
3335 the regular long. The regular long is then thrown away.
3336*/
3337static PyObject *
3338long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3339{
Anthony Baxter377be112006-04-11 06:54:30 +00003340 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003341 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003342
3343 assert(PyType_IsSubtype(type, &PyLong_Type));
3344 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3345 if (tmp == NULL)
3346 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003347 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003348 n = tmp->ob_size;
3349 if (n < 0)
3350 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003351 newobj = (PyLongObject *)type->tp_alloc(type, n);
3352 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003353 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003354 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003355 }
Anthony Baxter377be112006-04-11 06:54:30 +00003356 assert(PyLong_Check(newobj));
3357 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003358 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003359 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003360 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003361 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003362}
3363
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003364static PyObject *
3365long_getnewargs(PyLongObject *v)
3366{
3367 return Py_BuildValue("(N)", _PyLong_Copy(v));
3368}
3369
3370static PyMethodDef long_methods[] = {
3371 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3372 {NULL, NULL} /* sentinel */
3373};
3374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003376"long(x[, base]) -> integer\n\
3377\n\
3378Convert a string or number to a long integer, if possible. A floating\n\
3379point argument will be truncated towards zero (this does not include a\n\
3380string representation of a floating point number!) When converting a\n\
3381string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003383
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003384static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003385 (binaryfunc) long_add, /*nb_add*/
3386 (binaryfunc) long_sub, /*nb_subtract*/
3387 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003388 long_classic_div, /*nb_divide*/
3389 long_mod, /*nb_remainder*/
3390 long_divmod, /*nb_divmod*/
3391 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003392 (unaryfunc) long_neg, /*nb_negative*/
3393 (unaryfunc) long_pos, /*tp_positive*/
3394 (unaryfunc) long_abs, /*tp_absolute*/
3395 (inquiry) long_nonzero, /*tp_nonzero*/
3396 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003397 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003398 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003399 long_and, /*nb_and*/
3400 long_xor, /*nb_xor*/
3401 long_or, /*nb_or*/
3402 long_coerce, /*nb_coerce*/
3403 long_int, /*nb_int*/
3404 long_long, /*nb_long*/
3405 long_float, /*nb_float*/
3406 long_oct, /*nb_oct*/
3407 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003408 0, /* nb_inplace_add */
3409 0, /* nb_inplace_subtract */
3410 0, /* nb_inplace_multiply */
3411 0, /* nb_inplace_divide */
3412 0, /* nb_inplace_remainder */
3413 0, /* nb_inplace_power */
3414 0, /* nb_inplace_lshift */
3415 0, /* nb_inplace_rshift */
3416 0, /* nb_inplace_and */
3417 0, /* nb_inplace_xor */
3418 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003419 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003420 long_true_divide, /* nb_true_divide */
3421 0, /* nb_inplace_floor_divide */
3422 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003423 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003424};
3425
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003426PyTypeObject PyLong_Type = {
3427 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428 0, /* ob_size */
3429 "long", /* tp_name */
3430 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3431 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003432 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003433 0, /* tp_print */
3434 0, /* tp_getattr */
3435 0, /* tp_setattr */
3436 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003437 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003438 &long_as_number, /* tp_as_number */
3439 0, /* tp_as_sequence */
3440 0, /* tp_as_mapping */
3441 (hashfunc)long_hash, /* tp_hash */
3442 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003443 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003444 PyObject_GenericGetAttr, /* tp_getattro */
3445 0, /* tp_setattro */
3446 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3448 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003449 long_doc, /* tp_doc */
3450 0, /* tp_traverse */
3451 0, /* tp_clear */
3452 0, /* tp_richcompare */
3453 0, /* tp_weaklistoffset */
3454 0, /* tp_iter */
3455 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003456 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003457 0, /* tp_members */
3458 0, /* tp_getset */
3459 0, /* tp_base */
3460 0, /* tp_dict */
3461 0, /* tp_descr_get */
3462 0, /* tp_descr_set */
3463 0, /* tp_dictoffset */
3464 0, /* tp_init */
3465 0, /* tp_alloc */
3466 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003467 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003468};