blob: 9fb583281abd3dcf47f635a82e2160acd2530c9f [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
Christian Heimes7f39c9f2008-01-25 12:18:43 +000014 * being an internal Python long digit, in base PyLong_BASE).
Tim Peters5af4e6c2002-08-12 02:31:19 +000015 */
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{
Christian Heimese93237d2007-12-19 02:37:44 +000053 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +000054 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)
Christian Heimese93237d2007-12-19 02:37:44 +000059 Py_SIZE(v) = (Py_SIZE(v) < 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 }
Christian Heimes4956d2b2008-01-18 19:12:56 +000073 /* coverity[ampersand_in_size] */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000075}
76
Tim Peters64b5ce32001-09-10 20:52:51 +000077PyObject *
78_PyLong_Copy(PyLongObject *src)
79{
80 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000081 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000082
83 assert(src != NULL);
84 i = src->ob_size;
85 if (i < 0)
86 i = -(i);
87 result = _PyLong_New(i);
88 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000089 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000090 while (--i >= 0)
91 result->ob_digit[i] = src->ob_digit[i];
92 }
93 return (PyObject *)result;
94}
95
Guido van Rossumedcc38a1991-05-05 20:09:44 +000096/* Create a new long int object from a C long int */
97
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000099PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000100{
Tim Petersce9de2f2001-06-14 04:56:19 +0000101 PyLongObject *v;
102 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
103 int ndigits = 0;
104 int negative = 0;
105
106 if (ival < 0) {
107 ival = -ival;
108 negative = 1;
109 }
110
111 /* Count the number of Python digits.
112 We used to pick 5 ("big enough for anything"), but that's a
113 waste of time and space given that 5*15 = 75 bits are rarely
114 needed. */
115 t = (unsigned long)ival;
116 while (t) {
117 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000118 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000119 }
120 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000121 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000122 digit *p = v->ob_digit;
123 v->ob_size = negative ? -ndigits : ndigits;
124 t = (unsigned long)ival;
125 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000126 *p++ = (digit)(t & PyLong_MASK);
127 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000129 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000131}
132
Guido van Rossum53756b11997-01-03 17:14:46 +0000133/* Create a new long int object from a C unsigned long int */
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000136PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000137{
Tim Petersce9de2f2001-06-14 04:56:19 +0000138 PyLongObject *v;
139 unsigned long t;
140 int ndigits = 0;
141
142 /* Count the number of Python digits. */
143 t = (unsigned long)ival;
144 while (t) {
145 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000146 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000147 }
148 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000149 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000150 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000151 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000152 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000153 *p++ = (digit)(ival & PyLong_MASK);
154 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000156 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000158}
159
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000160/* Create a new long int object from a C double */
161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000164{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000166 double frac;
167 int i, ndig, expo, neg;
168 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000169 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000170 PyErr_SetString(PyExc_OverflowError,
171 "cannot convert float infinity to long");
172 return NULL;
173 }
Christian Heimes8267d1d2008-01-04 00:37:34 +0000174 if (Py_IS_NAN(dval)) {
175 return PyLong_FromLong(0L);
176 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000177 if (dval < 0.0) {
178 neg = 1;
179 dval = -dval;
180 }
181 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
182 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 return PyLong_FromLong(0L);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000184 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000186 if (v == NULL)
187 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000188 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000189 for (i = ndig; --i >= 0; ) {
190 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000191 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000192 frac = frac - (double)bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000193 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000194 }
195 if (neg)
Christian Heimese93237d2007-12-19 02:37:44 +0000196 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000198}
199
Armin Rigo7ccbca92006-10-04 12:17:45 +0000200/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
201 * anything about what happens when a signed integer operation overflows,
202 * and some compilers think they're doing you a favor by being "clever"
203 * then. The bit pattern for the largest postive signed long is
204 * (unsigned long)LONG_MAX, and for the smallest negative signed long
205 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
206 * However, some other compilers warn about applying unary minus to an
207 * unsigned operand. Hence the weird "0-".
208 */
209#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
210#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
211
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000212/* Get a C long int from a long int object.
213 Returns -1 and sets an error condition if overflow occurs. */
214
215long
Tim Peters9f688bf2000-07-07 15:53:28 +0000216PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000217{
Guido van Rossumf7531811998-05-26 14:33:37 +0000218 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000220 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 Py_ssize_t i;
222 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000223
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000225 if (vv != NULL && PyInt_Check(vv))
226 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000228 return -1;
229 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000231 i = v->ob_size;
232 sign = 1;
233 x = 0;
234 if (i < 0) {
235 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000236 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000237 }
238 while (--i >= 0) {
239 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000240 x = (x << PyLong_SHIFT) + v->ob_digit[i];
241 if ((x >> PyLong_SHIFT) != prev)
Guido van Rossumf7531811998-05-26 14:33:37 +0000242 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000243 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000244 /* Haven't lost any bits, but casting to long requires extra care
245 * (see comment above).
246 */
247 if (x <= (unsigned long)LONG_MAX) {
248 return (long)x * sign;
249 }
250 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
251 return LONG_MIN;
252 }
253 /* else overflow */
Guido van Rossumf7531811998-05-26 14:33:37 +0000254
255 overflow:
256 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000257 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000258 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000259}
260
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000261/* Get a Py_ssize_t from a long int object.
262 Returns -1 and sets an error condition if overflow occurs. */
263
264Py_ssize_t
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000265PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000266 register PyLongObject *v;
267 size_t x, prev;
268 Py_ssize_t i;
269 int sign;
270
271 if (vv == NULL || !PyLong_Check(vv)) {
272 PyErr_BadInternalCall();
273 return -1;
274 }
275 v = (PyLongObject *)vv;
276 i = v->ob_size;
277 sign = 1;
278 x = 0;
279 if (i < 0) {
280 sign = -1;
281 i = -(i);
282 }
283 while (--i >= 0) {
284 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000285 x = (x << PyLong_SHIFT) + v->ob_digit[i];
286 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000287 goto overflow;
288 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000289 /* Haven't lost any bits, but casting to a signed type requires
290 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000291 */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000292 if (x <= (size_t)PY_SSIZE_T_MAX) {
293 return (Py_ssize_t)x * sign;
294 }
295 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
296 return PY_SSIZE_T_MIN;
297 }
298 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299
300 overflow:
301 PyErr_SetString(PyExc_OverflowError,
302 "long int too large to convert to int");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000303 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000304}
305
Guido van Rossumd8c80482002-08-13 00:24:58 +0000306/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000307 Returns -1 and sets an error condition if overflow occurs. */
308
309unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000310PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000313 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000314 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000315
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000317 if (vv != NULL && PyInt_Check(vv)) {
318 long val = PyInt_AsLong(vv);
319 if (val < 0) {
320 PyErr_SetString(PyExc_OverflowError,
321 "can't convert negative value to unsigned long");
322 return (unsigned long) -1;
323 }
324 return val;
325 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000327 return (unsigned long) -1;
328 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000330 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000331 x = 0;
332 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000334 "can't convert negative value to unsigned long");
335 return (unsigned long) -1;
336 }
337 while (--i >= 0) {
338 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000339 x = (x << PyLong_SHIFT) + v->ob_digit[i];
340 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000342 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000343 return (unsigned long) -1;
344 }
345 }
346 return x;
347}
348
Thomas Hellera4ea6032003-04-17 18:55:45 +0000349/* Get a C unsigned long int from a long int object, ignoring the high bits.
350 Returns -1 and sets an error condition if an error occurs. */
351
352unsigned long
353PyLong_AsUnsignedLongMask(PyObject *vv)
354{
355 register PyLongObject *v;
356 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357 Py_ssize_t i;
358 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000359
360 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000361 if (vv != NULL && PyInt_Check(vv))
362 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000363 PyErr_BadInternalCall();
364 return (unsigned long) -1;
365 }
366 v = (PyLongObject *)vv;
367 i = v->ob_size;
368 sign = 1;
369 x = 0;
370 if (i < 0) {
371 sign = -1;
372 i = -i;
373 }
374 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000375 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000376 }
377 return x * sign;
378}
379
Tim Peters5b8132f2003-01-31 15:52:05 +0000380int
381_PyLong_Sign(PyObject *vv)
382{
383 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000384
385 assert(v != NULL);
386 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000387
Christian Heimese93237d2007-12-19 02:37:44 +0000388 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000389}
390
Tim Petersbaefd9e2003-01-28 20:37:45 +0000391size_t
392_PyLong_NumBits(PyObject *vv)
393{
394 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000395 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000396 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000397
398 assert(v != NULL);
399 assert(PyLong_Check(v));
Christian Heimese93237d2007-12-19 02:37:44 +0000400 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000401 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
402 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000403 digit msd = v->ob_digit[ndigits - 1];
404
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000405 result = (ndigits - 1) * PyLong_SHIFT;
406 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000407 goto Overflow;
408 do {
409 ++result;
410 if (result == 0)
411 goto Overflow;
412 msd >>= 1;
413 } while (msd);
414 }
415 return result;
416
417Overflow:
418 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
419 "to express in a platform size_t");
420 return (size_t)-1;
421}
422
Tim Peters2a9b3672001-06-11 21:23:58 +0000423PyObject *
424_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
425 int little_endian, int is_signed)
426{
427 const unsigned char* pstartbyte;/* LSB of bytes */
428 int incr; /* direction to move pstartbyte */
429 const unsigned char* pendbyte; /* MSB of bytes */
430 size_t numsignificantbytes; /* number of bytes that matter */
431 size_t ndigits; /* number of Python long digits */
432 PyLongObject* v; /* result */
433 int idigit = 0; /* next free index in v->ob_digit */
434
435 if (n == 0)
436 return PyLong_FromLong(0L);
437
438 if (little_endian) {
439 pstartbyte = bytes;
440 pendbyte = bytes + n - 1;
441 incr = 1;
442 }
443 else {
444 pstartbyte = bytes + n - 1;
445 pendbyte = bytes;
446 incr = -1;
447 }
448
449 if (is_signed)
450 is_signed = *pendbyte >= 0x80;
451
452 /* Compute numsignificantbytes. This consists of finding the most
453 significant byte. Leading 0 bytes are insignficant if the number
454 is positive, and leading 0xff bytes if negative. */
455 {
456 size_t i;
457 const unsigned char* p = pendbyte;
458 const int pincr = -incr; /* search MSB to LSB */
459 const unsigned char insignficant = is_signed ? 0xff : 0x00;
460
461 for (i = 0; i < n; ++i, p += pincr) {
462 if (*p != insignficant)
463 break;
464 }
465 numsignificantbytes = n - i;
466 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
467 actually has 2 significant bytes. OTOH, 0xff0001 ==
468 -0x00ffff, so we wouldn't *need* to bump it there; but we
469 do for 0xffff = -0x0001. To be safe without bothering to
470 check every case, bump it regardless. */
471 if (is_signed && numsignificantbytes < n)
472 ++numsignificantbytes;
473 }
474
475 /* How many Python long digits do we need? We have
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000476 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
Tim Peters2a9b3672001-06-11 21:23:58 +0000477 bits, so it's the ceiling of the quotient. */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000478 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Tim Peters2a9b3672001-06-11 21:23:58 +0000479 if (ndigits > (size_t)INT_MAX)
480 return PyErr_NoMemory();
481 v = _PyLong_New((int)ndigits);
482 if (v == NULL)
483 return NULL;
484
485 /* Copy the bits over. The tricky parts are computing 2's-comp on
486 the fly for signed numbers, and dealing with the mismatch between
487 8-bit bytes and (probably) 15-bit Python digits.*/
488 {
489 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000490 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000491 twodigits accum = 0; /* sliding register */
492 unsigned int accumbits = 0; /* number of bits in accum */
493 const unsigned char* p = pstartbyte;
494
495 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000496 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000497 /* Compute correction for 2's comp, if needed. */
498 if (is_signed) {
499 thisbyte = (0xff ^ thisbyte) + carry;
500 carry = thisbyte >> 8;
501 thisbyte &= 0xff;
502 }
503 /* Because we're going LSB to MSB, thisbyte is
504 more significant than what's already in accum,
505 so needs to be prepended to accum. */
506 accum |= thisbyte << accumbits;
507 accumbits += 8;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000508 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000509 /* There's enough to fill a Python digit. */
510 assert(idigit < (int)ndigits);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000511 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000512 ++idigit;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000513 accum >>= PyLong_SHIFT;
514 accumbits -= PyLong_SHIFT;
515 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000516 }
517 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000518 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000519 if (accumbits) {
520 assert(idigit < (int)ndigits);
521 v->ob_digit[idigit] = (digit)accum;
522 ++idigit;
523 }
524 }
525
Christian Heimese93237d2007-12-19 02:37:44 +0000526 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000527 return (PyObject *)long_normalize(v);
528}
529
530int
531_PyLong_AsByteArray(PyLongObject* v,
532 unsigned char* bytes, size_t n,
533 int little_endian, int is_signed)
534{
535 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000536 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000537 twodigits accum; /* sliding register */
538 unsigned int accumbits; /* # bits in accum */
539 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
540 twodigits carry; /* for computing 2's-comp */
541 size_t j; /* # bytes filled */
542 unsigned char* p; /* pointer to next byte in bytes */
543 int pincr; /* direction to move p */
544
545 assert(v != NULL && PyLong_Check(v));
546
Christian Heimese93237d2007-12-19 02:37:44 +0000547 if (Py_SIZE(v) < 0) {
548 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000549 if (!is_signed) {
550 PyErr_SetString(PyExc_TypeError,
551 "can't convert negative long to unsigned");
552 return -1;
553 }
554 do_twos_comp = 1;
555 }
556 else {
Christian Heimese93237d2007-12-19 02:37:44 +0000557 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000558 do_twos_comp = 0;
559 }
560
561 if (little_endian) {
562 p = bytes;
563 pincr = 1;
564 }
565 else {
566 p = bytes + n - 1;
567 pincr = -1;
568 }
569
Tim Peters898cf852001-06-13 20:50:08 +0000570 /* Copy over all the Python digits.
571 It's crucial that every Python digit except for the MSD contribute
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000572 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000573 normalized. */
574 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000575 j = 0;
576 accum = 0;
577 accumbits = 0;
578 carry = do_twos_comp ? 1 : 0;
579 for (i = 0; i < ndigits; ++i) {
580 twodigits thisdigit = v->ob_digit[i];
581 if (do_twos_comp) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000582 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
583 carry = thisdigit >> PyLong_SHIFT;
584 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000585 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000586 /* Because we're going LSB to MSB, thisdigit is more
587 significant than what's already in accum, so needs to be
588 prepended to accum. */
589 accum |= thisdigit << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000590 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000591
Tim Petersede05092001-06-14 08:53:38 +0000592 /* The most-significant digit may be (probably is) at least
593 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000594 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000595 /* Count # of sign bits -- they needn't be stored,
596 * although for signed conversion we need later to
597 * make sure at least one sign bit gets stored.
598 * First shift conceptual sign bit to real sign bit.
599 */
600 stwodigits s = (stwodigits)(thisdigit <<
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000601 (8*sizeof(stwodigits) - PyLong_SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000602 unsigned int nsignbits = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000603 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000604 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000605 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000606 }
Tim Petersede05092001-06-14 08:53:38 +0000607 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000608 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000609
Tim Peters2a9b3672001-06-11 21:23:58 +0000610 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000611 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000612 if (j >= n)
613 goto Overflow;
614 ++j;
615 *p = (unsigned char)(accum & 0xff);
616 p += pincr;
617 accumbits -= 8;
618 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000619 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000620 }
621
622 /* Store the straggler (if any). */
623 assert(accumbits < 8);
624 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000625 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000626 if (j >= n)
627 goto Overflow;
628 ++j;
629 if (do_twos_comp) {
630 /* Fill leading bits of the byte with sign bits
631 (appropriately pretending that the long had an
632 infinite supply of sign bits). */
633 accum |= (~(twodigits)0) << accumbits;
634 }
635 *p = (unsigned char)(accum & 0xff);
636 p += pincr;
637 }
Tim Peters05607ad2001-06-13 21:01:27 +0000638 else if (j == n && n > 0 && is_signed) {
639 /* The main loop filled the byte array exactly, so the code
640 just above didn't get to ensure there's a sign bit, and the
641 loop below wouldn't add one either. Make sure a sign bit
642 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000643 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000644 int sign_bit_set = msb >= 0x80;
645 assert(accumbits == 0);
646 if (sign_bit_set == do_twos_comp)
647 return 0;
648 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000649 goto Overflow;
650 }
Tim Peters05607ad2001-06-13 21:01:27 +0000651
652 /* Fill remaining bytes with copies of the sign bit. */
653 {
654 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
655 for ( ; j < n; ++j, p += pincr)
656 *p = signbyte;
657 }
658
Tim Peters2a9b3672001-06-11 21:23:58 +0000659 return 0;
660
661Overflow:
662 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
663 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000664
Tim Peters2a9b3672001-06-11 21:23:58 +0000665}
666
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000667double
668_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
669{
670/* NBITS_WANTED should be > the number of bits in a double's precision,
671 but small enough so that 2**NBITS_WANTED is within the normal double
672 range. nbitsneeded is set to 1 less than that because the most-significant
673 Python digit contains at least 1 significant bit, but we don't want to
674 bother counting them (catering to the worst case cheaply).
675
676 57 is one more than VAX-D double precision; I (Tim) don't know of a double
677 format with more precision than that; it's 1 larger so that we add in at
678 least one round bit to stand in for the ignored least-significant bits.
679*/
680#define NBITS_WANTED 57
681 PyLongObject *v;
682 double x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000683 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000684 Py_ssize_t i;
685 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000686 int nbitsneeded;
687
688 if (vv == NULL || !PyLong_Check(vv)) {
689 PyErr_BadInternalCall();
690 return -1;
691 }
692 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000693 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000694 sign = 1;
695 if (i < 0) {
696 sign = -1;
697 i = -(i);
698 }
699 else if (i == 0) {
700 *exponent = 0;
701 return 0.0;
702 }
703 --i;
704 x = (double)v->ob_digit[i];
705 nbitsneeded = NBITS_WANTED - 1;
706 /* Invariant: i Python digits remain unaccounted for. */
707 while (i > 0 && nbitsneeded > 0) {
708 --i;
709 x = x * multiplier + (double)v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000710 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000711 }
712 /* There are i digits we didn't shift in. Pretending they're all
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000713 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000714 *exponent = i;
715 assert(x > 0.0);
716 return x * sign;
717#undef NBITS_WANTED
718}
719
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000720/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000721
722double
Tim Peters9f688bf2000-07-07 15:53:28 +0000723PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000724{
Thomas Wouters553489a2006-02-01 21:32:04 +0000725 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000726 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000727
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 if (vv == NULL || !PyLong_Check(vv)) {
729 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000730 return -1;
731 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000732 x = _PyLong_AsScaledDouble(vv, &e);
733 if (x == -1.0 && PyErr_Occurred())
734 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000735 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
736 set correctly after a successful _PyLong_AsScaledDouble() call */
737 assert(e >= 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000738 if (e > INT_MAX / PyLong_SHIFT)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000739 goto overflow;
740 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000741 x = ldexp(x, e * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000742 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000743 goto overflow;
744 return x;
745
746overflow:
747 PyErr_SetString(PyExc_OverflowError,
748 "long int too large to convert to float");
749 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000750}
751
Guido van Rossum78694d91998-09-18 14:14:13 +0000752/* Create a new long (or int) object from a C pointer */
753
754PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000755PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000756{
Tim Peters70128a12001-06-16 08:48:40 +0000757#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000758 if ((long)p < 0)
759 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000760 return PyInt_FromLong((long)p);
761#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000762
Tim Peters70128a12001-06-16 08:48:40 +0000763#ifndef HAVE_LONG_LONG
764# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
765#endif
766#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000767# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000768#endif
769 /* optimize null pointers */
770 if (p == NULL)
771 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000772 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000773
774#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000775}
776
777/* Get a C pointer from a long object (or an int object in some cases) */
778
779void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000780PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000781{
782 /* This function will allow int or long objects. If vv is neither,
783 then the PyLong_AsLong*() functions will raise the exception:
784 PyExc_SystemError, "bad argument to internal function"
785 */
Tim Peters70128a12001-06-16 08:48:40 +0000786#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000787 long x;
788
Tim Peters70128a12001-06-16 08:48:40 +0000789 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000790 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000791 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000792 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000793 else
794 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000795#else
Tim Peters70128a12001-06-16 08:48:40 +0000796
797#ifndef HAVE_LONG_LONG
798# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
799#endif
800#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000801# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000802#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000803 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000804
Tim Peters70128a12001-06-16 08:48:40 +0000805 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000806 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000807 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000808 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000809 else
810 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000811
812#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000813
814 if (x == -1 && PyErr_Occurred())
815 return NULL;
816 return (void *)x;
817}
818
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000819#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000820
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000821/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000822 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000823 */
824
Tim Peterscf37dfc2001-06-14 18:42:50 +0000825#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000826
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000827/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000828
829PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000830PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000831{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000832 PyLongObject *v;
833 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
834 int ndigits = 0;
835 int negative = 0;
836
837 if (ival < 0) {
838 ival = -ival;
839 negative = 1;
840 }
841
842 /* Count the number of Python digits.
843 We used to pick 5 ("big enough for anything"), but that's a
844 waste of time and space given that 5*15 = 75 bits are rarely
845 needed. */
846 t = (unsigned PY_LONG_LONG)ival;
847 while (t) {
848 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000849 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000850 }
851 v = _PyLong_New(ndigits);
852 if (v != NULL) {
853 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000854 Py_SIZE(v) = negative ? -ndigits : ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000855 t = (unsigned PY_LONG_LONG)ival;
856 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000857 *p++ = (digit)(t & PyLong_MASK);
858 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000859 }
860 }
861 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000862}
863
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000864/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000865
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000866PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000867PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000868{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000869 PyLongObject *v;
870 unsigned PY_LONG_LONG t;
871 int ndigits = 0;
872
873 /* Count the number of Python digits. */
874 t = (unsigned PY_LONG_LONG)ival;
875 while (t) {
876 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000877 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000878 }
879 v = _PyLong_New(ndigits);
880 if (v != NULL) {
881 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000882 Py_SIZE(v) = ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000883 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000884 *p++ = (digit)(ival & PyLong_MASK);
885 ival >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000886 }
887 }
888 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000889}
890
Martin v. Löwis18e16552006-02-15 17:27:45 +0000891/* Create a new long int object from a C Py_ssize_t. */
892
893PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000894PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000895{
896 Py_ssize_t bytes = ival;
897 int one = 1;
898 return _PyLong_FromByteArray(
899 (unsigned char *)&bytes,
Kristján Valur Jónssonf0303942007-05-03 20:27:03 +0000900 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901}
902
903/* Create a new long int object from a C size_t. */
904
905PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000906PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907{
908 size_t bytes = ival;
909 int one = 1;
910 return _PyLong_FromByteArray(
911 (unsigned char *)&bytes,
912 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
913}
914
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000915/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000916 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000917
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000918PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000919PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000920{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000921 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000922 int one = 1;
923 int res;
924
Tim Petersd38b1c72001-09-30 05:09:37 +0000925 if (vv == NULL) {
926 PyErr_BadInternalCall();
927 return -1;
928 }
929 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000930 PyNumberMethods *nb;
931 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000932 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000933 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000934 if ((nb = vv->ob_type->tp_as_number) == NULL ||
935 nb->nb_int == NULL) {
936 PyErr_SetString(PyExc_TypeError, "an integer is required");
937 return -1;
938 }
939 io = (*nb->nb_int) (vv);
940 if (io == NULL)
941 return -1;
942 if (PyInt_Check(io)) {
943 bytes = PyInt_AsLong(io);
944 Py_DECREF(io);
945 return bytes;
946 }
947 if (PyLong_Check(io)) {
948 bytes = PyLong_AsLongLong(io);
949 Py_DECREF(io);
950 return bytes;
951 }
952 Py_DECREF(io);
953 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000954 return -1;
955 }
956
Tim Petersd1a7da62001-06-13 00:35:57 +0000957 res = _PyLong_AsByteArray(
958 (PyLongObject *)vv, (unsigned char *)&bytes,
959 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000960
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000961 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000962 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000963 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000964 else
965 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000966}
967
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000968/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000969 Return -1 and set an error if overflow occurs. */
970
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000971unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000972PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000973{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000974 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000975 int one = 1;
976 int res;
977
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000978 if (vv == NULL || !PyLong_Check(vv)) {
979 PyErr_BadInternalCall();
Skip Montanaro429433b2006-04-18 00:35:43 +0000980 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000981 }
982
Tim Petersd1a7da62001-06-13 00:35:57 +0000983 res = _PyLong_AsByteArray(
984 (PyLongObject *)vv, (unsigned char *)&bytes,
985 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000986
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000987 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000988 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000989 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000990 else
991 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000992}
Tim Petersd1a7da62001-06-13 00:35:57 +0000993
Thomas Hellera4ea6032003-04-17 18:55:45 +0000994/* Get a C unsigned long int from a long int object, ignoring the high bits.
995 Returns -1 and sets an error condition if an error occurs. */
996
997unsigned PY_LONG_LONG
998PyLong_AsUnsignedLongLongMask(PyObject *vv)
999{
1000 register PyLongObject *v;
1001 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001002 Py_ssize_t i;
1003 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001004
1005 if (vv == NULL || !PyLong_Check(vv)) {
1006 PyErr_BadInternalCall();
1007 return (unsigned long) -1;
1008 }
1009 v = (PyLongObject *)vv;
1010 i = v->ob_size;
1011 sign = 1;
1012 x = 0;
1013 if (i < 0) {
1014 sign = -1;
1015 i = -i;
1016 }
1017 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001018 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001019 }
1020 return x * sign;
1021}
Tim Petersd1a7da62001-06-13 00:35:57 +00001022#undef IS_LITTLE_ENDIAN
1023
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001024#endif /* HAVE_LONG_LONG */
1025
Neil Schemenauerba872e22001-01-04 01:46:03 +00001026
1027static int
1028convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001029 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001030 *a = (PyLongObject *) v;
1031 Py_INCREF(v);
1032 }
1033 else if (PyInt_Check(v)) {
1034 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1035 }
1036 else {
1037 return 0;
1038 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001039 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001040 *b = (PyLongObject *) w;
1041 Py_INCREF(w);
1042 }
1043 else if (PyInt_Check(w)) {
1044 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1045 }
1046 else {
1047 Py_DECREF(*a);
1048 return 0;
1049 }
1050 return 1;
1051}
1052
1053#define CONVERT_BINOP(v, w, a, b) \
1054 if (!convert_binop(v, w, a, b)) { \
1055 Py_INCREF(Py_NotImplemented); \
1056 return Py_NotImplemented; \
1057 }
1058
Tim Peters877a2122002-08-12 05:09:36 +00001059/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1060 * is modified in place, by adding y to it. Carries are propagated as far as
1061 * x[m-1], and the remaining carry (0 or 1) is returned.
1062 */
1063static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001065{
1066 int i;
1067 digit carry = 0;
1068
1069 assert(m >= n);
1070 for (i = 0; i < n; ++i) {
1071 carry += x[i] + y[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001072 x[i] = carry & PyLong_MASK;
1073 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001074 assert((carry & 1) == carry);
1075 }
1076 for (; carry && i < m; ++i) {
1077 carry += x[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001078 x[i] = carry & PyLong_MASK;
1079 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001080 assert((carry & 1) == carry);
1081 }
1082 return carry;
1083}
1084
1085/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1086 * is modified in place, by subtracting y from it. Borrows are propagated as
1087 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1088 */
1089static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001090v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001091{
1092 int i;
1093 digit borrow = 0;
1094
1095 assert(m >= n);
1096 for (i = 0; i < n; ++i) {
1097 borrow = x[i] - y[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001098 x[i] = borrow & PyLong_MASK;
1099 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001100 borrow &= 1; /* keep only 1 sign bit */
1101 }
1102 for (; borrow && i < m; ++i) {
1103 borrow = x[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001104 x[i] = borrow & PyLong_MASK;
1105 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001106 borrow &= 1;
1107 }
1108 return borrow;
1109}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001110
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001111/* Multiply by a single digit, ignoring the sign. */
1112
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001114mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001115{
1116 return muladd1(a, n, (digit)0);
1117}
1118
1119/* Multiply by a single digit and add a single digit, ignoring the sign. */
1120
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001122muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001123{
Christian Heimese93237d2007-12-19 02:37:44 +00001124 Py_ssize_t size_a = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001126 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001128
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001129 if (z == NULL)
1130 return NULL;
1131 for (i = 0; i < size_a; ++i) {
1132 carry += (twodigits)a->ob_digit[i] * n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001133 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1134 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001135 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001136 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001137 return long_normalize(z);
1138}
1139
Tim Peters212e6142001-07-14 12:23:19 +00001140/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1141 in pout, and returning the remainder. pin and pout point at the LSD.
1142 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1143 long_format, but that should be done with great care since longs are
1144 immutable. */
1145
1146static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001147inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001148{
1149 twodigits rem = 0;
1150
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001151 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001152 pin += size;
1153 pout += size;
1154 while (--size >= 0) {
1155 digit hi;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001156 rem = (rem << PyLong_SHIFT) + *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001157 *--pout = hi = (digit)(rem / n);
1158 rem -= hi * n;
1159 }
1160 return (digit)rem;
1161}
1162
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001163/* Divide a long integer by a digit, returning both the quotient
1164 (as function result) and the remainder (through *prem).
1165 The sign of a is ignored; n should not be zero. */
1166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001168divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001169{
Christian Heimese93237d2007-12-19 02:37:44 +00001170 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001172
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001173 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001175 if (z == NULL)
1176 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001177 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178 return long_normalize(z);
1179}
1180
1181/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001182 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001183 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001186long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 register PyLongObject *a = (PyLongObject *)aa;
1189 PyStringObject *str;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001190 Py_ssize_t i, j, sz;
Neal Norwitzc09efa82006-07-23 07:53:14 +00001191 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001192 char *p;
1193 int bits;
1194 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001196 if (a == NULL || !PyLong_Check(a)) {
1197 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001198 return NULL;
1199 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001200 assert(base >= 2 && base <= 36);
Christian Heimese93237d2007-12-19 02:37:44 +00001201 size_a = ABS(Py_SIZE(a));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001202
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001203 /* Compute a rough upper bound for the length of the string */
1204 i = base;
1205 bits = 0;
1206 while (i > 1) {
1207 ++bits;
1208 i >>= 1;
1209 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001210 i = 5 + (addL ? 1 : 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001211 j = size_a*PyLong_SHIFT + bits-1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001212 sz = i + j / bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001213 if (j / PyLong_SHIFT < size_a || sz < i) {
Armin Rigo7ccbca92006-10-04 12:17:45 +00001214 PyErr_SetString(PyExc_OverflowError,
1215 "long is too large to format");
1216 return NULL;
1217 }
1218 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001219 if (str == NULL)
1220 return NULL;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001221 p = PyString_AS_STRING(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001222 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001223 if (addL)
1224 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001225 if (a->ob_size < 0)
1226 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001227
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001228 if (a->ob_size == 0) {
1229 *--p = '0';
1230 }
1231 else if ((base & (base - 1)) == 0) {
1232 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001233 twodigits accum = 0;
1234 int accumbits = 0; /* # of bits in accum */
1235 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001236 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001237 while ((i >>= 1) > 1)
1238 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001239
1240 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001241 accum |= (twodigits)a->ob_digit[i] << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001242 accumbits += PyLong_SHIFT;
Tim Peters586b2e32001-07-15 09:11:14 +00001243 assert(accumbits >= basebits);
1244 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001245 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001246 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001247 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001248 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001249 accumbits -= basebits;
1250 accum >>= basebits;
1251 } while (i < size_a-1 ? accumbits >= basebits :
1252 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001253 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001254 }
1255 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001256 /* Not 0, and base not a power of 2. Divide repeatedly by
1257 base, but for speed use the highest power of base that
1258 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001259 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001260 digit *pin = a->ob_digit;
1261 PyLongObject *scratch;
1262 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001263 digit powbase = base; /* powbase == base ** power */
1264 int power = 1;
1265 for (;;) {
1266 unsigned long newpow = powbase * (unsigned long)base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001267 if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */
Tim Petersfad225f2001-07-13 02:59:26 +00001268 break;
1269 powbase = (digit)newpow;
1270 ++power;
1271 }
Tim Peters212e6142001-07-14 12:23:19 +00001272
1273 /* Get a scratch area for repeated division. */
1274 scratch = _PyLong_New(size);
1275 if (scratch == NULL) {
1276 Py_DECREF(str);
1277 return NULL;
1278 }
1279
1280 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001281 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001282 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001283 digit rem = inplace_divrem1(scratch->ob_digit,
1284 pin, size, powbase);
1285 pin = scratch->ob_digit; /* no need to use a again */
1286 if (pin[size - 1] == 0)
1287 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001288 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001289 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001290 Py_DECREF(str);
1291 return NULL;
1292 })
Tim Peters212e6142001-07-14 12:23:19 +00001293
1294 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001295 assert(ntostore > 0);
1296 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001297 digit nextrem = (digit)(rem / base);
1298 char c = (char)(rem - nextrem * base);
1299 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001300 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001301 *--p = c;
1302 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001303 --ntostore;
1304 /* Termination is a bit delicate: must not
1305 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001306 remaining quotient and rem are both 0. */
1307 } while (ntostore && (size || rem));
1308 } while (size != 0);
1309 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001310 }
1311
Guido van Rossum2c475421992-08-14 15:13:07 +00001312 if (base == 8) {
1313 if (size_a != 0)
1314 *--p = '0';
1315 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001316 else if (base == 16) {
1317 *--p = 'x';
1318 *--p = '0';
1319 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001320 else if (base != 10) {
1321 *--p = '#';
1322 *--p = '0' + base%10;
1323 if (base > 10)
1324 *--p = '0' + base/10;
1325 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001326 if (sign)
1327 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001328 if (p != PyString_AS_STRING(str)) {
1329 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001330 assert(p > q);
1331 do {
1332 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001333 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 _PyString_Resize((PyObject **)&str,
Armin Rigo7ccbca92006-10-04 12:17:45 +00001335 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001336 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001338}
1339
Tim Petersda53afa2006-05-25 17:34:03 +00001340/* Table of digit values for 8-bit string -> integer conversion.
1341 * '0' maps to 0, ..., '9' maps to 9.
1342 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1343 * All other indices map to 37.
1344 * Note that when converting a base B string, a char c is a legitimate
1345 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1346 */
1347int _PyLong_DigitValue[256] = {
Tim Peters696cf432006-05-24 21:10:40 +00001348 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1349 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1350 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1351 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 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, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1355 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 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,
1362 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1363 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Tim Peters696cf432006-05-24 21:10:40 +00001364};
1365
1366/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001367 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1368 * non-digit (which may be *str!). A normalized long is returned.
1369 * The point to this routine is that it takes time linear in the number of
1370 * string characters.
1371 */
1372static PyLongObject *
1373long_from_binary_base(char **str, int base)
1374{
1375 char *p = *str;
1376 char *start = p;
1377 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001378 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001379 PyLongObject *z;
1380 twodigits accum;
1381 int bits_in_accum;
1382 digit *pdigit;
1383
1384 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1385 n = base;
1386 for (bits_per_char = -1; n; ++bits_per_char)
1387 n >>= 1;
1388 /* n <- total # of bits needed, while setting p to end-of-string */
1389 n = 0;
Tim Petersda53afa2006-05-25 17:34:03 +00001390 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001391 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001392 *str = p;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001393 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1394 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001395 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001396 PyErr_SetString(PyExc_ValueError,
1397 "long string too large to convert");
1398 return NULL;
1399 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001400 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001401 z = _PyLong_New(n);
1402 if (z == NULL)
1403 return NULL;
1404 /* Read string from right, and fill in long from left; i.e.,
1405 * from least to most significant in both.
1406 */
1407 accum = 0;
1408 bits_in_accum = 0;
1409 pdigit = z->ob_digit;
1410 while (--p >= start) {
Tim Petersda53afa2006-05-25 17:34:03 +00001411 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001412 assert(k >= 0 && k < base);
1413 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001414 bits_in_accum += bits_per_char;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001415 if (bits_in_accum >= PyLong_SHIFT) {
1416 *pdigit++ = (digit)(accum & PyLong_MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001417 assert(pdigit - z->ob_digit <= (int)n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001418 accum >>= PyLong_SHIFT;
1419 bits_in_accum -= PyLong_SHIFT;
1420 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001421 }
1422 }
1423 if (bits_in_accum) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001424 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001425 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001426 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001427 }
1428 while (pdigit - z->ob_digit < n)
1429 *pdigit++ = 0;
1430 return long_normalize(z);
1431}
1432
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001433PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001434PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001435{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001436 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001437 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 PyLongObject *z;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001439 PyObject *strobj, *strrepr;
1440 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001441
Guido van Rossum472c04f1996-12-05 21:57:21 +00001442 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001443 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001444 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001445 return NULL;
1446 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001447 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001448 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001449 if (*str == '+')
1450 ++str;
1451 else if (*str == '-') {
1452 ++str;
1453 sign = -1;
1454 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001455 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001456 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001457 if (base == 0) {
1458 if (str[0] != '0')
1459 base = 10;
1460 else if (str[1] == 'x' || str[1] == 'X')
1461 base = 16;
1462 else
1463 base = 8;
1464 }
1465 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1466 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001467
Guido van Rossume6762971998-06-22 03:54:46 +00001468 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001469 if ((base & (base - 1)) == 0)
1470 z = long_from_binary_base(&str, base);
1471 else {
Tim Peters696cf432006-05-24 21:10:40 +00001472/***
1473Binary bases can be converted in time linear in the number of digits, because
1474Python's representation base is binary. Other bases (including decimal!) use
1475the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001476
Tim Peters696cf432006-05-24 21:10:40 +00001477First some math: the largest integer that can be expressed in N base-B digits
1478is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1479case number of Python digits needed to hold it is the smallest integer n s.t.
1480
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001481 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1482 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
1483 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001484
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001485The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
Tim Peters696cf432006-05-24 21:10:40 +00001486this quickly. A Python long with that much space is reserved near the start,
1487and the result is computed into it.
1488
1489The input string is actually treated as being in base base**i (i.e., i digits
1490are processed at a time), where two more static arrays hold:
1491
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001492 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
Tim Peters696cf432006-05-24 21:10:40 +00001493 convmultmax_base[base] = base ** convwidth_base[base]
1494
1495The first of these is the largest i such that i consecutive input digits
1496must fit in a single Python digit. The second is effectively the input
1497base we're really using.
1498
1499Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1500convmultmax_base[base], the result is "simply"
1501
1502 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1503
1504where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001505
1506Error analysis: as above, the number of Python digits `n` needed is worst-
1507case
1508
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001509 n >= N * log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001510
1511where `N` is the number of input digits in base `B`. This is computed via
1512
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001513 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001514
1515below. Two numeric concerns are how much space this can waste, and whether
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001516the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
Tim Peters9faa3ed2006-05-30 15:53:34 +00001517which is the default (and it's unlikely anyone changes that).
1518
1519Waste isn't a problem: provided the first input digit isn't 0, the difference
1520between the worst-case input with N digits and the smallest input with N
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001521digits is about a factor of B, but B is small compared to PyLong_BASE so at most
Tim Peters9faa3ed2006-05-30 15:53:34 +00001522one allocated Python digit can remain unused on that count. If
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001523N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001524and adding 1 returns a result 1 larger than necessary. However, that can't
1525happen: whenever B is a power of 2, long_from_binary_base() is called
1526instead, and it's impossible for B**i to be an integer power of 2**15 when
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001527B is not a power of 2 (i.e., it's impossible for N*log(B)/log(PyLong_BASE) to be
Tim Peters9faa3ed2006-05-30 15:53:34 +00001528an exact integer when B is not a power of 2, since B**i has a prime factor
1529other than 2 in that case, but (2**15)**j's only prime factor is 2).
1530
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001531The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001532is a little bit larger than an exact integer, but due to roundoff errors (in
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001533computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001534yields a numeric result a little less than that integer. Unfortunately, "how
1535close can a transcendental function get to an integer over some range?"
1536questions are generally theoretically intractable. Computer analysis via
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001537continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
Tim Peters9faa3ed2006-05-30 15:53:34 +00001538fractions, giving a sequence i/j of "the best" rational approximations. Then
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001539j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001540we can get very close to being in trouble, but very rarely. For example,
154176573 is a denominator in one of the continued-fraction approximations to
1542log(10)/log(2**15), and indeed:
1543
1544 >>> log(10)/log(2**15)*76573
1545 16958.000000654003
1546
1547is very close to an integer. If we were working with IEEE single-precision,
1548rounding errors could kill us. Finding worst cases in IEEE double-precision
1549requires better-than-double-precision log() functions, and Tim didn't bother.
1550Instead the code checks to see whether the allocated space is enough as each
1551new Python digit is added, and copies the whole thing to a larger long if not.
1552This should happen extremely rarely, and in fact I don't have a test case
1553that triggers it(!). Instead the code was tested by artificially allocating
1554just 1 digit at the start, so that the copying code was exercised for every
1555digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001556***/
1557 register twodigits c; /* current input character */
1558 Py_ssize_t size_z;
1559 int i;
1560 int convwidth;
1561 twodigits convmultmax, convmult;
1562 digit *pz, *pzstop;
1563 char* scan;
1564
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001565 static double log_base_PyLong_BASE[37] = {0.0e0,};
Tim Peters696cf432006-05-24 21:10:40 +00001566 static int convwidth_base[37] = {0,};
1567 static twodigits convmultmax_base[37] = {0,};
1568
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001569 if (log_base_PyLong_BASE[base] == 0.0) {
Tim Peters696cf432006-05-24 21:10:40 +00001570 twodigits convmax = base;
1571 int i = 1;
1572
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001573 log_base_PyLong_BASE[base] = log((double)base) /
1574 log((double)PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001575 for (;;) {
1576 twodigits next = convmax * base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001577 if (next > PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001578 break;
1579 convmax = next;
1580 ++i;
1581 }
1582 convmultmax_base[base] = convmax;
1583 assert(i > 0);
1584 convwidth_base[base] = i;
1585 }
1586
1587 /* Find length of the string of numeric characters. */
1588 scan = str;
Tim Petersda53afa2006-05-25 17:34:03 +00001589 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001590 ++scan;
1591
1592 /* Create a long object that can contain the largest possible
1593 * integer with this base and length. Note that there's no
1594 * need to initialize z->ob_digit -- no slot is read up before
1595 * being stored into.
1596 */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001597 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001598 /* Uncomment next line to test exceedingly rare copy code */
1599 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001600 assert(size_z > 0);
1601 z = _PyLong_New(size_z);
1602 if (z == NULL)
1603 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00001604 Py_SIZE(z) = 0;
Tim Peters696cf432006-05-24 21:10:40 +00001605
1606 /* `convwidth` consecutive input digits are treated as a single
1607 * digit in base `convmultmax`.
1608 */
1609 convwidth = convwidth_base[base];
1610 convmultmax = convmultmax_base[base];
1611
1612 /* Work ;-) */
1613 while (str < scan) {
1614 /* grab up to convwidth digits from the input string */
Tim Petersda53afa2006-05-25 17:34:03 +00001615 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00001616 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1617 c = (twodigits)(c * base +
Tim Petersda53afa2006-05-25 17:34:03 +00001618 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001619 assert(c < PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001620 }
1621
1622 convmult = convmultmax;
1623 /* Calculate the shift only if we couldn't get
1624 * convwidth digits.
1625 */
1626 if (i != convwidth) {
1627 convmult = base;
1628 for ( ; i > 1; --i)
1629 convmult *= base;
1630 }
1631
1632 /* Multiply z by convmult, and add c. */
1633 pz = z->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001634 pzstop = pz + Py_SIZE(z);
Tim Peters696cf432006-05-24 21:10:40 +00001635 for (; pz < pzstop; ++pz) {
1636 c += (twodigits)*pz * convmult;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001637 *pz = (digit)(c & PyLong_MASK);
1638 c >>= PyLong_SHIFT;
Tim Peters696cf432006-05-24 21:10:40 +00001639 }
1640 /* carry off the current end? */
1641 if (c) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001642 assert(c < PyLong_BASE);
Christian Heimese93237d2007-12-19 02:37:44 +00001643 if (Py_SIZE(z) < size_z) {
Tim Peters9faa3ed2006-05-30 15:53:34 +00001644 *pz = (digit)c;
Christian Heimese93237d2007-12-19 02:37:44 +00001645 ++Py_SIZE(z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001646 }
1647 else {
1648 PyLongObject *tmp;
1649 /* Extremely rare. Get more space. */
Christian Heimese93237d2007-12-19 02:37:44 +00001650 assert(Py_SIZE(z) == size_z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001651 tmp = _PyLong_New(size_z + 1);
1652 if (tmp == NULL) {
1653 Py_DECREF(z);
1654 return NULL;
1655 }
1656 memcpy(tmp->ob_digit,
1657 z->ob_digit,
1658 sizeof(digit) * size_z);
1659 Py_DECREF(z);
1660 z = tmp;
1661 z->ob_digit[size_z] = (digit)c;
1662 ++size_z;
1663 }
Tim Peters696cf432006-05-24 21:10:40 +00001664 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001665 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001666 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001667 if (z == NULL)
1668 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001669 if (str == start)
1670 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00001671 if (sign < 0)
Christian Heimese93237d2007-12-19 02:37:44 +00001672 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001673 if (*str == 'L' || *str == 'l')
1674 str++;
1675 while (*str && isspace(Py_CHARMASK(*str)))
1676 str++;
1677 if (*str != '\0')
1678 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001679 if (pend)
1680 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001681 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001682
1683 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001684 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001685 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1686 strobj = PyString_FromStringAndSize(orig_str, slen);
1687 if (strobj == NULL)
1688 return NULL;
1689 strrepr = PyObject_Repr(strobj);
1690 Py_DECREF(strobj);
1691 if (strrepr == NULL)
1692 return NULL;
1693 PyErr_Format(PyExc_ValueError,
1694 "invalid literal for long() with base %d: %s",
1695 base, PyString_AS_STRING(strrepr));
1696 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001697 return NULL;
1698}
1699
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001700#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001701PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001702PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001703{
Walter Dörwald07e14762002-11-06 16:15:14 +00001704 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001705 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001706
Walter Dörwald07e14762002-11-06 16:15:14 +00001707 if (buffer == NULL)
1708 return NULL;
1709
1710 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1711 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001712 return NULL;
1713 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001714 result = PyLong_FromString(buffer, NULL, base);
1715 PyMem_FREE(buffer);
1716 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001717}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001718#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001719
Tim Peters9f688bf2000-07-07 15:53:28 +00001720/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001721static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001722 (PyLongObject *, PyLongObject *, PyLongObject **);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001723static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00001724static int long_divrem(PyLongObject *, PyLongObject *,
1725 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001726
1727/* Long division with remainder, top-level routine */
1728
Guido van Rossume32e0141992-01-19 16:31:05 +00001729static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001730long_divrem(PyLongObject *a, PyLongObject *b,
1731 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001732{
Christian Heimese93237d2007-12-19 02:37:44 +00001733 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001734 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001735
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001737 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001738 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001739 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001740 }
1741 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001742 (size_a == size_b &&
1743 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001744 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001745 *pdiv = _PyLong_New(0);
Georg Brandlc02e1312007-04-11 17:16:24 +00001746 if (*pdiv == NULL)
1747 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001748 Py_INCREF(a);
1749 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001750 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001751 }
1752 if (size_b == 1) {
1753 digit rem = 0;
1754 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001755 if (z == NULL)
1756 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandlc02e1312007-04-11 17:16:24 +00001758 if (*prem == NULL) {
1759 Py_DECREF(z);
1760 return -1;
1761 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001762 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001763 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001764 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001765 if (z == NULL)
1766 return -1;
1767 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001768 /* Set the signs.
1769 The quotient z has the sign of a*b;
1770 the remainder r has the sign of a,
1771 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001772 if ((a->ob_size < 0) != (b->ob_size < 0))
1773 z->ob_size = -(z->ob_size);
1774 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1775 (*prem)->ob_size = -((*prem)->ob_size);
1776 *pdiv = z;
1777 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001778}
1779
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001780/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001781
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001782static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001783x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001784{
Christian Heimese93237d2007-12-19 02:37:44 +00001785 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001786 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001787 PyLongObject *v = mul1(v1, d);
1788 PyLongObject *w = mul1(w1, d);
1789 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001790 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001791
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001792 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001793 Py_XDECREF(v);
1794 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001795 return NULL;
1796 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001797
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001798 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimese93237d2007-12-19 02:37:44 +00001799 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
1800 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001801
Christian Heimese93237d2007-12-19 02:37:44 +00001802 size_v = ABS(Py_SIZE(v));
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001803 k = size_v - size_w;
1804 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001805
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001806 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001807 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1808 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001809 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001810 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001811
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001812 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001813 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001814 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001816 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001817 if (vj == w->ob_digit[size_w-1])
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001818 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001819 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001820 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001821 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001822
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001823 while (w->ob_digit[size_w-2]*q >
1824 ((
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001825 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001826 + v->ob_digit[j-1]
1827 - q*w->ob_digit[size_w-1]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001828 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001829 + v->ob_digit[j-2])
1830 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001831
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001832 for (i = 0; i < size_w && i+k < size_v; ++i) {
1833 twodigits z = w->ob_digit[i] * q;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001834 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001835 carry += v->ob_digit[i+k] - z
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001836 + ((twodigits)zz << PyLong_SHIFT);
1837 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1838 carry = Py_ARITHMETIC_RIGHT_SHIFT(PyLong_BASE_TWODIGITS_TYPE,
1839 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00001840 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001841 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001842
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001843 if (i+k < size_v) {
1844 carry += v->ob_digit[i+k];
1845 v->ob_digit[i+k] = 0;
1846 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001847
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001848 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001849 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001850 else {
1851 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001852 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001853 carry = 0;
1854 for (i = 0; i < size_w && i+k < size_v; ++i) {
1855 carry += v->ob_digit[i+k] + w->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001856 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001857 carry = Py_ARITHMETIC_RIGHT_SHIFT(
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001858 PyLong_BASE_TWODIGITS_TYPE,
1859 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001860 }
1861 }
1862 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001863
Guido van Rossumc206c761995-01-10 15:23:19 +00001864 if (a == NULL)
1865 *prem = NULL;
1866 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001867 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001868 *prem = divrem1(v, d, &d);
1869 /* d receives the (unused) remainder */
1870 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001871 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001872 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001873 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001874 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875 Py_DECREF(v);
1876 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001877 return a;
1878}
1879
1880/* Methods */
1881
1882static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001883long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001884{
Christian Heimese93237d2007-12-19 02:37:44 +00001885 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001886}
1887
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001888static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001889long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001890{
Fred Drake121ee271999-12-23 15:41:28 +00001891 return long_format(v, 10, 1);
1892}
1893
1894static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001895long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001896{
1897 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001898}
1899
1900static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001901long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001902{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001903 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001904
Christian Heimese93237d2007-12-19 02:37:44 +00001905 if (Py_SIZE(a) != Py_SIZE(b)) {
1906 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001907 sign = 0;
1908 else
Christian Heimese93237d2007-12-19 02:37:44 +00001909 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001910 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001911 else {
Christian Heimese93237d2007-12-19 02:37:44 +00001912 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001913 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1914 ;
1915 if (i < 0)
1916 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001917 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001918 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimese93237d2007-12-19 02:37:44 +00001919 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001920 sign = -sign;
1921 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001922 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001923 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001924}
1925
Guido van Rossum9bfef441993-03-29 10:43:31 +00001926static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001927long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001928{
1929 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001930 Py_ssize_t i;
1931 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001932
1933 /* This is designed so that Python ints and longs with the
1934 same value hash to the same value, otherwise comparisons
1935 of mapping keys will turn out weird */
1936 i = v->ob_size;
1937 sign = 1;
1938 x = 0;
1939 if (i < 0) {
1940 sign = -1;
1941 i = -(i);
1942 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001943#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Facundo Batistad544df72007-09-19 15:10:06 +00001944 /* The following loop produces a C long x such that (unsigned long)x
1945 is congruent to the absolute value of v modulo ULONG_MAX. The
1946 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00001947 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001948 /* Force a native long #-bits (32 or 64) circular shift */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001949 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001950 x += v->ob_digit[i];
Facundo Batistad544df72007-09-19 15:10:06 +00001951 /* If the addition above overflowed (thinking of x as
1952 unsigned), we compensate by incrementing. This preserves
1953 the value modulo ULONG_MAX. */
1954 if ((unsigned long)x < v->ob_digit[i])
1955 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001956 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001957#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001958 x = x * sign;
1959 if (x == -1)
1960 x = -2;
1961 return x;
1962}
1963
1964
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001965/* Add the absolute values of two long integers. */
1966
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001967static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001968x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001969{
Christian Heimese93237d2007-12-19 02:37:44 +00001970 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001971 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001972 int i;
1973 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001974
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001975 /* Ensure a is the larger of the two: */
1976 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001977 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001978 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001979 size_a = size_b;
1980 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001981 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001982 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001983 if (z == NULL)
1984 return NULL;
1985 for (i = 0; i < size_b; ++i) {
1986 carry += a->ob_digit[i] + b->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001987 z->ob_digit[i] = carry & PyLong_MASK;
1988 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001989 }
1990 for (; i < size_a; ++i) {
1991 carry += a->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001992 z->ob_digit[i] = carry & PyLong_MASK;
1993 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001994 }
1995 z->ob_digit[i] = carry;
1996 return long_normalize(z);
1997}
1998
1999/* Subtract the absolute values of two integers. */
2000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002001static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002002x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002003{
Christian Heimese93237d2007-12-19 02:37:44 +00002004 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002006 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002007 int sign = 1;
2008 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002009
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002010 /* Ensure a is the larger of the two: */
2011 if (size_a < size_b) {
2012 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002013 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002014 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002015 size_a = size_b;
2016 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002017 }
2018 else if (size_a == size_b) {
2019 /* Find highest digit where a and b differ: */
2020 i = size_a;
2021 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2022 ;
2023 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002024 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002025 if (a->ob_digit[i] < b->ob_digit[i]) {
2026 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002027 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002028 }
2029 size_a = size_b = i+1;
2030 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002031 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002032 if (z == NULL)
2033 return NULL;
2034 for (i = 0; i < size_b; ++i) {
2035 /* The following assumes unsigned arithmetic
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002036 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002037 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002038 z->ob_digit[i] = borrow & PyLong_MASK;
2039 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002040 borrow &= 1; /* Keep only one sign bit */
2041 }
2042 for (; i < size_a; ++i) {
2043 borrow = a->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002044 z->ob_digit[i] = borrow & PyLong_MASK;
2045 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002046 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002047 }
2048 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002049 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002050 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002051 return long_normalize(z);
2052}
2053
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002054static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002055long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002056{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002057 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002058
Neil Schemenauerba872e22001-01-04 01:46:03 +00002059 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2060
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002061 if (a->ob_size < 0) {
2062 if (b->ob_size < 0) {
2063 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002064 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002065 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002066 }
2067 else
2068 z = x_sub(b, a);
2069 }
2070 else {
2071 if (b->ob_size < 0)
2072 z = x_sub(a, b);
2073 else
2074 z = x_add(a, b);
2075 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002076 Py_DECREF(a);
2077 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002078 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002079}
2080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002081static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002082long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002083{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002084 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002085
Neil Schemenauerba872e22001-01-04 01:46:03 +00002086 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2087
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002088 if (a->ob_size < 0) {
2089 if (b->ob_size < 0)
2090 z = x_sub(a, b);
2091 else
2092 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002093 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002094 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002095 }
2096 else {
2097 if (b->ob_size < 0)
2098 z = x_add(a, b);
2099 else
2100 z = x_sub(a, b);
2101 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002102 Py_DECREF(a);
2103 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002104 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002105}
2106
Tim Peters5af4e6c2002-08-12 02:31:19 +00002107/* Grade school multiplication, ignoring the signs.
2108 * Returns the absolute value of the product, or NULL if error.
2109 */
2110static PyLongObject *
2111x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002112{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002113 PyLongObject *z;
Christian Heimese93237d2007-12-19 02:37:44 +00002114 Py_ssize_t size_a = ABS(Py_SIZE(a));
2115 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002116 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002117
Tim Peters5af4e6c2002-08-12 02:31:19 +00002118 z = _PyLong_New(size_a + size_b);
2119 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002120 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002121
Christian Heimese93237d2007-12-19 02:37:44 +00002122 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002123 if (a == b) {
2124 /* Efficient squaring per HAC, Algorithm 14.16:
2125 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2126 * Gives slightly less than a 2x speedup when a == b,
2127 * via exploiting that each entry in the multiplication
2128 * pyramid appears twice (except for the size_a squares).
2129 */
2130 for (i = 0; i < size_a; ++i) {
2131 twodigits carry;
2132 twodigits f = a->ob_digit[i];
2133 digit *pz = z->ob_digit + (i << 1);
2134 digit *pa = a->ob_digit + i + 1;
2135 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002136
Tim Peters0973b992004-08-29 22:16:50 +00002137 SIGCHECK({
2138 Py_DECREF(z);
2139 return NULL;
2140 })
2141
2142 carry = *pz + f * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002143 *pz++ = (digit)(carry & PyLong_MASK);
2144 carry >>= PyLong_SHIFT;
2145 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002146
2147 /* Now f is added in twice in each column of the
2148 * pyramid it appears. Same as adding f<<1 once.
2149 */
2150 f <<= 1;
2151 while (pa < paend) {
2152 carry += *pz + *pa++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002153 *pz++ = (digit)(carry & PyLong_MASK);
2154 carry >>= PyLong_SHIFT;
2155 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002156 }
2157 if (carry) {
2158 carry += *pz;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002159 *pz++ = (digit)(carry & PyLong_MASK);
2160 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002161 }
2162 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002163 *pz += (digit)(carry & PyLong_MASK);
2164 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002165 }
Tim Peters0973b992004-08-29 22:16:50 +00002166 }
2167 else { /* a is not the same as b -- gradeschool long mult */
2168 for (i = 0; i < size_a; ++i) {
2169 twodigits carry = 0;
2170 twodigits f = a->ob_digit[i];
2171 digit *pz = z->ob_digit + i;
2172 digit *pb = b->ob_digit;
2173 digit *pbend = b->ob_digit + size_b;
2174
2175 SIGCHECK({
2176 Py_DECREF(z);
2177 return NULL;
2178 })
2179
2180 while (pb < pbend) {
2181 carry += *pz + *pb++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002182 *pz++ = (digit)(carry & PyLong_MASK);
2183 carry >>= PyLong_SHIFT;
2184 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002185 }
2186 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002187 *pz += (digit)(carry & PyLong_MASK);
2188 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002189 }
2190 }
Tim Peters44121a62002-08-12 06:17:58 +00002191 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002192}
2193
2194/* A helper for Karatsuba multiplication (k_mul).
2195 Takes a long "n" and an integer "size" representing the place to
2196 split, and sets low and high such that abs(n) == (high << size) + low,
2197 viewing the shift as being by digits. The sign bit is ignored, and
2198 the return values are >= 0.
2199 Returns 0 on success, -1 on failure.
2200*/
2201static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002202kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002203{
2204 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002205 Py_ssize_t size_lo, size_hi;
Christian Heimese93237d2007-12-19 02:37:44 +00002206 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002207
2208 size_lo = MIN(size_n, size);
2209 size_hi = size_n - size_lo;
2210
2211 if ((hi = _PyLong_New(size_hi)) == NULL)
2212 return -1;
2213 if ((lo = _PyLong_New(size_lo)) == NULL) {
2214 Py_DECREF(hi);
2215 return -1;
2216 }
2217
2218 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2219 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2220
2221 *high = long_normalize(hi);
2222 *low = long_normalize(lo);
2223 return 0;
2224}
2225
Tim Peters60004642002-08-12 22:01:34 +00002226static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2227
Tim Peters5af4e6c2002-08-12 02:31:19 +00002228/* Karatsuba multiplication. Ignores the input signs, and returns the
2229 * absolute value of the product (or NULL if error).
2230 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2231 */
2232static PyLongObject *
2233k_mul(PyLongObject *a, PyLongObject *b)
2234{
Christian Heimese93237d2007-12-19 02:37:44 +00002235 Py_ssize_t asize = ABS(Py_SIZE(a));
2236 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002237 PyLongObject *ah = NULL;
2238 PyLongObject *al = NULL;
2239 PyLongObject *bh = NULL;
2240 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002241 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002242 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002243 Py_ssize_t shift; /* the number of digits we split off */
2244 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002245
Tim Peters5af4e6c2002-08-12 02:31:19 +00002246 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2247 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2248 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002249 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002250 * By picking X to be a power of 2, "*X" is just shifting, and it's
2251 * been reduced to 3 multiplies on numbers half the size.
2252 */
2253
Tim Petersfc07e562002-08-12 02:54:10 +00002254 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002255 * is largest.
2256 */
Tim Peters738eda72002-08-12 15:08:20 +00002257 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002258 t1 = a;
2259 a = b;
2260 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002261
2262 i = asize;
2263 asize = bsize;
2264 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002265 }
2266
2267 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002268 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2269 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002270 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002271 return _PyLong_New(0);
2272 else
2273 return x_mul(a, b);
2274 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002275
Tim Peters60004642002-08-12 22:01:34 +00002276 /* If a is small compared to b, splitting on b gives a degenerate
2277 * case with ah==0, and Karatsuba may be (even much) less efficient
2278 * than "grade school" then. However, we can still win, by viewing
2279 * b as a string of "big digits", each of width a->ob_size. That
2280 * leads to a sequence of balanced calls to k_mul.
2281 */
2282 if (2 * asize <= bsize)
2283 return k_lopsided_mul(a, b);
2284
Tim Petersd6974a52002-08-13 20:37:51 +00002285 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002286 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002287 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002288 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002289
Tim Peters0973b992004-08-29 22:16:50 +00002290 if (a == b) {
2291 bh = ah;
2292 bl = al;
2293 Py_INCREF(bh);
2294 Py_INCREF(bl);
2295 }
2296 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002297
Tim Petersd64c1de2002-08-12 17:36:03 +00002298 /* The plan:
2299 * 1. Allocate result space (asize + bsize digits: that's always
2300 * enough).
2301 * 2. Compute ah*bh, and copy into result at 2*shift.
2302 * 3. Compute al*bl, and copy into result at 0. Note that this
2303 * can't overlap with #2.
2304 * 4. Subtract al*bl from the result, starting at shift. This may
2305 * underflow (borrow out of the high digit), but we don't care:
2306 * we're effectively doing unsigned arithmetic mod
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002307 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
Tim Petersd64c1de2002-08-12 17:36:03 +00002308 * borrows and carries out of the high digit can be ignored.
2309 * 5. Subtract ah*bh from the result, starting at shift.
2310 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2311 * at shift.
2312 */
2313
2314 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002315 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002316 if (ret == NULL) goto fail;
2317#ifdef Py_DEBUG
2318 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002319 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002320#endif
Tim Peters44121a62002-08-12 06:17:58 +00002321
Tim Petersd64c1de2002-08-12 17:36:03 +00002322 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002323 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002324 assert(Py_SIZE(t1) >= 0);
2325 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002326 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimese93237d2007-12-19 02:37:44 +00002327 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002328
2329 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimese93237d2007-12-19 02:37:44 +00002330 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002331 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002332 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002333 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002334
Tim Petersd64c1de2002-08-12 17:36:03 +00002335 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002336 if ((t2 = k_mul(al, bl)) == NULL) {
2337 Py_DECREF(t1);
2338 goto fail;
2339 }
Christian Heimese93237d2007-12-19 02:37:44 +00002340 assert(Py_SIZE(t2) >= 0);
2341 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2342 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002343
2344 /* Zero out remaining digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002345 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002346 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002347 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002348
Tim Petersd64c1de2002-08-12 17:36:03 +00002349 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2350 * because it's fresher in cache.
2351 */
Christian Heimese93237d2007-12-19 02:37:44 +00002352 i = Py_SIZE(ret) - shift; /* # digits after shift */
2353 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002354 Py_DECREF(t2);
2355
Christian Heimese93237d2007-12-19 02:37:44 +00002356 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002357 Py_DECREF(t1);
2358
Tim Petersd64c1de2002-08-12 17:36:03 +00002359 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002360 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2361 Py_DECREF(ah);
2362 Py_DECREF(al);
2363 ah = al = NULL;
2364
Tim Peters0973b992004-08-29 22:16:50 +00002365 if (a == b) {
2366 t2 = t1;
2367 Py_INCREF(t2);
2368 }
2369 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002370 Py_DECREF(t1);
2371 goto fail;
2372 }
2373 Py_DECREF(bh);
2374 Py_DECREF(bl);
2375 bh = bl = NULL;
2376
Tim Peters738eda72002-08-12 15:08:20 +00002377 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002378 Py_DECREF(t1);
2379 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002380 if (t3 == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002381 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002382
Tim Petersd6974a52002-08-13 20:37:51 +00002383 /* Add t3. It's not obvious why we can't run out of room here.
2384 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002385 */
Christian Heimese93237d2007-12-19 02:37:44 +00002386 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002387 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002388
Tim Peters5af4e6c2002-08-12 02:31:19 +00002389 return long_normalize(ret);
2390
2391 fail:
2392 Py_XDECREF(ret);
2393 Py_XDECREF(ah);
2394 Py_XDECREF(al);
2395 Py_XDECREF(bh);
2396 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002397 return NULL;
2398}
2399
Tim Petersd6974a52002-08-13 20:37:51 +00002400/* (*) Why adding t3 can't "run out of room" above.
2401
Tim Petersab86c2b2002-08-15 20:06:00 +00002402Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2403to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002404
Tim Petersab86c2b2002-08-15 20:06:00 +000024051. For any integer i, i = c(i/2) + f(i/2). In particular,
2406 bsize = c(bsize/2) + f(bsize/2).
24072. shift = f(bsize/2)
24083. asize <= bsize
24094. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2410 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002411
Tim Petersab86c2b2002-08-15 20:06:00 +00002412We allocated asize + bsize result digits, and add t3 into them at an offset
2413of shift. This leaves asize+bsize-shift allocated digit positions for t3
2414to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2415asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002416
Tim Petersab86c2b2002-08-15 20:06:00 +00002417bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2418at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002419
Tim Petersab86c2b2002-08-15 20:06:00 +00002420If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2421digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2422most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002423
Tim Petersab86c2b2002-08-15 20:06:00 +00002424The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002425
Tim Petersab86c2b2002-08-15 20:06:00 +00002426 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002427
Tim Petersab86c2b2002-08-15 20:06:00 +00002428and we have asize + c(bsize/2) available digit positions. We need to show
2429this is always enough. An instance of c(bsize/2) cancels out in both, so
2430the question reduces to whether asize digits is enough to hold
2431(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2432then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2433asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002434digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002435asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002436c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2437is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2438bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002439
Tim Peters48d52c02002-08-14 17:07:32 +00002440Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2441clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2442ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002443*/
2444
Tim Peters60004642002-08-12 22:01:34 +00002445/* b has at least twice the digits of a, and a is big enough that Karatsuba
2446 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2447 * of slices, each with a->ob_size digits, and multiply the slices by a,
2448 * one at a time. This gives k_mul balanced inputs to work with, and is
2449 * also cache-friendly (we compute one double-width slice of the result
2450 * at a time, then move on, never bactracking except for the helpful
2451 * single-width slice overlap between successive partial sums).
2452 */
2453static PyLongObject *
2454k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2455{
Christian Heimese93237d2007-12-19 02:37:44 +00002456 const Py_ssize_t asize = ABS(Py_SIZE(a));
2457 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002458 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002459 PyLongObject *ret;
2460 PyLongObject *bslice = NULL;
2461
2462 assert(asize > KARATSUBA_CUTOFF);
2463 assert(2 * asize <= bsize);
2464
2465 /* Allocate result space, and zero it out. */
2466 ret = _PyLong_New(asize + bsize);
2467 if (ret == NULL)
2468 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002469 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002470
2471 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002472 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002473 if (bslice == NULL)
2474 goto fail;
2475
2476 nbdone = 0;
2477 while (bsize > 0) {
2478 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002479 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002480
2481 /* Multiply the next slice of b by a. */
2482 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2483 nbtouse * sizeof(digit));
Christian Heimese93237d2007-12-19 02:37:44 +00002484 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002485 product = k_mul(a, bslice);
2486 if (product == NULL)
2487 goto fail;
2488
2489 /* Add into result. */
Christian Heimese93237d2007-12-19 02:37:44 +00002490 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2491 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002492 Py_DECREF(product);
2493
2494 bsize -= nbtouse;
2495 nbdone += nbtouse;
2496 }
2497
2498 Py_DECREF(bslice);
2499 return long_normalize(ret);
2500
2501 fail:
2502 Py_DECREF(ret);
2503 Py_XDECREF(bslice);
2504 return NULL;
2505}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002506
2507static PyObject *
2508long_mul(PyLongObject *v, PyLongObject *w)
2509{
2510 PyLongObject *a, *b, *z;
2511
2512 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002513 Py_INCREF(Py_NotImplemented);
2514 return Py_NotImplemented;
2515 }
2516
Tim Petersd64c1de2002-08-12 17:36:03 +00002517 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002518 /* Negate if exactly one of the inputs is negative. */
2519 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002520 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002521 Py_DECREF(a);
2522 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002523 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002524}
2525
Guido van Rossume32e0141992-01-19 16:31:05 +00002526/* The / and % operators are now defined in terms of divmod().
2527 The expression a mod b has the value a - b*floor(a/b).
2528 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002529 |a| by |b|, with the sign of a. This is also expressed
2530 as a - b*trunc(a/b), if trunc truncates towards zero.
2531 Some examples:
2532 a b a rem b a mod b
2533 13 10 3 3
2534 -13 10 -3 7
2535 13 -10 3 -7
2536 -13 -10 -3 -3
2537 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002538 have different signs. We then subtract one from the 'div'
2539 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002540
Tim Peters47e52ee2004-08-30 02:44:38 +00002541/* Compute
2542 * *pdiv, *pmod = divmod(v, w)
2543 * NULL can be passed for pdiv or pmod, in which case that part of
2544 * the result is simply thrown away. The caller owns a reference to
2545 * each of these it requests (does not pass NULL for).
2546 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002547static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002548l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002549 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002550{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002551 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002552
Guido van Rossume32e0141992-01-19 16:31:05 +00002553 if (long_divrem(v, w, &div, &mod) < 0)
2554 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00002555 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2556 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002557 PyLongObject *temp;
2558 PyLongObject *one;
2559 temp = (PyLongObject *) long_add(mod, w);
2560 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002561 mod = temp;
2562 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002563 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002564 return -1;
2565 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002566 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002567 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002568 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2569 Py_DECREF(mod);
2570 Py_DECREF(div);
2571 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002572 return -1;
2573 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002574 Py_DECREF(one);
2575 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002576 div = temp;
2577 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002578 if (pdiv != NULL)
2579 *pdiv = div;
2580 else
2581 Py_DECREF(div);
2582
2583 if (pmod != NULL)
2584 *pmod = mod;
2585 else
2586 Py_DECREF(mod);
2587
Guido van Rossume32e0141992-01-19 16:31:05 +00002588 return 0;
2589}
2590
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002591static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002592long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002593{
Tim Peters47e52ee2004-08-30 02:44:38 +00002594 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002595
2596 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002597 if (l_divmod(a, b, &div, NULL) < 0)
2598 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002599 Py_DECREF(a);
2600 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002601 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002602}
2603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002605long_classic_div(PyObject *v, PyObject *w)
2606{
Tim Peters47e52ee2004-08-30 02:44:38 +00002607 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002608
2609 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002610 if (Py_DivisionWarningFlag &&
2611 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2612 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002613 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002614 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002615 Py_DECREF(a);
2616 Py_DECREF(b);
2617 return (PyObject *)div;
2618}
2619
2620static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002621long_true_divide(PyObject *v, PyObject *w)
2622{
Tim Peterse2a60002001-09-04 06:17:36 +00002623 PyLongObject *a, *b;
2624 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002625 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002626
2627 CONVERT_BINOP(v, w, &a, &b);
2628 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2629 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002630 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2631 Py_DECREF(a);
2632 Py_DECREF(b);
2633 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002634 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002635 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2636 but should really be set correctly after sucessful calls to
2637 _PyLong_AsScaledDouble() */
2638 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002639
2640 if (bd == 0.0) {
2641 PyErr_SetString(PyExc_ZeroDivisionError,
2642 "long division or modulo by zero");
2643 return NULL;
2644 }
2645
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002646 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002647 ad /= bd; /* overflow/underflow impossible here */
2648 aexp -= bexp;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002649 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002650 goto overflow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002651 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002652 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002653 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002654 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002655 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002656 goto overflow;
2657 return PyFloat_FromDouble(ad);
2658
2659overflow:
2660 PyErr_SetString(PyExc_OverflowError,
2661 "long/long too large for a float");
2662 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002663
Tim Peters20dab9f2001-09-04 05:31:47 +00002664}
2665
2666static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002667long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002668{
Tim Peters47e52ee2004-08-30 02:44:38 +00002669 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002670
2671 CONVERT_BINOP(v, w, &a, &b);
2672
Tim Peters47e52ee2004-08-30 02:44:38 +00002673 if (l_divmod(a, b, NULL, &mod) < 0)
2674 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002675 Py_DECREF(a);
2676 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002677 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002678}
2679
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002680static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002681long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002682{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002683 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002684 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002685
2686 CONVERT_BINOP(v, w, &a, &b);
2687
2688 if (l_divmod(a, b, &div, &mod) < 0) {
2689 Py_DECREF(a);
2690 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002691 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002692 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002693 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002694 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002695 PyTuple_SetItem(z, 0, (PyObject *) div);
2696 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002697 }
2698 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002699 Py_DECREF(div);
2700 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002701 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002702 Py_DECREF(a);
2703 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002704 return z;
2705}
2706
Tim Peters47e52ee2004-08-30 02:44:38 +00002707/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002708static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002709long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002710{
Tim Peters47e52ee2004-08-30 02:44:38 +00002711 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2712 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002713
Tim Peters47e52ee2004-08-30 02:44:38 +00002714 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002715 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002716 PyLongObject *temp = NULL;
2717
2718 /* 5-ary values. If the exponent is large enough, table is
2719 * precomputed so that table[i] == a**i % c for i in range(32).
2720 */
2721 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2722 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2723
2724 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002725 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002726 if (PyLong_Check(x)) {
2727 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002728 Py_INCREF(x);
2729 }
Tim Petersde7990b2005-07-17 23:45:23 +00002730 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002731 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002732 if (c == NULL)
2733 goto Error;
2734 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002735 else if (x == Py_None)
2736 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002737 else {
2738 Py_DECREF(a);
2739 Py_DECREF(b);
2740 Py_INCREF(Py_NotImplemented);
2741 return Py_NotImplemented;
2742 }
Tim Peters4c483c42001-09-05 06:24:58 +00002743
Christian Heimese93237d2007-12-19 02:37:44 +00002744 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00002745 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002746 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002747 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002748 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002749 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002750 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002751 /* else return a float. This works because we know
2752 that this calls float_pow() which converts its
2753 arguments to double. */
2754 Py_DECREF(a);
2755 Py_DECREF(b);
2756 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002757 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002758 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002759
2760 if (c) {
2761 /* if modulus == 0:
2762 raise ValueError() */
Christian Heimese93237d2007-12-19 02:37:44 +00002763 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002764 PyErr_SetString(PyExc_ValueError,
2765 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002766 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002767 }
2768
2769 /* if modulus < 0:
2770 negativeOutput = True
2771 modulus = -modulus */
Christian Heimese93237d2007-12-19 02:37:44 +00002772 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002773 negativeOutput = 1;
2774 temp = (PyLongObject *)_PyLong_Copy(c);
2775 if (temp == NULL)
2776 goto Error;
2777 Py_DECREF(c);
2778 c = temp;
2779 temp = NULL;
2780 c->ob_size = - c->ob_size;
2781 }
2782
2783 /* if modulus == 1:
2784 return 0 */
Christian Heimese93237d2007-12-19 02:37:44 +00002785 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002786 z = (PyLongObject *)PyLong_FromLong(0L);
2787 goto Done;
2788 }
2789
2790 /* if base < 0:
2791 base = base % modulus
2792 Having the base positive just makes things easier. */
Christian Heimese93237d2007-12-19 02:37:44 +00002793 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002794 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002795 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002796 Py_DECREF(a);
2797 a = temp;
2798 temp = NULL;
2799 }
2800 }
2801
2802 /* At this point a, b, and c are guaranteed non-negative UNLESS
2803 c is NULL, in which case a may be negative. */
2804
2805 z = (PyLongObject *)PyLong_FromLong(1L);
2806 if (z == NULL)
2807 goto Error;
2808
2809 /* Perform a modular reduction, X = X % c, but leave X alone if c
2810 * is NULL.
2811 */
2812#define REDUCE(X) \
2813 if (c != NULL) { \
2814 if (l_divmod(X, c, NULL, &temp) < 0) \
2815 goto Error; \
2816 Py_XDECREF(X); \
2817 X = temp; \
2818 temp = NULL; \
2819 }
2820
2821 /* Multiply two values, then reduce the result:
2822 result = X*Y % c. If c is NULL, skip the mod. */
2823#define MULT(X, Y, result) \
2824{ \
2825 temp = (PyLongObject *)long_mul(X, Y); \
2826 if (temp == NULL) \
2827 goto Error; \
2828 Py_XDECREF(result); \
2829 result = temp; \
2830 temp = NULL; \
2831 REDUCE(result) \
2832}
2833
Christian Heimese93237d2007-12-19 02:37:44 +00002834 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002835 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2836 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimese93237d2007-12-19 02:37:44 +00002837 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002838 digit bi = b->ob_digit[i];
2839
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002840 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002841 MULT(z, z, z)
2842 if (bi & j)
2843 MULT(z, a, z)
2844 }
2845 }
2846 }
2847 else {
2848 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2849 Py_INCREF(z); /* still holds 1L */
2850 table[0] = z;
2851 for (i = 1; i < 32; ++i)
2852 MULT(table[i-1], a, table[i])
2853
Christian Heimese93237d2007-12-19 02:37:44 +00002854 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002855 const digit bi = b->ob_digit[i];
2856
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002857 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002858 const int index = (bi >> j) & 0x1f;
2859 for (k = 0; k < 5; ++k)
2860 MULT(z, z, z)
2861 if (index)
2862 MULT(z, table[index], z)
2863 }
2864 }
2865 }
2866
Christian Heimese93237d2007-12-19 02:37:44 +00002867 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002868 temp = (PyLongObject *)long_sub(z, c);
2869 if (temp == NULL)
2870 goto Error;
2871 Py_DECREF(z);
2872 z = temp;
2873 temp = NULL;
2874 }
2875 goto Done;
2876
2877 Error:
2878 if (z != NULL) {
2879 Py_DECREF(z);
2880 z = NULL;
2881 }
2882 /* fall through */
2883 Done:
Christian Heimese93237d2007-12-19 02:37:44 +00002884 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002885 for (i = 0; i < 32; ++i)
2886 Py_XDECREF(table[i]);
2887 }
Tim Petersde7990b2005-07-17 23:45:23 +00002888 Py_DECREF(a);
2889 Py_DECREF(b);
2890 Py_XDECREF(c);
2891 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002892 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002893}
2894
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002895static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002896long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002897{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002898 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002899 PyLongObject *x;
2900 PyLongObject *w;
2901 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002902 if (w == NULL)
2903 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002904 x = (PyLongObject *) long_add(v, w);
2905 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002906 if (x == NULL)
2907 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002908 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002909 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002910}
2911
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002912static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002913long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002914{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002915 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002916 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002917 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002918 Py_INCREF(v);
2919 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002920 }
Tim Peters69c2de32001-09-11 22:31:33 +00002921 z = (PyLongObject *)_PyLong_Copy(v);
2922 if (z != NULL)
2923 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002924 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002925}
2926
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002927static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002928long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002929{
2930 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002931 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002932 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002933 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002934}
2935
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002936static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002937long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002938{
Christian Heimese93237d2007-12-19 02:37:44 +00002939 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002940}
2941
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002942static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002943long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002944{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002945 PyLongObject *a, *b;
2946 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002947 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002948 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002949 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002950
Neil Schemenauerba872e22001-01-04 01:46:03 +00002951 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2952
Christian Heimese93237d2007-12-19 02:37:44 +00002953 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002954 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002955 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002956 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002957 if (a1 == NULL)
2958 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002959 a2 = (PyLongObject *) long_rshift(a1, b);
2960 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002961 if (a2 == NULL)
2962 goto rshift_error;
2963 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002964 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002965 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002966 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002967
Neil Schemenauerba872e22001-01-04 01:46:03 +00002968 shiftby = PyLong_AsLong((PyObject *)b);
2969 if (shiftby == -1L && PyErr_Occurred())
2970 goto rshift_error;
2971 if (shiftby < 0) {
2972 PyErr_SetString(PyExc_ValueError,
2973 "negative shift count");
2974 goto rshift_error;
2975 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002976 wordshift = shiftby / PyLong_SHIFT;
Christian Heimese93237d2007-12-19 02:37:44 +00002977 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002978 if (newsize <= 0) {
2979 z = _PyLong_New(0);
2980 Py_DECREF(a);
2981 Py_DECREF(b);
2982 return (PyObject *)z;
2983 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002984 loshift = shiftby % PyLong_SHIFT;
2985 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002986 lomask = ((digit)1 << hishift) - 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002987 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002988 z = _PyLong_New(newsize);
2989 if (z == NULL)
2990 goto rshift_error;
Christian Heimese93237d2007-12-19 02:37:44 +00002991 if (Py_SIZE(a) < 0)
2992 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00002993 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2994 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2995 if (i+1 < newsize)
2996 z->ob_digit[i] |=
2997 (a->ob_digit[j+1] << hishift) & himask;
2998 }
2999 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003000 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003001rshift_error:
3002 Py_DECREF(a);
3003 Py_DECREF(b);
3004 return (PyObject *) z;
3005
Guido van Rossumc6913e71991-11-19 20:26:46 +00003006}
3007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003008static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003009long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003010{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003011 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003012 PyLongObject *a, *b;
3013 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003014 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003015 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003016 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003017
Neil Schemenauerba872e22001-01-04 01:46:03 +00003018 CONVERT_BINOP(v, w, &a, &b);
3019
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003020 shiftby = PyLong_AsLong((PyObject *)b);
3021 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003022 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003023 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003024 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003025 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003026 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003027 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003028 PyErr_SetString(PyExc_ValueError,
3029 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003030 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003031 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003032 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3033 wordshift = (int)shiftby / PyLong_SHIFT;
3034 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003035
3036 oldsize = ABS(a->ob_size);
3037 newsize = oldsize + wordshift;
3038 if (remshift)
3039 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003040 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003041 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003042 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003043 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003044 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003045 for (i = 0; i < wordshift; i++)
3046 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003047 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003048 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003049 accum |= (twodigits)a->ob_digit[j] << remshift;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003050 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3051 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003052 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003053 if (remshift)
3054 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003055 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003056 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003057 z = long_normalize(z);
3058lshift_error:
3059 Py_DECREF(a);
3060 Py_DECREF(b);
3061 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003062}
3063
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003064
3065/* Bitwise and/xor/or operations */
3066
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003067static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003068long_bitwise(PyLongObject *a,
3069 int op, /* '&', '|', '^' */
3070 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003071{
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003072 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003073 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003074 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003075 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003076 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003077 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003078 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003079
Christian Heimese93237d2007-12-19 02:37:44 +00003080 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003081 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003082 if (a == NULL)
3083 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003084 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003085 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003086 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003087 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003088 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003089 }
Christian Heimese93237d2007-12-19 02:37:44 +00003090 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003091 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003092 if (b == NULL) {
3093 Py_DECREF(a);
3094 return NULL;
3095 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003096 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003097 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003098 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003099 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003100 maskb = 0;
3101 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003102
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003103 negz = 0;
3104 switch (op) {
3105 case '^':
3106 if (maska != maskb) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003107 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003108 negz = -1;
3109 }
3110 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003111 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003112 if (maska && maskb) {
3113 op = '|';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003114 maska ^= PyLong_MASK;
3115 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003116 negz = -1;
3117 }
3118 break;
3119 case '|':
3120 if (maska || maskb) {
3121 op = '&';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003122 maska ^= PyLong_MASK;
3123 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003124 negz = -1;
3125 }
3126 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003127 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003128
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003129 /* JRH: The original logic here was to allocate the result value (z)
3130 as the longer of the two operands. However, there are some cases
3131 where the result is guaranteed to be shorter than that: AND of two
3132 positives, OR of two negatives: use the shorter number. AND with
3133 mixed signs: use the positive number. OR with mixed signs: use the
3134 negative number. After the transformations above, op will be '&'
3135 iff one of these cases applies, and mask will be non-0 for operands
3136 whose length should be ignored.
3137 */
3138
Christian Heimese93237d2007-12-19 02:37:44 +00003139 size_a = Py_SIZE(a);
3140 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003141 size_z = op == '&'
3142 ? (maska
3143 ? size_b
3144 : (maskb ? size_a : MIN(size_a, size_b)))
3145 : MAX(size_a, size_b);
3146 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003147 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003148 Py_DECREF(a);
3149 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003150 return NULL;
3151 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003152
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003153 for (i = 0; i < size_z; ++i) {
3154 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3155 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3156 switch (op) {
3157 case '&': z->ob_digit[i] = diga & digb; break;
3158 case '|': z->ob_digit[i] = diga | digb; break;
3159 case '^': z->ob_digit[i] = diga ^ digb; break;
3160 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003161 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003163 Py_DECREF(a);
3164 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003165 z = long_normalize(z);
3166 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003167 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003168 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003169 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003170 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003171}
3172
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003173static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003174long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003175{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003176 PyLongObject *a, *b;
3177 PyObject *c;
3178 CONVERT_BINOP(v, w, &a, &b);
3179 c = long_bitwise(a, '&', b);
3180 Py_DECREF(a);
3181 Py_DECREF(b);
3182 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003183}
3184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003185static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003186long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003187{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003188 PyLongObject *a, *b;
3189 PyObject *c;
3190 CONVERT_BINOP(v, w, &a, &b);
3191 c = long_bitwise(a, '^', b);
3192 Py_DECREF(a);
3193 Py_DECREF(b);
3194 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003195}
3196
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003197static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003198long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003199{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003200 PyLongObject *a, *b;
3201 PyObject *c;
3202 CONVERT_BINOP(v, w, &a, &b);
3203 c = long_bitwise(a, '|', b);
3204 Py_DECREF(a);
3205 Py_DECREF(b);
3206 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003207}
3208
Guido van Rossum234f9421993-06-17 12:35:49 +00003209static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003210long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003211{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003212 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003213 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandlc02e1312007-04-11 17:16:24 +00003214 if (*pw == NULL)
3215 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003216 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003217 return 0;
3218 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003219 else if (PyLong_Check(*pw)) {
3220 Py_INCREF(*pv);
3221 Py_INCREF(*pw);
3222 return 0;
3223 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003224 return 1; /* Can't do it */
3225}
3226
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003227static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003228long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003229{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003230 if (PyLong_CheckExact(v))
3231 Py_INCREF(v);
3232 else
3233 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003234 return v;
3235}
3236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003237static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003238long_int(PyObject *v)
3239{
3240 long x;
3241 x = PyLong_AsLong(v);
3242 if (PyErr_Occurred()) {
3243 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3244 PyErr_Clear();
3245 if (PyLong_CheckExact(v)) {
3246 Py_INCREF(v);
3247 return v;
3248 }
3249 else
3250 return _PyLong_Copy((PyLongObject *)v);
3251 }
3252 else
3253 return NULL;
3254 }
3255 return PyInt_FromLong(x);
3256}
3257
3258static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003259long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003260{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003261 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003262 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003263 if (result == -1.0 && PyErr_Occurred())
3264 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003265 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003266}
3267
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003268static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003269long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003270{
Fred Drake121ee271999-12-23 15:41:28 +00003271 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003272}
3273
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003274static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003275long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003276{
Fred Drake121ee271999-12-23 15:41:28 +00003277 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003278}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003279
3280static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003281long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003282
Tim Peters6d6c1a32001-08-02 04:15:00 +00003283static PyObject *
3284long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3285{
3286 PyObject *x = NULL;
3287 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003288 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003289
Guido van Rossumbef14172001-08-29 15:47:46 +00003290 if (type != &PyLong_Type)
3291 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003292 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3293 &x, &base))
3294 return NULL;
3295 if (x == NULL)
3296 return PyLong_FromLong(0L);
3297 if (base == -909)
3298 return PyNumber_Long(x);
Georg Brandl00cd8182007-03-06 18:41:12 +00003299 else if (PyString_Check(x)) {
3300 /* Since PyLong_FromString doesn't have a length parameter,
3301 * check here for possible NULs in the string. */
3302 char *string = PyString_AS_STRING(x);
3303 if (strlen(string) != PyString_Size(x)) {
3304 /* create a repr() of the input string,
3305 * just like PyLong_FromString does. */
3306 PyObject *srepr;
3307 srepr = PyObject_Repr(x);
3308 if (srepr == NULL)
3309 return NULL;
3310 PyErr_Format(PyExc_ValueError,
3311 "invalid literal for long() with base %d: %s",
3312 base, PyString_AS_STRING(srepr));
3313 Py_DECREF(srepr);
3314 return NULL;
3315 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003316 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandl00cd8182007-03-06 18:41:12 +00003317 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003318#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003319 else if (PyUnicode_Check(x))
3320 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3321 PyUnicode_GET_SIZE(x),
3322 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003323#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003324 else {
3325 PyErr_SetString(PyExc_TypeError,
3326 "long() can't convert non-string with explicit base");
3327 return NULL;
3328 }
3329}
3330
Guido van Rossumbef14172001-08-29 15:47:46 +00003331/* Wimpy, slow approach to tp_new calls for subtypes of long:
3332 first create a regular long from whatever arguments we got,
3333 then allocate a subtype instance and initialize it from
3334 the regular long. The regular long is then thrown away.
3335*/
3336static PyObject *
3337long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3338{
Anthony Baxter377be112006-04-11 06:54:30 +00003339 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003340 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003341
3342 assert(PyType_IsSubtype(type, &PyLong_Type));
3343 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3344 if (tmp == NULL)
3345 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003346 assert(PyLong_CheckExact(tmp));
Christian Heimese93237d2007-12-19 02:37:44 +00003347 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003348 if (n < 0)
3349 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003350 newobj = (PyLongObject *)type->tp_alloc(type, n);
3351 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003352 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003353 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003354 }
Anthony Baxter377be112006-04-11 06:54:30 +00003355 assert(PyLong_Check(newobj));
Christian Heimese93237d2007-12-19 02:37:44 +00003356 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003357 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003358 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003359 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003360 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003361}
3362
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003363static PyObject *
3364long_getnewargs(PyLongObject *v)
3365{
3366 return Py_BuildValue("(N)", _PyLong_Copy(v));
3367}
3368
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003369static PyObject *
3370long_getN(PyLongObject *v, void *context) {
3371 return PyLong_FromLong((intptr_t)context);
3372}
3373
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003374static PyMethodDef long_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003375 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3376 "Returns self, the complex conjugate of any long."},
3377 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3378 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003379 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3380 {NULL, NULL} /* sentinel */
3381};
3382
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003383static PyGetSetDef long_getset[] = {
3384 {"real",
3385 (getter)long_long, (setter)NULL,
3386 "the real part of a complex number",
3387 NULL},
3388 {"imag",
3389 (getter)long_getN, (setter)NULL,
3390 "the imaginary part of a complex number",
3391 (void*)0},
3392 {"numerator",
3393 (getter)long_long, (setter)NULL,
3394 "the numerator of a rational number in lowest terms",
3395 NULL},
3396 {"denominator",
3397 (getter)long_getN, (setter)NULL,
3398 "the denominator of a rational number in lowest terms",
3399 (void*)1},
3400 {NULL} /* Sentinel */
3401};
3402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003403PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003404"long(x[, base]) -> integer\n\
3405\n\
3406Convert a string or number to a long integer, if possible. A floating\n\
3407point argument will be truncated towards zero (this does not include a\n\
3408string representation of a floating point number!) When converting a\n\
3409string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003410converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003411
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003412static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003413 (binaryfunc) long_add, /*nb_add*/
3414 (binaryfunc) long_sub, /*nb_subtract*/
3415 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003416 long_classic_div, /*nb_divide*/
3417 long_mod, /*nb_remainder*/
3418 long_divmod, /*nb_divmod*/
3419 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003420 (unaryfunc) long_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003421 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003422 (unaryfunc) long_abs, /*tp_absolute*/
3423 (inquiry) long_nonzero, /*tp_nonzero*/
3424 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003425 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003426 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003427 long_and, /*nb_and*/
3428 long_xor, /*nb_xor*/
3429 long_or, /*nb_or*/
3430 long_coerce, /*nb_coerce*/
3431 long_int, /*nb_int*/
3432 long_long, /*nb_long*/
3433 long_float, /*nb_float*/
3434 long_oct, /*nb_oct*/
3435 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003436 0, /* nb_inplace_add */
3437 0, /* nb_inplace_subtract */
3438 0, /* nb_inplace_multiply */
3439 0, /* nb_inplace_divide */
3440 0, /* nb_inplace_remainder */
3441 0, /* nb_inplace_power */
3442 0, /* nb_inplace_lshift */
3443 0, /* nb_inplace_rshift */
3444 0, /* nb_inplace_and */
3445 0, /* nb_inplace_xor */
3446 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003447 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003448 long_true_divide, /* nb_true_divide */
3449 0, /* nb_inplace_floor_divide */
3450 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003451 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003452};
3453
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003454PyTypeObject PyLong_Type = {
3455 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456 0, /* ob_size */
3457 "long", /* tp_name */
3458 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3459 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003460 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003461 0, /* tp_print */
3462 0, /* tp_getattr */
3463 0, /* tp_setattr */
3464 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003465 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003466 &long_as_number, /* tp_as_number */
3467 0, /* tp_as_sequence */
3468 0, /* tp_as_mapping */
3469 (hashfunc)long_hash, /* tp_hash */
3470 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003471 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003472 PyObject_GenericGetAttr, /* tp_getattro */
3473 0, /* tp_setattro */
3474 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003475 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00003476 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003477 long_doc, /* tp_doc */
3478 0, /* tp_traverse */
3479 0, /* tp_clear */
3480 0, /* tp_richcompare */
3481 0, /* tp_weaklistoffset */
3482 0, /* tp_iter */
3483 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003484 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003485 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003486 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003487 0, /* tp_base */
3488 0, /* tp_dict */
3489 0, /* tp_descr_get */
3490 0, /* tp_descr_set */
3491 0, /* tp_dictoffset */
3492 0, /* tp_init */
3493 0, /* tp_alloc */
3494 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003495 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003496};