blob: 3ee2992bfca61f5f4f616d695741b53514c927fa [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 *);
Guido van Rossume32e0141992-01-19 16:31:05 +000038
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000040 if (--_Py_Ticker < 0) { \
41 _Py_Ticker = _Py_CheckInterval; \
Tim Petersd89fc222006-05-25 22:28:46 +000042 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000043 }
44
Guido van Rossumedcc38a1991-05-05 20:09:44 +000045/* Normalize (remove leading zeros from) a long int object.
46 Doesn't attempt to free the storage--in most cases, due to the nature
47 of the algorithms used, this could save at most be one word anyway. */
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000050long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000051{
Christian Heimese93237d2007-12-19 02:37:44 +000052 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +000053 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000054
Guido van Rossumedcc38a1991-05-05 20:09:44 +000055 while (i > 0 && v->ob_digit[i-1] == 0)
56 --i;
57 if (i != j)
Christian Heimese93237d2007-12-19 02:37:44 +000058 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000059 return v;
60}
61
62/* Allocate a new long int object with size digits.
63 Return NULL and set exception if we run out of memory. */
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000066_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000067{
Neal Norwitzc6a989a2006-05-10 06:57:58 +000068 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000069 PyErr_NoMemory();
70 return NULL;
71 }
Christian Heimes4956d2b2008-01-18 19:12:56 +000072 /* coverity[ampersand_in_size] */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000074}
75
Tim Peters64b5ce32001-09-10 20:52:51 +000076PyObject *
77_PyLong_Copy(PyLongObject *src)
78{
79 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000081
82 assert(src != NULL);
83 i = src->ob_size;
84 if (i < 0)
85 i = -(i);
86 result = _PyLong_New(i);
87 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000088 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000089 while (--i >= 0)
90 result->ob_digit[i] = src->ob_digit[i];
91 }
92 return (PyObject *)result;
93}
94
Guido van Rossumedcc38a1991-05-05 20:09:44 +000095/* Create a new long int object from a C long int */
96
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000098PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000099{
Tim Petersce9de2f2001-06-14 04:56:19 +0000100 PyLongObject *v;
101 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
102 int ndigits = 0;
103 int negative = 0;
104
105 if (ival < 0) {
106 ival = -ival;
107 negative = 1;
108 }
109
110 /* Count the number of Python digits.
111 We used to pick 5 ("big enough for anything"), but that's a
112 waste of time and space given that 5*15 = 75 bits are rarely
113 needed. */
114 t = (unsigned long)ival;
115 while (t) {
116 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000117 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000118 }
119 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000120 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000121 digit *p = v->ob_digit;
122 v->ob_size = negative ? -ndigits : ndigits;
123 t = (unsigned long)ival;
124 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000125 *p++ = (digit)(t & PyLong_MASK);
126 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000127 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000130}
131
Guido van Rossum53756b11997-01-03 17:14:46 +0000132/* Create a new long int object from a C unsigned long int */
133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000135PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000136{
Tim Petersce9de2f2001-06-14 04:56:19 +0000137 PyLongObject *v;
138 unsigned long t;
139 int ndigits = 0;
140
141 /* Count the number of Python digits. */
142 t = (unsigned long)ival;
143 while (t) {
144 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000145 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000146 }
147 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000148 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000149 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000150 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000151 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000152 *p++ = (digit)(ival & PyLong_MASK);
153 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000154 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000157}
158
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000159/* Create a new long int object from a C double */
160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000165 double frac;
166 int i, ndig, expo, neg;
167 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000168 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000169 PyErr_SetString(PyExc_OverflowError,
170 "cannot convert float infinity to long");
171 return NULL;
172 }
Christian Heimes8267d1d2008-01-04 00:37:34 +0000173 if (Py_IS_NAN(dval)) {
174 return PyLong_FromLong(0L);
175 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000176 if (dval < 0.0) {
177 neg = 1;
178 dval = -dval;
179 }
180 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
181 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 return PyLong_FromLong(0L);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000183 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000185 if (v == NULL)
186 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000187 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000188 for (i = ndig; --i >= 0; ) {
189 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000190 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000191 frac = frac - (double)bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000192 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000193 }
194 if (neg)
Christian Heimese93237d2007-12-19 02:37:44 +0000195 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000197}
198
Armin Rigo7ccbca92006-10-04 12:17:45 +0000199/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
200 * anything about what happens when a signed integer operation overflows,
201 * and some compilers think they're doing you a favor by being "clever"
202 * then. The bit pattern for the largest postive signed long is
203 * (unsigned long)LONG_MAX, and for the smallest negative signed long
204 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
205 * However, some other compilers warn about applying unary minus to an
206 * unsigned operand. Hence the weird "0-".
207 */
208#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
209#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
210
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000211/* Get a C long int from a long int object.
212 Returns -1 and sets an error condition if overflow occurs. */
213
214long
Tim Peters9f688bf2000-07-07 15:53:28 +0000215PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000216{
Guido van Rossumf7531811998-05-26 14:33:37 +0000217 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000219 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000220 Py_ssize_t i;
221 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000224 if (vv != NULL && PyInt_Check(vv))
225 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000227 return -1;
228 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000230 i = v->ob_size;
231 sign = 1;
232 x = 0;
233 if (i < 0) {
234 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000235 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000236 }
237 while (--i >= 0) {
238 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000239 x = (x << PyLong_SHIFT) + v->ob_digit[i];
240 if ((x >> PyLong_SHIFT) != prev)
Guido van Rossumf7531811998-05-26 14:33:37 +0000241 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000242 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000243 /* Haven't lost any bits, but casting to long requires extra care
244 * (see comment above).
245 */
246 if (x <= (unsigned long)LONG_MAX) {
247 return (long)x * sign;
248 }
249 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
250 return LONG_MIN;
251 }
252 /* else overflow */
Guido van Rossumf7531811998-05-26 14:33:37 +0000253
254 overflow:
255 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000256 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000257 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000258}
259
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000260/* Get a Py_ssize_t from a long int object.
261 Returns -1 and sets an error condition if overflow occurs. */
262
263Py_ssize_t
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000264PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000265 register PyLongObject *v;
266 size_t x, prev;
267 Py_ssize_t i;
268 int sign;
269
270 if (vv == NULL || !PyLong_Check(vv)) {
271 PyErr_BadInternalCall();
272 return -1;
273 }
274 v = (PyLongObject *)vv;
275 i = v->ob_size;
276 sign = 1;
277 x = 0;
278 if (i < 0) {
279 sign = -1;
280 i = -(i);
281 }
282 while (--i >= 0) {
283 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000284 x = (x << PyLong_SHIFT) + v->ob_digit[i];
285 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286 goto overflow;
287 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000288 /* Haven't lost any bits, but casting to a signed type requires
289 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000290 */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000291 if (x <= (size_t)PY_SSIZE_T_MAX) {
292 return (Py_ssize_t)x * sign;
293 }
294 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
295 return PY_SSIZE_T_MIN;
296 }
297 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000298
299 overflow:
300 PyErr_SetString(PyExc_OverflowError,
301 "long int too large to convert to int");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000302 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000303}
304
Guido van Rossumd8c80482002-08-13 00:24:58 +0000305/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000306 Returns -1 and sets an error condition if overflow occurs. */
307
308unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000309PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000310{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000312 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000313 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000314
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000315 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000316 if (vv != NULL && PyInt_Check(vv)) {
317 long val = PyInt_AsLong(vv);
318 if (val < 0) {
319 PyErr_SetString(PyExc_OverflowError,
320 "can't convert negative value to unsigned long");
321 return (unsigned long) -1;
322 }
323 return val;
324 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000326 return (unsigned long) -1;
327 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000329 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000330 x = 0;
331 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000333 "can't convert negative value to unsigned long");
334 return (unsigned long) -1;
335 }
336 while (--i >= 0) {
337 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000338 x = (x << PyLong_SHIFT) + v->ob_digit[i];
339 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000341 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000342 return (unsigned long) -1;
343 }
344 }
345 return x;
346}
347
Thomas Hellera4ea6032003-04-17 18:55:45 +0000348/* Get a C unsigned long int from a long int object, ignoring the high bits.
349 Returns -1 and sets an error condition if an error occurs. */
350
351unsigned long
352PyLong_AsUnsignedLongMask(PyObject *vv)
353{
354 register PyLongObject *v;
355 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000356 Py_ssize_t i;
357 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000358
359 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000360 if (vv != NULL && PyInt_Check(vv))
361 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000362 PyErr_BadInternalCall();
363 return (unsigned long) -1;
364 }
365 v = (PyLongObject *)vv;
366 i = v->ob_size;
367 sign = 1;
368 x = 0;
369 if (i < 0) {
370 sign = -1;
371 i = -i;
372 }
373 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000374 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000375 }
376 return x * sign;
377}
378
Tim Peters5b8132f2003-01-31 15:52:05 +0000379int
380_PyLong_Sign(PyObject *vv)
381{
382 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000383
384 assert(v != NULL);
385 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000386
Christian Heimese93237d2007-12-19 02:37:44 +0000387 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000388}
389
Tim Petersbaefd9e2003-01-28 20:37:45 +0000390size_t
391_PyLong_NumBits(PyObject *vv)
392{
393 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000394 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000396
397 assert(v != NULL);
398 assert(PyLong_Check(v));
Christian Heimese93237d2007-12-19 02:37:44 +0000399 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000400 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
401 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000402 digit msd = v->ob_digit[ndigits - 1];
403
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000404 result = (ndigits - 1) * PyLong_SHIFT;
405 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000406 goto Overflow;
407 do {
408 ++result;
409 if (result == 0)
410 goto Overflow;
411 msd >>= 1;
412 } while (msd);
413 }
414 return result;
415
416Overflow:
417 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
418 "to express in a platform size_t");
419 return (size_t)-1;
420}
421
Tim Peters2a9b3672001-06-11 21:23:58 +0000422PyObject *
423_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
424 int little_endian, int is_signed)
425{
426 const unsigned char* pstartbyte;/* LSB of bytes */
427 int incr; /* direction to move pstartbyte */
428 const unsigned char* pendbyte; /* MSB of bytes */
429 size_t numsignificantbytes; /* number of bytes that matter */
430 size_t ndigits; /* number of Python long digits */
431 PyLongObject* v; /* result */
432 int idigit = 0; /* next free index in v->ob_digit */
433
434 if (n == 0)
435 return PyLong_FromLong(0L);
436
437 if (little_endian) {
438 pstartbyte = bytes;
439 pendbyte = bytes + n - 1;
440 incr = 1;
441 }
442 else {
443 pstartbyte = bytes + n - 1;
444 pendbyte = bytes;
445 incr = -1;
446 }
447
448 if (is_signed)
449 is_signed = *pendbyte >= 0x80;
450
451 /* Compute numsignificantbytes. This consists of finding the most
452 significant byte. Leading 0 bytes are insignficant if the number
453 is positive, and leading 0xff bytes if negative. */
454 {
455 size_t i;
456 const unsigned char* p = pendbyte;
457 const int pincr = -incr; /* search MSB to LSB */
458 const unsigned char insignficant = is_signed ? 0xff : 0x00;
459
460 for (i = 0; i < n; ++i, p += pincr) {
461 if (*p != insignficant)
462 break;
463 }
464 numsignificantbytes = n - i;
465 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
466 actually has 2 significant bytes. OTOH, 0xff0001 ==
467 -0x00ffff, so we wouldn't *need* to bump it there; but we
468 do for 0xffff = -0x0001. To be safe without bothering to
469 check every case, bump it regardless. */
470 if (is_signed && numsignificantbytes < n)
471 ++numsignificantbytes;
472 }
473
474 /* How many Python long digits do we need? We have
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000475 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
Tim Peters2a9b3672001-06-11 21:23:58 +0000476 bits, so it's the ceiling of the quotient. */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000477 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Tim Peters2a9b3672001-06-11 21:23:58 +0000478 if (ndigits > (size_t)INT_MAX)
479 return PyErr_NoMemory();
480 v = _PyLong_New((int)ndigits);
481 if (v == NULL)
482 return NULL;
483
484 /* Copy the bits over. The tricky parts are computing 2's-comp on
485 the fly for signed numbers, and dealing with the mismatch between
486 8-bit bytes and (probably) 15-bit Python digits.*/
487 {
488 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000489 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000490 twodigits accum = 0; /* sliding register */
491 unsigned int accumbits = 0; /* number of bits in accum */
492 const unsigned char* p = pstartbyte;
493
494 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000495 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000496 /* Compute correction for 2's comp, if needed. */
497 if (is_signed) {
498 thisbyte = (0xff ^ thisbyte) + carry;
499 carry = thisbyte >> 8;
500 thisbyte &= 0xff;
501 }
502 /* Because we're going LSB to MSB, thisbyte is
503 more significant than what's already in accum,
504 so needs to be prepended to accum. */
505 accum |= thisbyte << accumbits;
506 accumbits += 8;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000507 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000508 /* There's enough to fill a Python digit. */
509 assert(idigit < (int)ndigits);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000510 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000511 ++idigit;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000512 accum >>= PyLong_SHIFT;
513 accumbits -= PyLong_SHIFT;
514 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000515 }
516 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000517 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000518 if (accumbits) {
519 assert(idigit < (int)ndigits);
520 v->ob_digit[idigit] = (digit)accum;
521 ++idigit;
522 }
523 }
524
Christian Heimese93237d2007-12-19 02:37:44 +0000525 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000526 return (PyObject *)long_normalize(v);
527}
528
529int
530_PyLong_AsByteArray(PyLongObject* v,
531 unsigned char* bytes, size_t n,
532 int little_endian, int is_signed)
533{
534 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000535 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000536 twodigits accum; /* sliding register */
537 unsigned int accumbits; /* # bits in accum */
538 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
539 twodigits carry; /* for computing 2's-comp */
540 size_t j; /* # bytes filled */
541 unsigned char* p; /* pointer to next byte in bytes */
542 int pincr; /* direction to move p */
543
544 assert(v != NULL && PyLong_Check(v));
545
Christian Heimese93237d2007-12-19 02:37:44 +0000546 if (Py_SIZE(v) < 0) {
547 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000548 if (!is_signed) {
549 PyErr_SetString(PyExc_TypeError,
550 "can't convert negative long to unsigned");
551 return -1;
552 }
553 do_twos_comp = 1;
554 }
555 else {
Christian Heimese93237d2007-12-19 02:37:44 +0000556 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000557 do_twos_comp = 0;
558 }
559
560 if (little_endian) {
561 p = bytes;
562 pincr = 1;
563 }
564 else {
565 p = bytes + n - 1;
566 pincr = -1;
567 }
568
Tim Peters898cf852001-06-13 20:50:08 +0000569 /* Copy over all the Python digits.
570 It's crucial that every Python digit except for the MSD contribute
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000571 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000572 normalized. */
573 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000574 j = 0;
575 accum = 0;
576 accumbits = 0;
577 carry = do_twos_comp ? 1 : 0;
578 for (i = 0; i < ndigits; ++i) {
579 twodigits thisdigit = v->ob_digit[i];
580 if (do_twos_comp) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000581 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
582 carry = thisdigit >> PyLong_SHIFT;
583 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000584 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000585 /* Because we're going LSB to MSB, thisdigit is more
586 significant than what's already in accum, so needs to be
587 prepended to accum. */
588 accum |= thisdigit << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000589 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000590
Tim Petersede05092001-06-14 08:53:38 +0000591 /* The most-significant digit may be (probably is) at least
592 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000593 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000594 /* Count # of sign bits -- they needn't be stored,
595 * although for signed conversion we need later to
596 * make sure at least one sign bit gets stored.
597 * First shift conceptual sign bit to real sign bit.
598 */
599 stwodigits s = (stwodigits)(thisdigit <<
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000600 (8*sizeof(stwodigits) - PyLong_SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000601 unsigned int nsignbits = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000602 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000603 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000604 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000605 }
Tim Petersede05092001-06-14 08:53:38 +0000606 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000607 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000608
Tim Peters2a9b3672001-06-11 21:23:58 +0000609 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000610 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000611 if (j >= n)
612 goto Overflow;
613 ++j;
614 *p = (unsigned char)(accum & 0xff);
615 p += pincr;
616 accumbits -= 8;
617 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000618 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000619 }
620
621 /* Store the straggler (if any). */
622 assert(accumbits < 8);
623 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000624 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000625 if (j >= n)
626 goto Overflow;
627 ++j;
628 if (do_twos_comp) {
629 /* Fill leading bits of the byte with sign bits
630 (appropriately pretending that the long had an
631 infinite supply of sign bits). */
632 accum |= (~(twodigits)0) << accumbits;
633 }
634 *p = (unsigned char)(accum & 0xff);
635 p += pincr;
636 }
Tim Peters05607ad2001-06-13 21:01:27 +0000637 else if (j == n && n > 0 && is_signed) {
638 /* The main loop filled the byte array exactly, so the code
639 just above didn't get to ensure there's a sign bit, and the
640 loop below wouldn't add one either. Make sure a sign bit
641 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000642 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000643 int sign_bit_set = msb >= 0x80;
644 assert(accumbits == 0);
645 if (sign_bit_set == do_twos_comp)
646 return 0;
647 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000648 goto Overflow;
649 }
Tim Peters05607ad2001-06-13 21:01:27 +0000650
651 /* Fill remaining bytes with copies of the sign bit. */
652 {
653 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
654 for ( ; j < n; ++j, p += pincr)
655 *p = signbyte;
656 }
657
Tim Peters2a9b3672001-06-11 21:23:58 +0000658 return 0;
659
660Overflow:
661 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
662 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000663
Tim Peters2a9b3672001-06-11 21:23:58 +0000664}
665
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000666double
667_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
668{
669/* NBITS_WANTED should be > the number of bits in a double's precision,
670 but small enough so that 2**NBITS_WANTED is within the normal double
671 range. nbitsneeded is set to 1 less than that because the most-significant
672 Python digit contains at least 1 significant bit, but we don't want to
673 bother counting them (catering to the worst case cheaply).
674
675 57 is one more than VAX-D double precision; I (Tim) don't know of a double
676 format with more precision than that; it's 1 larger so that we add in at
677 least one round bit to stand in for the ignored least-significant bits.
678*/
679#define NBITS_WANTED 57
680 PyLongObject *v;
681 double x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000682 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000683 Py_ssize_t i;
684 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000685 int nbitsneeded;
686
687 if (vv == NULL || !PyLong_Check(vv)) {
688 PyErr_BadInternalCall();
689 return -1;
690 }
691 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000692 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000693 sign = 1;
694 if (i < 0) {
695 sign = -1;
696 i = -(i);
697 }
698 else if (i == 0) {
699 *exponent = 0;
700 return 0.0;
701 }
702 --i;
703 x = (double)v->ob_digit[i];
704 nbitsneeded = NBITS_WANTED - 1;
705 /* Invariant: i Python digits remain unaccounted for. */
706 while (i > 0 && nbitsneeded > 0) {
707 --i;
708 x = x * multiplier + (double)v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000709 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000710 }
711 /* There are i digits we didn't shift in. Pretending they're all
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000712 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000713 *exponent = i;
714 assert(x > 0.0);
715 return x * sign;
716#undef NBITS_WANTED
717}
718
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000719/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000720
721double
Tim Peters9f688bf2000-07-07 15:53:28 +0000722PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000723{
Thomas Wouters553489a2006-02-01 21:32:04 +0000724 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000725 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000726
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 if (vv == NULL || !PyLong_Check(vv)) {
728 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000729 return -1;
730 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000731 x = _PyLong_AsScaledDouble(vv, &e);
732 if (x == -1.0 && PyErr_Occurred())
733 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000734 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
735 set correctly after a successful _PyLong_AsScaledDouble() call */
736 assert(e >= 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000737 if (e > INT_MAX / PyLong_SHIFT)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000738 goto overflow;
739 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000740 x = ldexp(x, e * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000741 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000742 goto overflow;
743 return x;
744
745overflow:
746 PyErr_SetString(PyExc_OverflowError,
747 "long int too large to convert to float");
748 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000749}
750
Guido van Rossum78694d91998-09-18 14:14:13 +0000751/* Create a new long (or int) object from a C pointer */
752
753PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000754PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000755{
Tim Peters70128a12001-06-16 08:48:40 +0000756#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000757 if ((long)p < 0)
758 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000759 return PyInt_FromLong((long)p);
760#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000761
Tim Peters70128a12001-06-16 08:48:40 +0000762#ifndef HAVE_LONG_LONG
763# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
764#endif
765#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000766# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000767#endif
768 /* optimize null pointers */
769 if (p == NULL)
770 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000771 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000772
773#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000774}
775
776/* Get a C pointer from a long object (or an int object in some cases) */
777
778void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000779PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000780{
781 /* This function will allow int or long objects. If vv is neither,
782 then the PyLong_AsLong*() functions will raise the exception:
783 PyExc_SystemError, "bad argument to internal function"
784 */
Tim Peters70128a12001-06-16 08:48:40 +0000785#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000786 long x;
787
Tim Peters70128a12001-06-16 08:48:40 +0000788 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000789 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000790 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000791 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000792 else
793 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000794#else
Tim Peters70128a12001-06-16 08:48:40 +0000795
796#ifndef HAVE_LONG_LONG
797# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
798#endif
799#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000800# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000801#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000802 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000803
Tim Peters70128a12001-06-16 08:48:40 +0000804 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000805 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000806 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000807 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000808 else
809 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000810
811#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000812
813 if (x == -1 && PyErr_Occurred())
814 return NULL;
815 return (void *)x;
816}
817
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000818#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000819
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000820/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000821 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000822 */
823
Tim Peterscf37dfc2001-06-14 18:42:50 +0000824#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000825
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000826/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000827
828PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000829PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000830{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000831 PyLongObject *v;
832 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
833 int ndigits = 0;
834 int negative = 0;
835
836 if (ival < 0) {
837 ival = -ival;
838 negative = 1;
839 }
840
841 /* Count the number of Python digits.
842 We used to pick 5 ("big enough for anything"), but that's a
843 waste of time and space given that 5*15 = 75 bits are rarely
844 needed. */
845 t = (unsigned PY_LONG_LONG)ival;
846 while (t) {
847 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000848 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000849 }
850 v = _PyLong_New(ndigits);
851 if (v != NULL) {
852 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000853 Py_SIZE(v) = negative ? -ndigits : ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000854 t = (unsigned PY_LONG_LONG)ival;
855 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000856 *p++ = (digit)(t & PyLong_MASK);
857 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000858 }
859 }
860 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000861}
862
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000863/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000864
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000865PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000866PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000867{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000868 PyLongObject *v;
869 unsigned PY_LONG_LONG t;
870 int ndigits = 0;
871
872 /* Count the number of Python digits. */
873 t = (unsigned PY_LONG_LONG)ival;
874 while (t) {
875 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000876 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000877 }
878 v = _PyLong_New(ndigits);
879 if (v != NULL) {
880 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000881 Py_SIZE(v) = ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000882 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000883 *p++ = (digit)(ival & PyLong_MASK);
884 ival >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000885 }
886 }
887 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000888}
889
Martin v. Löwis18e16552006-02-15 17:27:45 +0000890/* Create a new long int object from a C Py_ssize_t. */
891
892PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000893PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000894{
895 Py_ssize_t bytes = ival;
896 int one = 1;
897 return _PyLong_FromByteArray(
898 (unsigned char *)&bytes,
Kristján Valur Jónssonf0303942007-05-03 20:27:03 +0000899 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000900}
901
902/* Create a new long int object from a C size_t. */
903
904PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000905PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906{
907 size_t bytes = ival;
908 int one = 1;
909 return _PyLong_FromByteArray(
910 (unsigned char *)&bytes,
911 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
912}
913
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000914/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000915 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000916
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000917PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000918PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000919{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000920 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000921 int one = 1;
922 int res;
923
Tim Petersd38b1c72001-09-30 05:09:37 +0000924 if (vv == NULL) {
925 PyErr_BadInternalCall();
926 return -1;
927 }
928 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000929 PyNumberMethods *nb;
930 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000931 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000932 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000933 if ((nb = vv->ob_type->tp_as_number) == NULL ||
934 nb->nb_int == NULL) {
935 PyErr_SetString(PyExc_TypeError, "an integer is required");
936 return -1;
937 }
938 io = (*nb->nb_int) (vv);
939 if (io == NULL)
940 return -1;
941 if (PyInt_Check(io)) {
942 bytes = PyInt_AsLong(io);
943 Py_DECREF(io);
944 return bytes;
945 }
946 if (PyLong_Check(io)) {
947 bytes = PyLong_AsLongLong(io);
948 Py_DECREF(io);
949 return bytes;
950 }
951 Py_DECREF(io);
952 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000953 return -1;
954 }
955
Tim Petersd1a7da62001-06-13 00:35:57 +0000956 res = _PyLong_AsByteArray(
957 (PyLongObject *)vv, (unsigned char *)&bytes,
958 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000959
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000960 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000961 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000962 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000963 else
964 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000965}
966
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000967/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000968 Return -1 and set an error if overflow occurs. */
969
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000970unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000971PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000972{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000973 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000974 int one = 1;
975 int res;
976
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000977 if (vv == NULL || !PyLong_Check(vv)) {
978 PyErr_BadInternalCall();
Skip Montanaro429433b2006-04-18 00:35:43 +0000979 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000980 }
981
Tim Petersd1a7da62001-06-13 00:35:57 +0000982 res = _PyLong_AsByteArray(
983 (PyLongObject *)vv, (unsigned char *)&bytes,
984 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000985
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000986 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000987 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000988 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000989 else
990 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000991}
Tim Petersd1a7da62001-06-13 00:35:57 +0000992
Thomas Hellera4ea6032003-04-17 18:55:45 +0000993/* Get a C unsigned long int from a long int object, ignoring the high bits.
994 Returns -1 and sets an error condition if an error occurs. */
995
996unsigned PY_LONG_LONG
997PyLong_AsUnsignedLongLongMask(PyObject *vv)
998{
999 register PyLongObject *v;
1000 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001001 Py_ssize_t i;
1002 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001003
1004 if (vv == NULL || !PyLong_Check(vv)) {
1005 PyErr_BadInternalCall();
1006 return (unsigned long) -1;
1007 }
1008 v = (PyLongObject *)vv;
1009 i = v->ob_size;
1010 sign = 1;
1011 x = 0;
1012 if (i < 0) {
1013 sign = -1;
1014 i = -i;
1015 }
1016 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001017 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001018 }
1019 return x * sign;
1020}
Tim Petersd1a7da62001-06-13 00:35:57 +00001021#undef IS_LITTLE_ENDIAN
1022
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001023#endif /* HAVE_LONG_LONG */
1024
Neil Schemenauerba872e22001-01-04 01:46:03 +00001025
1026static int
1027convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001028 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001029 *a = (PyLongObject *) v;
1030 Py_INCREF(v);
1031 }
1032 else if (PyInt_Check(v)) {
1033 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1034 }
1035 else {
1036 return 0;
1037 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001038 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001039 *b = (PyLongObject *) w;
1040 Py_INCREF(w);
1041 }
1042 else if (PyInt_Check(w)) {
1043 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1044 }
1045 else {
1046 Py_DECREF(*a);
1047 return 0;
1048 }
1049 return 1;
1050}
1051
1052#define CONVERT_BINOP(v, w, a, b) \
1053 if (!convert_binop(v, w, a, b)) { \
1054 Py_INCREF(Py_NotImplemented); \
1055 return Py_NotImplemented; \
1056 }
1057
Tim Peters877a2122002-08-12 05:09:36 +00001058/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1059 * is modified in place, by adding y to it. Carries are propagated as far as
1060 * x[m-1], and the remaining carry (0 or 1) is returned.
1061 */
1062static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001063v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001064{
1065 int i;
1066 digit carry = 0;
1067
1068 assert(m >= n);
1069 for (i = 0; i < n; ++i) {
1070 carry += x[i] + y[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001071 x[i] = carry & PyLong_MASK;
1072 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001073 assert((carry & 1) == carry);
1074 }
1075 for (; carry && i < m; ++i) {
1076 carry += x[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001077 x[i] = carry & PyLong_MASK;
1078 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001079 assert((carry & 1) == carry);
1080 }
1081 return carry;
1082}
1083
1084/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1085 * is modified in place, by subtracting y from it. Borrows are propagated as
1086 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1087 */
1088static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001089v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001090{
1091 int i;
1092 digit borrow = 0;
1093
1094 assert(m >= n);
1095 for (i = 0; i < n; ++i) {
1096 borrow = x[i] - y[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001097 x[i] = borrow & PyLong_MASK;
1098 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001099 borrow &= 1; /* keep only 1 sign bit */
1100 }
1101 for (; borrow && i < m; ++i) {
1102 borrow = x[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001103 x[i] = borrow & PyLong_MASK;
1104 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001105 borrow &= 1;
1106 }
1107 return borrow;
1108}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001109
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001110/* Multiply by a single digit, ignoring the sign. */
1111
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001113mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001114{
1115 return muladd1(a, n, (digit)0);
1116}
1117
1118/* Multiply by a single digit and add a single digit, ignoring the sign. */
1119
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001120static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001121muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001122{
Christian Heimese93237d2007-12-19 02:37:44 +00001123 Py_ssize_t size_a = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001125 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001126 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001127
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001128 if (z == NULL)
1129 return NULL;
1130 for (i = 0; i < size_a; ++i) {
1131 carry += (twodigits)a->ob_digit[i] * n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001132 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1133 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001134 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001135 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001136 return long_normalize(z);
1137}
1138
Tim Peters212e6142001-07-14 12:23:19 +00001139/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1140 in pout, and returning the remainder. pin and pout point at the LSD.
1141 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Eric Smith5e527eb2008-02-10 01:36:53 +00001142 _PyLong_Format, but that should be done with great care since longs are
Tim Peters212e6142001-07-14 12:23:19 +00001143 immutable. */
1144
1145static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001147{
1148 twodigits rem = 0;
1149
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001150 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001151 pin += size;
1152 pout += size;
1153 while (--size >= 0) {
1154 digit hi;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001155 rem = (rem << PyLong_SHIFT) + *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001156 *--pout = hi = (digit)(rem / n);
1157 rem -= hi * n;
1158 }
1159 return (digit)rem;
1160}
1161
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001162/* Divide a long integer by a digit, returning both the quotient
1163 (as function result) and the remainder (through *prem).
1164 The sign of a is ignored; n should not be zero. */
1165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001167divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001168{
Christian Heimese93237d2007-12-19 02:37:44 +00001169 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001171
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001172 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001173 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001174 if (z == NULL)
1175 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001176 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001177 return long_normalize(z);
1178}
1179
Eric Smith5e527eb2008-02-10 01:36:53 +00001180/* Convert the long to a string object with given base,
1181 appending a base prefix of 0[box] if base is 2, 8 or 16.
1182 Add a trailing "L" if addL is non-zero.
1183 If newstyle is zero, then use the pre-2.6 behavior of octal having
1184 a leading "0", instead of the prefix "0o" */
1185PyAPI_FUNC(PyObject *)
1186_PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
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
Eric Smith5e527eb2008-02-10 01:36:53 +00001312 if (base == 2) {
1313 *--p = 'b';
1314 *--p = '0';
1315 }
1316 else if (base == 8) {
1317 if (newstyle) {
1318 *--p = 'o';
Guido van Rossum2c475421992-08-14 15:13:07 +00001319 *--p = '0';
Eric Smith5e527eb2008-02-10 01:36:53 +00001320 }
1321 else
1322 if (size_a != 0)
1323 *--p = '0';
Guido van Rossum2c475421992-08-14 15:13:07 +00001324 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001325 else if (base == 16) {
1326 *--p = 'x';
1327 *--p = '0';
1328 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001329 else if (base != 10) {
1330 *--p = '#';
1331 *--p = '0' + base%10;
1332 if (base > 10)
1333 *--p = '0' + base/10;
1334 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001335 if (sign)
1336 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 if (p != PyString_AS_STRING(str)) {
1338 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001339 assert(p > q);
1340 do {
1341 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001342 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343 _PyString_Resize((PyObject **)&str,
Armin Rigo7ccbca92006-10-04 12:17:45 +00001344 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001345 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001346 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001347}
1348
Tim Petersda53afa2006-05-25 17:34:03 +00001349/* Table of digit values for 8-bit string -> integer conversion.
1350 * '0' maps to 0, ..., '9' maps to 9.
1351 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1352 * All other indices map to 37.
1353 * Note that when converting a base B string, a char c is a legitimate
1354 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1355 */
1356int _PyLong_DigitValue[256] = {
Tim Peters696cf432006-05-24 21:10:40 +00001357 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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1361 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1362 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1363 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1364 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1365 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1366 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1367 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1368 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1369 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1370 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1371 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1372 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Tim Peters696cf432006-05-24 21:10:40 +00001373};
1374
1375/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001376 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1377 * non-digit (which may be *str!). A normalized long is returned.
1378 * The point to this routine is that it takes time linear in the number of
1379 * string characters.
1380 */
1381static PyLongObject *
1382long_from_binary_base(char **str, int base)
1383{
1384 char *p = *str;
1385 char *start = p;
1386 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001388 PyLongObject *z;
1389 twodigits accum;
1390 int bits_in_accum;
1391 digit *pdigit;
1392
1393 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1394 n = base;
1395 for (bits_per_char = -1; n; ++bits_per_char)
1396 n >>= 1;
1397 /* n <- total # of bits needed, while setting p to end-of-string */
1398 n = 0;
Tim Petersda53afa2006-05-25 17:34:03 +00001399 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001400 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001401 *str = p;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001402 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1403 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001404 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001405 PyErr_SetString(PyExc_ValueError,
1406 "long string too large to convert");
1407 return NULL;
1408 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001409 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001410 z = _PyLong_New(n);
1411 if (z == NULL)
1412 return NULL;
1413 /* Read string from right, and fill in long from left; i.e.,
1414 * from least to most significant in both.
1415 */
1416 accum = 0;
1417 bits_in_accum = 0;
1418 pdigit = z->ob_digit;
1419 while (--p >= start) {
Tim Petersda53afa2006-05-25 17:34:03 +00001420 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001421 assert(k >= 0 && k < base);
1422 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001423 bits_in_accum += bits_per_char;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001424 if (bits_in_accum >= PyLong_SHIFT) {
1425 *pdigit++ = (digit)(accum & PyLong_MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001426 assert(pdigit - z->ob_digit <= (int)n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001427 accum >>= PyLong_SHIFT;
1428 bits_in_accum -= PyLong_SHIFT;
1429 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001430 }
1431 }
1432 if (bits_in_accum) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001433 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001434 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001435 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001436 }
1437 while (pdigit - z->ob_digit < n)
1438 *pdigit++ = 0;
1439 return long_normalize(z);
1440}
1441
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001443PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001444{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001445 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001446 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001447 PyLongObject *z;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001448 PyObject *strobj, *strrepr;
1449 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001450
Guido van Rossum472c04f1996-12-05 21:57:21 +00001451 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001452 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001453 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001454 return NULL;
1455 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001456 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001457 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001458 if (*str == '+')
1459 ++str;
1460 else if (*str == '-') {
1461 ++str;
1462 sign = -1;
1463 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001464 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001465 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001466 if (base == 0) {
1467 if (str[0] != '0')
1468 base = 10;
1469 else if (str[1] == 'x' || str[1] == 'X')
1470 base = 16;
1471 else
1472 base = 8;
1473 }
1474 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1475 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001476
Guido van Rossume6762971998-06-22 03:54:46 +00001477 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001478 if ((base & (base - 1)) == 0)
1479 z = long_from_binary_base(&str, base);
1480 else {
Tim Peters696cf432006-05-24 21:10:40 +00001481/***
1482Binary bases can be converted in time linear in the number of digits, because
1483Python's representation base is binary. Other bases (including decimal!) use
1484the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001485
Tim Peters696cf432006-05-24 21:10:40 +00001486First some math: the largest integer that can be expressed in N base-B digits
1487is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1488case number of Python digits needed to hold it is the smallest integer n s.t.
1489
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001490 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1491 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
1492 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001493
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001494The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
Tim Peters696cf432006-05-24 21:10:40 +00001495this quickly. A Python long with that much space is reserved near the start,
1496and the result is computed into it.
1497
1498The input string is actually treated as being in base base**i (i.e., i digits
1499are processed at a time), where two more static arrays hold:
1500
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001501 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
Tim Peters696cf432006-05-24 21:10:40 +00001502 convmultmax_base[base] = base ** convwidth_base[base]
1503
1504The first of these is the largest i such that i consecutive input digits
1505must fit in a single Python digit. The second is effectively the input
1506base we're really using.
1507
1508Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1509convmultmax_base[base], the result is "simply"
1510
1511 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1512
1513where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001514
1515Error analysis: as above, the number of Python digits `n` needed is worst-
1516case
1517
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001518 n >= N * log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001519
1520where `N` is the number of input digits in base `B`. This is computed via
1521
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001522 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001523
1524below. Two numeric concerns are how much space this can waste, and whether
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001525the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
Tim Peters9faa3ed2006-05-30 15:53:34 +00001526which is the default (and it's unlikely anyone changes that).
1527
1528Waste isn't a problem: provided the first input digit isn't 0, the difference
1529between the worst-case input with N digits and the smallest input with N
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001530digits is about a factor of B, but B is small compared to PyLong_BASE so at most
Tim Peters9faa3ed2006-05-30 15:53:34 +00001531one allocated Python digit can remain unused on that count. If
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001532N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001533and adding 1 returns a result 1 larger than necessary. However, that can't
1534happen: whenever B is a power of 2, long_from_binary_base() is called
1535instead, and it's impossible for B**i to be an integer power of 2**15 when
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001536B 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 +00001537an exact integer when B is not a power of 2, since B**i has a prime factor
1538other than 2 in that case, but (2**15)**j's only prime factor is 2).
1539
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001540The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001541is a little bit larger than an exact integer, but due to roundoff errors (in
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001542computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001543yields a numeric result a little less than that integer. Unfortunately, "how
1544close can a transcendental function get to an integer over some range?"
1545questions are generally theoretically intractable. Computer analysis via
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001546continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
Tim Peters9faa3ed2006-05-30 15:53:34 +00001547fractions, giving a sequence i/j of "the best" rational approximations. Then
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001548j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001549we can get very close to being in trouble, but very rarely. For example,
155076573 is a denominator in one of the continued-fraction approximations to
1551log(10)/log(2**15), and indeed:
1552
1553 >>> log(10)/log(2**15)*76573
1554 16958.000000654003
1555
1556is very close to an integer. If we were working with IEEE single-precision,
1557rounding errors could kill us. Finding worst cases in IEEE double-precision
1558requires better-than-double-precision log() functions, and Tim didn't bother.
1559Instead the code checks to see whether the allocated space is enough as each
1560new Python digit is added, and copies the whole thing to a larger long if not.
1561This should happen extremely rarely, and in fact I don't have a test case
1562that triggers it(!). Instead the code was tested by artificially allocating
1563just 1 digit at the start, so that the copying code was exercised for every
1564digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001565***/
1566 register twodigits c; /* current input character */
1567 Py_ssize_t size_z;
1568 int i;
1569 int convwidth;
1570 twodigits convmultmax, convmult;
1571 digit *pz, *pzstop;
1572 char* scan;
1573
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001574 static double log_base_PyLong_BASE[37] = {0.0e0,};
Tim Peters696cf432006-05-24 21:10:40 +00001575 static int convwidth_base[37] = {0,};
1576 static twodigits convmultmax_base[37] = {0,};
1577
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001578 if (log_base_PyLong_BASE[base] == 0.0) {
Tim Peters696cf432006-05-24 21:10:40 +00001579 twodigits convmax = base;
1580 int i = 1;
1581
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001582 log_base_PyLong_BASE[base] = log((double)base) /
1583 log((double)PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001584 for (;;) {
1585 twodigits next = convmax * base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001586 if (next > PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001587 break;
1588 convmax = next;
1589 ++i;
1590 }
1591 convmultmax_base[base] = convmax;
1592 assert(i > 0);
1593 convwidth_base[base] = i;
1594 }
1595
1596 /* Find length of the string of numeric characters. */
1597 scan = str;
Tim Petersda53afa2006-05-25 17:34:03 +00001598 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001599 ++scan;
1600
1601 /* Create a long object that can contain the largest possible
1602 * integer with this base and length. Note that there's no
1603 * need to initialize z->ob_digit -- no slot is read up before
1604 * being stored into.
1605 */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001606 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001607 /* Uncomment next line to test exceedingly rare copy code */
1608 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001609 assert(size_z > 0);
1610 z = _PyLong_New(size_z);
1611 if (z == NULL)
1612 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00001613 Py_SIZE(z) = 0;
Tim Peters696cf432006-05-24 21:10:40 +00001614
1615 /* `convwidth` consecutive input digits are treated as a single
1616 * digit in base `convmultmax`.
1617 */
1618 convwidth = convwidth_base[base];
1619 convmultmax = convmultmax_base[base];
1620
1621 /* Work ;-) */
1622 while (str < scan) {
1623 /* grab up to convwidth digits from the input string */
Tim Petersda53afa2006-05-25 17:34:03 +00001624 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00001625 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1626 c = (twodigits)(c * base +
Tim Petersda53afa2006-05-25 17:34:03 +00001627 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001628 assert(c < PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001629 }
1630
1631 convmult = convmultmax;
1632 /* Calculate the shift only if we couldn't get
1633 * convwidth digits.
1634 */
1635 if (i != convwidth) {
1636 convmult = base;
1637 for ( ; i > 1; --i)
1638 convmult *= base;
1639 }
1640
1641 /* Multiply z by convmult, and add c. */
1642 pz = z->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001643 pzstop = pz + Py_SIZE(z);
Tim Peters696cf432006-05-24 21:10:40 +00001644 for (; pz < pzstop; ++pz) {
1645 c += (twodigits)*pz * convmult;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001646 *pz = (digit)(c & PyLong_MASK);
1647 c >>= PyLong_SHIFT;
Tim Peters696cf432006-05-24 21:10:40 +00001648 }
1649 /* carry off the current end? */
1650 if (c) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001651 assert(c < PyLong_BASE);
Christian Heimese93237d2007-12-19 02:37:44 +00001652 if (Py_SIZE(z) < size_z) {
Tim Peters9faa3ed2006-05-30 15:53:34 +00001653 *pz = (digit)c;
Christian Heimese93237d2007-12-19 02:37:44 +00001654 ++Py_SIZE(z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001655 }
1656 else {
1657 PyLongObject *tmp;
1658 /* Extremely rare. Get more space. */
Christian Heimese93237d2007-12-19 02:37:44 +00001659 assert(Py_SIZE(z) == size_z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001660 tmp = _PyLong_New(size_z + 1);
1661 if (tmp == NULL) {
1662 Py_DECREF(z);
1663 return NULL;
1664 }
1665 memcpy(tmp->ob_digit,
1666 z->ob_digit,
1667 sizeof(digit) * size_z);
1668 Py_DECREF(z);
1669 z = tmp;
1670 z->ob_digit[size_z] = (digit)c;
1671 ++size_z;
1672 }
Tim Peters696cf432006-05-24 21:10:40 +00001673 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001674 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001675 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001676 if (z == NULL)
1677 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001678 if (str == start)
1679 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00001680 if (sign < 0)
Christian Heimese93237d2007-12-19 02:37:44 +00001681 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001682 if (*str == 'L' || *str == 'l')
1683 str++;
1684 while (*str && isspace(Py_CHARMASK(*str)))
1685 str++;
1686 if (*str != '\0')
1687 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001688 if (pend)
1689 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001690 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001691
1692 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001693 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001694 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1695 strobj = PyString_FromStringAndSize(orig_str, slen);
1696 if (strobj == NULL)
1697 return NULL;
1698 strrepr = PyObject_Repr(strobj);
1699 Py_DECREF(strobj);
1700 if (strrepr == NULL)
1701 return NULL;
1702 PyErr_Format(PyExc_ValueError,
1703 "invalid literal for long() with base %d: %s",
1704 base, PyString_AS_STRING(strrepr));
1705 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001706 return NULL;
1707}
1708
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001709#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001710PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001711PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001712{
Walter Dörwald07e14762002-11-06 16:15:14 +00001713 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001714 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001715
Walter Dörwald07e14762002-11-06 16:15:14 +00001716 if (buffer == NULL)
1717 return NULL;
1718
1719 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1720 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001721 return NULL;
1722 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001723 result = PyLong_FromString(buffer, NULL, base);
1724 PyMem_FREE(buffer);
1725 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001726}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001727#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001728
Tim Peters9f688bf2000-07-07 15:53:28 +00001729/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001730static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001731 (PyLongObject *, PyLongObject *, PyLongObject **);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001732static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00001733static int long_divrem(PyLongObject *, PyLongObject *,
1734 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001735
1736/* Long division with remainder, top-level routine */
1737
Guido van Rossume32e0141992-01-19 16:31:05 +00001738static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001739long_divrem(PyLongObject *a, PyLongObject *b,
1740 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001741{
Christian Heimese93237d2007-12-19 02:37:44 +00001742 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001743 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001744
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001745 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001746 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001747 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001748 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001749 }
1750 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001751 (size_a == size_b &&
1752 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001753 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001754 *pdiv = _PyLong_New(0);
Georg Brandlc02e1312007-04-11 17:16:24 +00001755 if (*pdiv == NULL)
1756 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757 Py_INCREF(a);
1758 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001759 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001760 }
1761 if (size_b == 1) {
1762 digit rem = 0;
1763 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001764 if (z == NULL)
1765 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001766 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandlc02e1312007-04-11 17:16:24 +00001767 if (*prem == NULL) {
1768 Py_DECREF(z);
1769 return -1;
1770 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001771 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001772 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001773 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001774 if (z == NULL)
1775 return -1;
1776 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001777 /* Set the signs.
1778 The quotient z has the sign of a*b;
1779 the remainder r has the sign of a,
1780 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001781 if ((a->ob_size < 0) != (b->ob_size < 0))
1782 z->ob_size = -(z->ob_size);
1783 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1784 (*prem)->ob_size = -((*prem)->ob_size);
1785 *pdiv = z;
1786 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001787}
1788
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001789/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001790
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001791static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001792x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001793{
Christian Heimese93237d2007-12-19 02:37:44 +00001794 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001795 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001796 PyLongObject *v = mul1(v1, d);
1797 PyLongObject *w = mul1(w1, d);
1798 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001799 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001800
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001801 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001802 Py_XDECREF(v);
1803 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001804 return NULL;
1805 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001806
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001807 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimese93237d2007-12-19 02:37:44 +00001808 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
1809 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001810
Christian Heimese93237d2007-12-19 02:37:44 +00001811 size_v = ABS(Py_SIZE(v));
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001812 k = size_v - size_w;
1813 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001814
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001815 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001816 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1817 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001818 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001819 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001820
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001821 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001822 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001823 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001824 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001825 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001826 if (vj == w->ob_digit[size_w-1])
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001827 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001828 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001829 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001830 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001831
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001832 while (w->ob_digit[size_w-2]*q >
1833 ((
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001834 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001835 + v->ob_digit[j-1]
1836 - q*w->ob_digit[size_w-1]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001837 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001838 + v->ob_digit[j-2])
1839 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001840
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001841 for (i = 0; i < size_w && i+k < size_v; ++i) {
1842 twodigits z = w->ob_digit[i] * q;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001843 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001844 carry += v->ob_digit[i+k] - z
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001845 + ((twodigits)zz << PyLong_SHIFT);
1846 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1847 carry = Py_ARITHMETIC_RIGHT_SHIFT(PyLong_BASE_TWODIGITS_TYPE,
1848 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00001849 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001850 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001851
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001852 if (i+k < size_v) {
1853 carry += v->ob_digit[i+k];
1854 v->ob_digit[i+k] = 0;
1855 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001856
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001857 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001858 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001859 else {
1860 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001861 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001862 carry = 0;
1863 for (i = 0; i < size_w && i+k < size_v; ++i) {
1864 carry += v->ob_digit[i+k] + w->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001865 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001866 carry = Py_ARITHMETIC_RIGHT_SHIFT(
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001867 PyLong_BASE_TWODIGITS_TYPE,
1868 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001869 }
1870 }
1871 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001872
Guido van Rossumc206c761995-01-10 15:23:19 +00001873 if (a == NULL)
1874 *prem = NULL;
1875 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001876 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001877 *prem = divrem1(v, d, &d);
1878 /* d receives the (unused) remainder */
1879 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001880 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001881 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001882 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001883 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001884 Py_DECREF(v);
1885 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001886 return a;
1887}
1888
1889/* Methods */
1890
1891static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001892long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001893{
Christian Heimese93237d2007-12-19 02:37:44 +00001894 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001895}
1896
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001897static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001898long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001899{
Eric Smith5e527eb2008-02-10 01:36:53 +00001900 return _PyLong_Format(v, 10, 1, 0);
Fred Drake121ee271999-12-23 15:41:28 +00001901}
1902
1903static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001904long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001905{
Eric Smith5e527eb2008-02-10 01:36:53 +00001906 return _PyLong_Format(v, 10, 0, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001907}
1908
1909static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001910long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001911{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001912 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001913
Christian Heimese93237d2007-12-19 02:37:44 +00001914 if (Py_SIZE(a) != Py_SIZE(b)) {
1915 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001916 sign = 0;
1917 else
Christian Heimese93237d2007-12-19 02:37:44 +00001918 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001919 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001920 else {
Christian Heimese93237d2007-12-19 02:37:44 +00001921 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001922 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1923 ;
1924 if (i < 0)
1925 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001926 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001927 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimese93237d2007-12-19 02:37:44 +00001928 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001929 sign = -sign;
1930 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001931 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001932 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001933}
1934
Guido van Rossum9bfef441993-03-29 10:43:31 +00001935static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001936long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001937{
1938 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001939 Py_ssize_t i;
1940 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001941
1942 /* This is designed so that Python ints and longs with the
1943 same value hash to the same value, otherwise comparisons
1944 of mapping keys will turn out weird */
1945 i = v->ob_size;
1946 sign = 1;
1947 x = 0;
1948 if (i < 0) {
1949 sign = -1;
1950 i = -(i);
1951 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001952#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Facundo Batistad544df72007-09-19 15:10:06 +00001953 /* The following loop produces a C long x such that (unsigned long)x
1954 is congruent to the absolute value of v modulo ULONG_MAX. The
1955 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00001956 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001957 /* Force a native long #-bits (32 or 64) circular shift */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001958 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001959 x += v->ob_digit[i];
Facundo Batistad544df72007-09-19 15:10:06 +00001960 /* If the addition above overflowed (thinking of x as
1961 unsigned), we compensate by incrementing. This preserves
1962 the value modulo ULONG_MAX. */
1963 if ((unsigned long)x < v->ob_digit[i])
1964 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001965 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001966#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001967 x = x * sign;
1968 if (x == -1)
1969 x = -2;
1970 return x;
1971}
1972
1973
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001974/* Add the absolute values of two long integers. */
1975
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001976static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001977x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001978{
Christian Heimese93237d2007-12-19 02:37:44 +00001979 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001980 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001981 int i;
1982 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001983
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001984 /* Ensure a is the larger of the two: */
1985 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001986 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001987 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988 size_a = size_b;
1989 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001990 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001992 if (z == NULL)
1993 return NULL;
1994 for (i = 0; i < size_b; ++i) {
1995 carry += a->ob_digit[i] + b->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001996 z->ob_digit[i] = carry & PyLong_MASK;
1997 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001998 }
1999 for (; i < size_a; ++i) {
2000 carry += a->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002001 z->ob_digit[i] = carry & PyLong_MASK;
2002 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002003 }
2004 z->ob_digit[i] = carry;
2005 return long_normalize(z);
2006}
2007
2008/* Subtract the absolute values of two integers. */
2009
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002010static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002011x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002012{
Christian Heimese93237d2007-12-19 02:37:44 +00002013 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002014 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002015 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002016 int sign = 1;
2017 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002018
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002019 /* Ensure a is the larger of the two: */
2020 if (size_a < size_b) {
2021 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002022 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002023 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002024 size_a = size_b;
2025 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002026 }
2027 else if (size_a == size_b) {
2028 /* Find highest digit where a and b differ: */
2029 i = size_a;
2030 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2031 ;
2032 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002033 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002034 if (a->ob_digit[i] < b->ob_digit[i]) {
2035 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002036 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002037 }
2038 size_a = size_b = i+1;
2039 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002040 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002041 if (z == NULL)
2042 return NULL;
2043 for (i = 0; i < size_b; ++i) {
2044 /* The following assumes unsigned arithmetic
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002045 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002046 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002047 z->ob_digit[i] = borrow & PyLong_MASK;
2048 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002049 borrow &= 1; /* Keep only one sign bit */
2050 }
2051 for (; i < size_a; ++i) {
2052 borrow = a->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002053 z->ob_digit[i] = borrow & PyLong_MASK;
2054 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002055 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002056 }
2057 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002058 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002059 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002060 return long_normalize(z);
2061}
2062
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002063static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002064long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002065{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002066 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002067
Neil Schemenauerba872e22001-01-04 01:46:03 +00002068 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2069
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002070 if (a->ob_size < 0) {
2071 if (b->ob_size < 0) {
2072 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002073 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002074 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002075 }
2076 else
2077 z = x_sub(b, a);
2078 }
2079 else {
2080 if (b->ob_size < 0)
2081 z = x_sub(a, b);
2082 else
2083 z = x_add(a, b);
2084 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002085 Py_DECREF(a);
2086 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002087 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002088}
2089
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002090static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002091long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002092{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002093 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002094
Neil Schemenauerba872e22001-01-04 01:46:03 +00002095 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2096
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002097 if (a->ob_size < 0) {
2098 if (b->ob_size < 0)
2099 z = x_sub(a, b);
2100 else
2101 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002102 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002103 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002104 }
2105 else {
2106 if (b->ob_size < 0)
2107 z = x_add(a, b);
2108 else
2109 z = x_sub(a, b);
2110 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002111 Py_DECREF(a);
2112 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002113 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002114}
2115
Tim Peters5af4e6c2002-08-12 02:31:19 +00002116/* Grade school multiplication, ignoring the signs.
2117 * Returns the absolute value of the product, or NULL if error.
2118 */
2119static PyLongObject *
2120x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002121{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002122 PyLongObject *z;
Christian Heimese93237d2007-12-19 02:37:44 +00002123 Py_ssize_t size_a = ABS(Py_SIZE(a));
2124 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002125 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002126
Tim Peters5af4e6c2002-08-12 02:31:19 +00002127 z = _PyLong_New(size_a + size_b);
2128 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002129 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002130
Christian Heimese93237d2007-12-19 02:37:44 +00002131 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002132 if (a == b) {
2133 /* Efficient squaring per HAC, Algorithm 14.16:
2134 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2135 * Gives slightly less than a 2x speedup when a == b,
2136 * via exploiting that each entry in the multiplication
2137 * pyramid appears twice (except for the size_a squares).
2138 */
2139 for (i = 0; i < size_a; ++i) {
2140 twodigits carry;
2141 twodigits f = a->ob_digit[i];
2142 digit *pz = z->ob_digit + (i << 1);
2143 digit *pa = a->ob_digit + i + 1;
2144 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002145
Tim Peters0973b992004-08-29 22:16:50 +00002146 SIGCHECK({
2147 Py_DECREF(z);
2148 return NULL;
2149 })
2150
2151 carry = *pz + f * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002152 *pz++ = (digit)(carry & PyLong_MASK);
2153 carry >>= PyLong_SHIFT;
2154 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002155
2156 /* Now f is added in twice in each column of the
2157 * pyramid it appears. Same as adding f<<1 once.
2158 */
2159 f <<= 1;
2160 while (pa < paend) {
2161 carry += *pz + *pa++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002162 *pz++ = (digit)(carry & PyLong_MASK);
2163 carry >>= PyLong_SHIFT;
2164 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002165 }
2166 if (carry) {
2167 carry += *pz;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002168 *pz++ = (digit)(carry & PyLong_MASK);
2169 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002170 }
2171 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002172 *pz += (digit)(carry & PyLong_MASK);
2173 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002174 }
Tim Peters0973b992004-08-29 22:16:50 +00002175 }
2176 else { /* a is not the same as b -- gradeschool long mult */
2177 for (i = 0; i < size_a; ++i) {
2178 twodigits carry = 0;
2179 twodigits f = a->ob_digit[i];
2180 digit *pz = z->ob_digit + i;
2181 digit *pb = b->ob_digit;
2182 digit *pbend = b->ob_digit + size_b;
2183
2184 SIGCHECK({
2185 Py_DECREF(z);
2186 return NULL;
2187 })
2188
2189 while (pb < pbend) {
2190 carry += *pz + *pb++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002191 *pz++ = (digit)(carry & PyLong_MASK);
2192 carry >>= PyLong_SHIFT;
2193 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002194 }
2195 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002196 *pz += (digit)(carry & PyLong_MASK);
2197 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002198 }
2199 }
Tim Peters44121a62002-08-12 06:17:58 +00002200 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002201}
2202
2203/* A helper for Karatsuba multiplication (k_mul).
2204 Takes a long "n" and an integer "size" representing the place to
2205 split, and sets low and high such that abs(n) == (high << size) + low,
2206 viewing the shift as being by digits. The sign bit is ignored, and
2207 the return values are >= 0.
2208 Returns 0 on success, -1 on failure.
2209*/
2210static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002211kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002212{
2213 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002214 Py_ssize_t size_lo, size_hi;
Christian Heimese93237d2007-12-19 02:37:44 +00002215 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002216
2217 size_lo = MIN(size_n, size);
2218 size_hi = size_n - size_lo;
2219
2220 if ((hi = _PyLong_New(size_hi)) == NULL)
2221 return -1;
2222 if ((lo = _PyLong_New(size_lo)) == NULL) {
2223 Py_DECREF(hi);
2224 return -1;
2225 }
2226
2227 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2228 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2229
2230 *high = long_normalize(hi);
2231 *low = long_normalize(lo);
2232 return 0;
2233}
2234
Tim Peters60004642002-08-12 22:01:34 +00002235static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2236
Tim Peters5af4e6c2002-08-12 02:31:19 +00002237/* Karatsuba multiplication. Ignores the input signs, and returns the
2238 * absolute value of the product (or NULL if error).
2239 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2240 */
2241static PyLongObject *
2242k_mul(PyLongObject *a, PyLongObject *b)
2243{
Christian Heimese93237d2007-12-19 02:37:44 +00002244 Py_ssize_t asize = ABS(Py_SIZE(a));
2245 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002246 PyLongObject *ah = NULL;
2247 PyLongObject *al = NULL;
2248 PyLongObject *bh = NULL;
2249 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002250 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002251 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002252 Py_ssize_t shift; /* the number of digits we split off */
2253 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002254
Tim Peters5af4e6c2002-08-12 02:31:19 +00002255 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2256 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2257 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002258 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002259 * By picking X to be a power of 2, "*X" is just shifting, and it's
2260 * been reduced to 3 multiplies on numbers half the size.
2261 */
2262
Tim Petersfc07e562002-08-12 02:54:10 +00002263 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002264 * is largest.
2265 */
Tim Peters738eda72002-08-12 15:08:20 +00002266 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002267 t1 = a;
2268 a = b;
2269 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002270
2271 i = asize;
2272 asize = bsize;
2273 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002274 }
2275
2276 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002277 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2278 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002279 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002280 return _PyLong_New(0);
2281 else
2282 return x_mul(a, b);
2283 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002284
Tim Peters60004642002-08-12 22:01:34 +00002285 /* If a is small compared to b, splitting on b gives a degenerate
2286 * case with ah==0, and Karatsuba may be (even much) less efficient
2287 * than "grade school" then. However, we can still win, by viewing
2288 * b as a string of "big digits", each of width a->ob_size. That
2289 * leads to a sequence of balanced calls to k_mul.
2290 */
2291 if (2 * asize <= bsize)
2292 return k_lopsided_mul(a, b);
2293
Tim Petersd6974a52002-08-13 20:37:51 +00002294 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002295 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002296 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002297 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002298
Tim Peters0973b992004-08-29 22:16:50 +00002299 if (a == b) {
2300 bh = ah;
2301 bl = al;
2302 Py_INCREF(bh);
2303 Py_INCREF(bl);
2304 }
2305 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002306
Tim Petersd64c1de2002-08-12 17:36:03 +00002307 /* The plan:
2308 * 1. Allocate result space (asize + bsize digits: that's always
2309 * enough).
2310 * 2. Compute ah*bh, and copy into result at 2*shift.
2311 * 3. Compute al*bl, and copy into result at 0. Note that this
2312 * can't overlap with #2.
2313 * 4. Subtract al*bl from the result, starting at shift. This may
2314 * underflow (borrow out of the high digit), but we don't care:
2315 * we're effectively doing unsigned arithmetic mod
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002316 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
Tim Petersd64c1de2002-08-12 17:36:03 +00002317 * borrows and carries out of the high digit can be ignored.
2318 * 5. Subtract ah*bh from the result, starting at shift.
2319 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2320 * at shift.
2321 */
2322
2323 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002324 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002325 if (ret == NULL) goto fail;
2326#ifdef Py_DEBUG
2327 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002328 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002329#endif
Tim Peters44121a62002-08-12 06:17:58 +00002330
Tim Petersd64c1de2002-08-12 17:36:03 +00002331 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002332 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002333 assert(Py_SIZE(t1) >= 0);
2334 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002335 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimese93237d2007-12-19 02:37:44 +00002336 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002337
2338 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimese93237d2007-12-19 02:37:44 +00002339 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002340 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002341 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002342 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002343
Tim Petersd64c1de2002-08-12 17:36:03 +00002344 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002345 if ((t2 = k_mul(al, bl)) == NULL) {
2346 Py_DECREF(t1);
2347 goto fail;
2348 }
Christian Heimese93237d2007-12-19 02:37:44 +00002349 assert(Py_SIZE(t2) >= 0);
2350 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2351 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002352
2353 /* Zero out remaining digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002354 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002355 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002356 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002357
Tim Petersd64c1de2002-08-12 17:36:03 +00002358 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2359 * because it's fresher in cache.
2360 */
Christian Heimese93237d2007-12-19 02:37:44 +00002361 i = Py_SIZE(ret) - shift; /* # digits after shift */
2362 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002363 Py_DECREF(t2);
2364
Christian Heimese93237d2007-12-19 02:37:44 +00002365 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002366 Py_DECREF(t1);
2367
Tim Petersd64c1de2002-08-12 17:36:03 +00002368 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002369 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2370 Py_DECREF(ah);
2371 Py_DECREF(al);
2372 ah = al = NULL;
2373
Tim Peters0973b992004-08-29 22:16:50 +00002374 if (a == b) {
2375 t2 = t1;
2376 Py_INCREF(t2);
2377 }
2378 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002379 Py_DECREF(t1);
2380 goto fail;
2381 }
2382 Py_DECREF(bh);
2383 Py_DECREF(bl);
2384 bh = bl = NULL;
2385
Tim Peters738eda72002-08-12 15:08:20 +00002386 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002387 Py_DECREF(t1);
2388 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002389 if (t3 == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002390 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002391
Tim Petersd6974a52002-08-13 20:37:51 +00002392 /* Add t3. It's not obvious why we can't run out of room here.
2393 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002394 */
Christian Heimese93237d2007-12-19 02:37:44 +00002395 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002396 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002397
Tim Peters5af4e6c2002-08-12 02:31:19 +00002398 return long_normalize(ret);
2399
2400 fail:
2401 Py_XDECREF(ret);
2402 Py_XDECREF(ah);
2403 Py_XDECREF(al);
2404 Py_XDECREF(bh);
2405 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002406 return NULL;
2407}
2408
Tim Petersd6974a52002-08-13 20:37:51 +00002409/* (*) Why adding t3 can't "run out of room" above.
2410
Tim Petersab86c2b2002-08-15 20:06:00 +00002411Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2412to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002413
Tim Petersab86c2b2002-08-15 20:06:00 +000024141. For any integer i, i = c(i/2) + f(i/2). In particular,
2415 bsize = c(bsize/2) + f(bsize/2).
24162. shift = f(bsize/2)
24173. asize <= bsize
24184. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2419 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002420
Tim Petersab86c2b2002-08-15 20:06:00 +00002421We allocated asize + bsize result digits, and add t3 into them at an offset
2422of shift. This leaves asize+bsize-shift allocated digit positions for t3
2423to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2424asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002425
Tim Petersab86c2b2002-08-15 20:06:00 +00002426bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2427at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002428
Tim Petersab86c2b2002-08-15 20:06:00 +00002429If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2430digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2431most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002432
Tim Petersab86c2b2002-08-15 20:06:00 +00002433The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002434
Tim Petersab86c2b2002-08-15 20:06:00 +00002435 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002436
Tim Petersab86c2b2002-08-15 20:06:00 +00002437and we have asize + c(bsize/2) available digit positions. We need to show
2438this is always enough. An instance of c(bsize/2) cancels out in both, so
2439the question reduces to whether asize digits is enough to hold
2440(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2441then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2442asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002443digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002444asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002445c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2446is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2447bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002448
Tim Peters48d52c02002-08-14 17:07:32 +00002449Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2450clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2451ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002452*/
2453
Tim Peters60004642002-08-12 22:01:34 +00002454/* b has at least twice the digits of a, and a is big enough that Karatsuba
2455 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2456 * of slices, each with a->ob_size digits, and multiply the slices by a,
2457 * one at a time. This gives k_mul balanced inputs to work with, and is
2458 * also cache-friendly (we compute one double-width slice of the result
2459 * at a time, then move on, never bactracking except for the helpful
2460 * single-width slice overlap between successive partial sums).
2461 */
2462static PyLongObject *
2463k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2464{
Christian Heimese93237d2007-12-19 02:37:44 +00002465 const Py_ssize_t asize = ABS(Py_SIZE(a));
2466 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002467 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002468 PyLongObject *ret;
2469 PyLongObject *bslice = NULL;
2470
2471 assert(asize > KARATSUBA_CUTOFF);
2472 assert(2 * asize <= bsize);
2473
2474 /* Allocate result space, and zero it out. */
2475 ret = _PyLong_New(asize + bsize);
2476 if (ret == NULL)
2477 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002478 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002479
2480 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002481 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002482 if (bslice == NULL)
2483 goto fail;
2484
2485 nbdone = 0;
2486 while (bsize > 0) {
2487 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002488 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002489
2490 /* Multiply the next slice of b by a. */
2491 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2492 nbtouse * sizeof(digit));
Christian Heimese93237d2007-12-19 02:37:44 +00002493 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002494 product = k_mul(a, bslice);
2495 if (product == NULL)
2496 goto fail;
2497
2498 /* Add into result. */
Christian Heimese93237d2007-12-19 02:37:44 +00002499 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2500 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002501 Py_DECREF(product);
2502
2503 bsize -= nbtouse;
2504 nbdone += nbtouse;
2505 }
2506
2507 Py_DECREF(bslice);
2508 return long_normalize(ret);
2509
2510 fail:
2511 Py_DECREF(ret);
2512 Py_XDECREF(bslice);
2513 return NULL;
2514}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002515
2516static PyObject *
2517long_mul(PyLongObject *v, PyLongObject *w)
2518{
2519 PyLongObject *a, *b, *z;
2520
2521 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002522 Py_INCREF(Py_NotImplemented);
2523 return Py_NotImplemented;
2524 }
2525
Tim Petersd64c1de2002-08-12 17:36:03 +00002526 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002527 /* Negate if exactly one of the inputs is negative. */
2528 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002529 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002530 Py_DECREF(a);
2531 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002532 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002533}
2534
Guido van Rossume32e0141992-01-19 16:31:05 +00002535/* The / and % operators are now defined in terms of divmod().
2536 The expression a mod b has the value a - b*floor(a/b).
2537 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002538 |a| by |b|, with the sign of a. This is also expressed
2539 as a - b*trunc(a/b), if trunc truncates towards zero.
2540 Some examples:
2541 a b a rem b a mod b
2542 13 10 3 3
2543 -13 10 -3 7
2544 13 -10 3 -7
2545 -13 -10 -3 -3
2546 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002547 have different signs. We then subtract one from the 'div'
2548 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002549
Tim Peters47e52ee2004-08-30 02:44:38 +00002550/* Compute
2551 * *pdiv, *pmod = divmod(v, w)
2552 * NULL can be passed for pdiv or pmod, in which case that part of
2553 * the result is simply thrown away. The caller owns a reference to
2554 * each of these it requests (does not pass NULL for).
2555 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002556static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002557l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002558 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002559{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002560 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002561
Guido van Rossume32e0141992-01-19 16:31:05 +00002562 if (long_divrem(v, w, &div, &mod) < 0)
2563 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00002564 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2565 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002566 PyLongObject *temp;
2567 PyLongObject *one;
2568 temp = (PyLongObject *) long_add(mod, w);
2569 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002570 mod = temp;
2571 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002572 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002573 return -1;
2574 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002575 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002576 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002577 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2578 Py_DECREF(mod);
2579 Py_DECREF(div);
2580 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002581 return -1;
2582 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002583 Py_DECREF(one);
2584 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002585 div = temp;
2586 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002587 if (pdiv != NULL)
2588 *pdiv = div;
2589 else
2590 Py_DECREF(div);
2591
2592 if (pmod != NULL)
2593 *pmod = mod;
2594 else
2595 Py_DECREF(mod);
2596
Guido van Rossume32e0141992-01-19 16:31:05 +00002597 return 0;
2598}
2599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002600static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002601long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002602{
Tim Peters47e52ee2004-08-30 02:44:38 +00002603 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002604
2605 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002606 if (l_divmod(a, b, &div, NULL) < 0)
2607 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002608 Py_DECREF(a);
2609 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002610 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002611}
2612
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002613static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002614long_classic_div(PyObject *v, PyObject *w)
2615{
Tim Peters47e52ee2004-08-30 02:44:38 +00002616 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002617
2618 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002619 if (Py_DivisionWarningFlag &&
2620 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2621 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002622 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002623 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002624 Py_DECREF(a);
2625 Py_DECREF(b);
2626 return (PyObject *)div;
2627}
2628
2629static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002630long_true_divide(PyObject *v, PyObject *w)
2631{
Tim Peterse2a60002001-09-04 06:17:36 +00002632 PyLongObject *a, *b;
2633 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002634 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002635
2636 CONVERT_BINOP(v, w, &a, &b);
2637 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2638 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002639 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2640 Py_DECREF(a);
2641 Py_DECREF(b);
2642 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002643 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002644 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2645 but should really be set correctly after sucessful calls to
2646 _PyLong_AsScaledDouble() */
2647 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002648
2649 if (bd == 0.0) {
2650 PyErr_SetString(PyExc_ZeroDivisionError,
2651 "long division or modulo by zero");
2652 return NULL;
2653 }
2654
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002655 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002656 ad /= bd; /* overflow/underflow impossible here */
2657 aexp -= bexp;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002658 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002659 goto overflow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002660 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002661 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002662 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002663 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002664 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002665 goto overflow;
2666 return PyFloat_FromDouble(ad);
2667
2668overflow:
2669 PyErr_SetString(PyExc_OverflowError,
2670 "long/long too large for a float");
2671 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002672
Tim Peters20dab9f2001-09-04 05:31:47 +00002673}
2674
2675static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002676long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002677{
Tim Peters47e52ee2004-08-30 02:44:38 +00002678 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002679
2680 CONVERT_BINOP(v, w, &a, &b);
2681
Tim Peters47e52ee2004-08-30 02:44:38 +00002682 if (l_divmod(a, b, NULL, &mod) < 0)
2683 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002684 Py_DECREF(a);
2685 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002686 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002687}
2688
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002689static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002690long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002691{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002692 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002693 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002694
2695 CONVERT_BINOP(v, w, &a, &b);
2696
2697 if (l_divmod(a, b, &div, &mod) < 0) {
2698 Py_DECREF(a);
2699 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002700 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002701 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002702 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002703 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002704 PyTuple_SetItem(z, 0, (PyObject *) div);
2705 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002706 }
2707 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002708 Py_DECREF(div);
2709 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002710 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002711 Py_DECREF(a);
2712 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002713 return z;
2714}
2715
Tim Peters47e52ee2004-08-30 02:44:38 +00002716/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002717static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002718long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002719{
Tim Peters47e52ee2004-08-30 02:44:38 +00002720 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2721 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002722
Tim Peters47e52ee2004-08-30 02:44:38 +00002723 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002724 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002725 PyLongObject *temp = NULL;
2726
2727 /* 5-ary values. If the exponent is large enough, table is
2728 * precomputed so that table[i] == a**i % c for i in range(32).
2729 */
2730 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2731 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2732
2733 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002734 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002735 if (PyLong_Check(x)) {
2736 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002737 Py_INCREF(x);
2738 }
Tim Petersde7990b2005-07-17 23:45:23 +00002739 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002740 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002741 if (c == NULL)
2742 goto Error;
2743 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002744 else if (x == Py_None)
2745 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002746 else {
2747 Py_DECREF(a);
2748 Py_DECREF(b);
2749 Py_INCREF(Py_NotImplemented);
2750 return Py_NotImplemented;
2751 }
Tim Peters4c483c42001-09-05 06:24:58 +00002752
Christian Heimese93237d2007-12-19 02:37:44 +00002753 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00002754 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002755 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002756 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002757 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002758 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002759 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002760 /* else return a float. This works because we know
2761 that this calls float_pow() which converts its
2762 arguments to double. */
2763 Py_DECREF(a);
2764 Py_DECREF(b);
2765 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002766 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002767 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002768
2769 if (c) {
2770 /* if modulus == 0:
2771 raise ValueError() */
Christian Heimese93237d2007-12-19 02:37:44 +00002772 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002773 PyErr_SetString(PyExc_ValueError,
2774 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002775 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002776 }
2777
2778 /* if modulus < 0:
2779 negativeOutput = True
2780 modulus = -modulus */
Christian Heimese93237d2007-12-19 02:37:44 +00002781 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002782 negativeOutput = 1;
2783 temp = (PyLongObject *)_PyLong_Copy(c);
2784 if (temp == NULL)
2785 goto Error;
2786 Py_DECREF(c);
2787 c = temp;
2788 temp = NULL;
2789 c->ob_size = - c->ob_size;
2790 }
2791
2792 /* if modulus == 1:
2793 return 0 */
Christian Heimese93237d2007-12-19 02:37:44 +00002794 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002795 z = (PyLongObject *)PyLong_FromLong(0L);
2796 goto Done;
2797 }
2798
2799 /* if base < 0:
2800 base = base % modulus
2801 Having the base positive just makes things easier. */
Christian Heimese93237d2007-12-19 02:37:44 +00002802 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002803 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002804 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002805 Py_DECREF(a);
2806 a = temp;
2807 temp = NULL;
2808 }
2809 }
2810
2811 /* At this point a, b, and c are guaranteed non-negative UNLESS
2812 c is NULL, in which case a may be negative. */
2813
2814 z = (PyLongObject *)PyLong_FromLong(1L);
2815 if (z == NULL)
2816 goto Error;
2817
2818 /* Perform a modular reduction, X = X % c, but leave X alone if c
2819 * is NULL.
2820 */
2821#define REDUCE(X) \
2822 if (c != NULL) { \
2823 if (l_divmod(X, c, NULL, &temp) < 0) \
2824 goto Error; \
2825 Py_XDECREF(X); \
2826 X = temp; \
2827 temp = NULL; \
2828 }
2829
2830 /* Multiply two values, then reduce the result:
2831 result = X*Y % c. If c is NULL, skip the mod. */
2832#define MULT(X, Y, result) \
2833{ \
2834 temp = (PyLongObject *)long_mul(X, Y); \
2835 if (temp == NULL) \
2836 goto Error; \
2837 Py_XDECREF(result); \
2838 result = temp; \
2839 temp = NULL; \
2840 REDUCE(result) \
2841}
2842
Christian Heimese93237d2007-12-19 02:37:44 +00002843 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002844 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2845 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimese93237d2007-12-19 02:37:44 +00002846 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002847 digit bi = b->ob_digit[i];
2848
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002849 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002850 MULT(z, z, z)
2851 if (bi & j)
2852 MULT(z, a, z)
2853 }
2854 }
2855 }
2856 else {
2857 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2858 Py_INCREF(z); /* still holds 1L */
2859 table[0] = z;
2860 for (i = 1; i < 32; ++i)
2861 MULT(table[i-1], a, table[i])
2862
Christian Heimese93237d2007-12-19 02:37:44 +00002863 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002864 const digit bi = b->ob_digit[i];
2865
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002866 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002867 const int index = (bi >> j) & 0x1f;
2868 for (k = 0; k < 5; ++k)
2869 MULT(z, z, z)
2870 if (index)
2871 MULT(z, table[index], z)
2872 }
2873 }
2874 }
2875
Christian Heimese93237d2007-12-19 02:37:44 +00002876 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002877 temp = (PyLongObject *)long_sub(z, c);
2878 if (temp == NULL)
2879 goto Error;
2880 Py_DECREF(z);
2881 z = temp;
2882 temp = NULL;
2883 }
2884 goto Done;
2885
2886 Error:
2887 if (z != NULL) {
2888 Py_DECREF(z);
2889 z = NULL;
2890 }
2891 /* fall through */
2892 Done:
Christian Heimese93237d2007-12-19 02:37:44 +00002893 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002894 for (i = 0; i < 32; ++i)
2895 Py_XDECREF(table[i]);
2896 }
Tim Petersde7990b2005-07-17 23:45:23 +00002897 Py_DECREF(a);
2898 Py_DECREF(b);
2899 Py_XDECREF(c);
2900 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002901 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002902}
2903
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002904static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002905long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002906{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002907 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002908 PyLongObject *x;
2909 PyLongObject *w;
2910 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002911 if (w == NULL)
2912 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002913 x = (PyLongObject *) long_add(v, w);
2914 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002915 if (x == NULL)
2916 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002917 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002918 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002919}
2920
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002921static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002922long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002923{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002924 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002925 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002926 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002927 Py_INCREF(v);
2928 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002929 }
Tim Peters69c2de32001-09-11 22:31:33 +00002930 z = (PyLongObject *)_PyLong_Copy(v);
2931 if (z != NULL)
2932 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002933 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002934}
2935
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002936static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002937long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002938{
2939 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002940 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002941 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002942 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002943}
2944
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002945static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002946long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002947{
Christian Heimese93237d2007-12-19 02:37:44 +00002948 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002949}
2950
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002951static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002952long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002953{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002954 PyLongObject *a, *b;
2955 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002956 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002957 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002958 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002959
Neil Schemenauerba872e22001-01-04 01:46:03 +00002960 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2961
Christian Heimese93237d2007-12-19 02:37:44 +00002962 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002963 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002964 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002965 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002966 if (a1 == NULL)
2967 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002968 a2 = (PyLongObject *) long_rshift(a1, b);
2969 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002970 if (a2 == NULL)
2971 goto rshift_error;
2972 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002973 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002974 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002975 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002976
Neil Schemenauerba872e22001-01-04 01:46:03 +00002977 shiftby = PyLong_AsLong((PyObject *)b);
2978 if (shiftby == -1L && PyErr_Occurred())
2979 goto rshift_error;
2980 if (shiftby < 0) {
2981 PyErr_SetString(PyExc_ValueError,
2982 "negative shift count");
2983 goto rshift_error;
2984 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002985 wordshift = shiftby / PyLong_SHIFT;
Christian Heimese93237d2007-12-19 02:37:44 +00002986 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002987 if (newsize <= 0) {
2988 z = _PyLong_New(0);
2989 Py_DECREF(a);
2990 Py_DECREF(b);
2991 return (PyObject *)z;
2992 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002993 loshift = shiftby % PyLong_SHIFT;
2994 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002995 lomask = ((digit)1 << hishift) - 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002996 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002997 z = _PyLong_New(newsize);
2998 if (z == NULL)
2999 goto rshift_error;
Christian Heimese93237d2007-12-19 02:37:44 +00003000 if (Py_SIZE(a) < 0)
3001 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003002 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3003 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3004 if (i+1 < newsize)
3005 z->ob_digit[i] |=
3006 (a->ob_digit[j+1] << hishift) & himask;
3007 }
3008 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003009 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003010rshift_error:
3011 Py_DECREF(a);
3012 Py_DECREF(b);
3013 return (PyObject *) z;
3014
Guido van Rossumc6913e71991-11-19 20:26:46 +00003015}
3016
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003017static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003018long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003019{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003020 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003021 PyLongObject *a, *b;
3022 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003023 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003024 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003025 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003026
Neil Schemenauerba872e22001-01-04 01:46:03 +00003027 CONVERT_BINOP(v, w, &a, &b);
3028
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003029 shiftby = PyLong_AsLong((PyObject *)b);
3030 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003031 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003032 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003033 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003034 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003035 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003036 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003037 PyErr_SetString(PyExc_ValueError,
3038 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003039 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003040 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003041 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3042 wordshift = (int)shiftby / PyLong_SHIFT;
3043 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003044
3045 oldsize = ABS(a->ob_size);
3046 newsize = oldsize + wordshift;
3047 if (remshift)
3048 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003049 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003050 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003051 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003052 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003053 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003054 for (i = 0; i < wordshift; i++)
3055 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003056 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003057 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003058 accum |= (twodigits)a->ob_digit[j] << remshift;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003059 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3060 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003061 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003062 if (remshift)
3063 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003064 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003065 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003066 z = long_normalize(z);
3067lshift_error:
3068 Py_DECREF(a);
3069 Py_DECREF(b);
3070 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003071}
3072
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003073
3074/* Bitwise and/xor/or operations */
3075
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003076static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003077long_bitwise(PyLongObject *a,
3078 int op, /* '&', '|', '^' */
3079 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003080{
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003081 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003082 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003083 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003084 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003085 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003086 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003087 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003088
Christian Heimese93237d2007-12-19 02:37:44 +00003089 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003090 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003091 if (a == NULL)
3092 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003093 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003094 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003095 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003096 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003097 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003098 }
Christian Heimese93237d2007-12-19 02:37:44 +00003099 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003100 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003101 if (b == NULL) {
3102 Py_DECREF(a);
3103 return NULL;
3104 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003105 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003106 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003107 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003108 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003109 maskb = 0;
3110 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003111
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003112 negz = 0;
3113 switch (op) {
3114 case '^':
3115 if (maska != maskb) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003116 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003117 negz = -1;
3118 }
3119 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003120 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003121 if (maska && maskb) {
3122 op = '|';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003123 maska ^= PyLong_MASK;
3124 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003125 negz = -1;
3126 }
3127 break;
3128 case '|':
3129 if (maska || maskb) {
3130 op = '&';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003131 maska ^= PyLong_MASK;
3132 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003133 negz = -1;
3134 }
3135 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003136 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003137
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003138 /* JRH: The original logic here was to allocate the result value (z)
3139 as the longer of the two operands. However, there are some cases
3140 where the result is guaranteed to be shorter than that: AND of two
3141 positives, OR of two negatives: use the shorter number. AND with
3142 mixed signs: use the positive number. OR with mixed signs: use the
3143 negative number. After the transformations above, op will be '&'
3144 iff one of these cases applies, and mask will be non-0 for operands
3145 whose length should be ignored.
3146 */
3147
Christian Heimese93237d2007-12-19 02:37:44 +00003148 size_a = Py_SIZE(a);
3149 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003150 size_z = op == '&'
3151 ? (maska
3152 ? size_b
3153 : (maskb ? size_a : MIN(size_a, size_b)))
3154 : MAX(size_a, size_b);
3155 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003156 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003157 Py_DECREF(a);
3158 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003159 return NULL;
3160 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003161
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003162 for (i = 0; i < size_z; ++i) {
3163 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3164 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3165 switch (op) {
3166 case '&': z->ob_digit[i] = diga & digb; break;
3167 case '|': z->ob_digit[i] = diga | digb; break;
3168 case '^': z->ob_digit[i] = diga ^ digb; break;
3169 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003170 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003172 Py_DECREF(a);
3173 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003174 z = long_normalize(z);
3175 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003176 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003177 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003178 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003179 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003180}
3181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003182static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003183long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003184{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003185 PyLongObject *a, *b;
3186 PyObject *c;
3187 CONVERT_BINOP(v, w, &a, &b);
3188 c = long_bitwise(a, '&', b);
3189 Py_DECREF(a);
3190 Py_DECREF(b);
3191 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003192}
3193
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003194static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003195long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003196{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003197 PyLongObject *a, *b;
3198 PyObject *c;
3199 CONVERT_BINOP(v, w, &a, &b);
3200 c = long_bitwise(a, '^', b);
3201 Py_DECREF(a);
3202 Py_DECREF(b);
3203 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003204}
3205
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003206static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003207long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003208{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003209 PyLongObject *a, *b;
3210 PyObject *c;
3211 CONVERT_BINOP(v, w, &a, &b);
3212 c = long_bitwise(a, '|', b);
3213 Py_DECREF(a);
3214 Py_DECREF(b);
3215 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003216}
3217
Guido van Rossum234f9421993-06-17 12:35:49 +00003218static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003219long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003220{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003221 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003222 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandlc02e1312007-04-11 17:16:24 +00003223 if (*pw == NULL)
3224 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003225 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003226 return 0;
3227 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003228 else if (PyLong_Check(*pw)) {
3229 Py_INCREF(*pv);
3230 Py_INCREF(*pw);
3231 return 0;
3232 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003233 return 1; /* Can't do it */
3234}
3235
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003236static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003237long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003238{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003239 if (PyLong_CheckExact(v))
3240 Py_INCREF(v);
3241 else
3242 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003243 return v;
3244}
3245
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003246static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003247long_int(PyObject *v)
3248{
3249 long x;
3250 x = PyLong_AsLong(v);
3251 if (PyErr_Occurred()) {
3252 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3253 PyErr_Clear();
3254 if (PyLong_CheckExact(v)) {
3255 Py_INCREF(v);
3256 return v;
3257 }
3258 else
3259 return _PyLong_Copy((PyLongObject *)v);
3260 }
3261 else
3262 return NULL;
3263 }
3264 return PyInt_FromLong(x);
3265}
3266
3267static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003268long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003269{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003270 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003271 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003272 if (result == -1.0 && PyErr_Occurred())
3273 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003274 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003275}
3276
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003277static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003278long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003279{
Eric Smith5e527eb2008-02-10 01:36:53 +00003280 return _PyLong_Format(v, 8, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003281}
3282
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003283static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003284long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003285{
Eric Smith5e527eb2008-02-10 01:36:53 +00003286 return _PyLong_Format(v, 16, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003287}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003288
3289static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003290long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003291
Tim Peters6d6c1a32001-08-02 04:15:00 +00003292static PyObject *
3293long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3294{
3295 PyObject *x = NULL;
3296 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003297 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003298
Guido van Rossumbef14172001-08-29 15:47:46 +00003299 if (type != &PyLong_Type)
3300 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003301 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3302 &x, &base))
3303 return NULL;
3304 if (x == NULL)
3305 return PyLong_FromLong(0L);
3306 if (base == -909)
3307 return PyNumber_Long(x);
Georg Brandl00cd8182007-03-06 18:41:12 +00003308 else if (PyString_Check(x)) {
3309 /* Since PyLong_FromString doesn't have a length parameter,
3310 * check here for possible NULs in the string. */
3311 char *string = PyString_AS_STRING(x);
3312 if (strlen(string) != PyString_Size(x)) {
3313 /* create a repr() of the input string,
3314 * just like PyLong_FromString does. */
3315 PyObject *srepr;
3316 srepr = PyObject_Repr(x);
3317 if (srepr == NULL)
3318 return NULL;
3319 PyErr_Format(PyExc_ValueError,
3320 "invalid literal for long() with base %d: %s",
3321 base, PyString_AS_STRING(srepr));
3322 Py_DECREF(srepr);
3323 return NULL;
3324 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003325 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandl00cd8182007-03-06 18:41:12 +00003326 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003327#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003328 else if (PyUnicode_Check(x))
3329 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3330 PyUnicode_GET_SIZE(x),
3331 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003332#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003333 else {
3334 PyErr_SetString(PyExc_TypeError,
3335 "long() can't convert non-string with explicit base");
3336 return NULL;
3337 }
3338}
3339
Guido van Rossumbef14172001-08-29 15:47:46 +00003340/* Wimpy, slow approach to tp_new calls for subtypes of long:
3341 first create a regular long from whatever arguments we got,
3342 then allocate a subtype instance and initialize it from
3343 the regular long. The regular long is then thrown away.
3344*/
3345static PyObject *
3346long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3347{
Anthony Baxter377be112006-04-11 06:54:30 +00003348 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003349 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003350
3351 assert(PyType_IsSubtype(type, &PyLong_Type));
3352 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3353 if (tmp == NULL)
3354 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003355 assert(PyLong_CheckExact(tmp));
Christian Heimese93237d2007-12-19 02:37:44 +00003356 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003357 if (n < 0)
3358 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003359 newobj = (PyLongObject *)type->tp_alloc(type, n);
3360 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003361 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003362 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003363 }
Anthony Baxter377be112006-04-11 06:54:30 +00003364 assert(PyLong_Check(newobj));
Christian Heimese93237d2007-12-19 02:37:44 +00003365 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003366 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003367 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003368 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003369 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003370}
3371
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003372static PyObject *
3373long_getnewargs(PyLongObject *v)
3374{
3375 return Py_BuildValue("(N)", _PyLong_Copy(v));
3376}
3377
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003378static PyObject *
3379long_getN(PyLongObject *v, void *context) {
3380 return PyLong_FromLong((intptr_t)context);
3381}
3382
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003383static PyMethodDef long_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003384 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3385 "Returns self, the complex conjugate of any long."},
3386 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3387 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003388 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3389 {NULL, NULL} /* sentinel */
3390};
3391
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003392static PyGetSetDef long_getset[] = {
3393 {"real",
3394 (getter)long_long, (setter)NULL,
3395 "the real part of a complex number",
3396 NULL},
3397 {"imag",
3398 (getter)long_getN, (setter)NULL,
3399 "the imaginary part of a complex number",
3400 (void*)0},
3401 {"numerator",
3402 (getter)long_long, (setter)NULL,
3403 "the numerator of a rational number in lowest terms",
3404 NULL},
3405 {"denominator",
3406 (getter)long_getN, (setter)NULL,
3407 "the denominator of a rational number in lowest terms",
3408 (void*)1},
3409 {NULL} /* Sentinel */
3410};
3411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003412PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003413"long(x[, base]) -> integer\n\
3414\n\
3415Convert a string or number to a long integer, if possible. A floating\n\
3416point argument will be truncated towards zero (this does not include a\n\
3417string representation of a floating point number!) When converting a\n\
3418string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003419converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003420
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003421static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003422 (binaryfunc) long_add, /*nb_add*/
3423 (binaryfunc) long_sub, /*nb_subtract*/
3424 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003425 long_classic_div, /*nb_divide*/
3426 long_mod, /*nb_remainder*/
3427 long_divmod, /*nb_divmod*/
3428 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003429 (unaryfunc) long_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003430 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003431 (unaryfunc) long_abs, /*tp_absolute*/
3432 (inquiry) long_nonzero, /*tp_nonzero*/
3433 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003434 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003435 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003436 long_and, /*nb_and*/
3437 long_xor, /*nb_xor*/
3438 long_or, /*nb_or*/
3439 long_coerce, /*nb_coerce*/
3440 long_int, /*nb_int*/
3441 long_long, /*nb_long*/
3442 long_float, /*nb_float*/
3443 long_oct, /*nb_oct*/
3444 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003445 0, /* nb_inplace_add */
3446 0, /* nb_inplace_subtract */
3447 0, /* nb_inplace_multiply */
3448 0, /* nb_inplace_divide */
3449 0, /* nb_inplace_remainder */
3450 0, /* nb_inplace_power */
3451 0, /* nb_inplace_lshift */
3452 0, /* nb_inplace_rshift */
3453 0, /* nb_inplace_and */
3454 0, /* nb_inplace_xor */
3455 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003456 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003457 long_true_divide, /* nb_true_divide */
3458 0, /* nb_inplace_floor_divide */
3459 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003460 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003461};
3462
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003463PyTypeObject PyLong_Type = {
3464 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003465 0, /* ob_size */
3466 "long", /* tp_name */
3467 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3468 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003469 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003470 0, /* tp_print */
3471 0, /* tp_getattr */
3472 0, /* tp_setattr */
3473 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003474 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003475 &long_as_number, /* tp_as_number */
3476 0, /* tp_as_sequence */
3477 0, /* tp_as_mapping */
3478 (hashfunc)long_hash, /* tp_hash */
3479 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003480 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003481 PyObject_GenericGetAttr, /* tp_getattro */
3482 0, /* tp_setattro */
3483 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003484 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00003485 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003486 long_doc, /* tp_doc */
3487 0, /* tp_traverse */
3488 0, /* tp_clear */
3489 0, /* tp_richcompare */
3490 0, /* tp_weaklistoffset */
3491 0, /* tp_iter */
3492 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003493 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003494 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003495 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003496 0, /* tp_base */
3497 0, /* tp_dict */
3498 0, /* tp_descr_get */
3499 0, /* tp_descr_set */
3500 0, /* tp_dictoffset */
3501 0, /* tp_init */
3502 0, /* tp_alloc */
3503 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003504 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003505};