blob: 2e13d6141f17f83164b345a30ec8a6e006fab054 [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) {
Eric Smith9ff19b52008-03-17 17:32:20 +00001468 /* No base given. Deduce the base from the contents
1469 of the string */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001470 if (str[0] != '0')
1471 base = 10;
1472 else if (str[1] == 'x' || str[1] == 'X')
1473 base = 16;
Eric Smith9ff19b52008-03-17 17:32:20 +00001474 else if (str[1] == 'o' || str[1] == 'O')
1475 base = 8;
1476 else if (str[1] == 'b' || str[1] == 'B')
1477 base = 2;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001478 else
Eric Smith9ff19b52008-03-17 17:32:20 +00001479 /* "old" (C-style) octal literal, still valid in
1480 2.x, although illegal in 3.x */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001481 base = 8;
1482 }
Eric Smith9ff19b52008-03-17 17:32:20 +00001483 /* Whether or not we were deducing the base, skip leading chars
1484 as needed */
1485 if (str[0] == '0' &&
1486 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1487 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
1488 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001489 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001490
Guido van Rossume6762971998-06-22 03:54:46 +00001491 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001492 if ((base & (base - 1)) == 0)
1493 z = long_from_binary_base(&str, base);
1494 else {
Tim Peters696cf432006-05-24 21:10:40 +00001495/***
1496Binary bases can be converted in time linear in the number of digits, because
1497Python's representation base is binary. Other bases (including decimal!) use
1498the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001499
Tim Peters696cf432006-05-24 21:10:40 +00001500First some math: the largest integer that can be expressed in N base-B digits
1501is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1502case number of Python digits needed to hold it is the smallest integer n s.t.
1503
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001504 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1505 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
1506 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001507
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001508The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
Tim Peters696cf432006-05-24 21:10:40 +00001509this quickly. A Python long with that much space is reserved near the start,
1510and the result is computed into it.
1511
1512The input string is actually treated as being in base base**i (i.e., i digits
1513are processed at a time), where two more static arrays hold:
1514
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001515 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
Tim Peters696cf432006-05-24 21:10:40 +00001516 convmultmax_base[base] = base ** convwidth_base[base]
1517
1518The first of these is the largest i such that i consecutive input digits
1519must fit in a single Python digit. The second is effectively the input
1520base we're really using.
1521
1522Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1523convmultmax_base[base], the result is "simply"
1524
1525 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1526
1527where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001528
1529Error analysis: as above, the number of Python digits `n` needed is worst-
1530case
1531
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001532 n >= N * log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001533
1534where `N` is the number of input digits in base `B`. This is computed via
1535
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001536 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001537
1538below. Two numeric concerns are how much space this can waste, and whether
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001539the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
Tim Peters9faa3ed2006-05-30 15:53:34 +00001540which is the default (and it's unlikely anyone changes that).
1541
1542Waste isn't a problem: provided the first input digit isn't 0, the difference
1543between the worst-case input with N digits and the smallest input with N
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001544digits is about a factor of B, but B is small compared to PyLong_BASE so at most
Tim Peters9faa3ed2006-05-30 15:53:34 +00001545one allocated Python digit can remain unused on that count. If
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001546N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001547and adding 1 returns a result 1 larger than necessary. However, that can't
1548happen: whenever B is a power of 2, long_from_binary_base() is called
1549instead, and it's impossible for B**i to be an integer power of 2**15 when
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001550B 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 +00001551an exact integer when B is not a power of 2, since B**i has a prime factor
1552other than 2 in that case, but (2**15)**j's only prime factor is 2).
1553
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001554The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001555is a little bit larger than an exact integer, but due to roundoff errors (in
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001556computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001557yields a numeric result a little less than that integer. Unfortunately, "how
1558close can a transcendental function get to an integer over some range?"
1559questions are generally theoretically intractable. Computer analysis via
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001560continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
Tim Peters9faa3ed2006-05-30 15:53:34 +00001561fractions, giving a sequence i/j of "the best" rational approximations. Then
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001562j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001563we can get very close to being in trouble, but very rarely. For example,
156476573 is a denominator in one of the continued-fraction approximations to
1565log(10)/log(2**15), and indeed:
1566
1567 >>> log(10)/log(2**15)*76573
1568 16958.000000654003
1569
1570is very close to an integer. If we were working with IEEE single-precision,
1571rounding errors could kill us. Finding worst cases in IEEE double-precision
1572requires better-than-double-precision log() functions, and Tim didn't bother.
1573Instead the code checks to see whether the allocated space is enough as each
1574new Python digit is added, and copies the whole thing to a larger long if not.
1575This should happen extremely rarely, and in fact I don't have a test case
1576that triggers it(!). Instead the code was tested by artificially allocating
1577just 1 digit at the start, so that the copying code was exercised for every
1578digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001579***/
1580 register twodigits c; /* current input character */
1581 Py_ssize_t size_z;
1582 int i;
1583 int convwidth;
1584 twodigits convmultmax, convmult;
1585 digit *pz, *pzstop;
1586 char* scan;
1587
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001588 static double log_base_PyLong_BASE[37] = {0.0e0,};
Tim Peters696cf432006-05-24 21:10:40 +00001589 static int convwidth_base[37] = {0,};
1590 static twodigits convmultmax_base[37] = {0,};
1591
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001592 if (log_base_PyLong_BASE[base] == 0.0) {
Tim Peters696cf432006-05-24 21:10:40 +00001593 twodigits convmax = base;
1594 int i = 1;
1595
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001596 log_base_PyLong_BASE[base] = log((double)base) /
1597 log((double)PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001598 for (;;) {
1599 twodigits next = convmax * base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001600 if (next > PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001601 break;
1602 convmax = next;
1603 ++i;
1604 }
1605 convmultmax_base[base] = convmax;
1606 assert(i > 0);
1607 convwidth_base[base] = i;
1608 }
1609
1610 /* Find length of the string of numeric characters. */
1611 scan = str;
Tim Petersda53afa2006-05-25 17:34:03 +00001612 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001613 ++scan;
1614
1615 /* Create a long object that can contain the largest possible
1616 * integer with this base and length. Note that there's no
1617 * need to initialize z->ob_digit -- no slot is read up before
1618 * being stored into.
1619 */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001620 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001621 /* Uncomment next line to test exceedingly rare copy code */
1622 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001623 assert(size_z > 0);
1624 z = _PyLong_New(size_z);
1625 if (z == NULL)
1626 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00001627 Py_SIZE(z) = 0;
Tim Peters696cf432006-05-24 21:10:40 +00001628
1629 /* `convwidth` consecutive input digits are treated as a single
1630 * digit in base `convmultmax`.
1631 */
1632 convwidth = convwidth_base[base];
1633 convmultmax = convmultmax_base[base];
1634
1635 /* Work ;-) */
1636 while (str < scan) {
1637 /* grab up to convwidth digits from the input string */
Tim Petersda53afa2006-05-25 17:34:03 +00001638 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00001639 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1640 c = (twodigits)(c * base +
Tim Petersda53afa2006-05-25 17:34:03 +00001641 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001642 assert(c < PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001643 }
1644
1645 convmult = convmultmax;
1646 /* Calculate the shift only if we couldn't get
1647 * convwidth digits.
1648 */
1649 if (i != convwidth) {
1650 convmult = base;
1651 for ( ; i > 1; --i)
1652 convmult *= base;
1653 }
1654
1655 /* Multiply z by convmult, and add c. */
1656 pz = z->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001657 pzstop = pz + Py_SIZE(z);
Tim Peters696cf432006-05-24 21:10:40 +00001658 for (; pz < pzstop; ++pz) {
1659 c += (twodigits)*pz * convmult;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001660 *pz = (digit)(c & PyLong_MASK);
1661 c >>= PyLong_SHIFT;
Tim Peters696cf432006-05-24 21:10:40 +00001662 }
1663 /* carry off the current end? */
1664 if (c) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001665 assert(c < PyLong_BASE);
Christian Heimese93237d2007-12-19 02:37:44 +00001666 if (Py_SIZE(z) < size_z) {
Tim Peters9faa3ed2006-05-30 15:53:34 +00001667 *pz = (digit)c;
Christian Heimese93237d2007-12-19 02:37:44 +00001668 ++Py_SIZE(z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001669 }
1670 else {
1671 PyLongObject *tmp;
1672 /* Extremely rare. Get more space. */
Christian Heimese93237d2007-12-19 02:37:44 +00001673 assert(Py_SIZE(z) == size_z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00001674 tmp = _PyLong_New(size_z + 1);
1675 if (tmp == NULL) {
1676 Py_DECREF(z);
1677 return NULL;
1678 }
1679 memcpy(tmp->ob_digit,
1680 z->ob_digit,
1681 sizeof(digit) * size_z);
1682 Py_DECREF(z);
1683 z = tmp;
1684 z->ob_digit[size_z] = (digit)c;
1685 ++size_z;
1686 }
Tim Peters696cf432006-05-24 21:10:40 +00001687 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001688 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001689 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001690 if (z == NULL)
1691 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001692 if (str == start)
1693 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00001694 if (sign < 0)
Christian Heimese93237d2007-12-19 02:37:44 +00001695 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001696 if (*str == 'L' || *str == 'l')
1697 str++;
1698 while (*str && isspace(Py_CHARMASK(*str)))
1699 str++;
1700 if (*str != '\0')
1701 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001702 if (pend)
1703 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001704 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001705
1706 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001707 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001708 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1709 strobj = PyString_FromStringAndSize(orig_str, slen);
1710 if (strobj == NULL)
1711 return NULL;
1712 strrepr = PyObject_Repr(strobj);
1713 Py_DECREF(strobj);
1714 if (strrepr == NULL)
1715 return NULL;
1716 PyErr_Format(PyExc_ValueError,
1717 "invalid literal for long() with base %d: %s",
1718 base, PyString_AS_STRING(strrepr));
1719 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001720 return NULL;
1721}
1722
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001723#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001724PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001725PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001726{
Walter Dörwald07e14762002-11-06 16:15:14 +00001727 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001728 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001729
Walter Dörwald07e14762002-11-06 16:15:14 +00001730 if (buffer == NULL)
1731 return NULL;
1732
1733 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1734 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001735 return NULL;
1736 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001737 result = PyLong_FromString(buffer, NULL, base);
1738 PyMem_FREE(buffer);
1739 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001740}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001741#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742
Tim Peters9f688bf2000-07-07 15:53:28 +00001743/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001744static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001745 (PyLongObject *, PyLongObject *, PyLongObject **);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001746static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00001747static int long_divrem(PyLongObject *, PyLongObject *,
1748 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001749
1750/* Long division with remainder, top-level routine */
1751
Guido van Rossume32e0141992-01-19 16:31:05 +00001752static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001753long_divrem(PyLongObject *a, PyLongObject *b,
1754 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001755{
Christian Heimese93237d2007-12-19 02:37:44 +00001756 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001758
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001759 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001760 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001761 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001762 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001763 }
1764 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001765 (size_a == size_b &&
1766 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001767 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001768 *pdiv = _PyLong_New(0);
Georg Brandlc02e1312007-04-11 17:16:24 +00001769 if (*pdiv == NULL)
1770 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001771 Py_INCREF(a);
1772 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001773 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001774 }
1775 if (size_b == 1) {
1776 digit rem = 0;
1777 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001778 if (z == NULL)
1779 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001780 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandlc02e1312007-04-11 17:16:24 +00001781 if (*prem == NULL) {
1782 Py_DECREF(z);
1783 return -1;
1784 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001785 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001786 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001787 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001788 if (z == NULL)
1789 return -1;
1790 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001791 /* Set the signs.
1792 The quotient z has the sign of a*b;
1793 the remainder r has the sign of a,
1794 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001795 if ((a->ob_size < 0) != (b->ob_size < 0))
1796 z->ob_size = -(z->ob_size);
1797 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1798 (*prem)->ob_size = -((*prem)->ob_size);
1799 *pdiv = z;
1800 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001801}
1802
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001803/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001804
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001805static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001806x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001807{
Christian Heimese93237d2007-12-19 02:37:44 +00001808 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001809 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001810 PyLongObject *v = mul1(v1, d);
1811 PyLongObject *w = mul1(w1, d);
1812 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001813 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001814
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001816 Py_XDECREF(v);
1817 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001818 return NULL;
1819 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001820
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001821 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimese93237d2007-12-19 02:37:44 +00001822 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
1823 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001824
Christian Heimese93237d2007-12-19 02:37:44 +00001825 size_v = ABS(Py_SIZE(v));
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001826 k = size_v - size_w;
1827 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001828
Neal Norwitzc6a989a2006-05-10 06:57:58 +00001829 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001830 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1831 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001832 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001833 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001834
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001835 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001836 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001837 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001838 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001839 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001840 if (vj == w->ob_digit[size_w-1])
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001841 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001842 else
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001843 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001844 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001845
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001846 while (w->ob_digit[size_w-2]*q >
1847 ((
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001848 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001849 + v->ob_digit[j-1]
1850 - q*w->ob_digit[size_w-1]
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001851 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001852 + v->ob_digit[j-2])
1853 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001854
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001855 for (i = 0; i < size_w && i+k < size_v; ++i) {
1856 twodigits z = w->ob_digit[i] * q;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001857 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001858 carry += v->ob_digit[i+k] - z
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001859 + ((twodigits)zz << PyLong_SHIFT);
1860 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1861 carry = Py_ARITHMETIC_RIGHT_SHIFT(PyLong_BASE_TWODIGITS_TYPE,
1862 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00001863 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001864 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001865
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001866 if (i+k < size_v) {
1867 carry += v->ob_digit[i+k];
1868 v->ob_digit[i+k] = 0;
1869 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001870
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001871 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001872 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001873 else {
1874 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001875 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001876 carry = 0;
1877 for (i = 0; i < size_w && i+k < size_v; ++i) {
1878 carry += v->ob_digit[i+k] + w->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001879 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001880 carry = Py_ARITHMETIC_RIGHT_SHIFT(
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001881 PyLong_BASE_TWODIGITS_TYPE,
1882 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001883 }
1884 }
1885 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001886
Guido van Rossumc206c761995-01-10 15:23:19 +00001887 if (a == NULL)
1888 *prem = NULL;
1889 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001890 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001891 *prem = divrem1(v, d, &d);
1892 /* d receives the (unused) remainder */
1893 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001894 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001895 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001896 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001897 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001898 Py_DECREF(v);
1899 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001900 return a;
1901}
1902
1903/* Methods */
1904
1905static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001906long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001907{
Christian Heimese93237d2007-12-19 02:37:44 +00001908 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001909}
1910
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001911static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001912long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001913{
Eric Smith5e527eb2008-02-10 01:36:53 +00001914 return _PyLong_Format(v, 10, 1, 0);
Fred Drake121ee271999-12-23 15:41:28 +00001915}
1916
1917static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001918long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001919{
Eric Smith5e527eb2008-02-10 01:36:53 +00001920 return _PyLong_Format(v, 10, 0, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001921}
1922
1923static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001924long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001925{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001926 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001927
Christian Heimese93237d2007-12-19 02:37:44 +00001928 if (Py_SIZE(a) != Py_SIZE(b)) {
1929 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001930 sign = 0;
1931 else
Christian Heimese93237d2007-12-19 02:37:44 +00001932 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001933 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001934 else {
Christian Heimese93237d2007-12-19 02:37:44 +00001935 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001936 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1937 ;
1938 if (i < 0)
1939 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001940 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001941 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimese93237d2007-12-19 02:37:44 +00001942 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001943 sign = -sign;
1944 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001945 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001946 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001947}
1948
Guido van Rossum9bfef441993-03-29 10:43:31 +00001949static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001950long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001951{
1952 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001953 Py_ssize_t i;
1954 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001955
1956 /* This is designed so that Python ints and longs with the
1957 same value hash to the same value, otherwise comparisons
1958 of mapping keys will turn out weird */
1959 i = v->ob_size;
1960 sign = 1;
1961 x = 0;
1962 if (i < 0) {
1963 sign = -1;
1964 i = -(i);
1965 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001966#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Facundo Batistad544df72007-09-19 15:10:06 +00001967 /* The following loop produces a C long x such that (unsigned long)x
1968 is congruent to the absolute value of v modulo ULONG_MAX. The
1969 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00001970 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001971 /* Force a native long #-bits (32 or 64) circular shift */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001972 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001973 x += v->ob_digit[i];
Facundo Batistad544df72007-09-19 15:10:06 +00001974 /* If the addition above overflowed (thinking of x as
1975 unsigned), we compensate by incrementing. This preserves
1976 the value modulo ULONG_MAX. */
1977 if ((unsigned long)x < v->ob_digit[i])
1978 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001979 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001980#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001981 x = x * sign;
1982 if (x == -1)
1983 x = -2;
1984 return x;
1985}
1986
1987
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001988/* Add the absolute values of two long integers. */
1989
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001990static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001991x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001992{
Christian Heimese93237d2007-12-19 02:37:44 +00001993 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001994 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001995 int i;
1996 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001997
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001998 /* Ensure a is the larger of the two: */
1999 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002000 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002001 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002002 size_a = size_b;
2003 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002004 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002006 if (z == NULL)
2007 return NULL;
2008 for (i = 0; i < size_b; ++i) {
2009 carry += a->ob_digit[i] + b->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002010 z->ob_digit[i] = carry & PyLong_MASK;
2011 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002012 }
2013 for (; i < size_a; ++i) {
2014 carry += a->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002015 z->ob_digit[i] = carry & PyLong_MASK;
2016 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002017 }
2018 z->ob_digit[i] = carry;
2019 return long_normalize(z);
2020}
2021
2022/* Subtract the absolute values of two integers. */
2023
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002024static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002025x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002026{
Christian Heimese93237d2007-12-19 02:37:44 +00002027 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002028 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002029 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002030 int sign = 1;
2031 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002032
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002033 /* Ensure a is the larger of the two: */
2034 if (size_a < size_b) {
2035 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002036 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002037 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002038 size_a = size_b;
2039 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002040 }
2041 else if (size_a == size_b) {
2042 /* Find highest digit where a and b differ: */
2043 i = size_a;
2044 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2045 ;
2046 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002048 if (a->ob_digit[i] < b->ob_digit[i]) {
2049 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002050 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002051 }
2052 size_a = size_b = i+1;
2053 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002054 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002055 if (z == NULL)
2056 return NULL;
2057 for (i = 0; i < size_b; ++i) {
2058 /* The following assumes unsigned arithmetic
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002059 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002060 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002061 z->ob_digit[i] = borrow & PyLong_MASK;
2062 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002063 borrow &= 1; /* Keep only one sign bit */
2064 }
2065 for (; i < size_a; ++i) {
2066 borrow = a->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002067 z->ob_digit[i] = borrow & PyLong_MASK;
2068 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002069 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002070 }
2071 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002072 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002073 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002074 return long_normalize(z);
2075}
2076
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002077static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002078long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002079{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002080 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002081
Neil Schemenauerba872e22001-01-04 01:46:03 +00002082 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2083
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002084 if (a->ob_size < 0) {
2085 if (b->ob_size < 0) {
2086 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002087 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002088 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002089 }
2090 else
2091 z = x_sub(b, a);
2092 }
2093 else {
2094 if (b->ob_size < 0)
2095 z = x_sub(a, b);
2096 else
2097 z = x_add(a, b);
2098 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002099 Py_DECREF(a);
2100 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002101 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002102}
2103
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002104static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002105long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002106{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002107 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002108
Neil Schemenauerba872e22001-01-04 01:46:03 +00002109 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2110
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002111 if (a->ob_size < 0) {
2112 if (b->ob_size < 0)
2113 z = x_sub(a, b);
2114 else
2115 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002116 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002117 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002118 }
2119 else {
2120 if (b->ob_size < 0)
2121 z = x_add(a, b);
2122 else
2123 z = x_sub(a, b);
2124 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002125 Py_DECREF(a);
2126 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002127 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002128}
2129
Tim Peters5af4e6c2002-08-12 02:31:19 +00002130/* Grade school multiplication, ignoring the signs.
2131 * Returns the absolute value of the product, or NULL if error.
2132 */
2133static PyLongObject *
2134x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002135{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002136 PyLongObject *z;
Christian Heimese93237d2007-12-19 02:37:44 +00002137 Py_ssize_t size_a = ABS(Py_SIZE(a));
2138 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002139 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002140
Tim Peters5af4e6c2002-08-12 02:31:19 +00002141 z = _PyLong_New(size_a + size_b);
2142 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002143 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002144
Christian Heimese93237d2007-12-19 02:37:44 +00002145 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002146 if (a == b) {
2147 /* Efficient squaring per HAC, Algorithm 14.16:
2148 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2149 * Gives slightly less than a 2x speedup when a == b,
2150 * via exploiting that each entry in the multiplication
2151 * pyramid appears twice (except for the size_a squares).
2152 */
2153 for (i = 0; i < size_a; ++i) {
2154 twodigits carry;
2155 twodigits f = a->ob_digit[i];
2156 digit *pz = z->ob_digit + (i << 1);
2157 digit *pa = a->ob_digit + i + 1;
2158 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002159
Tim Peters0973b992004-08-29 22:16:50 +00002160 SIGCHECK({
2161 Py_DECREF(z);
2162 return NULL;
2163 })
2164
2165 carry = *pz + f * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002166 *pz++ = (digit)(carry & PyLong_MASK);
2167 carry >>= PyLong_SHIFT;
2168 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002169
2170 /* Now f is added in twice in each column of the
2171 * pyramid it appears. Same as adding f<<1 once.
2172 */
2173 f <<= 1;
2174 while (pa < paend) {
2175 carry += *pz + *pa++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002176 *pz++ = (digit)(carry & PyLong_MASK);
2177 carry >>= PyLong_SHIFT;
2178 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002179 }
2180 if (carry) {
2181 carry += *pz;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002182 *pz++ = (digit)(carry & PyLong_MASK);
2183 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002184 }
2185 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002186 *pz += (digit)(carry & PyLong_MASK);
2187 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002188 }
Tim Peters0973b992004-08-29 22:16:50 +00002189 }
2190 else { /* a is not the same as b -- gradeschool long mult */
2191 for (i = 0; i < size_a; ++i) {
2192 twodigits carry = 0;
2193 twodigits f = a->ob_digit[i];
2194 digit *pz = z->ob_digit + i;
2195 digit *pb = b->ob_digit;
2196 digit *pbend = b->ob_digit + size_b;
2197
2198 SIGCHECK({
2199 Py_DECREF(z);
2200 return NULL;
2201 })
2202
2203 while (pb < pbend) {
2204 carry += *pz + *pb++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002205 *pz++ = (digit)(carry & PyLong_MASK);
2206 carry >>= PyLong_SHIFT;
2207 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002208 }
2209 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002210 *pz += (digit)(carry & PyLong_MASK);
2211 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002212 }
2213 }
Tim Peters44121a62002-08-12 06:17:58 +00002214 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002215}
2216
2217/* A helper for Karatsuba multiplication (k_mul).
2218 Takes a long "n" and an integer "size" representing the place to
2219 split, and sets low and high such that abs(n) == (high << size) + low,
2220 viewing the shift as being by digits. The sign bit is ignored, and
2221 the return values are >= 0.
2222 Returns 0 on success, -1 on failure.
2223*/
2224static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002225kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002226{
2227 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002228 Py_ssize_t size_lo, size_hi;
Christian Heimese93237d2007-12-19 02:37:44 +00002229 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002230
2231 size_lo = MIN(size_n, size);
2232 size_hi = size_n - size_lo;
2233
2234 if ((hi = _PyLong_New(size_hi)) == NULL)
2235 return -1;
2236 if ((lo = _PyLong_New(size_lo)) == NULL) {
2237 Py_DECREF(hi);
2238 return -1;
2239 }
2240
2241 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2242 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2243
2244 *high = long_normalize(hi);
2245 *low = long_normalize(lo);
2246 return 0;
2247}
2248
Tim Peters60004642002-08-12 22:01:34 +00002249static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2250
Tim Peters5af4e6c2002-08-12 02:31:19 +00002251/* Karatsuba multiplication. Ignores the input signs, and returns the
2252 * absolute value of the product (or NULL if error).
2253 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2254 */
2255static PyLongObject *
2256k_mul(PyLongObject *a, PyLongObject *b)
2257{
Christian Heimese93237d2007-12-19 02:37:44 +00002258 Py_ssize_t asize = ABS(Py_SIZE(a));
2259 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002260 PyLongObject *ah = NULL;
2261 PyLongObject *al = NULL;
2262 PyLongObject *bh = NULL;
2263 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002264 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002265 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002266 Py_ssize_t shift; /* the number of digits we split off */
2267 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002268
Tim Peters5af4e6c2002-08-12 02:31:19 +00002269 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2270 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2271 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002272 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002273 * By picking X to be a power of 2, "*X" is just shifting, and it's
2274 * been reduced to 3 multiplies on numbers half the size.
2275 */
2276
Tim Petersfc07e562002-08-12 02:54:10 +00002277 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002278 * is largest.
2279 */
Tim Peters738eda72002-08-12 15:08:20 +00002280 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002281 t1 = a;
2282 a = b;
2283 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002284
2285 i = asize;
2286 asize = bsize;
2287 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002288 }
2289
2290 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002291 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2292 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002293 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002294 return _PyLong_New(0);
2295 else
2296 return x_mul(a, b);
2297 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002298
Tim Peters60004642002-08-12 22:01:34 +00002299 /* If a is small compared to b, splitting on b gives a degenerate
2300 * case with ah==0, and Karatsuba may be (even much) less efficient
2301 * than "grade school" then. However, we can still win, by viewing
2302 * b as a string of "big digits", each of width a->ob_size. That
2303 * leads to a sequence of balanced calls to k_mul.
2304 */
2305 if (2 * asize <= bsize)
2306 return k_lopsided_mul(a, b);
2307
Tim Petersd6974a52002-08-13 20:37:51 +00002308 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002309 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002310 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002311 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002312
Tim Peters0973b992004-08-29 22:16:50 +00002313 if (a == b) {
2314 bh = ah;
2315 bl = al;
2316 Py_INCREF(bh);
2317 Py_INCREF(bl);
2318 }
2319 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002320
Tim Petersd64c1de2002-08-12 17:36:03 +00002321 /* The plan:
2322 * 1. Allocate result space (asize + bsize digits: that's always
2323 * enough).
2324 * 2. Compute ah*bh, and copy into result at 2*shift.
2325 * 3. Compute al*bl, and copy into result at 0. Note that this
2326 * can't overlap with #2.
2327 * 4. Subtract al*bl from the result, starting at shift. This may
2328 * underflow (borrow out of the high digit), but we don't care:
2329 * we're effectively doing unsigned arithmetic mod
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002330 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
Tim Petersd64c1de2002-08-12 17:36:03 +00002331 * borrows and carries out of the high digit can be ignored.
2332 * 5. Subtract ah*bh from the result, starting at shift.
2333 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2334 * at shift.
2335 */
2336
2337 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002338 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002339 if (ret == NULL) goto fail;
2340#ifdef Py_DEBUG
2341 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002342 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002343#endif
Tim Peters44121a62002-08-12 06:17:58 +00002344
Tim Petersd64c1de2002-08-12 17:36:03 +00002345 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002346 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002347 assert(Py_SIZE(t1) >= 0);
2348 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002349 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimese93237d2007-12-19 02:37:44 +00002350 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002351
2352 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimese93237d2007-12-19 02:37:44 +00002353 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002354 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002355 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002356 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002357
Tim Petersd64c1de2002-08-12 17:36:03 +00002358 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002359 if ((t2 = k_mul(al, bl)) == NULL) {
2360 Py_DECREF(t1);
2361 goto fail;
2362 }
Christian Heimese93237d2007-12-19 02:37:44 +00002363 assert(Py_SIZE(t2) >= 0);
2364 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2365 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002366
2367 /* Zero out remaining digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002368 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002369 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002370 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002371
Tim Petersd64c1de2002-08-12 17:36:03 +00002372 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2373 * because it's fresher in cache.
2374 */
Christian Heimese93237d2007-12-19 02:37:44 +00002375 i = Py_SIZE(ret) - shift; /* # digits after shift */
2376 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002377 Py_DECREF(t2);
2378
Christian Heimese93237d2007-12-19 02:37:44 +00002379 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002380 Py_DECREF(t1);
2381
Tim Petersd64c1de2002-08-12 17:36:03 +00002382 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002383 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2384 Py_DECREF(ah);
2385 Py_DECREF(al);
2386 ah = al = NULL;
2387
Tim Peters0973b992004-08-29 22:16:50 +00002388 if (a == b) {
2389 t2 = t1;
2390 Py_INCREF(t2);
2391 }
2392 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002393 Py_DECREF(t1);
2394 goto fail;
2395 }
2396 Py_DECREF(bh);
2397 Py_DECREF(bl);
2398 bh = bl = NULL;
2399
Tim Peters738eda72002-08-12 15:08:20 +00002400 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002401 Py_DECREF(t1);
2402 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002403 if (t3 == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002404 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002405
Tim Petersd6974a52002-08-13 20:37:51 +00002406 /* Add t3. It's not obvious why we can't run out of room here.
2407 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002408 */
Christian Heimese93237d2007-12-19 02:37:44 +00002409 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002410 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002411
Tim Peters5af4e6c2002-08-12 02:31:19 +00002412 return long_normalize(ret);
2413
2414 fail:
2415 Py_XDECREF(ret);
2416 Py_XDECREF(ah);
2417 Py_XDECREF(al);
2418 Py_XDECREF(bh);
2419 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002420 return NULL;
2421}
2422
Tim Petersd6974a52002-08-13 20:37:51 +00002423/* (*) Why adding t3 can't "run out of room" above.
2424
Tim Petersab86c2b2002-08-15 20:06:00 +00002425Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2426to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002427
Tim Petersab86c2b2002-08-15 20:06:00 +000024281. For any integer i, i = c(i/2) + f(i/2). In particular,
2429 bsize = c(bsize/2) + f(bsize/2).
24302. shift = f(bsize/2)
24313. asize <= bsize
24324. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2433 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002434
Tim Petersab86c2b2002-08-15 20:06:00 +00002435We allocated asize + bsize result digits, and add t3 into them at an offset
2436of shift. This leaves asize+bsize-shift allocated digit positions for t3
2437to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2438asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002439
Tim Petersab86c2b2002-08-15 20:06:00 +00002440bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2441at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002442
Tim Petersab86c2b2002-08-15 20:06:00 +00002443If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2444digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2445most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002446
Tim Petersab86c2b2002-08-15 20:06:00 +00002447The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002448
Tim Petersab86c2b2002-08-15 20:06:00 +00002449 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002450
Tim Petersab86c2b2002-08-15 20:06:00 +00002451and we have asize + c(bsize/2) available digit positions. We need to show
2452this is always enough. An instance of c(bsize/2) cancels out in both, so
2453the question reduces to whether asize digits is enough to hold
2454(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2455then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2456asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002457digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002458asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002459c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2460is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2461bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002462
Tim Peters48d52c02002-08-14 17:07:32 +00002463Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2464clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2465ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002466*/
2467
Tim Peters60004642002-08-12 22:01:34 +00002468/* b has at least twice the digits of a, and a is big enough that Karatsuba
2469 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2470 * of slices, each with a->ob_size digits, and multiply the slices by a,
2471 * one at a time. This gives k_mul balanced inputs to work with, and is
2472 * also cache-friendly (we compute one double-width slice of the result
2473 * at a time, then move on, never bactracking except for the helpful
2474 * single-width slice overlap between successive partial sums).
2475 */
2476static PyLongObject *
2477k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2478{
Christian Heimese93237d2007-12-19 02:37:44 +00002479 const Py_ssize_t asize = ABS(Py_SIZE(a));
2480 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002481 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002482 PyLongObject *ret;
2483 PyLongObject *bslice = NULL;
2484
2485 assert(asize > KARATSUBA_CUTOFF);
2486 assert(2 * asize <= bsize);
2487
2488 /* Allocate result space, and zero it out. */
2489 ret = _PyLong_New(asize + bsize);
2490 if (ret == NULL)
2491 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002492 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002493
2494 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002495 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002496 if (bslice == NULL)
2497 goto fail;
2498
2499 nbdone = 0;
2500 while (bsize > 0) {
2501 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002502 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002503
2504 /* Multiply the next slice of b by a. */
2505 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2506 nbtouse * sizeof(digit));
Christian Heimese93237d2007-12-19 02:37:44 +00002507 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002508 product = k_mul(a, bslice);
2509 if (product == NULL)
2510 goto fail;
2511
2512 /* Add into result. */
Christian Heimese93237d2007-12-19 02:37:44 +00002513 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2514 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002515 Py_DECREF(product);
2516
2517 bsize -= nbtouse;
2518 nbdone += nbtouse;
2519 }
2520
2521 Py_DECREF(bslice);
2522 return long_normalize(ret);
2523
2524 fail:
2525 Py_DECREF(ret);
2526 Py_XDECREF(bslice);
2527 return NULL;
2528}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002529
2530static PyObject *
2531long_mul(PyLongObject *v, PyLongObject *w)
2532{
2533 PyLongObject *a, *b, *z;
2534
2535 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002536 Py_INCREF(Py_NotImplemented);
2537 return Py_NotImplemented;
2538 }
2539
Tim Petersd64c1de2002-08-12 17:36:03 +00002540 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002541 /* Negate if exactly one of the inputs is negative. */
2542 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002543 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002544 Py_DECREF(a);
2545 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002546 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002547}
2548
Guido van Rossume32e0141992-01-19 16:31:05 +00002549/* The / and % operators are now defined in terms of divmod().
2550 The expression a mod b has the value a - b*floor(a/b).
2551 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002552 |a| by |b|, with the sign of a. This is also expressed
2553 as a - b*trunc(a/b), if trunc truncates towards zero.
2554 Some examples:
2555 a b a rem b a mod b
2556 13 10 3 3
2557 -13 10 -3 7
2558 13 -10 3 -7
2559 -13 -10 -3 -3
2560 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002561 have different signs. We then subtract one from the 'div'
2562 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002563
Tim Peters47e52ee2004-08-30 02:44:38 +00002564/* Compute
2565 * *pdiv, *pmod = divmod(v, w)
2566 * NULL can be passed for pdiv or pmod, in which case that part of
2567 * the result is simply thrown away. The caller owns a reference to
2568 * each of these it requests (does not pass NULL for).
2569 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002570static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002571l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002572 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002573{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002574 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002575
Guido van Rossume32e0141992-01-19 16:31:05 +00002576 if (long_divrem(v, w, &div, &mod) < 0)
2577 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00002578 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2579 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002580 PyLongObject *temp;
2581 PyLongObject *one;
2582 temp = (PyLongObject *) long_add(mod, w);
2583 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002584 mod = temp;
2585 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002586 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002587 return -1;
2588 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002589 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002590 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002591 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2592 Py_DECREF(mod);
2593 Py_DECREF(div);
2594 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002595 return -1;
2596 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002597 Py_DECREF(one);
2598 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002599 div = temp;
2600 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002601 if (pdiv != NULL)
2602 *pdiv = div;
2603 else
2604 Py_DECREF(div);
2605
2606 if (pmod != NULL)
2607 *pmod = mod;
2608 else
2609 Py_DECREF(mod);
2610
Guido van Rossume32e0141992-01-19 16:31:05 +00002611 return 0;
2612}
2613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002614static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002615long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002616{
Tim Peters47e52ee2004-08-30 02:44:38 +00002617 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002618
2619 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002620 if (l_divmod(a, b, &div, NULL) < 0)
2621 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002622 Py_DECREF(a);
2623 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002624 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002625}
2626
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002627static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002628long_classic_div(PyObject *v, PyObject *w)
2629{
Tim Peters47e52ee2004-08-30 02:44:38 +00002630 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002631
2632 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002633 if (Py_DivisionWarningFlag &&
2634 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2635 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002636 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002637 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002638 Py_DECREF(a);
2639 Py_DECREF(b);
2640 return (PyObject *)div;
2641}
2642
2643static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002644long_true_divide(PyObject *v, PyObject *w)
2645{
Tim Peterse2a60002001-09-04 06:17:36 +00002646 PyLongObject *a, *b;
2647 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002648 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002649
2650 CONVERT_BINOP(v, w, &a, &b);
2651 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2652 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002653 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2654 Py_DECREF(a);
2655 Py_DECREF(b);
2656 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002657 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002658 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2659 but should really be set correctly after sucessful calls to
2660 _PyLong_AsScaledDouble() */
2661 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002662
2663 if (bd == 0.0) {
2664 PyErr_SetString(PyExc_ZeroDivisionError,
2665 "long division or modulo by zero");
2666 return NULL;
2667 }
2668
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002669 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002670 ad /= bd; /* overflow/underflow impossible here */
2671 aexp -= bexp;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002672 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002673 goto overflow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002674 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002675 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002676 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002677 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002678 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002679 goto overflow;
2680 return PyFloat_FromDouble(ad);
2681
2682overflow:
2683 PyErr_SetString(PyExc_OverflowError,
2684 "long/long too large for a float");
2685 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002686
Tim Peters20dab9f2001-09-04 05:31:47 +00002687}
2688
2689static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002690long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002691{
Tim Peters47e52ee2004-08-30 02:44:38 +00002692 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002693
2694 CONVERT_BINOP(v, w, &a, &b);
2695
Tim Peters47e52ee2004-08-30 02:44:38 +00002696 if (l_divmod(a, b, NULL, &mod) < 0)
2697 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002698 Py_DECREF(a);
2699 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002700 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002701}
2702
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002703static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002704long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002705{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002706 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002707 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002708
2709 CONVERT_BINOP(v, w, &a, &b);
2710
2711 if (l_divmod(a, b, &div, &mod) < 0) {
2712 Py_DECREF(a);
2713 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002714 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002715 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002716 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002717 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002718 PyTuple_SetItem(z, 0, (PyObject *) div);
2719 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002720 }
2721 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002722 Py_DECREF(div);
2723 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002724 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002725 Py_DECREF(a);
2726 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002727 return z;
2728}
2729
Tim Peters47e52ee2004-08-30 02:44:38 +00002730/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002731static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002732long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002733{
Tim Peters47e52ee2004-08-30 02:44:38 +00002734 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2735 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002736
Tim Peters47e52ee2004-08-30 02:44:38 +00002737 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002738 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002739 PyLongObject *temp = NULL;
2740
2741 /* 5-ary values. If the exponent is large enough, table is
2742 * precomputed so that table[i] == a**i % c for i in range(32).
2743 */
2744 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2745 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2746
2747 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002748 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002749 if (PyLong_Check(x)) {
2750 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002751 Py_INCREF(x);
2752 }
Tim Petersde7990b2005-07-17 23:45:23 +00002753 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002754 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002755 if (c == NULL)
2756 goto Error;
2757 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002758 else if (x == Py_None)
2759 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002760 else {
2761 Py_DECREF(a);
2762 Py_DECREF(b);
2763 Py_INCREF(Py_NotImplemented);
2764 return Py_NotImplemented;
2765 }
Tim Peters4c483c42001-09-05 06:24:58 +00002766
Christian Heimese93237d2007-12-19 02:37:44 +00002767 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00002768 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002769 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002770 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002771 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002772 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002773 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002774 /* else return a float. This works because we know
2775 that this calls float_pow() which converts its
2776 arguments to double. */
2777 Py_DECREF(a);
2778 Py_DECREF(b);
2779 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002780 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002781 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002782
2783 if (c) {
2784 /* if modulus == 0:
2785 raise ValueError() */
Christian Heimese93237d2007-12-19 02:37:44 +00002786 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002787 PyErr_SetString(PyExc_ValueError,
2788 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002789 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002790 }
2791
2792 /* if modulus < 0:
2793 negativeOutput = True
2794 modulus = -modulus */
Christian Heimese93237d2007-12-19 02:37:44 +00002795 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002796 negativeOutput = 1;
2797 temp = (PyLongObject *)_PyLong_Copy(c);
2798 if (temp == NULL)
2799 goto Error;
2800 Py_DECREF(c);
2801 c = temp;
2802 temp = NULL;
2803 c->ob_size = - c->ob_size;
2804 }
2805
2806 /* if modulus == 1:
2807 return 0 */
Christian Heimese93237d2007-12-19 02:37:44 +00002808 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002809 z = (PyLongObject *)PyLong_FromLong(0L);
2810 goto Done;
2811 }
2812
2813 /* if base < 0:
2814 base = base % modulus
2815 Having the base positive just makes things easier. */
Christian Heimese93237d2007-12-19 02:37:44 +00002816 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002817 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002818 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002819 Py_DECREF(a);
2820 a = temp;
2821 temp = NULL;
2822 }
2823 }
2824
2825 /* At this point a, b, and c are guaranteed non-negative UNLESS
2826 c is NULL, in which case a may be negative. */
2827
2828 z = (PyLongObject *)PyLong_FromLong(1L);
2829 if (z == NULL)
2830 goto Error;
2831
2832 /* Perform a modular reduction, X = X % c, but leave X alone if c
2833 * is NULL.
2834 */
2835#define REDUCE(X) \
2836 if (c != NULL) { \
2837 if (l_divmod(X, c, NULL, &temp) < 0) \
2838 goto Error; \
2839 Py_XDECREF(X); \
2840 X = temp; \
2841 temp = NULL; \
2842 }
2843
2844 /* Multiply two values, then reduce the result:
2845 result = X*Y % c. If c is NULL, skip the mod. */
2846#define MULT(X, Y, result) \
2847{ \
2848 temp = (PyLongObject *)long_mul(X, Y); \
2849 if (temp == NULL) \
2850 goto Error; \
2851 Py_XDECREF(result); \
2852 result = temp; \
2853 temp = NULL; \
2854 REDUCE(result) \
2855}
2856
Christian Heimese93237d2007-12-19 02:37:44 +00002857 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002858 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2859 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimese93237d2007-12-19 02:37:44 +00002860 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002861 digit bi = b->ob_digit[i];
2862
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002863 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002864 MULT(z, z, z)
2865 if (bi & j)
2866 MULT(z, a, z)
2867 }
2868 }
2869 }
2870 else {
2871 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2872 Py_INCREF(z); /* still holds 1L */
2873 table[0] = z;
2874 for (i = 1; i < 32; ++i)
2875 MULT(table[i-1], a, table[i])
2876
Christian Heimese93237d2007-12-19 02:37:44 +00002877 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002878 const digit bi = b->ob_digit[i];
2879
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002880 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002881 const int index = (bi >> j) & 0x1f;
2882 for (k = 0; k < 5; ++k)
2883 MULT(z, z, z)
2884 if (index)
2885 MULT(z, table[index], z)
2886 }
2887 }
2888 }
2889
Christian Heimese93237d2007-12-19 02:37:44 +00002890 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002891 temp = (PyLongObject *)long_sub(z, c);
2892 if (temp == NULL)
2893 goto Error;
2894 Py_DECREF(z);
2895 z = temp;
2896 temp = NULL;
2897 }
2898 goto Done;
2899
2900 Error:
2901 if (z != NULL) {
2902 Py_DECREF(z);
2903 z = NULL;
2904 }
2905 /* fall through */
2906 Done:
Christian Heimese93237d2007-12-19 02:37:44 +00002907 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002908 for (i = 0; i < 32; ++i)
2909 Py_XDECREF(table[i]);
2910 }
Tim Petersde7990b2005-07-17 23:45:23 +00002911 Py_DECREF(a);
2912 Py_DECREF(b);
2913 Py_XDECREF(c);
2914 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002915 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002916}
2917
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002918static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002919long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002920{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002921 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002922 PyLongObject *x;
2923 PyLongObject *w;
2924 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002925 if (w == NULL)
2926 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002927 x = (PyLongObject *) long_add(v, w);
2928 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002929 if (x == NULL)
2930 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002931 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002932 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002933}
2934
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002935static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002936long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002937{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002938 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002939 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002940 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002941 Py_INCREF(v);
2942 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002943 }
Tim Peters69c2de32001-09-11 22:31:33 +00002944 z = (PyLongObject *)_PyLong_Copy(v);
2945 if (z != NULL)
2946 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002947 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002948}
2949
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002950static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002951long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002952{
2953 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002954 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002955 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002956 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002957}
2958
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002959static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002960long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002961{
Christian Heimese93237d2007-12-19 02:37:44 +00002962 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002963}
2964
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002965static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002966long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002967{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002968 PyLongObject *a, *b;
2969 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002970 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002971 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002972 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002973
Neil Schemenauerba872e22001-01-04 01:46:03 +00002974 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2975
Christian Heimese93237d2007-12-19 02:37:44 +00002976 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002977 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002978 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002979 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002980 if (a1 == NULL)
2981 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002982 a2 = (PyLongObject *) long_rshift(a1, b);
2983 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002984 if (a2 == NULL)
2985 goto rshift_error;
2986 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002987 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002988 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002989 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002990
Neil Schemenauerba872e22001-01-04 01:46:03 +00002991 shiftby = PyLong_AsLong((PyObject *)b);
2992 if (shiftby == -1L && PyErr_Occurred())
2993 goto rshift_error;
2994 if (shiftby < 0) {
2995 PyErr_SetString(PyExc_ValueError,
2996 "negative shift count");
2997 goto rshift_error;
2998 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002999 wordshift = shiftby / PyLong_SHIFT;
Christian Heimese93237d2007-12-19 02:37:44 +00003000 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003001 if (newsize <= 0) {
3002 z = _PyLong_New(0);
3003 Py_DECREF(a);
3004 Py_DECREF(b);
3005 return (PyObject *)z;
3006 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003007 loshift = shiftby % PyLong_SHIFT;
3008 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003009 lomask = ((digit)1 << hishift) - 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003010 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003011 z = _PyLong_New(newsize);
3012 if (z == NULL)
3013 goto rshift_error;
Christian Heimese93237d2007-12-19 02:37:44 +00003014 if (Py_SIZE(a) < 0)
3015 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003016 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3017 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3018 if (i+1 < newsize)
3019 z->ob_digit[i] |=
3020 (a->ob_digit[j+1] << hishift) & himask;
3021 }
3022 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003023 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003024rshift_error:
3025 Py_DECREF(a);
3026 Py_DECREF(b);
3027 return (PyObject *) z;
3028
Guido van Rossumc6913e71991-11-19 20:26:46 +00003029}
3030
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003031static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003032long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003033{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003034 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003035 PyLongObject *a, *b;
3036 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003037 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003038 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003039 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003040
Neil Schemenauerba872e22001-01-04 01:46:03 +00003041 CONVERT_BINOP(v, w, &a, &b);
3042
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003043 shiftby = PyLong_AsLong((PyObject *)b);
3044 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003045 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003046 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003047 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003048 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003049 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003050 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003051 PyErr_SetString(PyExc_ValueError,
3052 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003053 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003054 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003055 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3056 wordshift = (int)shiftby / PyLong_SHIFT;
3057 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003058
3059 oldsize = ABS(a->ob_size);
3060 newsize = oldsize + wordshift;
3061 if (remshift)
3062 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003063 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003064 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003065 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003066 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003067 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003068 for (i = 0; i < wordshift; i++)
3069 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003070 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003071 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003072 accum |= (twodigits)a->ob_digit[j] << remshift;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003073 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3074 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003075 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003076 if (remshift)
3077 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003078 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003079 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003080 z = long_normalize(z);
3081lshift_error:
3082 Py_DECREF(a);
3083 Py_DECREF(b);
3084 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003085}
3086
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003087
3088/* Bitwise and/xor/or operations */
3089
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003090static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003091long_bitwise(PyLongObject *a,
3092 int op, /* '&', '|', '^' */
3093 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003094{
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003095 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003096 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003097 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003098 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003099 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003100 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003101 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003102
Christian Heimese93237d2007-12-19 02:37:44 +00003103 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003104 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003105 if (a == NULL)
3106 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003107 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003108 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003109 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003110 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003111 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003112 }
Christian Heimese93237d2007-12-19 02:37:44 +00003113 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003114 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003115 if (b == NULL) {
3116 Py_DECREF(a);
3117 return NULL;
3118 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003119 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003120 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003121 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003122 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003123 maskb = 0;
3124 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003125
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003126 negz = 0;
3127 switch (op) {
3128 case '^':
3129 if (maska != maskb) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003130 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003131 negz = -1;
3132 }
3133 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003134 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003135 if (maska && maskb) {
3136 op = '|';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003137 maska ^= PyLong_MASK;
3138 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003139 negz = -1;
3140 }
3141 break;
3142 case '|':
3143 if (maska || maskb) {
3144 op = '&';
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003145 maska ^= PyLong_MASK;
3146 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003147 negz = -1;
3148 }
3149 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003150 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003151
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003152 /* JRH: The original logic here was to allocate the result value (z)
3153 as the longer of the two operands. However, there are some cases
3154 where the result is guaranteed to be shorter than that: AND of two
3155 positives, OR of two negatives: use the shorter number. AND with
3156 mixed signs: use the positive number. OR with mixed signs: use the
3157 negative number. After the transformations above, op will be '&'
3158 iff one of these cases applies, and mask will be non-0 for operands
3159 whose length should be ignored.
3160 */
3161
Christian Heimese93237d2007-12-19 02:37:44 +00003162 size_a = Py_SIZE(a);
3163 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003164 size_z = op == '&'
3165 ? (maska
3166 ? size_b
3167 : (maskb ? size_a : MIN(size_a, size_b)))
3168 : MAX(size_a, size_b);
3169 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003170 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003171 Py_DECREF(a);
3172 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003173 return NULL;
3174 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003175
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003176 for (i = 0; i < size_z; ++i) {
3177 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3178 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3179 switch (op) {
3180 case '&': z->ob_digit[i] = diga & digb; break;
3181 case '|': z->ob_digit[i] = diga | digb; break;
3182 case '^': z->ob_digit[i] = diga ^ digb; break;
3183 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003184 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003186 Py_DECREF(a);
3187 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003188 z = long_normalize(z);
3189 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003190 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003191 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003192 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003193 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003194}
3195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003196static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003197long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003198{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003199 PyLongObject *a, *b;
3200 PyObject *c;
3201 CONVERT_BINOP(v, w, &a, &b);
3202 c = long_bitwise(a, '&', b);
3203 Py_DECREF(a);
3204 Py_DECREF(b);
3205 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003206}
3207
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003208static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003209long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003210{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003211 PyLongObject *a, *b;
3212 PyObject *c;
3213 CONVERT_BINOP(v, w, &a, &b);
3214 c = long_bitwise(a, '^', b);
3215 Py_DECREF(a);
3216 Py_DECREF(b);
3217 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003218}
3219
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003220static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003221long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003222{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003223 PyLongObject *a, *b;
3224 PyObject *c;
3225 CONVERT_BINOP(v, w, &a, &b);
3226 c = long_bitwise(a, '|', b);
3227 Py_DECREF(a);
3228 Py_DECREF(b);
3229 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003230}
3231
Guido van Rossum234f9421993-06-17 12:35:49 +00003232static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003233long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003234{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003235 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003236 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandlc02e1312007-04-11 17:16:24 +00003237 if (*pw == NULL)
3238 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003239 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003240 return 0;
3241 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003242 else if (PyLong_Check(*pw)) {
3243 Py_INCREF(*pv);
3244 Py_INCREF(*pw);
3245 return 0;
3246 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003247 return 1; /* Can't do it */
3248}
3249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003250static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003251long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003252{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003253 if (PyLong_CheckExact(v))
3254 Py_INCREF(v);
3255 else
3256 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003257 return v;
3258}
3259
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003260static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003261long_int(PyObject *v)
3262{
3263 long x;
3264 x = PyLong_AsLong(v);
3265 if (PyErr_Occurred()) {
3266 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3267 PyErr_Clear();
3268 if (PyLong_CheckExact(v)) {
3269 Py_INCREF(v);
3270 return v;
3271 }
3272 else
3273 return _PyLong_Copy((PyLongObject *)v);
3274 }
3275 else
3276 return NULL;
3277 }
3278 return PyInt_FromLong(x);
3279}
3280
3281static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003282long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003283{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003284 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003285 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003286 if (result == -1.0 && PyErr_Occurred())
3287 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003288 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003289}
3290
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003291static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003292long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003293{
Eric Smith5e527eb2008-02-10 01:36:53 +00003294 return _PyLong_Format(v, 8, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003295}
3296
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003297static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003298long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003299{
Eric Smith5e527eb2008-02-10 01:36:53 +00003300 return _PyLong_Format(v, 16, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003301}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003302
3303static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003304long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003305
Tim Peters6d6c1a32001-08-02 04:15:00 +00003306static PyObject *
3307long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3308{
3309 PyObject *x = NULL;
3310 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003311 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003312
Guido van Rossumbef14172001-08-29 15:47:46 +00003313 if (type != &PyLong_Type)
3314 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003315 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3316 &x, &base))
3317 return NULL;
3318 if (x == NULL)
3319 return PyLong_FromLong(0L);
3320 if (base == -909)
3321 return PyNumber_Long(x);
Georg Brandl00cd8182007-03-06 18:41:12 +00003322 else if (PyString_Check(x)) {
3323 /* Since PyLong_FromString doesn't have a length parameter,
3324 * check here for possible NULs in the string. */
3325 char *string = PyString_AS_STRING(x);
3326 if (strlen(string) != PyString_Size(x)) {
3327 /* create a repr() of the input string,
3328 * just like PyLong_FromString does. */
3329 PyObject *srepr;
3330 srepr = PyObject_Repr(x);
3331 if (srepr == NULL)
3332 return NULL;
3333 PyErr_Format(PyExc_ValueError,
3334 "invalid literal for long() with base %d: %s",
3335 base, PyString_AS_STRING(srepr));
3336 Py_DECREF(srepr);
3337 return NULL;
3338 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003339 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandl00cd8182007-03-06 18:41:12 +00003340 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003341#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003342 else if (PyUnicode_Check(x))
3343 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3344 PyUnicode_GET_SIZE(x),
3345 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003346#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347 else {
3348 PyErr_SetString(PyExc_TypeError,
3349 "long() can't convert non-string with explicit base");
3350 return NULL;
3351 }
3352}
3353
Guido van Rossumbef14172001-08-29 15:47:46 +00003354/* Wimpy, slow approach to tp_new calls for subtypes of long:
3355 first create a regular long from whatever arguments we got,
3356 then allocate a subtype instance and initialize it from
3357 the regular long. The regular long is then thrown away.
3358*/
3359static PyObject *
3360long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3361{
Anthony Baxter377be112006-04-11 06:54:30 +00003362 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003363 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003364
3365 assert(PyType_IsSubtype(type, &PyLong_Type));
3366 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3367 if (tmp == NULL)
3368 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003369 assert(PyLong_CheckExact(tmp));
Christian Heimese93237d2007-12-19 02:37:44 +00003370 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003371 if (n < 0)
3372 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003373 newobj = (PyLongObject *)type->tp_alloc(type, n);
3374 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003375 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003376 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003377 }
Anthony Baxter377be112006-04-11 06:54:30 +00003378 assert(PyLong_Check(newobj));
Christian Heimese93237d2007-12-19 02:37:44 +00003379 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003380 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003381 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003382 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003383 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003384}
3385
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003386static PyObject *
3387long_getnewargs(PyLongObject *v)
3388{
3389 return Py_BuildValue("(N)", _PyLong_Copy(v));
3390}
3391
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003392static PyObject *
3393long_getN(PyLongObject *v, void *context) {
Jeffrey Yasskin5de250e2008-03-18 01:09:59 +00003394 return PyLong_FromLong((Py_intptr_t)context);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003395}
3396
Eric Smitha9f7d622008-02-17 19:46:49 +00003397static PyObject *
3398long__format__(PyObject *self, PyObject *args)
3399{
3400 PyObject *format_spec;
3401
3402 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3403 return NULL;
3404 if (PyString_Check(format_spec))
3405 return string_long__format__(self, args);
3406 if (PyUnicode_Check(format_spec)) {
3407 /* Convert format_spec to a str */
3408 PyObject *result = NULL;
3409 PyObject *newargs = NULL;
3410 PyObject *string_format_spec = NULL;
3411
3412 string_format_spec = PyObject_Str(format_spec);
3413 if (string_format_spec == NULL)
3414 goto done;
3415
3416 newargs = Py_BuildValue("(O)", string_format_spec);
3417 if (newargs == NULL)
3418 goto done;
3419
3420 result = string_long__format__(self, newargs);
3421
3422 done:
3423 Py_XDECREF(string_format_spec);
3424 Py_XDECREF(newargs);
3425 return result;
3426 }
3427 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
3428 return NULL;
3429}
3430
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003431static PyMethodDef long_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003432 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3433 "Returns self, the complex conjugate of any long."},
3434 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3435 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003436 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00003437 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003438 {NULL, NULL} /* sentinel */
3439};
3440
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003441static PyGetSetDef long_getset[] = {
3442 {"real",
3443 (getter)long_long, (setter)NULL,
3444 "the real part of a complex number",
3445 NULL},
3446 {"imag",
3447 (getter)long_getN, (setter)NULL,
3448 "the imaginary part of a complex number",
3449 (void*)0},
3450 {"numerator",
3451 (getter)long_long, (setter)NULL,
3452 "the numerator of a rational number in lowest terms",
3453 NULL},
3454 {"denominator",
3455 (getter)long_getN, (setter)NULL,
3456 "the denominator of a rational number in lowest terms",
3457 (void*)1},
3458 {NULL} /* Sentinel */
3459};
3460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003462"long(x[, base]) -> integer\n\
3463\n\
3464Convert a string or number to a long integer, if possible. A floating\n\
3465point argument will be truncated towards zero (this does not include a\n\
3466string representation of a floating point number!) When converting a\n\
3467string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003468converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003469
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003470static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003471 (binaryfunc) long_add, /*nb_add*/
3472 (binaryfunc) long_sub, /*nb_subtract*/
3473 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003474 long_classic_div, /*nb_divide*/
3475 long_mod, /*nb_remainder*/
3476 long_divmod, /*nb_divmod*/
3477 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003478 (unaryfunc) long_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003479 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003480 (unaryfunc) long_abs, /*tp_absolute*/
3481 (inquiry) long_nonzero, /*tp_nonzero*/
3482 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003483 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003484 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003485 long_and, /*nb_and*/
3486 long_xor, /*nb_xor*/
3487 long_or, /*nb_or*/
3488 long_coerce, /*nb_coerce*/
3489 long_int, /*nb_int*/
3490 long_long, /*nb_long*/
3491 long_float, /*nb_float*/
3492 long_oct, /*nb_oct*/
3493 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003494 0, /* nb_inplace_add */
3495 0, /* nb_inplace_subtract */
3496 0, /* nb_inplace_multiply */
3497 0, /* nb_inplace_divide */
3498 0, /* nb_inplace_remainder */
3499 0, /* nb_inplace_power */
3500 0, /* nb_inplace_lshift */
3501 0, /* nb_inplace_rshift */
3502 0, /* nb_inplace_and */
3503 0, /* nb_inplace_xor */
3504 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003505 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003506 long_true_divide, /* nb_true_divide */
3507 0, /* nb_inplace_floor_divide */
3508 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003509 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003510};
3511
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003512PyTypeObject PyLong_Type = {
3513 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003514 0, /* ob_size */
3515 "long", /* tp_name */
3516 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3517 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003518 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003519 0, /* tp_print */
3520 0, /* tp_getattr */
3521 0, /* tp_setattr */
3522 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003523 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003524 &long_as_number, /* tp_as_number */
3525 0, /* tp_as_sequence */
3526 0, /* tp_as_mapping */
3527 (hashfunc)long_hash, /* tp_hash */
3528 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003529 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003530 PyObject_GenericGetAttr, /* tp_getattro */
3531 0, /* tp_setattro */
3532 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003533 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00003534 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003535 long_doc, /* tp_doc */
3536 0, /* tp_traverse */
3537 0, /* tp_clear */
3538 0, /* tp_richcompare */
3539 0, /* tp_weaklistoffset */
3540 0, /* tp_iter */
3541 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003542 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003543 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003544 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003545 0, /* tp_base */
3546 0, /* tp_dict */
3547 0, /* tp_descr_get */
3548 0, /* tp_descr_set */
3549 0, /* tp_dictoffset */
3550 0, /* tp_init */
3551 0, /* tp_alloc */
3552 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003553 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003554};