blob: 46ed7132aad1f36c7c9aebd299d29a1f32209683 [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"
Eric Smitha9f7d622008-02-17 19:46:49 +00009#include "formatter_string.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000011#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000012
Tim Peters5af4e6c2002-08-12 02:31:19 +000013/* For long multiplication, use the O(N**2) school algorithm unless
14 * both operands contain more than KARATSUBA_CUTOFF digits (this
Christian Heimes7f39c9f2008-01-25 12:18:43 +000015 * being an internal Python long digit, in base PyLong_BASE).
Tim Peters5af4e6c2002-08-12 02:31:19 +000016 */
Tim Peters0973b992004-08-29 22:16:50 +000017#define KARATSUBA_CUTOFF 70
18#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000019
Tim Peters47e52ee2004-08-30 02:44:38 +000020/* For exponentiation, use the binary left-to-right algorithm
21 * unless the exponent contains more than FIVEARY_CUTOFF digits.
22 * In that case, do 5 bits at a time. The potential drawback is that
23 * a table of 2**5 intermediate results is computed.
24 */
25#define FIVEARY_CUTOFF 8
26
Guido van Rossume32e0141992-01-19 16:31:05 +000027#define ABS(x) ((x) < 0 ? -(x) : (x))
28
Tim Peters5af4e6c2002-08-12 02:31:19 +000029#undef MIN
30#undef MAX
31#define MAX(x, y) ((x) < (y) ? (y) : (x))
32#define MIN(x, y) ((x) > (y) ? (y) : (x))
33
Guido van Rossume32e0141992-01-19 16:31:05 +000034/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000035static PyLongObject *long_normalize(PyLongObject *);
36static PyLongObject *mul1(PyLongObject *, wdigit);
37static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000038static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Tim Petersd89fc222006-05-25 22:28:46 +000043 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000044 }
45
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046/* Normalize (remove leading zeros from) a long int object.
47 Doesn't attempt to free the storage--in most cases, due to the nature
48 of the algorithms used, this could save at most be one word anyway. */
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000051long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052{
Christian Heimese93237d2007-12-19 02:37:44 +000053 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +000054 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000055
Guido van Rossumedcc38a1991-05-05 20:09:44 +000056 while (i > 0 && v->ob_digit[i-1] == 0)
57 --i;
58 if (i != j)
Christian Heimese93237d2007-12-19 02:37:44 +000059 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060 return v;
61}
62
63/* Allocate a new long int object with size digits.
64 Return NULL and set exception if we run out of memory. */
65
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000067_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068{
Neal Norwitzc6a989a2006-05-10 06:57:58 +000069 if (size > PY_SSIZE_T_MAX) {
Martin v. Löwis18e16552006-02-15 17:27:45 +000070 PyErr_NoMemory();
71 return NULL;
72 }
Christian Heimes4956d2b2008-01-18 19:12:56 +000073 /* coverity[ampersand_in_size] */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000075}
76
Tim Peters64b5ce32001-09-10 20:52:51 +000077PyObject *
78_PyLong_Copy(PyLongObject *src)
79{
80 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000081 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000082
83 assert(src != NULL);
84 i = src->ob_size;
85 if (i < 0)
86 i = -(i);
87 result = _PyLong_New(i);
88 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000089 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000090 while (--i >= 0)
91 result->ob_digit[i] = src->ob_digit[i];
92 }
93 return (PyObject *)result;
94}
95
Guido van Rossumedcc38a1991-05-05 20:09:44 +000096/* Create a new long int object from a C long int */
97
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000099PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000100{
Tim Petersce9de2f2001-06-14 04:56:19 +0000101 PyLongObject *v;
102 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
103 int ndigits = 0;
104 int negative = 0;
105
106 if (ival < 0) {
107 ival = -ival;
108 negative = 1;
109 }
110
111 /* Count the number of Python digits.
112 We used to pick 5 ("big enough for anything"), but that's a
113 waste of time and space given that 5*15 = 75 bits are rarely
114 needed. */
115 t = (unsigned long)ival;
116 while (t) {
117 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000118 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000119 }
120 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000121 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000122 digit *p = v->ob_digit;
123 v->ob_size = negative ? -ndigits : ndigits;
124 t = (unsigned long)ival;
125 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000126 *p++ = (digit)(t & PyLong_MASK);
127 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000129 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000131}
132
Guido van Rossum53756b11997-01-03 17:14:46 +0000133/* Create a new long int object from a C unsigned long int */
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000136PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000137{
Tim Petersce9de2f2001-06-14 04:56:19 +0000138 PyLongObject *v;
139 unsigned long t;
140 int ndigits = 0;
141
142 /* Count the number of Python digits. */
143 t = (unsigned long)ival;
144 while (t) {
145 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000146 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000147 }
148 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000149 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000150 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000151 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000152 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000153 *p++ = (digit)(ival & PyLong_MASK);
154 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000156 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000158}
159
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000160/* Create a new long int object from a C double */
161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000164{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000166 double frac;
167 int i, ndig, expo, neg;
168 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000169 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000170 PyErr_SetString(PyExc_OverflowError,
171 "cannot convert float infinity to long");
172 return NULL;
173 }
Christian Heimes8267d1d2008-01-04 00:37:34 +0000174 if (Py_IS_NAN(dval)) {
175 return PyLong_FromLong(0L);
176 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000177 if (dval < 0.0) {
178 neg = 1;
179 dval = -dval;
180 }
181 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
182 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 return PyLong_FromLong(0L);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000184 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000186 if (v == NULL)
187 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000188 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000189 for (i = ndig; --i >= 0; ) {
190 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000191 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000192 frac = frac - (double)bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000193 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000194 }
195 if (neg)
Christian Heimese93237d2007-12-19 02:37:44 +0000196 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000198}
199
Armin Rigo7ccbca92006-10-04 12:17:45 +0000200/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
201 * anything about what happens when a signed integer operation overflows,
202 * and some compilers think they're doing you a favor by being "clever"
203 * then. The bit pattern for the largest postive signed long is
204 * (unsigned long)LONG_MAX, and for the smallest negative signed long
205 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
206 * However, some other compilers warn about applying unary minus to an
207 * unsigned operand. Hence the weird "0-".
208 */
209#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
210#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
211
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000212/* Get a C long int from a long int object.
213 Returns -1 and sets an error condition if overflow occurs. */
214
215long
Tim Peters9f688bf2000-07-07 15:53:28 +0000216PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000217{
Guido van Rossumf7531811998-05-26 14:33:37 +0000218 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000220 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 Py_ssize_t i;
222 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000223
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000225 if (vv != NULL && PyInt_Check(vv))
226 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000228 return -1;
229 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000231 i = v->ob_size;
232 sign = 1;
233 x = 0;
234 if (i < 0) {
235 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000236 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000237 }
238 while (--i >= 0) {
239 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000240 x = (x << PyLong_SHIFT) + v->ob_digit[i];
241 if ((x >> PyLong_SHIFT) != prev)
Guido van Rossumf7531811998-05-26 14:33:37 +0000242 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000243 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000244 /* Haven't lost any bits, but casting to long requires extra care
245 * (see comment above).
246 */
247 if (x <= (unsigned long)LONG_MAX) {
248 return (long)x * sign;
249 }
250 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
251 return LONG_MIN;
252 }
253 /* else overflow */
Guido van Rossumf7531811998-05-26 14:33:37 +0000254
255 overflow:
256 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000257 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000258 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000259}
260
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000261/* Get a Py_ssize_t from a long int object.
262 Returns -1 and sets an error condition if overflow occurs. */
263
264Py_ssize_t
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000265PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000266 register PyLongObject *v;
267 size_t x, prev;
268 Py_ssize_t i;
269 int sign;
270
271 if (vv == NULL || !PyLong_Check(vv)) {
272 PyErr_BadInternalCall();
273 return -1;
274 }
275 v = (PyLongObject *)vv;
276 i = v->ob_size;
277 sign = 1;
278 x = 0;
279 if (i < 0) {
280 sign = -1;
281 i = -(i);
282 }
283 while (--i >= 0) {
284 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000285 x = (x << PyLong_SHIFT) + v->ob_digit[i];
286 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000287 goto overflow;
288 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000289 /* Haven't lost any bits, but casting to a signed type requires
290 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000291 */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000292 if (x <= (size_t)PY_SSIZE_T_MAX) {
293 return (Py_ssize_t)x * sign;
294 }
295 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
296 return PY_SSIZE_T_MIN;
297 }
298 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299
300 overflow:
301 PyErr_SetString(PyExc_OverflowError,
302 "long int too large to convert to int");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000303 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000304}
305
Guido van Rossumd8c80482002-08-13 00:24:58 +0000306/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000307 Returns -1 and sets an error condition if overflow occurs. */
308
309unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000310PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000313 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000314 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000315
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000317 if (vv != NULL && PyInt_Check(vv)) {
318 long val = PyInt_AsLong(vv);
319 if (val < 0) {
320 PyErr_SetString(PyExc_OverflowError,
321 "can't convert negative value to unsigned long");
322 return (unsigned long) -1;
323 }
324 return val;
325 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000327 return (unsigned long) -1;
328 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000330 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000331 x = 0;
332 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000334 "can't convert negative value to unsigned long");
335 return (unsigned long) -1;
336 }
337 while (--i >= 0) {
338 prev = x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000339 x = (x << PyLong_SHIFT) + v->ob_digit[i];
340 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000342 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000343 return (unsigned long) -1;
344 }
345 }
346 return x;
347}
348
Thomas Hellera4ea6032003-04-17 18:55:45 +0000349/* Get a C unsigned long int from a long int object, ignoring the high bits.
350 Returns -1 and sets an error condition if an error occurs. */
351
352unsigned long
353PyLong_AsUnsignedLongMask(PyObject *vv)
354{
355 register PyLongObject *v;
356 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357 Py_ssize_t i;
358 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000359
360 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000361 if (vv != NULL && PyInt_Check(vv))
362 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000363 PyErr_BadInternalCall();
364 return (unsigned long) -1;
365 }
366 v = (PyLongObject *)vv;
367 i = v->ob_size;
368 sign = 1;
369 x = 0;
370 if (i < 0) {
371 sign = -1;
372 i = -i;
373 }
374 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000375 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000376 }
377 return x * sign;
378}
379
Tim Peters5b8132f2003-01-31 15:52:05 +0000380int
381_PyLong_Sign(PyObject *vv)
382{
383 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000384
385 assert(v != NULL);
386 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000387
Christian Heimese93237d2007-12-19 02:37:44 +0000388 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000389}
390
Tim Petersbaefd9e2003-01-28 20:37:45 +0000391size_t
392_PyLong_NumBits(PyObject *vv)
393{
394 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000395 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000396 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000397
398 assert(v != NULL);
399 assert(PyLong_Check(v));
Christian Heimese93237d2007-12-19 02:37:44 +0000400 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000401 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
402 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000403 digit msd = v->ob_digit[ndigits - 1];
404
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000405 result = (ndigits - 1) * PyLong_SHIFT;
406 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000407 goto Overflow;
408 do {
409 ++result;
410 if (result == 0)
411 goto Overflow;
412 msd >>= 1;
413 } while (msd);
414 }
415 return result;
416
417Overflow:
418 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
419 "to express in a platform size_t");
420 return (size_t)-1;
421}
422
Tim Peters2a9b3672001-06-11 21:23:58 +0000423PyObject *
424_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
425 int little_endian, int is_signed)
426{
427 const unsigned char* pstartbyte;/* LSB of bytes */
428 int incr; /* direction to move pstartbyte */
429 const unsigned char* pendbyte; /* MSB of bytes */
430 size_t numsignificantbytes; /* number of bytes that matter */
431 size_t ndigits; /* number of Python long digits */
432 PyLongObject* v; /* result */
433 int idigit = 0; /* next free index in v->ob_digit */
434
435 if (n == 0)
436 return PyLong_FromLong(0L);
437
438 if (little_endian) {
439 pstartbyte = bytes;
440 pendbyte = bytes + n - 1;
441 incr = 1;
442 }
443 else {
444 pstartbyte = bytes + n - 1;
445 pendbyte = bytes;
446 incr = -1;
447 }
448
449 if (is_signed)
450 is_signed = *pendbyte >= 0x80;
451
452 /* Compute numsignificantbytes. This consists of finding the most
453 significant byte. Leading 0 bytes are insignficant if the number
454 is positive, and leading 0xff bytes if negative. */
455 {
456 size_t i;
457 const unsigned char* p = pendbyte;
458 const int pincr = -incr; /* search MSB to LSB */
459 const unsigned char insignficant = is_signed ? 0xff : 0x00;
460
461 for (i = 0; i < n; ++i, p += pincr) {
462 if (*p != insignficant)
463 break;
464 }
465 numsignificantbytes = n - i;
466 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
467 actually has 2 significant bytes. OTOH, 0xff0001 ==
468 -0x00ffff, so we wouldn't *need* to bump it there; but we
469 do for 0xffff = -0x0001. To be safe without bothering to
470 check every case, bump it regardless. */
471 if (is_signed && numsignificantbytes < n)
472 ++numsignificantbytes;
473 }
474
475 /* How many Python long digits do we need? We have
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000476 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
Tim Peters2a9b3672001-06-11 21:23:58 +0000477 bits, so it's the ceiling of the quotient. */
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000478 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Tim Peters2a9b3672001-06-11 21:23:58 +0000479 if (ndigits > (size_t)INT_MAX)
480 return PyErr_NoMemory();
481 v = _PyLong_New((int)ndigits);
482 if (v == NULL)
483 return NULL;
484
485 /* Copy the bits over. The tricky parts are computing 2's-comp on
486 the fly for signed numbers, and dealing with the mismatch between
487 8-bit bytes and (probably) 15-bit Python digits.*/
488 {
489 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000490 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000491 twodigits accum = 0; /* sliding register */
492 unsigned int accumbits = 0; /* number of bits in accum */
493 const unsigned char* p = pstartbyte;
494
495 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000496 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000497 /* Compute correction for 2's comp, if needed. */
498 if (is_signed) {
499 thisbyte = (0xff ^ thisbyte) + carry;
500 carry = thisbyte >> 8;
501 thisbyte &= 0xff;
502 }
503 /* Because we're going LSB to MSB, thisbyte is
504 more significant than what's already in accum,
505 so needs to be prepended to accum. */
506 accum |= thisbyte << accumbits;
507 accumbits += 8;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000508 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000509 /* There's enough to fill a Python digit. */
510 assert(idigit < (int)ndigits);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000511 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000512 ++idigit;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000513 accum >>= PyLong_SHIFT;
514 accumbits -= PyLong_SHIFT;
515 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000516 }
517 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000518 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000519 if (accumbits) {
520 assert(idigit < (int)ndigits);
521 v->ob_digit[idigit] = (digit)accum;
522 ++idigit;
523 }
524 }
525
Christian Heimese93237d2007-12-19 02:37:44 +0000526 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000527 return (PyObject *)long_normalize(v);
528}
529
530int
531_PyLong_AsByteArray(PyLongObject* v,
532 unsigned char* bytes, size_t n,
533 int little_endian, int is_signed)
534{
535 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000536 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000537 twodigits accum; /* sliding register */
538 unsigned int accumbits; /* # bits in accum */
539 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
540 twodigits carry; /* for computing 2's-comp */
541 size_t j; /* # bytes filled */
542 unsigned char* p; /* pointer to next byte in bytes */
543 int pincr; /* direction to move p */
544
545 assert(v != NULL && PyLong_Check(v));
546
Christian Heimese93237d2007-12-19 02:37:44 +0000547 if (Py_SIZE(v) < 0) {
548 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000549 if (!is_signed) {
550 PyErr_SetString(PyExc_TypeError,
551 "can't convert negative long to unsigned");
552 return -1;
553 }
554 do_twos_comp = 1;
555 }
556 else {
Christian Heimese93237d2007-12-19 02:37:44 +0000557 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000558 do_twos_comp = 0;
559 }
560
561 if (little_endian) {
562 p = bytes;
563 pincr = 1;
564 }
565 else {
566 p = bytes + n - 1;
567 pincr = -1;
568 }
569
Tim Peters898cf852001-06-13 20:50:08 +0000570 /* Copy over all the Python digits.
571 It's crucial that every Python digit except for the MSD contribute
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000572 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000573 normalized. */
574 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000575 j = 0;
576 accum = 0;
577 accumbits = 0;
578 carry = do_twos_comp ? 1 : 0;
579 for (i = 0; i < ndigits; ++i) {
580 twodigits thisdigit = v->ob_digit[i];
581 if (do_twos_comp) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000582 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
583 carry = thisdigit >> PyLong_SHIFT;
584 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000585 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000586 /* Because we're going LSB to MSB, thisdigit is more
587 significant than what's already in accum, so needs to be
588 prepended to accum. */
589 accum |= thisdigit << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000590 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000591
Tim Petersede05092001-06-14 08:53:38 +0000592 /* The most-significant digit may be (probably is) at least
593 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000594 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000595 /* Count # of sign bits -- they needn't be stored,
596 * although for signed conversion we need later to
597 * make sure at least one sign bit gets stored.
598 * First shift conceptual sign bit to real sign bit.
599 */
600 stwodigits s = (stwodigits)(thisdigit <<
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000601 (8*sizeof(stwodigits) - PyLong_SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000602 unsigned int nsignbits = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000603 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000604 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000605 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000606 }
Tim Petersede05092001-06-14 08:53:38 +0000607 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000608 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000609
Tim Peters2a9b3672001-06-11 21:23:58 +0000610 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000611 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000612 if (j >= n)
613 goto Overflow;
614 ++j;
615 *p = (unsigned char)(accum & 0xff);
616 p += pincr;
617 accumbits -= 8;
618 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000619 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000620 }
621
622 /* Store the straggler (if any). */
623 assert(accumbits < 8);
624 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000625 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000626 if (j >= n)
627 goto Overflow;
628 ++j;
629 if (do_twos_comp) {
630 /* Fill leading bits of the byte with sign bits
631 (appropriately pretending that the long had an
632 infinite supply of sign bits). */
633 accum |= (~(twodigits)0) << accumbits;
634 }
635 *p = (unsigned char)(accum & 0xff);
636 p += pincr;
637 }
Tim Peters05607ad2001-06-13 21:01:27 +0000638 else if (j == n && n > 0 && is_signed) {
639 /* The main loop filled the byte array exactly, so the code
640 just above didn't get to ensure there's a sign bit, and the
641 loop below wouldn't add one either. Make sure a sign bit
642 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000643 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000644 int sign_bit_set = msb >= 0x80;
645 assert(accumbits == 0);
646 if (sign_bit_set == do_twos_comp)
647 return 0;
648 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000649 goto Overflow;
650 }
Tim Peters05607ad2001-06-13 21:01:27 +0000651
652 /* Fill remaining bytes with copies of the sign bit. */
653 {
654 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
655 for ( ; j < n; ++j, p += pincr)
656 *p = signbyte;
657 }
658
Tim Peters2a9b3672001-06-11 21:23:58 +0000659 return 0;
660
661Overflow:
662 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
663 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000664
Tim Peters2a9b3672001-06-11 21:23:58 +0000665}
666
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000667double
668_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
669{
670/* NBITS_WANTED should be > the number of bits in a double's precision,
671 but small enough so that 2**NBITS_WANTED is within the normal double
672 range. nbitsneeded is set to 1 less than that because the most-significant
673 Python digit contains at least 1 significant bit, but we don't want to
674 bother counting them (catering to the worst case cheaply).
675
676 57 is one more than VAX-D double precision; I (Tim) don't know of a double
677 format with more precision than that; it's 1 larger so that we add in at
678 least one round bit to stand in for the ignored least-significant bits.
679*/
680#define NBITS_WANTED 57
681 PyLongObject *v;
682 double x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000683 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000684 Py_ssize_t i;
685 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000686 int nbitsneeded;
687
688 if (vv == NULL || !PyLong_Check(vv)) {
689 PyErr_BadInternalCall();
690 return -1;
691 }
692 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000693 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000694 sign = 1;
695 if (i < 0) {
696 sign = -1;
697 i = -(i);
698 }
699 else if (i == 0) {
700 *exponent = 0;
701 return 0.0;
702 }
703 --i;
704 x = (double)v->ob_digit[i];
705 nbitsneeded = NBITS_WANTED - 1;
706 /* Invariant: i Python digits remain unaccounted for. */
707 while (i > 0 && nbitsneeded > 0) {
708 --i;
709 x = x * multiplier + (double)v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000710 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000711 }
712 /* There are i digits we didn't shift in. Pretending they're all
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000713 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000714 *exponent = i;
715 assert(x > 0.0);
716 return x * sign;
717#undef NBITS_WANTED
718}
719
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000720/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000721
722double
Tim Peters9f688bf2000-07-07 15:53:28 +0000723PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000724{
Thomas Wouters553489a2006-02-01 21:32:04 +0000725 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000726 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000727
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 if (vv == NULL || !PyLong_Check(vv)) {
729 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000730 return -1;
731 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000732 x = _PyLong_AsScaledDouble(vv, &e);
733 if (x == -1.0 && PyErr_Occurred())
734 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000735 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
736 set correctly after a successful _PyLong_AsScaledDouble() call */
737 assert(e >= 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000738 if (e > INT_MAX / PyLong_SHIFT)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000739 goto overflow;
740 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000741 x = ldexp(x, e * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000742 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000743 goto overflow;
744 return x;
745
746overflow:
747 PyErr_SetString(PyExc_OverflowError,
748 "long int too large to convert to float");
749 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000750}
751
Guido van Rossum78694d91998-09-18 14:14:13 +0000752/* Create a new long (or int) object from a C pointer */
753
754PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000755PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000756{
Tim Peters70128a12001-06-16 08:48:40 +0000757#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000758 if ((long)p < 0)
759 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000760 return PyInt_FromLong((long)p);
761#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000762
Tim Peters70128a12001-06-16 08:48:40 +0000763#ifndef HAVE_LONG_LONG
764# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
765#endif
766#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000767# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000768#endif
769 /* optimize null pointers */
770 if (p == NULL)
771 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000772 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000773
774#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000775}
776
777/* Get a C pointer from a long object (or an int object in some cases) */
778
779void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000780PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000781{
782 /* This function will allow int or long objects. If vv is neither,
783 then the PyLong_AsLong*() functions will raise the exception:
784 PyExc_SystemError, "bad argument to internal function"
785 */
Tim Peters70128a12001-06-16 08:48:40 +0000786#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000787 long x;
788
Tim Peters70128a12001-06-16 08:48:40 +0000789 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000790 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000791 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000792 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000793 else
794 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000795#else
Tim Peters70128a12001-06-16 08:48:40 +0000796
797#ifndef HAVE_LONG_LONG
798# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
799#endif
800#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000801# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000802#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000803 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000804
Tim Peters70128a12001-06-16 08:48:40 +0000805 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000806 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000807 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000808 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000809 else
810 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000811
812#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000813
814 if (x == -1 && PyErr_Occurred())
815 return NULL;
816 return (void *)x;
817}
818
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000819#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000820
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000821/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000822 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000823 */
824
Tim Peterscf37dfc2001-06-14 18:42:50 +0000825#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000826
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000827/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000828
829PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000830PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000831{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000832 PyLongObject *v;
833 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
834 int ndigits = 0;
835 int negative = 0;
836
837 if (ival < 0) {
838 ival = -ival;
839 negative = 1;
840 }
841
842 /* Count the number of Python digits.
843 We used to pick 5 ("big enough for anything"), but that's a
844 waste of time and space given that 5*15 = 75 bits are rarely
845 needed. */
846 t = (unsigned PY_LONG_LONG)ival;
847 while (t) {
848 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000849 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000850 }
851 v = _PyLong_New(ndigits);
852 if (v != NULL) {
853 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000854 Py_SIZE(v) = negative ? -ndigits : ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000855 t = (unsigned PY_LONG_LONG)ival;
856 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000857 *p++ = (digit)(t & PyLong_MASK);
858 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000859 }
860 }
861 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000862}
863
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000864/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000865
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000866PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000867PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000868{
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000869 PyLongObject *v;
870 unsigned PY_LONG_LONG t;
871 int ndigits = 0;
872
873 /* Count the number of Python digits. */
874 t = (unsigned PY_LONG_LONG)ival;
875 while (t) {
876 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000877 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000878 }
879 v = _PyLong_New(ndigits);
880 if (v != NULL) {
881 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000882 Py_SIZE(v) = ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000883 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000884 *p++ = (digit)(ival & PyLong_MASK);
885 ival >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +0000886 }
887 }
888 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000889}
890
Martin v. Löwis18e16552006-02-15 17:27:45 +0000891/* Create a new long int object from a C Py_ssize_t. */
892
893PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000894PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000895{
896 Py_ssize_t bytes = ival;
897 int one = 1;
898 return _PyLong_FromByteArray(
899 (unsigned char *)&bytes,
Kristján Valur Jónssonf0303942007-05-03 20:27:03 +0000900 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901}
902
903/* Create a new long int object from a C size_t. */
904
905PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000906PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907{
908 size_t bytes = ival;
909 int one = 1;
910 return _PyLong_FromByteArray(
911 (unsigned char *)&bytes,
912 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
913}
914
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000915/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000916 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000917
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000918PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000919PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000920{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000921 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000922 int one = 1;
923 int res;
924
Tim Petersd38b1c72001-09-30 05:09:37 +0000925 if (vv == NULL) {
926 PyErr_BadInternalCall();
927 return -1;
928 }
929 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000930 PyNumberMethods *nb;
931 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000932 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000933 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000934 if ((nb = vv->ob_type->tp_as_number) == NULL ||
935 nb->nb_int == NULL) {
936 PyErr_SetString(PyExc_TypeError, "an integer is required");
937 return -1;
938 }
939 io = (*nb->nb_int) (vv);
940 if (io == NULL)
941 return -1;
942 if (PyInt_Check(io)) {
943 bytes = PyInt_AsLong(io);
944 Py_DECREF(io);
945 return bytes;
946 }
947 if (PyLong_Check(io)) {
948 bytes = PyLong_AsLongLong(io);
949 Py_DECREF(io);
950 return bytes;
951 }
952 Py_DECREF(io);
953 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000954 return -1;
955 }
956
Tim Petersd1a7da62001-06-13 00:35:57 +0000957 res = _PyLong_AsByteArray(
958 (PyLongObject *)vv, (unsigned char *)&bytes,
959 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000960
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000961 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000962 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000963 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000964 else
965 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000966}
967
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000968/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000969 Return -1 and set an error if overflow occurs. */
970
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000971unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000972PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000973{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000974 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000975 int one = 1;
976 int res;
977
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000978 if (vv == NULL || !PyLong_Check(vv)) {
979 PyErr_BadInternalCall();
Skip Montanaro429433b2006-04-18 00:35:43 +0000980 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000981 }
982
Tim Petersd1a7da62001-06-13 00:35:57 +0000983 res = _PyLong_AsByteArray(
984 (PyLongObject *)vv, (unsigned char *)&bytes,
985 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000986
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000987 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000988 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000989 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000990 else
991 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000992}
Tim Petersd1a7da62001-06-13 00:35:57 +0000993
Thomas Hellera4ea6032003-04-17 18:55:45 +0000994/* Get a C unsigned long int from a long int object, ignoring the high bits.
995 Returns -1 and sets an error condition if an error occurs. */
996
997unsigned PY_LONG_LONG
998PyLong_AsUnsignedLongLongMask(PyObject *vv)
999{
1000 register PyLongObject *v;
1001 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001002 Py_ssize_t i;
1003 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001004
1005 if (vv == NULL || !PyLong_Check(vv)) {
1006 PyErr_BadInternalCall();
1007 return (unsigned long) -1;
1008 }
1009 v = (PyLongObject *)vv;
1010 i = v->ob_size;
1011 sign = 1;
1012 x = 0;
1013 if (i < 0) {
1014 sign = -1;
1015 i = -i;
1016 }
1017 while (--i >= 0) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001018 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001019 }
1020 return x * sign;
1021}
Tim Petersd1a7da62001-06-13 00:35:57 +00001022#undef IS_LITTLE_ENDIAN
1023
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001024#endif /* HAVE_LONG_LONG */
1025
Neil Schemenauerba872e22001-01-04 01:46:03 +00001026
1027static int
1028convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001029 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001030 *a = (PyLongObject *) v;
1031 Py_INCREF(v);
1032 }
1033 else if (PyInt_Check(v)) {
1034 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1035 }
1036 else {
1037 return 0;
1038 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001039 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001040 *b = (PyLongObject *) w;
1041 Py_INCREF(w);
1042 }
1043 else if (PyInt_Check(w)) {
1044 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1045 }
1046 else {
1047 Py_DECREF(*a);
1048 return 0;
1049 }
1050 return 1;
1051}
1052
1053#define CONVERT_BINOP(v, w, a, b) \
1054 if (!convert_binop(v, w, a, b)) { \
1055 Py_INCREF(Py_NotImplemented); \
1056 return Py_NotImplemented; \
1057 }
1058
Tim Peters877a2122002-08-12 05:09:36 +00001059/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1060 * is modified in place, by adding y to it. Carries are propagated as far as
1061 * x[m-1], and the remaining carry (0 or 1) is returned.
1062 */
1063static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001065{
1066 int i;
1067 digit carry = 0;
1068
1069 assert(m >= n);
1070 for (i = 0; i < n; ++i) {
1071 carry += x[i] + y[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001072 x[i] = carry & PyLong_MASK;
1073 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001074 assert((carry & 1) == carry);
1075 }
1076 for (; carry && i < m; ++i) {
1077 carry += x[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001078 x[i] = carry & PyLong_MASK;
1079 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001080 assert((carry & 1) == carry);
1081 }
1082 return carry;
1083}
1084
1085/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1086 * is modified in place, by subtracting y from it. Borrows are propagated as
1087 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1088 */
1089static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001090v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001091{
1092 int i;
1093 digit borrow = 0;
1094
1095 assert(m >= n);
1096 for (i = 0; i < n; ++i) {
1097 borrow = x[i] - y[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001098 x[i] = borrow & PyLong_MASK;
1099 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001100 borrow &= 1; /* keep only 1 sign bit */
1101 }
1102 for (; borrow && i < m; ++i) {
1103 borrow = x[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001104 x[i] = borrow & PyLong_MASK;
1105 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001106 borrow &= 1;
1107 }
1108 return borrow;
1109}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001110
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001111/* Multiply by a single digit, ignoring the sign. */
1112
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001114mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001115{
1116 return muladd1(a, n, (digit)0);
1117}
1118
1119/* Multiply by a single digit and add a single digit, ignoring the sign. */
1120
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001122muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001123{
Christian Heimese93237d2007-12-19 02:37:44 +00001124 Py_ssize_t size_a = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001126 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001128
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001129 if (z == NULL)
1130 return NULL;
1131 for (i = 0; i < size_a; ++i) {
1132 carry += (twodigits)a->ob_digit[i] * n;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001133 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1134 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001135 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001136 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001137 return long_normalize(z);
1138}
1139
Tim Peters212e6142001-07-14 12:23:19 +00001140/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1141 in pout, and returning the remainder. pin and pout point at the LSD.
1142 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Eric Smith5e527eb2008-02-10 01:36:53 +00001143 _PyLong_Format, but that should be done with great care since longs are
Tim Peters212e6142001-07-14 12:23:19 +00001144 immutable. */
1145
1146static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001147inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001148{
1149 twodigits rem = 0;
1150
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001151 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001152 pin += size;
1153 pout += size;
1154 while (--size >= 0) {
1155 digit hi;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001156 rem = (rem << PyLong_SHIFT) + *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001157 *--pout = hi = (digit)(rem / n);
1158 rem -= hi * n;
1159 }
1160 return (digit)rem;
1161}
1162
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001163/* Divide a long integer by a digit, returning both the quotient
1164 (as function result) and the remainder (through *prem).
1165 The sign of a is ignored; n should not be zero. */
1166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001168divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001169{
Christian Heimese93237d2007-12-19 02:37:44 +00001170 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001172
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001173 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001175 if (z == NULL)
1176 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001177 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178 return long_normalize(z);
1179}
1180
Eric Smith5e527eb2008-02-10 01:36:53 +00001181/* Convert the long to a string object with given base,
1182 appending a base prefix of 0[box] if base is 2, 8 or 16.
1183 Add a trailing "L" if addL is non-zero.
1184 If newstyle is zero, then use the pre-2.6 behavior of octal having
1185 a leading "0", instead of the prefix "0o" */
1186PyAPI_FUNC(PyObject *)
1187_PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001188{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189 register PyLongObject *a = (PyLongObject *)aa;
1190 PyStringObject *str;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001191 Py_ssize_t i, j, sz;
Neal Norwitzc09efa82006-07-23 07:53:14 +00001192 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001193 char *p;
1194 int bits;
1195 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001196
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001197 if (a == NULL || !PyLong_Check(a)) {
1198 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001199 return NULL;
1200 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001201 assert(base >= 2 && base <= 36);
Christian Heimese93237d2007-12-19 02:37:44 +00001202 size_a = ABS(Py_SIZE(a));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001203
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001204 /* Compute a rough upper bound for the length of the string */
1205 i = base;
1206 bits = 0;
1207 while (i > 1) {
1208 ++bits;
1209 i >>= 1;
1210 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001211 i = 5 + (addL ? 1 : 0);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001212 j = size_a*PyLong_SHIFT + bits-1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001213 sz = i + j / bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001214 if (j / PyLong_SHIFT < size_a || sz < i) {
Armin Rigo7ccbca92006-10-04 12:17:45 +00001215 PyErr_SetString(PyExc_OverflowError,
1216 "long is too large to format");
1217 return NULL;
1218 }
1219 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001220 if (str == NULL)
1221 return NULL;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001222 p = PyString_AS_STRING(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001223 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001224 if (addL)
1225 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001226 if (a->ob_size < 0)
1227 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001228
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001229 if (a->ob_size == 0) {
1230 *--p = '0';
1231 }
1232 else if ((base & (base - 1)) == 0) {
1233 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001234 twodigits accum = 0;
1235 int accumbits = 0; /* # of bits in accum */
1236 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001237 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001238 while ((i >>= 1) > 1)
1239 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001240
1241 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001242 accum |= (twodigits)a->ob_digit[i] << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001243 accumbits += PyLong_SHIFT;
Tim Peters586b2e32001-07-15 09:11:14 +00001244 assert(accumbits >= basebits);
1245 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001246 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001247 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001248 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001249 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001250 accumbits -= basebits;
1251 accum >>= basebits;
1252 } while (i < size_a-1 ? accumbits >= basebits :
1253 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001254 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001255 }
1256 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001257 /* Not 0, and base not a power of 2. Divide repeatedly by
1258 base, but for speed use the highest power of base that
1259 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001260 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001261 digit *pin = a->ob_digit;
1262 PyLongObject *scratch;
1263 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001264 digit powbase = base; /* powbase == base ** power */
1265 int power = 1;
1266 for (;;) {
1267 unsigned long newpow = powbase * (unsigned long)base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001268 if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */
Tim Petersfad225f2001-07-13 02:59:26 +00001269 break;
1270 powbase = (digit)newpow;
1271 ++power;
1272 }
Tim Peters212e6142001-07-14 12:23:19 +00001273
1274 /* Get a scratch area for repeated division. */
1275 scratch = _PyLong_New(size);
1276 if (scratch == NULL) {
1277 Py_DECREF(str);
1278 return NULL;
1279 }
1280
1281 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001282 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001283 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001284 digit rem = inplace_divrem1(scratch->ob_digit,
1285 pin, size, powbase);
1286 pin = scratch->ob_digit; /* no need to use a again */
1287 if (pin[size - 1] == 0)
1288 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001289 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001290 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001291 Py_DECREF(str);
1292 return NULL;
1293 })
Tim Peters212e6142001-07-14 12:23:19 +00001294
1295 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001296 assert(ntostore > 0);
1297 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001298 digit nextrem = (digit)(rem / base);
1299 char c = (char)(rem - nextrem * base);
1300 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001301 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001302 *--p = c;
1303 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001304 --ntostore;
1305 /* Termination is a bit delicate: must not
1306 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001307 remaining quotient and rem are both 0. */
1308 } while (ntostore && (size || rem));
1309 } while (size != 0);
1310 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001311 }
1312
Eric Smith5e527eb2008-02-10 01:36:53 +00001313 if (base == 2) {
1314 *--p = 'b';
1315 *--p = '0';
1316 }
1317 else if (base == 8) {
1318 if (newstyle) {
1319 *--p = 'o';
Guido van Rossum2c475421992-08-14 15:13:07 +00001320 *--p = '0';
Eric Smith5e527eb2008-02-10 01:36:53 +00001321 }
1322 else
1323 if (size_a != 0)
1324 *--p = '0';
Guido van Rossum2c475421992-08-14 15:13:07 +00001325 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001326 else if (base == 16) {
1327 *--p = 'x';
1328 *--p = '0';
1329 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001330 else if (base != 10) {
1331 *--p = '#';
1332 *--p = '0' + base%10;
1333 if (base > 10)
1334 *--p = '0' + base/10;
1335 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001336 if (sign)
1337 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 if (p != PyString_AS_STRING(str)) {
1339 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001340 assert(p > q);
1341 do {
1342 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001343 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 _PyString_Resize((PyObject **)&str,
Armin Rigo7ccbca92006-10-04 12:17:45 +00001345 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001346 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001348}
1349
Tim Petersda53afa2006-05-25 17:34:03 +00001350/* Table of digit values for 8-bit string -> integer conversion.
1351 * '0' maps to 0, ..., '9' maps to 9.
1352 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1353 * All other indices map to 37.
1354 * Note that when converting a base B string, a char c is a legitimate
1355 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1356 */
1357int _PyLong_DigitValue[256] = {
Tim Peters696cf432006-05-24 21:10:40 +00001358 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1359 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1360 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1361 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1362 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1363 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1364 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1365 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 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,
1373 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Tim Peters696cf432006-05-24 21:10:40 +00001374};
1375
1376/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001377 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1378 * non-digit (which may be *str!). A normalized long is returned.
1379 * The point to this routine is that it takes time linear in the number of
1380 * string characters.
1381 */
1382static PyLongObject *
1383long_from_binary_base(char **str, int base)
1384{
1385 char *p = *str;
1386 char *start = p;
1387 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001389 PyLongObject *z;
1390 twodigits accum;
1391 int bits_in_accum;
1392 digit *pdigit;
1393
1394 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1395 n = base;
1396 for (bits_per_char = -1; n; ++bits_per_char)
1397 n >>= 1;
1398 /* n <- total # of bits needed, while setting p to end-of-string */
1399 n = 0;
Tim Petersda53afa2006-05-25 17:34:03 +00001400 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001401 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001402 *str = p;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001403 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1404 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001405 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001406 PyErr_SetString(PyExc_ValueError,
1407 "long string too large to convert");
1408 return NULL;
1409 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001410 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001411 z = _PyLong_New(n);
1412 if (z == NULL)
1413 return NULL;
1414 /* Read string from right, and fill in long from left; i.e.,
1415 * from least to most significant in both.
1416 */
1417 accum = 0;
1418 bits_in_accum = 0;
1419 pdigit = z->ob_digit;
1420 while (--p >= start) {
Tim Petersda53afa2006-05-25 17:34:03 +00001421 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001422 assert(k >= 0 && k < base);
1423 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001424 bits_in_accum += bits_per_char;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001425 if (bits_in_accum >= PyLong_SHIFT) {
1426 *pdigit++ = (digit)(accum & PyLong_MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001427 assert(pdigit - z->ob_digit <= (int)n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001428 accum >>= PyLong_SHIFT;
1429 bits_in_accum -= PyLong_SHIFT;
1430 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001431 }
1432 }
1433 if (bits_in_accum) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001434 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001435 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001436 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001437 }
1438 while (pdigit - z->ob_digit < n)
1439 *pdigit++ = 0;
1440 return long_normalize(z);
1441}
1442
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001443PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001444PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001445{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001446 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001447 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001448 PyLongObject *z;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001449 PyObject *strobj, *strrepr;
1450 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001451
Guido van Rossum472c04f1996-12-05 21:57:21 +00001452 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001453 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001454 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001455 return NULL;
1456 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001457 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001458 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001459 if (*str == '+')
1460 ++str;
1461 else if (*str == '-') {
1462 ++str;
1463 sign = -1;
1464 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001465 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001466 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001467 if (base == 0) {
1468 if (str[0] != '0')
1469 base = 10;
1470 else if (str[1] == 'x' || str[1] == 'X')
1471 base = 16;
1472 else
1473 base = 8;
1474 }
1475 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1476 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001477
Guido van Rossume6762971998-06-22 03:54:46 +00001478 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001479 if ((base & (base - 1)) == 0)
1480 z = long_from_binary_base(&str, base);
1481 else {
Tim Peters696cf432006-05-24 21:10:40 +00001482/***
1483Binary bases can be converted in time linear in the number of digits, because
1484Python's representation base is binary. Other bases (including decimal!) use
1485the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001486
Tim Peters696cf432006-05-24 21:10:40 +00001487First some math: the largest integer that can be expressed in N base-B digits
1488is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1489case number of Python digits needed to hold it is the smallest integer n s.t.
1490
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001491 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1492 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
1493 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001494
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001495The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
Tim Peters696cf432006-05-24 21:10:40 +00001496this quickly. A Python long with that much space is reserved near the start,
1497and the result is computed into it.
1498
1499The input string is actually treated as being in base base**i (i.e., i digits
1500are processed at a time), where two more static arrays hold:
1501
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001502 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
Tim Peters696cf432006-05-24 21:10:40 +00001503 convmultmax_base[base] = base ** convwidth_base[base]
1504
1505The first of these is the largest i such that i consecutive input digits
1506must fit in a single Python digit. The second is effectively the input
1507base we're really using.
1508
1509Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1510convmultmax_base[base], the result is "simply"
1511
1512 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1513
1514where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001515
1516Error analysis: as above, the number of Python digits `n` needed is worst-
1517case
1518
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001519 n >= N * log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001520
1521where `N` is the number of input digits in base `B`. This is computed via
1522
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001523 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001524
1525below. Two numeric concerns are how much space this can waste, and whether
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001526the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
Tim Peters9faa3ed2006-05-30 15:53:34 +00001527which is the default (and it's unlikely anyone changes that).
1528
1529Waste isn't a problem: provided the first input digit isn't 0, the difference
1530between the worst-case input with N digits and the smallest input with N
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001531digits is about a factor of B, but B is small compared to PyLong_BASE so at most
Tim Peters9faa3ed2006-05-30 15:53:34 +00001532one allocated Python digit can remain unused on that count. If
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001533N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001534and adding 1 returns a result 1 larger than necessary. However, that can't
1535happen: whenever B is a power of 2, long_from_binary_base() is called
1536instead, and it's impossible for B**i to be an integer power of 2**15 when
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001537B 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 +00001538an exact integer when B is not a power of 2, since B**i has a prime factor
1539other than 2 in that case, but (2**15)**j's only prime factor is 2).
1540
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001541The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001542is a little bit larger than an exact integer, but due to roundoff errors (in
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001543computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001544yields a numeric result a little less than that integer. Unfortunately, "how
1545close can a transcendental function get to an integer over some range?"
1546questions are generally theoretically intractable. Computer analysis via
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001547continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
Tim Peters9faa3ed2006-05-30 15:53:34 +00001548fractions, giving a sequence i/j of "the best" rational approximations. Then
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001549j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001550we can get very close to being in trouble, but very rarely. For example,
155176573 is a denominator in one of the continued-fraction approximations to
1552log(10)/log(2**15), and indeed:
1553
1554 >>> log(10)/log(2**15)*76573
1555 16958.000000654003
1556
1557is very close to an integer. If we were working with IEEE single-precision,
1558rounding errors could kill us. Finding worst cases in IEEE double-precision
1559requires better-than-double-precision log() functions, and Tim didn't bother.
1560Instead the code checks to see whether the allocated space is enough as each
1561new Python digit is added, and copies the whole thing to a larger long if not.
1562This should happen extremely rarely, and in fact I don't have a test case
1563that triggers it(!). Instead the code was tested by artificially allocating
1564just 1 digit at the start, so that the copying code was exercised for every
1565digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001566***/
1567 register twodigits c; /* current input character */
1568 Py_ssize_t size_z;
1569 int i;
1570 int convwidth;
1571 twodigits convmultmax, convmult;
1572 digit *pz, *pzstop;
1573 char* scan;
1574
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001575 static double log_base_PyLong_BASE[37] = {0.0e0,};
Tim Peters696cf432006-05-24 21:10:40 +00001576 static int convwidth_base[37] = {0,};
1577 static twodigits convmultmax_base[37] = {0,};
1578
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001579 if (log_base_PyLong_BASE[base] == 0.0) {
Tim Peters696cf432006-05-24 21:10:40 +00001580 twodigits convmax = base;
1581 int i = 1;
1582
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001583 log_base_PyLong_BASE[base] = log((double)base) /
1584 log((double)PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001585 for (;;) {
1586 twodigits next = convmax * base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001587 if (next > PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001588 break;
1589 convmax = next;
1590 ++i;
1591 }
1592 convmultmax_base[base] = convmax;
1593 assert(i > 0);
1594 convwidth_base[base] = i;
1595 }
1596
1597 /* Find length of the string of numeric characters. */
1598 scan = str;
Tim Petersda53afa2006-05-25 17:34:03 +00001599 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001600 ++scan;
1601
1602 /* Create a long object that can contain the largest possible
1603 * integer with this base and length. Note that there's no
1604 * need to initialize z->ob_digit -- no slot is read up before
1605 * being stored into.
1606 */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001607 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001608 /* Uncomment next line to test exceedingly rare copy code */
1609 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001610 assert(size_z > 0);
1611 z = _PyLong_New(size_z);
1612 if (z == NULL)
1613 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00001614 Py_SIZE(z) = 0;
Tim Peters696cf432006-05-24 21:10:40 +00001615
1616 /* `convwidth` consecutive input digits are treated as a single
1617 * digit in base `convmultmax`.
1618 */
1619 convwidth = convwidth_base[base];
1620 convmultmax = convmultmax_base[base];
1621
1622 /* Work ;-) */
1623 while (str < scan) {
1624 /* grab up to convwidth digits from the input string */
Tim Petersda53afa2006-05-25 17:34:03 +00001625 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00001626 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1627 c = (twodigits)(c * base +
Tim Petersda53afa2006-05-25 17:34:03 +00001628 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001629 assert(c < PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001630 }
1631
1632 convmult = convmultmax;
1633 /* Calculate the shift only if we couldn't get
1634 * convwidth digits.
1635 */
1636 if (i != convwidth) {
1637 convmult = base;
1638 for ( ; i > 1; --i)
1639 convmult *= base;
1640 }
1641
1642 /* Multiply z by convmult, and add c. */
1643 pz = z->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001644 pzstop = pz + Py_SIZE(z);
Tim Peters696cf432006-05-24 21:10:40 +00001645 for (; pz < pzstop; ++pz) {
1646 c += (twodigits)*pz * convmult;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001647 *pz = (digit)(c & PyLong_MASK);
1648 c >>= PyLong_SHIFT;
Tim Peters696cf432006-05-24 21:10:40 +00001649 }
1650 /* carry off the current end? */
1651 if (c) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001652 assert(c < PyLong_BASE);
Christian Heimese93237d2007-12-19 02:37:44 +00001653 if (Py_SIZE(z) < size_z) {
Tim Peters9faa3ed2006-05-30 15:53:34 +00001654 *pz = (digit)c;
Christian Heimese93237d2007-12-19 02:37:44 +00001655 ++Py_SIZE(z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001656 }
1657 else {
1658 PyLongObject *tmp;
1659 /* Extremely rare. Get more space. */
Christian Heimese93237d2007-12-19 02:37:44 +00001660 assert(Py_SIZE(z) == size_z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001661 tmp = _PyLong_New(size_z + 1);
1662 if (tmp == NULL) {
1663 Py_DECREF(z);
1664 return NULL;
1665 }
1666 memcpy(tmp->ob_digit,
1667 z->ob_digit,
1668 sizeof(digit) * size_z);
1669 Py_DECREF(z);
1670 z = tmp;
1671 z->ob_digit[size_z] = (digit)c;
1672 ++size_z;
1673 }
Tim Peters696cf432006-05-24 21:10:40 +00001674 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001675 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001676 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001677 if (z == NULL)
1678 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001679 if (str == start)
1680 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00001681 if (sign < 0)
Christian Heimese93237d2007-12-19 02:37:44 +00001682 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001683 if (*str == 'L' || *str == 'l')
1684 str++;
1685 while (*str && isspace(Py_CHARMASK(*str)))
1686 str++;
1687 if (*str != '\0')
1688 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001689 if (pend)
1690 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001691 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001692
1693 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001694 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001695 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1696 strobj = PyString_FromStringAndSize(orig_str, slen);
1697 if (strobj == NULL)
1698 return NULL;
1699 strrepr = PyObject_Repr(strobj);
1700 Py_DECREF(strobj);
1701 if (strrepr == NULL)
1702 return NULL;
1703 PyErr_Format(PyExc_ValueError,
1704 "invalid literal for long() with base %d: %s",
1705 base, PyString_AS_STRING(strrepr));
1706 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001707 return NULL;
1708}
1709
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001710#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001711PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001712PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001713{
Walter Dörwald07e14762002-11-06 16:15:14 +00001714 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001715 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001716
Walter Dörwald07e14762002-11-06 16:15:14 +00001717 if (buffer == NULL)
1718 return NULL;
1719
1720 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1721 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001722 return NULL;
1723 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001724 result = PyLong_FromString(buffer, NULL, base);
1725 PyMem_FREE(buffer);
1726 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001727}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001728#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001729
Tim Peters9f688bf2000-07-07 15:53:28 +00001730/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001731static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001732 (PyLongObject *, PyLongObject *, PyLongObject **);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001733static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00001734static int long_divrem(PyLongObject *, PyLongObject *,
1735 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736
1737/* Long division with remainder, top-level routine */
1738
Guido van Rossume32e0141992-01-19 16:31:05 +00001739static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001740long_divrem(PyLongObject *a, PyLongObject *b,
1741 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742{
Christian Heimese93237d2007-12-19 02:37:44 +00001743 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001744 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001745
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001746 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001747 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001748 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001749 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001750 }
1751 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001752 (size_a == size_b &&
1753 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001754 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755 *pdiv = _PyLong_New(0);
Georg Brandlc02e1312007-04-11 17:16:24 +00001756 if (*pdiv == NULL)
1757 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001758 Py_INCREF(a);
1759 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001760 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001761 }
1762 if (size_b == 1) {
1763 digit rem = 0;
1764 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001765 if (z == NULL)
1766 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001767 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandlc02e1312007-04-11 17:16:24 +00001768 if (*prem == NULL) {
1769 Py_DECREF(z);
1770 return -1;
1771 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001772 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001773 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001774 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001775 if (z == NULL)
1776 return -1;
1777 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001778 /* Set the signs.
1779 The quotient z has the sign of a*b;
1780 the remainder r has the sign of a,
1781 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001782 if ((a->ob_size < 0) != (b->ob_size < 0))
1783 z->ob_size = -(z->ob_size);
1784 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1785 (*prem)->ob_size = -((*prem)->ob_size);
1786 *pdiv = z;
1787 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001788}
1789
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001790/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001791
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001792static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001793x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001794{
Christian Heimese93237d2007-12-19 02:37:44 +00001795 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001796 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001797 PyLongObject *v = mul1(v1, d);
1798 PyLongObject *w = mul1(w1, d);
1799 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001800 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001801
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001802 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001803 Py_XDECREF(v);
1804 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001805 return NULL;
1806 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001807
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001808 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimese93237d2007-12-19 02:37:44 +00001809 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
1810 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001811
Christian Heimese93237d2007-12-19 02:37:44 +00001812 size_v = ABS(Py_SIZE(v));
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001813 k = size_v - size_w;
1814 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001815
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001816 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001817 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1818 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001819 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001820 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001821
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001822 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001823 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001824 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001825 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001826 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001827 if (vj == w->ob_digit[size_w-1])
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001828 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001829 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001830 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001831 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001832
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001833 while (w->ob_digit[size_w-2]*q >
1834 ((
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001835 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001836 + v->ob_digit[j-1]
1837 - q*w->ob_digit[size_w-1]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001838 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001839 + v->ob_digit[j-2])
1840 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001841
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001842 for (i = 0; i < size_w && i+k < size_v; ++i) {
1843 twodigits z = w->ob_digit[i] * q;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001844 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001845 carry += v->ob_digit[i+k] - z
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001846 + ((twodigits)zz << PyLong_SHIFT);
1847 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1848 carry = Py_ARITHMETIC_RIGHT_SHIFT(PyLong_BASE_TWODIGITS_TYPE,
1849 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00001850 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001851 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001852
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001853 if (i+k < size_v) {
1854 carry += v->ob_digit[i+k];
1855 v->ob_digit[i+k] = 0;
1856 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001857
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001858 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001859 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001860 else {
1861 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001862 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001863 carry = 0;
1864 for (i = 0; i < size_w && i+k < size_v; ++i) {
1865 carry += v->ob_digit[i+k] + w->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001866 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001867 carry = Py_ARITHMETIC_RIGHT_SHIFT(
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001868 PyLong_BASE_TWODIGITS_TYPE,
1869 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001870 }
1871 }
1872 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001873
Guido van Rossumc206c761995-01-10 15:23:19 +00001874 if (a == NULL)
1875 *prem = NULL;
1876 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001877 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001878 *prem = divrem1(v, d, &d);
1879 /* d receives the (unused) remainder */
1880 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001881 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001882 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001883 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001884 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001885 Py_DECREF(v);
1886 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001887 return a;
1888}
1889
1890/* Methods */
1891
1892static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001893long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001894{
Christian Heimese93237d2007-12-19 02:37:44 +00001895 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001896}
1897
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001898static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001899long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001900{
Eric Smith5e527eb2008-02-10 01:36:53 +00001901 return _PyLong_Format(v, 10, 1, 0);
Fred Drake121ee271999-12-23 15:41:28 +00001902}
1903
1904static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001905long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001906{
Eric Smith5e527eb2008-02-10 01:36:53 +00001907 return _PyLong_Format(v, 10, 0, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001908}
1909
1910static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001911long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001912{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001913 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001914
Christian Heimese93237d2007-12-19 02:37:44 +00001915 if (Py_SIZE(a) != Py_SIZE(b)) {
1916 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001917 sign = 0;
1918 else
Christian Heimese93237d2007-12-19 02:37:44 +00001919 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001920 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001921 else {
Christian Heimese93237d2007-12-19 02:37:44 +00001922 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001923 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1924 ;
1925 if (i < 0)
1926 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001927 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001928 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimese93237d2007-12-19 02:37:44 +00001929 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001930 sign = -sign;
1931 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001932 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001933 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001934}
1935
Guido van Rossum9bfef441993-03-29 10:43:31 +00001936static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001937long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001938{
1939 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001940 Py_ssize_t i;
1941 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001942
1943 /* This is designed so that Python ints and longs with the
1944 same value hash to the same value, otherwise comparisons
1945 of mapping keys will turn out weird */
1946 i = v->ob_size;
1947 sign = 1;
1948 x = 0;
1949 if (i < 0) {
1950 sign = -1;
1951 i = -(i);
1952 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001953#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Facundo Batistad544df72007-09-19 15:10:06 +00001954 /* The following loop produces a C long x such that (unsigned long)x
1955 is congruent to the absolute value of v modulo ULONG_MAX. The
1956 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00001957 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001958 /* Force a native long #-bits (32 or 64) circular shift */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001959 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001960 x += v->ob_digit[i];
Facundo Batistad544df72007-09-19 15:10:06 +00001961 /* If the addition above overflowed (thinking of x as
1962 unsigned), we compensate by incrementing. This preserves
1963 the value modulo ULONG_MAX. */
1964 if ((unsigned long)x < v->ob_digit[i])
1965 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001966 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001967#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001968 x = x * sign;
1969 if (x == -1)
1970 x = -2;
1971 return x;
1972}
1973
1974
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001975/* Add the absolute values of two long integers. */
1976
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001977static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001978x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001979{
Christian Heimese93237d2007-12-19 02:37:44 +00001980 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001981 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001982 int i;
1983 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001984
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001985 /* Ensure a is the larger of the two: */
1986 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001987 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001988 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001989 size_a = size_b;
1990 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001991 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001992 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001993 if (z == NULL)
1994 return NULL;
1995 for (i = 0; i < size_b; ++i) {
1996 carry += a->ob_digit[i] + b->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001997 z->ob_digit[i] = carry & PyLong_MASK;
1998 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001999 }
2000 for (; i < size_a; ++i) {
2001 carry += a->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002002 z->ob_digit[i] = carry & PyLong_MASK;
2003 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002004 }
2005 z->ob_digit[i] = carry;
2006 return long_normalize(z);
2007}
2008
2009/* Subtract the absolute values of two integers. */
2010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002012x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002013{
Christian Heimese93237d2007-12-19 02:37:44 +00002014 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002015 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002016 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002017 int sign = 1;
2018 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002019
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002020 /* Ensure a is the larger of the two: */
2021 if (size_a < size_b) {
2022 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002023 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002024 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002025 size_a = size_b;
2026 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002027 }
2028 else if (size_a == size_b) {
2029 /* Find highest digit where a and b differ: */
2030 i = size_a;
2031 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2032 ;
2033 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002034 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002035 if (a->ob_digit[i] < b->ob_digit[i]) {
2036 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002037 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002038 }
2039 size_a = size_b = i+1;
2040 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002041 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002042 if (z == NULL)
2043 return NULL;
2044 for (i = 0; i < size_b; ++i) {
2045 /* The following assumes unsigned arithmetic
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002046 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002047 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002048 z->ob_digit[i] = borrow & PyLong_MASK;
2049 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002050 borrow &= 1; /* Keep only one sign bit */
2051 }
2052 for (; i < size_a; ++i) {
2053 borrow = a->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002054 z->ob_digit[i] = borrow & PyLong_MASK;
2055 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002056 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002057 }
2058 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002059 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002060 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002061 return long_normalize(z);
2062}
2063
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002064static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002065long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002066{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002067 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002068
Neil Schemenauerba872e22001-01-04 01:46:03 +00002069 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2070
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002071 if (a->ob_size < 0) {
2072 if (b->ob_size < 0) {
2073 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002074 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002075 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002076 }
2077 else
2078 z = x_sub(b, a);
2079 }
2080 else {
2081 if (b->ob_size < 0)
2082 z = x_sub(a, b);
2083 else
2084 z = x_add(a, b);
2085 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002086 Py_DECREF(a);
2087 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002088 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002089}
2090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002091static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002092long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002093{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002094 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002095
Neil Schemenauerba872e22001-01-04 01:46:03 +00002096 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2097
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002098 if (a->ob_size < 0) {
2099 if (b->ob_size < 0)
2100 z = x_sub(a, b);
2101 else
2102 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002103 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002104 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002105 }
2106 else {
2107 if (b->ob_size < 0)
2108 z = x_add(a, b);
2109 else
2110 z = x_sub(a, b);
2111 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002112 Py_DECREF(a);
2113 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002114 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002115}
2116
Tim Peters5af4e6c2002-08-12 02:31:19 +00002117/* Grade school multiplication, ignoring the signs.
2118 * Returns the absolute value of the product, or NULL if error.
2119 */
2120static PyLongObject *
2121x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002122{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002123 PyLongObject *z;
Christian Heimese93237d2007-12-19 02:37:44 +00002124 Py_ssize_t size_a = ABS(Py_SIZE(a));
2125 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002126 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002127
Tim Peters5af4e6c2002-08-12 02:31:19 +00002128 z = _PyLong_New(size_a + size_b);
2129 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002130 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002131
Christian Heimese93237d2007-12-19 02:37:44 +00002132 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002133 if (a == b) {
2134 /* Efficient squaring per HAC, Algorithm 14.16:
2135 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2136 * Gives slightly less than a 2x speedup when a == b,
2137 * via exploiting that each entry in the multiplication
2138 * pyramid appears twice (except for the size_a squares).
2139 */
2140 for (i = 0; i < size_a; ++i) {
2141 twodigits carry;
2142 twodigits f = a->ob_digit[i];
2143 digit *pz = z->ob_digit + (i << 1);
2144 digit *pa = a->ob_digit + i + 1;
2145 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002146
Tim Peters0973b992004-08-29 22:16:50 +00002147 SIGCHECK({
2148 Py_DECREF(z);
2149 return NULL;
2150 })
2151
2152 carry = *pz + f * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002153 *pz++ = (digit)(carry & PyLong_MASK);
2154 carry >>= PyLong_SHIFT;
2155 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002156
2157 /* Now f is added in twice in each column of the
2158 * pyramid it appears. Same as adding f<<1 once.
2159 */
2160 f <<= 1;
2161 while (pa < paend) {
2162 carry += *pz + *pa++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002163 *pz++ = (digit)(carry & PyLong_MASK);
2164 carry >>= PyLong_SHIFT;
2165 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002166 }
2167 if (carry) {
2168 carry += *pz;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002169 *pz++ = (digit)(carry & PyLong_MASK);
2170 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002171 }
2172 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002173 *pz += (digit)(carry & PyLong_MASK);
2174 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002175 }
Tim Peters0973b992004-08-29 22:16:50 +00002176 }
2177 else { /* a is not the same as b -- gradeschool long mult */
2178 for (i = 0; i < size_a; ++i) {
2179 twodigits carry = 0;
2180 twodigits f = a->ob_digit[i];
2181 digit *pz = z->ob_digit + i;
2182 digit *pb = b->ob_digit;
2183 digit *pbend = b->ob_digit + size_b;
2184
2185 SIGCHECK({
2186 Py_DECREF(z);
2187 return NULL;
2188 })
2189
2190 while (pb < pbend) {
2191 carry += *pz + *pb++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002192 *pz++ = (digit)(carry & PyLong_MASK);
2193 carry >>= PyLong_SHIFT;
2194 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002195 }
2196 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002197 *pz += (digit)(carry & PyLong_MASK);
2198 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002199 }
2200 }
Tim Peters44121a62002-08-12 06:17:58 +00002201 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002202}
2203
2204/* A helper for Karatsuba multiplication (k_mul).
2205 Takes a long "n" and an integer "size" representing the place to
2206 split, and sets low and high such that abs(n) == (high << size) + low,
2207 viewing the shift as being by digits. The sign bit is ignored, and
2208 the return values are >= 0.
2209 Returns 0 on success, -1 on failure.
2210*/
2211static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002212kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002213{
2214 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002215 Py_ssize_t size_lo, size_hi;
Christian Heimese93237d2007-12-19 02:37:44 +00002216 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002217
2218 size_lo = MIN(size_n, size);
2219 size_hi = size_n - size_lo;
2220
2221 if ((hi = _PyLong_New(size_hi)) == NULL)
2222 return -1;
2223 if ((lo = _PyLong_New(size_lo)) == NULL) {
2224 Py_DECREF(hi);
2225 return -1;
2226 }
2227
2228 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2229 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2230
2231 *high = long_normalize(hi);
2232 *low = long_normalize(lo);
2233 return 0;
2234}
2235
Tim Peters60004642002-08-12 22:01:34 +00002236static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2237
Tim Peters5af4e6c2002-08-12 02:31:19 +00002238/* Karatsuba multiplication. Ignores the input signs, and returns the
2239 * absolute value of the product (or NULL if error).
2240 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2241 */
2242static PyLongObject *
2243k_mul(PyLongObject *a, PyLongObject *b)
2244{
Christian Heimese93237d2007-12-19 02:37:44 +00002245 Py_ssize_t asize = ABS(Py_SIZE(a));
2246 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002247 PyLongObject *ah = NULL;
2248 PyLongObject *al = NULL;
2249 PyLongObject *bh = NULL;
2250 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002251 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002252 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002253 Py_ssize_t shift; /* the number of digits we split off */
2254 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002255
Tim Peters5af4e6c2002-08-12 02:31:19 +00002256 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2257 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2258 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002259 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002260 * By picking X to be a power of 2, "*X" is just shifting, and it's
2261 * been reduced to 3 multiplies on numbers half the size.
2262 */
2263
Tim Petersfc07e562002-08-12 02:54:10 +00002264 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002265 * is largest.
2266 */
Tim Peters738eda72002-08-12 15:08:20 +00002267 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002268 t1 = a;
2269 a = b;
2270 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002271
2272 i = asize;
2273 asize = bsize;
2274 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002275 }
2276
2277 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002278 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2279 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002280 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002281 return _PyLong_New(0);
2282 else
2283 return x_mul(a, b);
2284 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002285
Tim Peters60004642002-08-12 22:01:34 +00002286 /* If a is small compared to b, splitting on b gives a degenerate
2287 * case with ah==0, and Karatsuba may be (even much) less efficient
2288 * than "grade school" then. However, we can still win, by viewing
2289 * b as a string of "big digits", each of width a->ob_size. That
2290 * leads to a sequence of balanced calls to k_mul.
2291 */
2292 if (2 * asize <= bsize)
2293 return k_lopsided_mul(a, b);
2294
Tim Petersd6974a52002-08-13 20:37:51 +00002295 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002296 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002297 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002298 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002299
Tim Peters0973b992004-08-29 22:16:50 +00002300 if (a == b) {
2301 bh = ah;
2302 bl = al;
2303 Py_INCREF(bh);
2304 Py_INCREF(bl);
2305 }
2306 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002307
Tim Petersd64c1de2002-08-12 17:36:03 +00002308 /* The plan:
2309 * 1. Allocate result space (asize + bsize digits: that's always
2310 * enough).
2311 * 2. Compute ah*bh, and copy into result at 2*shift.
2312 * 3. Compute al*bl, and copy into result at 0. Note that this
2313 * can't overlap with #2.
2314 * 4. Subtract al*bl from the result, starting at shift. This may
2315 * underflow (borrow out of the high digit), but we don't care:
2316 * we're effectively doing unsigned arithmetic mod
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002317 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
Tim Petersd64c1de2002-08-12 17:36:03 +00002318 * borrows and carries out of the high digit can be ignored.
2319 * 5. Subtract ah*bh from the result, starting at shift.
2320 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2321 * at shift.
2322 */
2323
2324 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002325 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002326 if (ret == NULL) goto fail;
2327#ifdef Py_DEBUG
2328 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002329 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002330#endif
Tim Peters44121a62002-08-12 06:17:58 +00002331
Tim Petersd64c1de2002-08-12 17:36:03 +00002332 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002333 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002334 assert(Py_SIZE(t1) >= 0);
2335 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002336 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimese93237d2007-12-19 02:37:44 +00002337 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002338
2339 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimese93237d2007-12-19 02:37:44 +00002340 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002341 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002342 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002343 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002344
Tim Petersd64c1de2002-08-12 17:36:03 +00002345 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002346 if ((t2 = k_mul(al, bl)) == NULL) {
2347 Py_DECREF(t1);
2348 goto fail;
2349 }
Christian Heimese93237d2007-12-19 02:37:44 +00002350 assert(Py_SIZE(t2) >= 0);
2351 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2352 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002353
2354 /* Zero out remaining digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002355 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002356 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002357 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002358
Tim Petersd64c1de2002-08-12 17:36:03 +00002359 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2360 * because it's fresher in cache.
2361 */
Christian Heimese93237d2007-12-19 02:37:44 +00002362 i = Py_SIZE(ret) - shift; /* # digits after shift */
2363 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002364 Py_DECREF(t2);
2365
Christian Heimese93237d2007-12-19 02:37:44 +00002366 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002367 Py_DECREF(t1);
2368
Tim Petersd64c1de2002-08-12 17:36:03 +00002369 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002370 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2371 Py_DECREF(ah);
2372 Py_DECREF(al);
2373 ah = al = NULL;
2374
Tim Peters0973b992004-08-29 22:16:50 +00002375 if (a == b) {
2376 t2 = t1;
2377 Py_INCREF(t2);
2378 }
2379 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002380 Py_DECREF(t1);
2381 goto fail;
2382 }
2383 Py_DECREF(bh);
2384 Py_DECREF(bl);
2385 bh = bl = NULL;
2386
Tim Peters738eda72002-08-12 15:08:20 +00002387 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002388 Py_DECREF(t1);
2389 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002390 if (t3 == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002391 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002392
Tim Petersd6974a52002-08-13 20:37:51 +00002393 /* Add t3. It's not obvious why we can't run out of room here.
2394 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002395 */
Christian Heimese93237d2007-12-19 02:37:44 +00002396 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002397 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002398
Tim Peters5af4e6c2002-08-12 02:31:19 +00002399 return long_normalize(ret);
2400
2401 fail:
2402 Py_XDECREF(ret);
2403 Py_XDECREF(ah);
2404 Py_XDECREF(al);
2405 Py_XDECREF(bh);
2406 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002407 return NULL;
2408}
2409
Tim Petersd6974a52002-08-13 20:37:51 +00002410/* (*) Why adding t3 can't "run out of room" above.
2411
Tim Petersab86c2b2002-08-15 20:06:00 +00002412Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2413to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002414
Tim Petersab86c2b2002-08-15 20:06:00 +000024151. For any integer i, i = c(i/2) + f(i/2). In particular,
2416 bsize = c(bsize/2) + f(bsize/2).
24172. shift = f(bsize/2)
24183. asize <= bsize
24194. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2420 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002421
Tim Petersab86c2b2002-08-15 20:06:00 +00002422We allocated asize + bsize result digits, and add t3 into them at an offset
2423of shift. This leaves asize+bsize-shift allocated digit positions for t3
2424to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2425asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002426
Tim Petersab86c2b2002-08-15 20:06:00 +00002427bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2428at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002429
Tim Petersab86c2b2002-08-15 20:06:00 +00002430If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2431digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2432most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002433
Tim Petersab86c2b2002-08-15 20:06:00 +00002434The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002435
Tim Petersab86c2b2002-08-15 20:06:00 +00002436 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002437
Tim Petersab86c2b2002-08-15 20:06:00 +00002438and we have asize + c(bsize/2) available digit positions. We need to show
2439this is always enough. An instance of c(bsize/2) cancels out in both, so
2440the question reduces to whether asize digits is enough to hold
2441(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2442then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2443asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002444digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002445asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002446c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2447is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2448bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002449
Tim Peters48d52c02002-08-14 17:07:32 +00002450Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2451clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2452ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002453*/
2454
Tim Peters60004642002-08-12 22:01:34 +00002455/* b has at least twice the digits of a, and a is big enough that Karatsuba
2456 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2457 * of slices, each with a->ob_size digits, and multiply the slices by a,
2458 * one at a time. This gives k_mul balanced inputs to work with, and is
2459 * also cache-friendly (we compute one double-width slice of the result
2460 * at a time, then move on, never bactracking except for the helpful
2461 * single-width slice overlap between successive partial sums).
2462 */
2463static PyLongObject *
2464k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2465{
Christian Heimese93237d2007-12-19 02:37:44 +00002466 const Py_ssize_t asize = ABS(Py_SIZE(a));
2467 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002468 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002469 PyLongObject *ret;
2470 PyLongObject *bslice = NULL;
2471
2472 assert(asize > KARATSUBA_CUTOFF);
2473 assert(2 * asize <= bsize);
2474
2475 /* Allocate result space, and zero it out. */
2476 ret = _PyLong_New(asize + bsize);
2477 if (ret == NULL)
2478 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002479 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002480
2481 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002482 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002483 if (bslice == NULL)
2484 goto fail;
2485
2486 nbdone = 0;
2487 while (bsize > 0) {
2488 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002489 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002490
2491 /* Multiply the next slice of b by a. */
2492 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2493 nbtouse * sizeof(digit));
Christian Heimese93237d2007-12-19 02:37:44 +00002494 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002495 product = k_mul(a, bslice);
2496 if (product == NULL)
2497 goto fail;
2498
2499 /* Add into result. */
Christian Heimese93237d2007-12-19 02:37:44 +00002500 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2501 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002502 Py_DECREF(product);
2503
2504 bsize -= nbtouse;
2505 nbdone += nbtouse;
2506 }
2507
2508 Py_DECREF(bslice);
2509 return long_normalize(ret);
2510
2511 fail:
2512 Py_DECREF(ret);
2513 Py_XDECREF(bslice);
2514 return NULL;
2515}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002516
2517static PyObject *
2518long_mul(PyLongObject *v, PyLongObject *w)
2519{
2520 PyLongObject *a, *b, *z;
2521
2522 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002523 Py_INCREF(Py_NotImplemented);
2524 return Py_NotImplemented;
2525 }
2526
Tim Petersd64c1de2002-08-12 17:36:03 +00002527 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002528 /* Negate if exactly one of the inputs is negative. */
2529 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002530 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002531 Py_DECREF(a);
2532 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002533 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002534}
2535
Guido van Rossume32e0141992-01-19 16:31:05 +00002536/* The / and % operators are now defined in terms of divmod().
2537 The expression a mod b has the value a - b*floor(a/b).
2538 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002539 |a| by |b|, with the sign of a. This is also expressed
2540 as a - b*trunc(a/b), if trunc truncates towards zero.
2541 Some examples:
2542 a b a rem b a mod b
2543 13 10 3 3
2544 -13 10 -3 7
2545 13 -10 3 -7
2546 -13 -10 -3 -3
2547 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002548 have different signs. We then subtract one from the 'div'
2549 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002550
Tim Peters47e52ee2004-08-30 02:44:38 +00002551/* Compute
2552 * *pdiv, *pmod = divmod(v, w)
2553 * NULL can be passed for pdiv or pmod, in which case that part of
2554 * the result is simply thrown away. The caller owns a reference to
2555 * each of these it requests (does not pass NULL for).
2556 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002557static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002558l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002559 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002560{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002561 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002562
Guido van Rossume32e0141992-01-19 16:31:05 +00002563 if (long_divrem(v, w, &div, &mod) < 0)
2564 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00002565 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2566 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002567 PyLongObject *temp;
2568 PyLongObject *one;
2569 temp = (PyLongObject *) long_add(mod, w);
2570 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002571 mod = temp;
2572 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002573 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002574 return -1;
2575 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002576 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002577 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002578 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2579 Py_DECREF(mod);
2580 Py_DECREF(div);
2581 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002582 return -1;
2583 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002584 Py_DECREF(one);
2585 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002586 div = temp;
2587 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002588 if (pdiv != NULL)
2589 *pdiv = div;
2590 else
2591 Py_DECREF(div);
2592
2593 if (pmod != NULL)
2594 *pmod = mod;
2595 else
2596 Py_DECREF(mod);
2597
Guido van Rossume32e0141992-01-19 16:31:05 +00002598 return 0;
2599}
2600
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002601static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002602long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002603{
Tim Peters47e52ee2004-08-30 02:44:38 +00002604 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002605
2606 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002607 if (l_divmod(a, b, &div, NULL) < 0)
2608 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002609 Py_DECREF(a);
2610 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002611 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002612}
2613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002614static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002615long_classic_div(PyObject *v, PyObject *w)
2616{
Tim Peters47e52ee2004-08-30 02:44:38 +00002617 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002618
2619 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002620 if (Py_DivisionWarningFlag &&
2621 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2622 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002623 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002624 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002625 Py_DECREF(a);
2626 Py_DECREF(b);
2627 return (PyObject *)div;
2628}
2629
2630static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002631long_true_divide(PyObject *v, PyObject *w)
2632{
Tim Peterse2a60002001-09-04 06:17:36 +00002633 PyLongObject *a, *b;
2634 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002635 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002636
2637 CONVERT_BINOP(v, w, &a, &b);
2638 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2639 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002640 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2641 Py_DECREF(a);
2642 Py_DECREF(b);
2643 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002644 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002645 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2646 but should really be set correctly after sucessful calls to
2647 _PyLong_AsScaledDouble() */
2648 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002649
2650 if (bd == 0.0) {
2651 PyErr_SetString(PyExc_ZeroDivisionError,
2652 "long division or modulo by zero");
2653 return NULL;
2654 }
2655
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002656 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002657 ad /= bd; /* overflow/underflow impossible here */
2658 aexp -= bexp;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002659 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002660 goto overflow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002661 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002662 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002663 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002664 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002665 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002666 goto overflow;
2667 return PyFloat_FromDouble(ad);
2668
2669overflow:
2670 PyErr_SetString(PyExc_OverflowError,
2671 "long/long too large for a float");
2672 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002673
Tim Peters20dab9f2001-09-04 05:31:47 +00002674}
2675
2676static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002677long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002678{
Tim Peters47e52ee2004-08-30 02:44:38 +00002679 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002680
2681 CONVERT_BINOP(v, w, &a, &b);
2682
Tim Peters47e52ee2004-08-30 02:44:38 +00002683 if (l_divmod(a, b, NULL, &mod) < 0)
2684 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002685 Py_DECREF(a);
2686 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002687 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002688}
2689
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002690static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002691long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002692{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002693 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002694 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002695
2696 CONVERT_BINOP(v, w, &a, &b);
2697
2698 if (l_divmod(a, b, &div, &mod) < 0) {
2699 Py_DECREF(a);
2700 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002701 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002702 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002703 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002704 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002705 PyTuple_SetItem(z, 0, (PyObject *) div);
2706 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002707 }
2708 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002709 Py_DECREF(div);
2710 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002711 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002712 Py_DECREF(a);
2713 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002714 return z;
2715}
2716
Tim Peters47e52ee2004-08-30 02:44:38 +00002717/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002718static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002719long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002720{
Tim Peters47e52ee2004-08-30 02:44:38 +00002721 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2722 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002723
Tim Peters47e52ee2004-08-30 02:44:38 +00002724 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002725 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002726 PyLongObject *temp = NULL;
2727
2728 /* 5-ary values. If the exponent is large enough, table is
2729 * precomputed so that table[i] == a**i % c for i in range(32).
2730 */
2731 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2732 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2733
2734 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002735 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002736 if (PyLong_Check(x)) {
2737 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002738 Py_INCREF(x);
2739 }
Tim Petersde7990b2005-07-17 23:45:23 +00002740 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002741 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002742 if (c == NULL)
2743 goto Error;
2744 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002745 else if (x == Py_None)
2746 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002747 else {
2748 Py_DECREF(a);
2749 Py_DECREF(b);
2750 Py_INCREF(Py_NotImplemented);
2751 return Py_NotImplemented;
2752 }
Tim Peters4c483c42001-09-05 06:24:58 +00002753
Christian Heimese93237d2007-12-19 02:37:44 +00002754 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00002755 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002756 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002757 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002758 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002759 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002760 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002761 /* else return a float. This works because we know
2762 that this calls float_pow() which converts its
2763 arguments to double. */
2764 Py_DECREF(a);
2765 Py_DECREF(b);
2766 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002767 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002768 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002769
2770 if (c) {
2771 /* if modulus == 0:
2772 raise ValueError() */
Christian Heimese93237d2007-12-19 02:37:44 +00002773 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002774 PyErr_SetString(PyExc_ValueError,
2775 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002776 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002777 }
2778
2779 /* if modulus < 0:
2780 negativeOutput = True
2781 modulus = -modulus */
Christian Heimese93237d2007-12-19 02:37:44 +00002782 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002783 negativeOutput = 1;
2784 temp = (PyLongObject *)_PyLong_Copy(c);
2785 if (temp == NULL)
2786 goto Error;
2787 Py_DECREF(c);
2788 c = temp;
2789 temp = NULL;
2790 c->ob_size = - c->ob_size;
2791 }
2792
2793 /* if modulus == 1:
2794 return 0 */
Christian Heimese93237d2007-12-19 02:37:44 +00002795 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002796 z = (PyLongObject *)PyLong_FromLong(0L);
2797 goto Done;
2798 }
2799
2800 /* if base < 0:
2801 base = base % modulus
2802 Having the base positive just makes things easier. */
Christian Heimese93237d2007-12-19 02:37:44 +00002803 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002804 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002805 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002806 Py_DECREF(a);
2807 a = temp;
2808 temp = NULL;
2809 }
2810 }
2811
2812 /* At this point a, b, and c are guaranteed non-negative UNLESS
2813 c is NULL, in which case a may be negative. */
2814
2815 z = (PyLongObject *)PyLong_FromLong(1L);
2816 if (z == NULL)
2817 goto Error;
2818
2819 /* Perform a modular reduction, X = X % c, but leave X alone if c
2820 * is NULL.
2821 */
2822#define REDUCE(X) \
2823 if (c != NULL) { \
2824 if (l_divmod(X, c, NULL, &temp) < 0) \
2825 goto Error; \
2826 Py_XDECREF(X); \
2827 X = temp; \
2828 temp = NULL; \
2829 }
2830
2831 /* Multiply two values, then reduce the result:
2832 result = X*Y % c. If c is NULL, skip the mod. */
2833#define MULT(X, Y, result) \
2834{ \
2835 temp = (PyLongObject *)long_mul(X, Y); \
2836 if (temp == NULL) \
2837 goto Error; \
2838 Py_XDECREF(result); \
2839 result = temp; \
2840 temp = NULL; \
2841 REDUCE(result) \
2842}
2843
Christian Heimese93237d2007-12-19 02:37:44 +00002844 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002845 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2846 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimese93237d2007-12-19 02:37:44 +00002847 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002848 digit bi = b->ob_digit[i];
2849
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002850 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002851 MULT(z, z, z)
2852 if (bi & j)
2853 MULT(z, a, z)
2854 }
2855 }
2856 }
2857 else {
2858 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2859 Py_INCREF(z); /* still holds 1L */
2860 table[0] = z;
2861 for (i = 1; i < 32; ++i)
2862 MULT(table[i-1], a, table[i])
2863
Christian Heimese93237d2007-12-19 02:37:44 +00002864 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002865 const digit bi = b->ob_digit[i];
2866
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002867 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002868 const int index = (bi >> j) & 0x1f;
2869 for (k = 0; k < 5; ++k)
2870 MULT(z, z, z)
2871 if (index)
2872 MULT(z, table[index], z)
2873 }
2874 }
2875 }
2876
Christian Heimese93237d2007-12-19 02:37:44 +00002877 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002878 temp = (PyLongObject *)long_sub(z, c);
2879 if (temp == NULL)
2880 goto Error;
2881 Py_DECREF(z);
2882 z = temp;
2883 temp = NULL;
2884 }
2885 goto Done;
2886
2887 Error:
2888 if (z != NULL) {
2889 Py_DECREF(z);
2890 z = NULL;
2891 }
2892 /* fall through */
2893 Done:
Christian Heimese93237d2007-12-19 02:37:44 +00002894 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002895 for (i = 0; i < 32; ++i)
2896 Py_XDECREF(table[i]);
2897 }
Tim Petersde7990b2005-07-17 23:45:23 +00002898 Py_DECREF(a);
2899 Py_DECREF(b);
2900 Py_XDECREF(c);
2901 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002902 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002903}
2904
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002905static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002906long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002907{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002908 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002909 PyLongObject *x;
2910 PyLongObject *w;
2911 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002912 if (w == NULL)
2913 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002914 x = (PyLongObject *) long_add(v, w);
2915 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002916 if (x == NULL)
2917 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002918 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002919 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002920}
2921
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002922static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002923long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002924{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002925 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002926 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002927 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002928 Py_INCREF(v);
2929 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002930 }
Tim Peters69c2de32001-09-11 22:31:33 +00002931 z = (PyLongObject *)_PyLong_Copy(v);
2932 if (z != NULL)
2933 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002934 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002935}
2936
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002937static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002938long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002939{
2940 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002941 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002942 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002943 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002944}
2945
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002946static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002947long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002948{
Christian Heimese93237d2007-12-19 02:37:44 +00002949 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002950}
2951
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002952static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002953long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002954{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002955 PyLongObject *a, *b;
2956 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002957 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002958 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002959 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002960
Neil Schemenauerba872e22001-01-04 01:46:03 +00002961 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2962
Christian Heimese93237d2007-12-19 02:37:44 +00002963 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002964 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002965 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002966 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002967 if (a1 == NULL)
2968 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002969 a2 = (PyLongObject *) long_rshift(a1, b);
2970 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002971 if (a2 == NULL)
2972 goto rshift_error;
2973 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002974 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002975 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002976 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002977
Neil Schemenauerba872e22001-01-04 01:46:03 +00002978 shiftby = PyLong_AsLong((PyObject *)b);
2979 if (shiftby == -1L && PyErr_Occurred())
2980 goto rshift_error;
2981 if (shiftby < 0) {
2982 PyErr_SetString(PyExc_ValueError,
2983 "negative shift count");
2984 goto rshift_error;
2985 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002986 wordshift = shiftby / PyLong_SHIFT;
Christian Heimese93237d2007-12-19 02:37:44 +00002987 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002988 if (newsize <= 0) {
2989 z = _PyLong_New(0);
2990 Py_DECREF(a);
2991 Py_DECREF(b);
2992 return (PyObject *)z;
2993 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002994 loshift = shiftby % PyLong_SHIFT;
2995 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002996 lomask = ((digit)1 << hishift) - 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002997 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002998 z = _PyLong_New(newsize);
2999 if (z == NULL)
3000 goto rshift_error;
Christian Heimese93237d2007-12-19 02:37:44 +00003001 if (Py_SIZE(a) < 0)
3002 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003003 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3004 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3005 if (i+1 < newsize)
3006 z->ob_digit[i] |=
3007 (a->ob_digit[j+1] << hishift) & himask;
3008 }
3009 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003010 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003011rshift_error:
3012 Py_DECREF(a);
3013 Py_DECREF(b);
3014 return (PyObject *) z;
3015
Guido van Rossumc6913e71991-11-19 20:26:46 +00003016}
3017
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003018static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003019long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003020{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003021 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003022 PyLongObject *a, *b;
3023 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003024 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003025 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003026 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003027
Neil Schemenauerba872e22001-01-04 01:46:03 +00003028 CONVERT_BINOP(v, w, &a, &b);
3029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003030 shiftby = PyLong_AsLong((PyObject *)b);
3031 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003032 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003033 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003034 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003035 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003036 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003037 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003038 PyErr_SetString(PyExc_ValueError,
3039 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003040 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003041 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003042 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3043 wordshift = (int)shiftby / PyLong_SHIFT;
3044 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003045
3046 oldsize = ABS(a->ob_size);
3047 newsize = oldsize + wordshift;
3048 if (remshift)
3049 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003050 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003051 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003052 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003053 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003054 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003055 for (i = 0; i < wordshift; i++)
3056 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003057 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003058 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003059 accum |= (twodigits)a->ob_digit[j] << remshift;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003060 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3061 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003062 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003063 if (remshift)
3064 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003065 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003066 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003067 z = long_normalize(z);
3068lshift_error:
3069 Py_DECREF(a);
3070 Py_DECREF(b);
3071 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003072}
3073
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003074
3075/* Bitwise and/xor/or operations */
3076
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003077static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003078long_bitwise(PyLongObject *a,
3079 int op, /* '&', '|', '^' */
3080 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003081{
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003082 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003083 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003084 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003085 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003086 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003087 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003088 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003089
Christian Heimese93237d2007-12-19 02:37:44 +00003090 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003091 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003092 if (a == NULL)
3093 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003094 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003095 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003096 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003097 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003098 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003099 }
Christian Heimese93237d2007-12-19 02:37:44 +00003100 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003101 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003102 if (b == NULL) {
3103 Py_DECREF(a);
3104 return NULL;
3105 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003106 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003107 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003108 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003109 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003110 maskb = 0;
3111 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003112
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003113 negz = 0;
3114 switch (op) {
3115 case '^':
3116 if (maska != maskb) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003117 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003118 negz = -1;
3119 }
3120 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003121 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003122 if (maska && maskb) {
3123 op = '|';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003124 maska ^= PyLong_MASK;
3125 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003126 negz = -1;
3127 }
3128 break;
3129 case '|':
3130 if (maska || maskb) {
3131 op = '&';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003132 maska ^= PyLong_MASK;
3133 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003134 negz = -1;
3135 }
3136 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003137 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003138
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003139 /* JRH: The original logic here was to allocate the result value (z)
3140 as the longer of the two operands. However, there are some cases
3141 where the result is guaranteed to be shorter than that: AND of two
3142 positives, OR of two negatives: use the shorter number. AND with
3143 mixed signs: use the positive number. OR with mixed signs: use the
3144 negative number. After the transformations above, op will be '&'
3145 iff one of these cases applies, and mask will be non-0 for operands
3146 whose length should be ignored.
3147 */
3148
Christian Heimese93237d2007-12-19 02:37:44 +00003149 size_a = Py_SIZE(a);
3150 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003151 size_z = op == '&'
3152 ? (maska
3153 ? size_b
3154 : (maskb ? size_a : MIN(size_a, size_b)))
3155 : MAX(size_a, size_b);
3156 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003157 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003158 Py_DECREF(a);
3159 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003160 return NULL;
3161 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003162
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003163 for (i = 0; i < size_z; ++i) {
3164 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3165 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3166 switch (op) {
3167 case '&': z->ob_digit[i] = diga & digb; break;
3168 case '|': z->ob_digit[i] = diga | digb; break;
3169 case '^': z->ob_digit[i] = diga ^ digb; break;
3170 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003171 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003172
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003173 Py_DECREF(a);
3174 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003175 z = long_normalize(z);
3176 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003177 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003178 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003179 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003180 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003181}
3182
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003183static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003184long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003185{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003186 PyLongObject *a, *b;
3187 PyObject *c;
3188 CONVERT_BINOP(v, w, &a, &b);
3189 c = long_bitwise(a, '&', b);
3190 Py_DECREF(a);
3191 Py_DECREF(b);
3192 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003193}
3194
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003195static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003196long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003197{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003198 PyLongObject *a, *b;
3199 PyObject *c;
3200 CONVERT_BINOP(v, w, &a, &b);
3201 c = long_bitwise(a, '^', b);
3202 Py_DECREF(a);
3203 Py_DECREF(b);
3204 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003205}
3206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003207static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003208long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003209{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003210 PyLongObject *a, *b;
3211 PyObject *c;
3212 CONVERT_BINOP(v, w, &a, &b);
3213 c = long_bitwise(a, '|', b);
3214 Py_DECREF(a);
3215 Py_DECREF(b);
3216 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003217}
3218
Guido van Rossum234f9421993-06-17 12:35:49 +00003219static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003220long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003221{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003222 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003223 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandlc02e1312007-04-11 17:16:24 +00003224 if (*pw == NULL)
3225 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003226 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003227 return 0;
3228 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003229 else if (PyLong_Check(*pw)) {
3230 Py_INCREF(*pv);
3231 Py_INCREF(*pw);
3232 return 0;
3233 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003234 return 1; /* Can't do it */
3235}
3236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003237static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003238long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003239{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003240 if (PyLong_CheckExact(v))
3241 Py_INCREF(v);
3242 else
3243 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003244 return v;
3245}
3246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003247static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003248long_int(PyObject *v)
3249{
3250 long x;
3251 x = PyLong_AsLong(v);
3252 if (PyErr_Occurred()) {
3253 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3254 PyErr_Clear();
3255 if (PyLong_CheckExact(v)) {
3256 Py_INCREF(v);
3257 return v;
3258 }
3259 else
3260 return _PyLong_Copy((PyLongObject *)v);
3261 }
3262 else
3263 return NULL;
3264 }
3265 return PyInt_FromLong(x);
3266}
3267
3268static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003269long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003270{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003271 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003272 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003273 if (result == -1.0 && PyErr_Occurred())
3274 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003275 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003276}
3277
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003278static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003279long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003280{
Eric Smith5e527eb2008-02-10 01:36:53 +00003281 return _PyLong_Format(v, 8, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003282}
3283
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003284static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003285long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003286{
Eric Smith5e527eb2008-02-10 01:36:53 +00003287 return _PyLong_Format(v, 16, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003288}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003289
3290static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003291long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003292
Tim Peters6d6c1a32001-08-02 04:15:00 +00003293static PyObject *
3294long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3295{
3296 PyObject *x = NULL;
3297 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003298 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003299
Guido van Rossumbef14172001-08-29 15:47:46 +00003300 if (type != &PyLong_Type)
3301 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003302 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3303 &x, &base))
3304 return NULL;
3305 if (x == NULL)
3306 return PyLong_FromLong(0L);
3307 if (base == -909)
3308 return PyNumber_Long(x);
Georg Brandl00cd8182007-03-06 18:41:12 +00003309 else if (PyString_Check(x)) {
3310 /* Since PyLong_FromString doesn't have a length parameter,
3311 * check here for possible NULs in the string. */
3312 char *string = PyString_AS_STRING(x);
3313 if (strlen(string) != PyString_Size(x)) {
3314 /* create a repr() of the input string,
3315 * just like PyLong_FromString does. */
3316 PyObject *srepr;
3317 srepr = PyObject_Repr(x);
3318 if (srepr == NULL)
3319 return NULL;
3320 PyErr_Format(PyExc_ValueError,
3321 "invalid literal for long() with base %d: %s",
3322 base, PyString_AS_STRING(srepr));
3323 Py_DECREF(srepr);
3324 return NULL;
3325 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003326 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandl00cd8182007-03-06 18:41:12 +00003327 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003328#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003329 else if (PyUnicode_Check(x))
3330 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3331 PyUnicode_GET_SIZE(x),
3332 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003333#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003334 else {
3335 PyErr_SetString(PyExc_TypeError,
3336 "long() can't convert non-string with explicit base");
3337 return NULL;
3338 }
3339}
3340
Guido van Rossumbef14172001-08-29 15:47:46 +00003341/* Wimpy, slow approach to tp_new calls for subtypes of long:
3342 first create a regular long from whatever arguments we got,
3343 then allocate a subtype instance and initialize it from
3344 the regular long. The regular long is then thrown away.
3345*/
3346static PyObject *
3347long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3348{
Anthony Baxter377be112006-04-11 06:54:30 +00003349 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003350 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003351
3352 assert(PyType_IsSubtype(type, &PyLong_Type));
3353 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3354 if (tmp == NULL)
3355 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003356 assert(PyLong_CheckExact(tmp));
Christian Heimese93237d2007-12-19 02:37:44 +00003357 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003358 if (n < 0)
3359 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003360 newobj = (PyLongObject *)type->tp_alloc(type, n);
3361 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003362 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003363 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003364 }
Anthony Baxter377be112006-04-11 06:54:30 +00003365 assert(PyLong_Check(newobj));
Christian Heimese93237d2007-12-19 02:37:44 +00003366 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003367 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003368 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003369 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003370 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003371}
3372
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003373static PyObject *
3374long_getnewargs(PyLongObject *v)
3375{
3376 return Py_BuildValue("(N)", _PyLong_Copy(v));
3377}
3378
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003379static PyObject *
3380long_getN(PyLongObject *v, void *context) {
3381 return PyLong_FromLong((intptr_t)context);
3382}
3383
Eric Smitha9f7d622008-02-17 19:46:49 +00003384static PyObject *
3385long__format__(PyObject *self, PyObject *args)
3386{
3387 PyObject *format_spec;
3388
3389 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3390 return NULL;
3391 if (PyString_Check(format_spec))
3392 return string_long__format__(self, args);
3393 if (PyUnicode_Check(format_spec)) {
3394 /* Convert format_spec to a str */
3395 PyObject *result = NULL;
3396 PyObject *newargs = NULL;
3397 PyObject *string_format_spec = NULL;
3398
3399 string_format_spec = PyObject_Str(format_spec);
3400 if (string_format_spec == NULL)
3401 goto done;
3402
3403 newargs = Py_BuildValue("(O)", string_format_spec);
3404 if (newargs == NULL)
3405 goto done;
3406
3407 result = string_long__format__(self, newargs);
3408
3409 done:
3410 Py_XDECREF(string_format_spec);
3411 Py_XDECREF(newargs);
3412 return result;
3413 }
3414 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
3415 return NULL;
3416}
3417
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003418static PyMethodDef long_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003419 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3420 "Returns self, the complex conjugate of any long."},
3421 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3422 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003423 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00003424 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003425 {NULL, NULL} /* sentinel */
3426};
3427
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003428static PyGetSetDef long_getset[] = {
3429 {"real",
3430 (getter)long_long, (setter)NULL,
3431 "the real part of a complex number",
3432 NULL},
3433 {"imag",
3434 (getter)long_getN, (setter)NULL,
3435 "the imaginary part of a complex number",
3436 (void*)0},
3437 {"numerator",
3438 (getter)long_long, (setter)NULL,
3439 "the numerator of a rational number in lowest terms",
3440 NULL},
3441 {"denominator",
3442 (getter)long_getN, (setter)NULL,
3443 "the denominator of a rational number in lowest terms",
3444 (void*)1},
3445 {NULL} /* Sentinel */
3446};
3447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003448PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003449"long(x[, base]) -> integer\n\
3450\n\
3451Convert a string or number to a long integer, if possible. A floating\n\
3452point argument will be truncated towards zero (this does not include a\n\
3453string representation of a floating point number!) When converting a\n\
3454string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003455converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003457static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003458 (binaryfunc) long_add, /*nb_add*/
3459 (binaryfunc) long_sub, /*nb_subtract*/
3460 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003461 long_classic_div, /*nb_divide*/
3462 long_mod, /*nb_remainder*/
3463 long_divmod, /*nb_divmod*/
3464 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003465 (unaryfunc) long_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003466 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003467 (unaryfunc) long_abs, /*tp_absolute*/
3468 (inquiry) long_nonzero, /*tp_nonzero*/
3469 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003470 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003471 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003472 long_and, /*nb_and*/
3473 long_xor, /*nb_xor*/
3474 long_or, /*nb_or*/
3475 long_coerce, /*nb_coerce*/
3476 long_int, /*nb_int*/
3477 long_long, /*nb_long*/
3478 long_float, /*nb_float*/
3479 long_oct, /*nb_oct*/
3480 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003481 0, /* nb_inplace_add */
3482 0, /* nb_inplace_subtract */
3483 0, /* nb_inplace_multiply */
3484 0, /* nb_inplace_divide */
3485 0, /* nb_inplace_remainder */
3486 0, /* nb_inplace_power */
3487 0, /* nb_inplace_lshift */
3488 0, /* nb_inplace_rshift */
3489 0, /* nb_inplace_and */
3490 0, /* nb_inplace_xor */
3491 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003492 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003493 long_true_divide, /* nb_true_divide */
3494 0, /* nb_inplace_floor_divide */
3495 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003496 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003497};
3498
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003499PyTypeObject PyLong_Type = {
3500 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003501 0, /* ob_size */
3502 "long", /* tp_name */
3503 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3504 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003505 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003506 0, /* tp_print */
3507 0, /* tp_getattr */
3508 0, /* tp_setattr */
3509 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003510 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003511 &long_as_number, /* tp_as_number */
3512 0, /* tp_as_sequence */
3513 0, /* tp_as_mapping */
3514 (hashfunc)long_hash, /* tp_hash */
3515 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003516 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003517 PyObject_GenericGetAttr, /* tp_getattro */
3518 0, /* tp_setattro */
3519 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003520 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00003521 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003522 long_doc, /* tp_doc */
3523 0, /* tp_traverse */
3524 0, /* tp_clear */
3525 0, /* tp_richcompare */
3526 0, /* tp_weaklistoffset */
3527 0, /* tp_iter */
3528 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003529 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003530 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003531 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003532 0, /* tp_base */
3533 0, /* tp_dict */
3534 0, /* tp_descr_get */
3535 0, /* tp_descr_set */
3536 0, /* tp_dictoffset */
3537 0, /* tp_init */
3538 0, /* tp_alloc */
3539 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003540 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003541};