blob: b0170d27063d74c720e5169b020e11ece1da6fed [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"
Mark Dickinsonefc82f72009-03-20 15:51:55 +00009#include "structseq.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010
Mark Dickinson6736cf82009-04-20 21:13:33 +000011#include <float.h>
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000012#include <ctype.h>
Mark Dickinson2ffb26f2009-02-15 10:13:41 +000013#include <stddef.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000014
Tim Peters5af4e6c2002-08-12 02:31:19 +000015/* For long multiplication, use the O(N**2) school algorithm unless
16 * both operands contain more than KARATSUBA_CUTOFF digits (this
Christian Heimes7f39c9f2008-01-25 12:18:43 +000017 * being an internal Python long digit, in base PyLong_BASE).
Tim Peters5af4e6c2002-08-12 02:31:19 +000018 */
Tim Peters0973b992004-08-29 22:16:50 +000019#define KARATSUBA_CUTOFF 70
20#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000021
Tim Peters47e52ee2004-08-30 02:44:38 +000022/* For exponentiation, use the binary left-to-right algorithm
23 * unless the exponent contains more than FIVEARY_CUTOFF digits.
24 * In that case, do 5 bits at a time. The potential drawback is that
25 * a table of 2**5 intermediate results is computed.
26 */
27#define FIVEARY_CUTOFF 8
28
Guido van Rossume32e0141992-01-19 16:31:05 +000029#define ABS(x) ((x) < 0 ? -(x) : (x))
30
Tim Peters5af4e6c2002-08-12 02:31:19 +000031#undef MIN
32#undef MAX
33#define MAX(x, y) ((x) < (y) ? (y) : (x))
34#define MIN(x, y) ((x) > (y) ? (y) : (x))
35
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000037 if (--_Py_Ticker < 0) { \
38 _Py_Ticker = _Py_CheckInterval; \
Tim Petersd89fc222006-05-25 22:28:46 +000039 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000040 }
41
Mark Dickinson6736cf82009-04-20 21:13:33 +000042/* forward declaration */
43static int bits_in_digit(digit d);
44
Guido van Rossumedcc38a1991-05-05 20:09:44 +000045/* Normalize (remove leading zeros from) a long int object.
46 Doesn't attempt to free the storage--in most cases, due to the nature
47 of the algorithms used, this could save at most be one word anyway. */
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000050long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000051{
Christian Heimese93237d2007-12-19 02:37:44 +000052 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +000053 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000054
Guido van Rossumedcc38a1991-05-05 20:09:44 +000055 while (i > 0 && v->ob_digit[i-1] == 0)
56 --i;
57 if (i != j)
Christian Heimese93237d2007-12-19 02:37:44 +000058 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000059 return v;
60}
61
62/* Allocate a new long int object with size digits.
63 Return NULL and set exception if we run out of memory. */
64
Mark Dickinson2ffb26f2009-02-15 10:13:41 +000065#define MAX_LONG_DIGITS \
66 ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
67
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000069_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000070{
Mark Dickinsonbcf6b182009-02-15 15:48:39 +000071 if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
Mark Dickinson2ffb26f2009-02-15 10:13:41 +000072 PyErr_SetString(PyExc_OverflowError,
73 "too many digits in integer");
Martin v. Löwis18e16552006-02-15 17:27:45 +000074 return NULL;
75 }
Christian Heimes4956d2b2008-01-18 19:12:56 +000076 /* coverity[ampersand_in_size] */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +000077 /* XXX(nnorwitz): PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect
78 overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000080}
81
Tim Peters64b5ce32001-09-10 20:52:51 +000082PyObject *
83_PyLong_Copy(PyLongObject *src)
84{
85 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000086 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000087
88 assert(src != NULL);
89 i = src->ob_size;
90 if (i < 0)
91 i = -(i);
92 result = _PyLong_New(i);
93 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000094 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000095 while (--i >= 0)
96 result->ob_digit[i] = src->ob_digit[i];
97 }
98 return (PyObject *)result;
99}
100
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000101/* Create a new long int object from a C long int */
102
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000104PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000105{
Tim Petersce9de2f2001-06-14 04:56:19 +0000106 PyLongObject *v;
Mark Dickinson27a63252008-04-15 20:51:18 +0000107 unsigned long abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000108 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
109 int ndigits = 0;
110 int negative = 0;
111
112 if (ival < 0) {
Mark Dickinson27a63252008-04-15 20:51:18 +0000113 /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
114 ANSI C says that the result of -ival is undefined when ival
115 == LONG_MIN. Hence the following workaround. */
116 abs_ival = (unsigned long)(-1-ival) + 1;
Tim Petersce9de2f2001-06-14 04:56:19 +0000117 negative = 1;
118 }
Mark Dickinson27a63252008-04-15 20:51:18 +0000119 else {
120 abs_ival = (unsigned long)ival;
121 }
Tim Petersce9de2f2001-06-14 04:56:19 +0000122
123 /* Count the number of Python digits.
124 We used to pick 5 ("big enough for anything"), but that's a
125 waste of time and space given that 5*15 = 75 bits are rarely
126 needed. */
Mark Dickinson27a63252008-04-15 20:51:18 +0000127 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000128 while (t) {
129 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000130 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000131 }
132 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000133 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000134 digit *p = v->ob_digit;
135 v->ob_size = negative ? -ndigits : ndigits;
Mark Dickinson27a63252008-04-15 20:51:18 +0000136 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000137 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000138 *p++ = (digit)(t & PyLong_MASK);
139 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000140 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000141 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000143}
144
Guido van Rossum53756b11997-01-03 17:14:46 +0000145/* Create a new long int object from a C unsigned long int */
146
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000148PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000149{
Tim Petersce9de2f2001-06-14 04:56:19 +0000150 PyLongObject *v;
151 unsigned long t;
152 int ndigits = 0;
153
154 /* Count the number of Python digits. */
155 t = (unsigned long)ival;
156 while (t) {
157 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000158 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000159 }
160 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000161 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000162 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +0000163 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000164 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000165 *p++ = (digit)(ival & PyLong_MASK);
166 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000167 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000168 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000170}
171
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000172/* Create a new long int object from a C double */
173
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000176{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000178 double frac;
179 int i, ndig, expo, neg;
180 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000181 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000182 PyErr_SetString(PyExc_OverflowError,
Mark Dickinsonb6467572008-08-04 21:30:09 +0000183 "cannot convert float infinity to integer");
Guido van Rossum1a23c241999-09-27 17:11:52 +0000184 return NULL;
185 }
Christian Heimes8267d1d2008-01-04 00:37:34 +0000186 if (Py_IS_NAN(dval)) {
Mark Dickinsonb6467572008-08-04 21:30:09 +0000187 PyErr_SetString(PyExc_ValueError,
188 "cannot convert float NaN to integer");
189 return NULL;
Christian Heimes8267d1d2008-01-04 00:37:34 +0000190 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000191 if (dval < 0.0) {
192 neg = 1;
193 dval = -dval;
194 }
195 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
196 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 return PyLong_FromLong(0L);
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000198 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000200 if (v == NULL)
201 return NULL;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000202 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000203 for (i = ndig; --i >= 0; ) {
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000204 digit bits = (digit)frac;
205 v->ob_digit[i] = bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000206 frac = frac - (double)bits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000207 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000208 }
209 if (neg)
Christian Heimese93237d2007-12-19 02:37:44 +0000210 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000212}
213
Armin Rigo7ccbca92006-10-04 12:17:45 +0000214/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
215 * anything about what happens when a signed integer operation overflows,
216 * and some compilers think they're doing you a favor by being "clever"
217 * then. The bit pattern for the largest postive signed long is
218 * (unsigned long)LONG_MAX, and for the smallest negative signed long
219 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
220 * However, some other compilers warn about applying unary minus to an
221 * unsigned operand. Hence the weird "0-".
222 */
223#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
224#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
225
Mark Dickinsone31d3002009-12-21 11:21:25 +0000226/* Get a C long int from a Python long or Python int object.
227 On overflow, returns -1 and sets *overflow to 1 or -1 depending
228 on the sign of the result. Otherwise *overflow is 0.
229
230 For other errors (e.g., type error), returns -1 and sets an error
231 condition.
232*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000233
234long
Mark Dickinsone31d3002009-12-21 11:21:25 +0000235PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000236{
Guido van Rossumf7531811998-05-26 14:33:37 +0000237 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000239 unsigned long x, prev;
Mark Dickinsone31d3002009-12-21 11:21:25 +0000240 long res;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000241 Py_ssize_t i;
242 int sign;
Mark Dickinsone31d3002009-12-21 11:21:25 +0000243 int do_decref = 0; /* if nb_int was called */
Guido van Rossumf7531811998-05-26 14:33:37 +0000244
Mark Dickinsone31d3002009-12-21 11:21:25 +0000245 *overflow = 0;
246 if (vv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000248 return -1;
249 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000250
Mark Dickinsone31d3002009-12-21 11:21:25 +0000251 if(PyInt_Check(vv))
252 return PyInt_AsLong(vv);
253
254 if (!PyLong_Check(vv)) {
255 PyNumberMethods *nb;
256 nb = vv->ob_type->tp_as_number;
257 if (nb == NULL || nb->nb_int == NULL) {
258 PyErr_SetString(PyExc_TypeError,
259 "an integer is required");
260 return -1;
261 }
262 vv = (*nb->nb_int) (vv);
263 if (vv == NULL)
264 return -1;
265 do_decref = 1;
266 if(PyInt_Check(vv)) {
267 res = PyInt_AsLong(vv);
268 goto exit;
269 }
270 if (!PyLong_Check(vv)) {
271 Py_DECREF(vv);
272 PyErr_SetString(PyExc_TypeError,
273 "nb_int should return int object");
274 return -1;
275 }
276 }
277
278 res = -1;
279 v = (PyLongObject *)vv;
280 i = Py_SIZE(v);
281
282 switch (i) {
283 case -1:
284 res = -(sdigit)v->ob_digit[0];
285 break;
286 case 0:
287 res = 0;
288 break;
289 case 1:
290 res = v->ob_digit[0];
291 break;
292 default:
293 sign = 1;
294 x = 0;
295 if (i < 0) {
296 sign = -1;
297 i = -(i);
298 }
299 while (--i >= 0) {
300 prev = x;
301 x = (x << PyLong_SHIFT) + v->ob_digit[i];
302 if ((x >> PyLong_SHIFT) != prev) {
303 *overflow = sign;
304 goto exit;
305 }
306 }
307 /* Haven't lost any bits, but casting to long requires extra
308 * care (see comment above).
309 */
310 if (x <= (unsigned long)LONG_MAX) {
311 res = (long)x * sign;
312 }
313 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
314 res = LONG_MIN;
315 }
316 else {
317 *overflow = sign;
318 /* res is already set to -1 */
319 }
320 }
321 exit:
322 if (do_decref) {
323 Py_DECREF(vv);
324 }
325 return res;
326}
327
328/* Get a C long int from a long int object.
329 Returns -1 and sets an error condition if overflow occurs. */
330
331long
332PyLong_AsLong(PyObject *obj)
333{
334 int overflow;
335 long result = PyLong_AsLongAndOverflow(obj, &overflow);
336 if (overflow) {
337 /* XXX: could be cute and give a different
338 message for overflow == -1 */
339 PyErr_SetString(PyExc_OverflowError,
340 "Python int too large to convert to C long");
341 }
342 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000343}
344
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000345/* Get a Py_ssize_t from a long int object.
346 Returns -1 and sets an error condition if overflow occurs. */
347
348Py_ssize_t
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000349PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000350 register PyLongObject *v;
351 size_t x, prev;
352 Py_ssize_t i;
353 int sign;
354
355 if (vv == NULL || !PyLong_Check(vv)) {
356 PyErr_BadInternalCall();
357 return -1;
358 }
359 v = (PyLongObject *)vv;
360 i = v->ob_size;
361 sign = 1;
362 x = 0;
363 if (i < 0) {
364 sign = -1;
365 i = -(i);
366 }
367 while (--i >= 0) {
368 prev = x;
Mark Dickinson71adc932009-09-28 16:52:40 +0000369 x = (x << PyLong_SHIFT) | v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000370 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 goto overflow;
372 }
Armin Rigo7ccbca92006-10-04 12:17:45 +0000373 /* Haven't lost any bits, but casting to a signed type requires
374 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000375 */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000376 if (x <= (size_t)PY_SSIZE_T_MAX) {
377 return (Py_ssize_t)x * sign;
378 }
379 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
380 return PY_SSIZE_T_MIN;
381 }
382 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383
384 overflow:
385 PyErr_SetString(PyExc_OverflowError,
386 "long int too large to convert to int");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000387 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000388}
389
Guido van Rossumd8c80482002-08-13 00:24:58 +0000390/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000391 Returns -1 and sets an error condition if overflow occurs. */
392
393unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000394PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000395{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000397 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000398 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000401 if (vv != NULL && PyInt_Check(vv)) {
402 long val = PyInt_AsLong(vv);
403 if (val < 0) {
404 PyErr_SetString(PyExc_OverflowError,
405 "can't convert negative value to unsigned long");
406 return (unsigned long) -1;
407 }
408 return val;
409 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000411 return (unsigned long) -1;
412 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000414 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000415 x = 0;
416 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000418 "can't convert negative value to unsigned long");
419 return (unsigned long) -1;
420 }
421 while (--i >= 0) {
422 prev = x;
Mark Dickinson71adc932009-09-28 16:52:40 +0000423 x = (x << PyLong_SHIFT) | v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000424 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000426 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000427 return (unsigned long) -1;
428 }
429 }
430 return x;
431}
432
Thomas Hellera4ea6032003-04-17 18:55:45 +0000433/* Get a C unsigned long int from a long int object, ignoring the high bits.
434 Returns -1 and sets an error condition if an error occurs. */
435
436unsigned long
437PyLong_AsUnsignedLongMask(PyObject *vv)
438{
439 register PyLongObject *v;
440 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 Py_ssize_t i;
442 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000443
444 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000445 if (vv != NULL && PyInt_Check(vv))
446 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000447 PyErr_BadInternalCall();
448 return (unsigned long) -1;
449 }
450 v = (PyLongObject *)vv;
451 i = v->ob_size;
452 sign = 1;
453 x = 0;
454 if (i < 0) {
455 sign = -1;
456 i = -i;
457 }
458 while (--i >= 0) {
Mark Dickinson71adc932009-09-28 16:52:40 +0000459 x = (x << PyLong_SHIFT) | v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000460 }
461 return x * sign;
462}
463
Tim Peters5b8132f2003-01-31 15:52:05 +0000464int
465_PyLong_Sign(PyObject *vv)
466{
467 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000468
469 assert(v != NULL);
470 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000471
Christian Heimese93237d2007-12-19 02:37:44 +0000472 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000473}
474
Tim Petersbaefd9e2003-01-28 20:37:45 +0000475size_t
476_PyLong_NumBits(PyObject *vv)
477{
478 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000479 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000481
482 assert(v != NULL);
483 assert(PyLong_Check(v));
Christian Heimese93237d2007-12-19 02:37:44 +0000484 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000485 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
486 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000487 digit msd = v->ob_digit[ndigits - 1];
488
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000489 result = (ndigits - 1) * PyLong_SHIFT;
490 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000491 goto Overflow;
492 do {
493 ++result;
494 if (result == 0)
495 goto Overflow;
496 msd >>= 1;
497 } while (msd);
498 }
499 return result;
500
501Overflow:
502 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
503 "to express in a platform size_t");
504 return (size_t)-1;
505}
506
Tim Peters2a9b3672001-06-11 21:23:58 +0000507PyObject *
508_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
509 int little_endian, int is_signed)
510{
511 const unsigned char* pstartbyte;/* LSB of bytes */
512 int incr; /* direction to move pstartbyte */
513 const unsigned char* pendbyte; /* MSB of bytes */
514 size_t numsignificantbytes; /* number of bytes that matter */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000515 Py_ssize_t ndigits; /* number of Python long digits */
Tim Peters2a9b3672001-06-11 21:23:58 +0000516 PyLongObject* v; /* result */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000517 Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
Tim Peters2a9b3672001-06-11 21:23:58 +0000518
519 if (n == 0)
520 return PyLong_FromLong(0L);
521
522 if (little_endian) {
523 pstartbyte = bytes;
524 pendbyte = bytes + n - 1;
525 incr = 1;
526 }
527 else {
528 pstartbyte = bytes + n - 1;
529 pendbyte = bytes;
530 incr = -1;
531 }
532
533 if (is_signed)
534 is_signed = *pendbyte >= 0x80;
535
536 /* Compute numsignificantbytes. This consists of finding the most
537 significant byte. Leading 0 bytes are insignficant if the number
538 is positive, and leading 0xff bytes if negative. */
539 {
540 size_t i;
541 const unsigned char* p = pendbyte;
542 const int pincr = -incr; /* search MSB to LSB */
543 const unsigned char insignficant = is_signed ? 0xff : 0x00;
544
545 for (i = 0; i < n; ++i, p += pincr) {
546 if (*p != insignficant)
547 break;
548 }
549 numsignificantbytes = n - i;
550 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
551 actually has 2 significant bytes. OTOH, 0xff0001 ==
552 -0x00ffff, so we wouldn't *need* to bump it there; but we
553 do for 0xffff = -0x0001. To be safe without bothering to
554 check every case, bump it regardless. */
555 if (is_signed && numsignificantbytes < n)
556 ++numsignificantbytes;
557 }
558
559 /* How many Python long digits do we need? We have
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000560 8*numsignificantbytes bits, and each Python long digit has
561 PyLong_SHIFT bits, so it's the ceiling of the quotient. */
562 /* catch overflow before it happens */
563 if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
564 PyErr_SetString(PyExc_OverflowError,
565 "byte array too long to convert to int");
566 return NULL;
567 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000568 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000569 v = _PyLong_New(ndigits);
Tim Peters2a9b3672001-06-11 21:23:58 +0000570 if (v == NULL)
571 return NULL;
572
573 /* Copy the bits over. The tricky parts are computing 2's-comp on
574 the fly for signed numbers, and dealing with the mismatch between
575 8-bit bytes and (probably) 15-bit Python digits.*/
576 {
577 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000578 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000579 twodigits accum = 0; /* sliding register */
580 unsigned int accumbits = 0; /* number of bits in accum */
581 const unsigned char* p = pstartbyte;
582
583 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000584 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000585 /* Compute correction for 2's comp, if needed. */
586 if (is_signed) {
587 thisbyte = (0xff ^ thisbyte) + carry;
588 carry = thisbyte >> 8;
589 thisbyte &= 0xff;
590 }
591 /* Because we're going LSB to MSB, thisbyte is
592 more significant than what's already in accum,
593 so needs to be prepended to accum. */
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000594 accum |= (twodigits)thisbyte << accumbits;
Tim Peters2a9b3672001-06-11 21:23:58 +0000595 accumbits += 8;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000596 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000597 /* There's enough to fill a Python digit. */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000598 assert(idigit < ndigits);
599 v->ob_digit[idigit] = (digit)(accum &
600 PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000601 ++idigit;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000602 accum >>= PyLong_SHIFT;
603 accumbits -= PyLong_SHIFT;
604 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000605 }
606 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000607 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000608 if (accumbits) {
Mark Dickinson2ffb26f2009-02-15 10:13:41 +0000609 assert(idigit < ndigits);
Tim Peters2a9b3672001-06-11 21:23:58 +0000610 v->ob_digit[idigit] = (digit)accum;
611 ++idigit;
612 }
613 }
614
Christian Heimese93237d2007-12-19 02:37:44 +0000615 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000616 return (PyObject *)long_normalize(v);
617}
618
619int
620_PyLong_AsByteArray(PyLongObject* v,
621 unsigned char* bytes, size_t n,
622 int little_endian, int is_signed)
623{
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000624 Py_ssize_t i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000626 twodigits accum; /* sliding register */
627 unsigned int accumbits; /* # bits in accum */
628 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
Mark Dickinson1afe6dd2009-01-25 22:12:31 +0000629 digit carry; /* for computing 2's-comp */
Tim Peters2a9b3672001-06-11 21:23:58 +0000630 size_t j; /* # bytes filled */
631 unsigned char* p; /* pointer to next byte in bytes */
632 int pincr; /* direction to move p */
633
634 assert(v != NULL && PyLong_Check(v));
635
Christian Heimese93237d2007-12-19 02:37:44 +0000636 if (Py_SIZE(v) < 0) {
637 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000638 if (!is_signed) {
Mark Dickinson4015f622009-02-10 15:46:50 +0000639 PyErr_SetString(PyExc_OverflowError,
Tim Peters2a9b3672001-06-11 21:23:58 +0000640 "can't convert negative long to unsigned");
641 return -1;
642 }
643 do_twos_comp = 1;
644 }
645 else {
Christian Heimese93237d2007-12-19 02:37:44 +0000646 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000647 do_twos_comp = 0;
648 }
649
650 if (little_endian) {
651 p = bytes;
652 pincr = 1;
653 }
654 else {
655 p = bytes + n - 1;
656 pincr = -1;
657 }
658
Tim Peters898cf852001-06-13 20:50:08 +0000659 /* Copy over all the Python digits.
660 It's crucial that every Python digit except for the MSD contribute
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000661 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000662 normalized. */
663 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000664 j = 0;
665 accum = 0;
666 accumbits = 0;
667 carry = do_twos_comp ? 1 : 0;
668 for (i = 0; i < ndigits; ++i) {
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000669 digit thisdigit = v->ob_digit[i];
Tim Peters2a9b3672001-06-11 21:23:58 +0000670 if (do_twos_comp) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000671 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
672 carry = thisdigit >> PyLong_SHIFT;
673 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000674 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000675 /* Because we're going LSB to MSB, thisdigit is more
676 significant than what's already in accum, so needs to be
677 prepended to accum. */
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000678 accum |= (twodigits)thisdigit << accumbits;
Tim Peters8bc84b42001-06-12 19:17:03 +0000679
Tim Petersede05092001-06-14 08:53:38 +0000680 /* The most-significant digit may be (probably is) at least
681 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000682 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000683 /* Count # of sign bits -- they needn't be stored,
684 * although for signed conversion we need later to
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000685 * make sure at least one sign bit gets stored. */
686 digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
687 thisdigit;
688 while (s != 0) {
689 s >>= 1;
690 accumbits++;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000691 }
Tim Peters7a3bfc32001-06-12 01:22:22 +0000692 }
Mark Dickinsonff84aa82009-01-24 15:27:44 +0000693 else
694 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000695
Tim Peters2a9b3672001-06-11 21:23:58 +0000696 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000697 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000698 if (j >= n)
699 goto Overflow;
700 ++j;
701 *p = (unsigned char)(accum & 0xff);
702 p += pincr;
703 accumbits -= 8;
704 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000705 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000706 }
707
708 /* Store the straggler (if any). */
709 assert(accumbits < 8);
710 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000711 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000712 if (j >= n)
713 goto Overflow;
714 ++j;
715 if (do_twos_comp) {
716 /* Fill leading bits of the byte with sign bits
717 (appropriately pretending that the long had an
718 infinite supply of sign bits). */
719 accum |= (~(twodigits)0) << accumbits;
720 }
721 *p = (unsigned char)(accum & 0xff);
722 p += pincr;
723 }
Tim Peters05607ad2001-06-13 21:01:27 +0000724 else if (j == n && n > 0 && is_signed) {
725 /* The main loop filled the byte array exactly, so the code
726 just above didn't get to ensure there's a sign bit, and the
727 loop below wouldn't add one either. Make sure a sign bit
728 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000729 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000730 int sign_bit_set = msb >= 0x80;
731 assert(accumbits == 0);
732 if (sign_bit_set == do_twos_comp)
733 return 0;
734 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000735 goto Overflow;
736 }
Tim Peters05607ad2001-06-13 21:01:27 +0000737
738 /* Fill remaining bytes with copies of the sign bit. */
739 {
740 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
741 for ( ; j < n; ++j, p += pincr)
742 *p = signbyte;
743 }
744
Tim Peters2a9b3672001-06-11 21:23:58 +0000745 return 0;
746
747Overflow:
748 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
749 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000750
Tim Peters2a9b3672001-06-11 21:23:58 +0000751}
752
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000753double
754_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
755{
756/* NBITS_WANTED should be > the number of bits in a double's precision,
757 but small enough so that 2**NBITS_WANTED is within the normal double
758 range. nbitsneeded is set to 1 less than that because the most-significant
759 Python digit contains at least 1 significant bit, but we don't want to
760 bother counting them (catering to the worst case cheaply).
761
762 57 is one more than VAX-D double precision; I (Tim) don't know of a double
763 format with more precision than that; it's 1 larger so that we add in at
764 least one round bit to stand in for the ignored least-significant bits.
765*/
766#define NBITS_WANTED 57
767 PyLongObject *v;
768 double x;
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000769 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000770 Py_ssize_t i;
771 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000772 int nbitsneeded;
773
774 if (vv == NULL || !PyLong_Check(vv)) {
775 PyErr_BadInternalCall();
776 return -1;
777 }
778 v = (PyLongObject *)vv;
Christian Heimese93237d2007-12-19 02:37:44 +0000779 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000780 sign = 1;
781 if (i < 0) {
782 sign = -1;
783 i = -(i);
784 }
785 else if (i == 0) {
786 *exponent = 0;
787 return 0.0;
788 }
789 --i;
790 x = (double)v->ob_digit[i];
791 nbitsneeded = NBITS_WANTED - 1;
792 /* Invariant: i Python digits remain unaccounted for. */
793 while (i > 0 && nbitsneeded > 0) {
794 --i;
795 x = x * multiplier + (double)v->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000796 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000797 }
798 /* There are i digits we didn't shift in. Pretending they're all
Christian Heimes7f39c9f2008-01-25 12:18:43 +0000799 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000800 *exponent = i;
801 assert(x > 0.0);
802 return x * sign;
803#undef NBITS_WANTED
804}
805
Mark Dickinson6736cf82009-04-20 21:13:33 +0000806/* Get a C double from a long int object. Rounds to the nearest double,
807 using the round-half-to-even rule in the case of a tie. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000808
809double
Tim Peters9f688bf2000-07-07 15:53:28 +0000810PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000811{
Mark Dickinson6736cf82009-04-20 21:13:33 +0000812 PyLongObject *v = (PyLongObject *)vv;
813 Py_ssize_t rnd_digit, rnd_bit, m, n;
814 digit lsb, *d;
815 int round_up = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000816 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 if (vv == NULL || !PyLong_Check(vv)) {
819 PyErr_BadInternalCall();
Tim Peters9fffa3e2001-09-04 05:14:19 +0000820 return -1.0;
Mark Dickinson6736cf82009-04-20 21:13:33 +0000821 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000822
Mark Dickinson6736cf82009-04-20 21:13:33 +0000823 /* Notes on the method: for simplicity, assume v is positive and >=
824 2**DBL_MANT_DIG. (For negative v we just ignore the sign until the
825 end; for small v no rounding is necessary.) Write n for the number
826 of bits in v, so that 2**(n-1) <= v < 2**n, and n > DBL_MANT_DIG.
827
828 Some terminology: the *rounding bit* of v is the 1st bit of v that
829 will be rounded away (bit n - DBL_MANT_DIG - 1); the *parity bit*
830 is the bit immediately above. The round-half-to-even rule says
831 that we round up if the rounding bit is set, unless v is exactly
832 halfway between two floats and the parity bit is zero.
833
834 Write d[0] ... d[m] for the digits of v, least to most significant.
835 Let rnd_bit be the index of the rounding bit, and rnd_digit the
836 index of the PyLong digit containing the rounding bit. Then the
837 bits of the digit d[rnd_digit] look something like:
838
839 rounding bit
840 |
841 v
842 msb -> sssssrttttttttt <- lsb
843 ^
844 |
845 parity bit
846
847 where 's' represents a 'significant bit' that will be included in
848 the mantissa of the result, 'r' is the rounding bit, and 't'
849 represents a 'trailing bit' following the rounding bit. Note that
850 if the rounding bit is at the top of d[rnd_digit] then the parity
851 bit will be the lsb of d[rnd_digit+1]. If we set
852
853 lsb = 1 << (rnd_bit % PyLong_SHIFT)
854
855 then d[rnd_digit] & (PyLong_BASE - 2*lsb) selects just the
856 significant bits of d[rnd_digit], d[rnd_digit] & (lsb-1) gets the
857 trailing bits, and d[rnd_digit] & lsb gives the rounding bit.
858
859 We initialize the double x to the integer given by digits
860 d[rnd_digit:m-1], but with the rounding bit and trailing bits of
861 d[rnd_digit] masked out. So the value of x comes from the top
862 DBL_MANT_DIG bits of v, multiplied by 2*lsb. Note that in the loop
863 that produces x, all floating-point operations are exact (assuming
864 that FLT_RADIX==2). Now if we're rounding down, the value we want
865 to return is simply
866
867 x * 2**(PyLong_SHIFT * rnd_digit).
868
869 and if we're rounding up, it's
870
871 (x + 2*lsb) * 2**(PyLong_SHIFT * rnd_digit).
872
873 Under the round-half-to-even rule, we round up if, and only
874 if, the rounding bit is set *and* at least one of the
875 following three conditions is satisfied:
876
877 (1) the parity bit is set, or
878 (2) at least one of the trailing bits of d[rnd_digit] is set, or
879 (3) at least one of the digits d[i], 0 <= i < rnd_digit
880 is nonzero.
881
882 Finally, we have to worry about overflow. If v >= 2**DBL_MAX_EXP,
883 or equivalently n > DBL_MAX_EXP, then overflow occurs. If v <
884 2**DBL_MAX_EXP then we're usually safe, but there's a corner case
885 to consider: if v is very close to 2**DBL_MAX_EXP then it's
886 possible that v is rounded up to exactly 2**DBL_MAX_EXP, and then
887 again overflow occurs.
888 */
889
890 if (Py_SIZE(v) == 0)
891 return 0.0;
892 m = ABS(Py_SIZE(v)) - 1;
893 d = v->ob_digit;
894 assert(d[m]); /* v should be normalized */
895
896 /* fast path for case where 0 < abs(v) < 2**DBL_MANT_DIG */
897 if (m < DBL_MANT_DIG / PyLong_SHIFT ||
898 (m == DBL_MANT_DIG / PyLong_SHIFT &&
899 d[m] < (digit)1 << DBL_MANT_DIG%PyLong_SHIFT)) {
900 x = d[m];
901 while (--m >= 0)
902 x = x*PyLong_BASE + d[m];
903 return Py_SIZE(v) < 0 ? -x : x;
904 }
905
906 /* if m is huge then overflow immediately; otherwise, compute the
907 number of bits n in v. The condition below implies n (= #bits) >=
908 m * PyLong_SHIFT + 1 > DBL_MAX_EXP, hence v >= 2**DBL_MAX_EXP. */
909 if (m > (DBL_MAX_EXP-1)/PyLong_SHIFT)
910 goto overflow;
911 n = m * PyLong_SHIFT + bits_in_digit(d[m]);
912 if (n > DBL_MAX_EXP)
913 goto overflow;
914
915 /* find location of rounding bit */
916 assert(n > DBL_MANT_DIG); /* dealt with |v| < 2**DBL_MANT_DIG above */
917 rnd_bit = n - DBL_MANT_DIG - 1;
918 rnd_digit = rnd_bit/PyLong_SHIFT;
919 lsb = (digit)1 << (rnd_bit%PyLong_SHIFT);
920
921 /* Get top DBL_MANT_DIG bits of v. Assumes PyLong_SHIFT <
922 DBL_MANT_DIG, so we'll need bits from at least 2 digits of v. */
923 x = d[m];
924 assert(m > rnd_digit);
925 while (--m > rnd_digit)
926 x = x*PyLong_BASE + d[m];
927 x = x*PyLong_BASE + (d[m] & (PyLong_BASE-2*lsb));
928
929 /* decide whether to round up, using round-half-to-even */
930 assert(m == rnd_digit);
931 if (d[m] & lsb) { /* if (rounding bit is set) */
932 digit parity_bit;
933 if (lsb == PyLong_BASE/2)
934 parity_bit = d[m+1] & 1;
935 else
936 parity_bit = d[m] & 2*lsb;
937 if (parity_bit)
938 round_up = 1;
939 else if (d[m] & (lsb-1))
940 round_up = 1;
941 else {
942 while (--m >= 0) {
943 if (d[m]) {
944 round_up = 1;
945 break;
946 }
947 }
948 }
949 }
950
951 /* and round up if necessary */
952 if (round_up) {
953 x += 2*lsb;
954 if (n == DBL_MAX_EXP &&
955 x == ldexp((double)(2*lsb), DBL_MANT_DIG)) {
956 /* overflow corner case */
957 goto overflow;
958 }
959 }
960
961 /* shift, adjust for sign, and return */
962 x = ldexp(x, rnd_digit*PyLong_SHIFT);
963 return Py_SIZE(v) < 0 ? -x : x;
964
965 overflow:
Tim Peters9fffa3e2001-09-04 05:14:19 +0000966 PyErr_SetString(PyExc_OverflowError,
967 "long int too large to convert to float");
968 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000969}
970
Guido van Rossum78694d91998-09-18 14:14:13 +0000971/* Create a new long (or int) object from a C pointer */
972
973PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000974PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000975{
Tim Peters70128a12001-06-16 08:48:40 +0000976#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000977 if ((long)p < 0)
978 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000979 return PyInt_FromLong((long)p);
980#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000981
Tim Peters70128a12001-06-16 08:48:40 +0000982#ifndef HAVE_LONG_LONG
983# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
984#endif
985#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000986# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000987#endif
988 /* optimize null pointers */
989 if (p == NULL)
990 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000991 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000992
993#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000994}
995
996/* Get a C pointer from a long object (or an int object in some cases) */
997
998void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000999PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +00001000{
1001 /* This function will allow int or long objects. If vv is neither,
1002 then the PyLong_AsLong*() functions will raise the exception:
1003 PyExc_SystemError, "bad argument to internal function"
1004 */
Tim Peters70128a12001-06-16 08:48:40 +00001005#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +00001006 long x;
1007
Tim Peters70128a12001-06-16 08:48:40 +00001008 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +00001009 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +00001010 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001011 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +00001012 else
1013 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +00001014#else
Tim Peters70128a12001-06-16 08:48:40 +00001015
1016#ifndef HAVE_LONG_LONG
1017# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
1018#endif
1019#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001020# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +00001021#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001022 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001023
Tim Peters70128a12001-06-16 08:48:40 +00001024 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +00001025 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +00001026 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001027 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +00001028 else
1029 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +00001030
1031#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +00001032
1033 if (x == -1 && PyErr_Occurred())
1034 return NULL;
1035 return (void *)x;
1036}
1037
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001038#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +00001039
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001040/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +00001041 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001042 */
1043
Tim Peterscf37dfc2001-06-14 18:42:50 +00001044#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +00001045
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001046/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001047
1048PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001049PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001050{
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001051 PyLongObject *v;
Mark Dickinson27a63252008-04-15 20:51:18 +00001052 unsigned PY_LONG_LONG abs_ival;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001053 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
1054 int ndigits = 0;
1055 int negative = 0;
1056
1057 if (ival < 0) {
Mark Dickinson27a63252008-04-15 20:51:18 +00001058 /* avoid signed overflow on negation; see comments
1059 in PyLong_FromLong above. */
1060 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001061 negative = 1;
1062 }
Mark Dickinson27a63252008-04-15 20:51:18 +00001063 else {
1064 abs_ival = (unsigned PY_LONG_LONG)ival;
1065 }
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001066
1067 /* Count the number of Python digits.
1068 We used to pick 5 ("big enough for anything"), but that's a
1069 waste of time and space given that 5*15 = 75 bits are rarely
1070 needed. */
Mark Dickinson27a63252008-04-15 20:51:18 +00001071 t = abs_ival;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001072 while (t) {
1073 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001074 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001075 }
1076 v = _PyLong_New(ndigits);
1077 if (v != NULL) {
1078 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001079 Py_SIZE(v) = negative ? -ndigits : ndigits;
Mark Dickinson27a63252008-04-15 20:51:18 +00001080 t = abs_ival;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001081 while (t) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001082 *p++ = (digit)(t & PyLong_MASK);
1083 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001084 }
1085 }
1086 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001087}
1088
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001089/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +00001090
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001091PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001092PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001093{
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001094 PyLongObject *v;
1095 unsigned PY_LONG_LONG t;
1096 int ndigits = 0;
1097
1098 /* Count the number of Python digits. */
1099 t = (unsigned PY_LONG_LONG)ival;
1100 while (t) {
1101 ++ndigits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001102 t >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001103 }
1104 v = _PyLong_New(ndigits);
1105 if (v != NULL) {
1106 digit *p = v->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00001107 Py_SIZE(v) = ndigits;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001108 while (ival) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001109 *p++ = (digit)(ival & PyLong_MASK);
1110 ival >>= PyLong_SHIFT;
Bob Ippolitoa85bf202006-05-25 18:20:23 +00001111 }
1112 }
1113 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001114}
1115
Martin v. Löwis18e16552006-02-15 17:27:45 +00001116/* Create a new long int object from a C Py_ssize_t. */
1117
1118PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001119PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001120{
1121 Py_ssize_t bytes = ival;
1122 int one = 1;
1123 return _PyLong_FromByteArray(
1124 (unsigned char *)&bytes,
Kristján Valur Jónssonf0303942007-05-03 20:27:03 +00001125 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001126}
1127
1128/* Create a new long int object from a C size_t. */
1129
1130PyObject *
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001131PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001132{
1133 size_t bytes = ival;
1134 int one = 1;
1135 return _PyLong_FromByteArray(
1136 (unsigned char *)&bytes,
1137 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
1138}
1139
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001140/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001141 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001142
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001143PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001144PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001145{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001146 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001147 int one = 1;
1148 int res;
1149
Tim Petersd38b1c72001-09-30 05:09:37 +00001150 if (vv == NULL) {
1151 PyErr_BadInternalCall();
1152 return -1;
1153 }
1154 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001155 PyNumberMethods *nb;
1156 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +00001157 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001158 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001159 if ((nb = vv->ob_type->tp_as_number) == NULL ||
1160 nb->nb_int == NULL) {
1161 PyErr_SetString(PyExc_TypeError, "an integer is required");
1162 return -1;
1163 }
1164 io = (*nb->nb_int) (vv);
1165 if (io == NULL)
1166 return -1;
1167 if (PyInt_Check(io)) {
1168 bytes = PyInt_AsLong(io);
1169 Py_DECREF(io);
1170 return bytes;
1171 }
1172 if (PyLong_Check(io)) {
1173 bytes = PyLong_AsLongLong(io);
1174 Py_DECREF(io);
1175 return bytes;
1176 }
1177 Py_DECREF(io);
1178 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001179 return -1;
1180 }
1181
Tim Petersd1a7da62001-06-13 00:35:57 +00001182 res = _PyLong_AsByteArray(
1183 (PyLongObject *)vv, (unsigned char *)&bytes,
1184 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001185
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001186 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001187 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001188 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001189 else
1190 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001191}
1192
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001193/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001194 Return -1 and set an error if overflow occurs. */
1195
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001196unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001197PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001198{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001199 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001200 int one = 1;
1201 int res;
1202
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001203 if (vv == NULL || !PyLong_Check(vv)) {
1204 PyErr_BadInternalCall();
Skip Montanaro429433b2006-04-18 00:35:43 +00001205 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001206 }
1207
Tim Petersd1a7da62001-06-13 00:35:57 +00001208 res = _PyLong_AsByteArray(
1209 (PyLongObject *)vv, (unsigned char *)&bytes,
1210 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001211
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001212 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001213 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001214 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001215 else
1216 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001217}
Tim Petersd1a7da62001-06-13 00:35:57 +00001218
Thomas Hellera4ea6032003-04-17 18:55:45 +00001219/* Get a C unsigned long int from a long int object, ignoring the high bits.
1220 Returns -1 and sets an error condition if an error occurs. */
1221
1222unsigned PY_LONG_LONG
1223PyLong_AsUnsignedLongLongMask(PyObject *vv)
1224{
1225 register PyLongObject *v;
1226 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001227 Py_ssize_t i;
1228 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001229
1230 if (vv == NULL || !PyLong_Check(vv)) {
1231 PyErr_BadInternalCall();
1232 return (unsigned long) -1;
1233 }
1234 v = (PyLongObject *)vv;
1235 i = v->ob_size;
1236 sign = 1;
1237 x = 0;
1238 if (i < 0) {
1239 sign = -1;
1240 i = -i;
1241 }
1242 while (--i >= 0) {
Mark Dickinson71adc932009-09-28 16:52:40 +00001243 x = (x << PyLong_SHIFT) | v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001244 }
1245 return x * sign;
1246}
Tim Petersd1a7da62001-06-13 00:35:57 +00001247#undef IS_LITTLE_ENDIAN
1248
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001249#endif /* HAVE_LONG_LONG */
1250
Neil Schemenauerba872e22001-01-04 01:46:03 +00001251
1252static int
1253convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001254 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001255 *a = (PyLongObject *) v;
1256 Py_INCREF(v);
1257 }
1258 else if (PyInt_Check(v)) {
1259 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1260 }
1261 else {
1262 return 0;
1263 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001264 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001265 *b = (PyLongObject *) w;
1266 Py_INCREF(w);
1267 }
1268 else if (PyInt_Check(w)) {
1269 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1270 }
1271 else {
1272 Py_DECREF(*a);
1273 return 0;
1274 }
1275 return 1;
1276}
1277
1278#define CONVERT_BINOP(v, w, a, b) \
1279 if (!convert_binop(v, w, a, b)) { \
1280 Py_INCREF(Py_NotImplemented); \
1281 return Py_NotImplemented; \
1282 }
1283
Mark Dickinson0b666bf2009-03-23 18:25:13 +00001284/* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
1285 2**k if d is nonzero, else 0. */
1286
1287static const unsigned char BitLengthTable[32] = {
1288 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
1289 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
1290};
1291
1292static int
1293bits_in_digit(digit d)
1294{
1295 int d_bits = 0;
1296 while (d >= 32) {
1297 d_bits += 6;
1298 d >>= 6;
1299 }
1300 d_bits += (int)BitLengthTable[d];
1301 return d_bits;
1302}
1303
Tim Peters877a2122002-08-12 05:09:36 +00001304/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1305 * is modified in place, by adding y to it. Carries are propagated as far as
1306 * x[m-1], and the remaining carry (0 or 1) is returned.
1307 */
1308static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001309v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001310{
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001311 Py_ssize_t i;
Tim Peters877a2122002-08-12 05:09:36 +00001312 digit carry = 0;
1313
1314 assert(m >= n);
1315 for (i = 0; i < n; ++i) {
1316 carry += x[i] + y[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001317 x[i] = carry & PyLong_MASK;
1318 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001319 assert((carry & 1) == carry);
1320 }
1321 for (; carry && i < m; ++i) {
1322 carry += x[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001323 x[i] = carry & PyLong_MASK;
1324 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001325 assert((carry & 1) == carry);
1326 }
1327 return carry;
1328}
1329
1330/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1331 * is modified in place, by subtracting y from it. Borrows are propagated as
1332 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1333 */
1334static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001335v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001336{
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001337 Py_ssize_t i;
Tim Peters877a2122002-08-12 05:09:36 +00001338 digit borrow = 0;
1339
1340 assert(m >= n);
1341 for (i = 0; i < n; ++i) {
1342 borrow = x[i] - y[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001343 x[i] = borrow & PyLong_MASK;
1344 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001345 borrow &= 1; /* keep only 1 sign bit */
1346 }
1347 for (; borrow && i < m; ++i) {
1348 borrow = x[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001349 x[i] = borrow & PyLong_MASK;
1350 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001351 borrow &= 1;
1352 }
1353 return borrow;
1354}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001355
Mark Dickinson0b666bf2009-03-23 18:25:13 +00001356/* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put
1357 * result in z[0:m], and return the d bits shifted out of the top.
1358 */
1359static digit
1360v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001361{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362 Py_ssize_t i;
Mark Dickinson0b666bf2009-03-23 18:25:13 +00001363 digit carry = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001364
Mark Dickinson0b666bf2009-03-23 18:25:13 +00001365 assert(0 <= d && d < PyLong_SHIFT);
1366 for (i=0; i < m; i++) {
1367 twodigits acc = (twodigits)a[i] << d | carry;
1368 z[i] = (digit)acc & PyLong_MASK;
1369 carry = (digit)(acc >> PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001370 }
Mark Dickinson0b666bf2009-03-23 18:25:13 +00001371 return carry;
1372}
1373
1374/* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put
1375 * result in z[0:m], and return the d bits shifted out of the bottom.
1376 */
1377static digit
1378v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
1379{
1380 Py_ssize_t i;
1381 digit carry = 0;
1382 digit mask = ((digit)1 << d) - 1U;
1383
1384 assert(0 <= d && d < PyLong_SHIFT);
1385 for (i=m; i-- > 0;) {
1386 twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
1387 carry = (digit)acc & mask;
1388 z[i] = (digit)(acc >> d);
1389 }
1390 return carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001391}
1392
Tim Peters212e6142001-07-14 12:23:19 +00001393/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1394 in pout, and returning the remainder. pin and pout point at the LSD.
1395 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Eric Smith5e527eb2008-02-10 01:36:53 +00001396 _PyLong_Format, but that should be done with great care since longs are
Tim Peters212e6142001-07-14 12:23:19 +00001397 immutable. */
1398
1399static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001400inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001401{
1402 twodigits rem = 0;
1403
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001404 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001405 pin += size;
1406 pout += size;
1407 while (--size >= 0) {
1408 digit hi;
Mark Dickinson71adc932009-09-28 16:52:40 +00001409 rem = (rem << PyLong_SHIFT) | *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001410 *--pout = hi = (digit)(rem / n);
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001411 rem -= (twodigits)hi * n;
Tim Peters212e6142001-07-14 12:23:19 +00001412 }
1413 return (digit)rem;
1414}
1415
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001416/* Divide a long integer by a digit, returning both the quotient
1417 (as function result) and the remainder (through *prem).
1418 The sign of a is ignored; n should not be zero. */
1419
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001420static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001421divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001422{
Christian Heimese93237d2007-12-19 02:37:44 +00001423 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001424 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001425
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001426 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001428 if (z == NULL)
1429 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001430 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001431 return long_normalize(z);
1432}
1433
Mark Dickinsonaa2adc82009-09-16 22:10:56 +00001434/* Convert a long integer to a base 10 string. Returns a new non-shared
1435 string. (Return value is non-shared so that callers can modify the
1436 returned value if necessary.) */
1437
1438static PyObject *
1439long_to_decimal_string(PyObject *aa, int addL)
1440{
1441 PyLongObject *scratch, *a;
1442 PyObject *str;
1443 Py_ssize_t size, strlen, size_a, i, j;
1444 digit *pout, *pin, rem, tenpow;
1445 char *p;
1446 int negative;
1447
1448 a = (PyLongObject *)aa;
1449 if (a == NULL || !PyLong_Check(a)) {
1450 PyErr_BadInternalCall();
1451 return NULL;
1452 }
1453 size_a = ABS(Py_SIZE(a));
1454 negative = Py_SIZE(a) < 0;
1455
1456 /* quick and dirty upper bound for the number of digits
1457 required to express a in base _PyLong_DECIMAL_BASE:
1458
1459 #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE))
1460
1461 But log2(a) < size_a * PyLong_SHIFT, and
1462 log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
1463 > 3 * _PyLong_DECIMAL_SHIFT
1464 */
1465 if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) {
1466 PyErr_SetString(PyExc_OverflowError,
1467 "long is too large to format");
1468 return NULL;
1469 }
1470 /* the expression size_a * PyLong_SHIFT is now safe from overflow */
1471 size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT);
1472 scratch = _PyLong_New(size);
1473 if (scratch == NULL)
1474 return NULL;
1475
1476 /* convert array of base _PyLong_BASE digits in pin to an array of
1477 base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP,
1478 Volume 2 (3rd edn), section 4.4, Method 1b). */
1479 pin = a->ob_digit;
1480 pout = scratch->ob_digit;
1481 size = 0;
1482 for (i = size_a; --i >= 0; ) {
1483 digit hi = pin[i];
1484 for (j = 0; j < size; j++) {
1485 twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi;
Mark Dickinson40ee8612009-09-21 16:16:44 +00001486 hi = (digit)(z / _PyLong_DECIMAL_BASE);
1487 pout[j] = (digit)(z - (twodigits)hi *
1488 _PyLong_DECIMAL_BASE);
Mark Dickinsonaa2adc82009-09-16 22:10:56 +00001489 }
1490 while (hi) {
1491 pout[size++] = hi % _PyLong_DECIMAL_BASE;
1492 hi /= _PyLong_DECIMAL_BASE;
1493 }
1494 /* check for keyboard interrupt */
1495 SIGCHECK({
1496 Py_DECREF(scratch);
1497 return NULL;
1498 })
1499 }
1500 /* pout should have at least one digit, so that the case when a = 0
1501 works correctly */
1502 if (size == 0)
1503 pout[size++] = 0;
1504
1505 /* calculate exact length of output string, and allocate */
1506 strlen = (addL != 0) + negative +
1507 1 + (size - 1) * _PyLong_DECIMAL_SHIFT;
1508 tenpow = 10;
1509 rem = pout[size-1];
1510 while (rem >= tenpow) {
1511 tenpow *= 10;
1512 strlen++;
1513 }
1514 str = PyString_FromStringAndSize(NULL, strlen);
1515 if (str == NULL) {
1516 Py_DECREF(scratch);
1517 return NULL;
1518 }
1519
1520 /* fill the string right-to-left */
1521 p = PyString_AS_STRING(str) + strlen;
1522 *p = '\0';
1523 if (addL)
1524 *--p = 'L';
1525 /* pout[0] through pout[size-2] contribute exactly
1526 _PyLong_DECIMAL_SHIFT digits each */
1527 for (i=0; i < size - 1; i++) {
1528 rem = pout[i];
1529 for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) {
1530 *--p = '0' + rem % 10;
1531 rem /= 10;
1532 }
1533 }
1534 /* pout[size-1]: always produce at least one decimal digit */
1535 rem = pout[i];
1536 do {
1537 *--p = '0' + rem % 10;
1538 rem /= 10;
1539 } while (rem != 0);
1540
1541 /* and sign */
1542 if (negative)
1543 *--p = '-';
1544
1545 /* check we've counted correctly */
1546 assert(p == PyString_AS_STRING(str));
1547 Py_DECREF(scratch);
1548 return (PyObject *)str;
1549}
1550
Eric Smith5e527eb2008-02-10 01:36:53 +00001551/* Convert the long to a string object with given base,
1552 appending a base prefix of 0[box] if base is 2, 8 or 16.
1553 Add a trailing "L" if addL is non-zero.
1554 If newstyle is zero, then use the pre-2.6 behavior of octal having
1555 a leading "0", instead of the prefix "0o" */
1556PyAPI_FUNC(PyObject *)
1557_PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001558{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559 register PyLongObject *a = (PyLongObject *)aa;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001560 PyStringObject *str;
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001561 Py_ssize_t i, sz;
Neal Norwitzc09efa82006-07-23 07:53:14 +00001562 Py_ssize_t size_a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001563 char *p;
1564 int bits;
1565 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001566
Mark Dickinsonaa2adc82009-09-16 22:10:56 +00001567 if (base == 10)
1568 return long_to_decimal_string((PyObject *)a, addL);
1569
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001570 if (a == NULL || !PyLong_Check(a)) {
1571 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001572 return NULL;
1573 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001574 assert(base >= 2 && base <= 36);
Christian Heimese93237d2007-12-19 02:37:44 +00001575 size_a = ABS(Py_SIZE(a));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001576
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001577 /* Compute a rough upper bound for the length of the string */
1578 i = base;
1579 bits = 0;
1580 while (i > 1) {
1581 ++bits;
1582 i >>= 1;
1583 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001584 i = 5 + (addL ? 1 : 0);
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001585 /* ensure we don't get signed overflow in sz calculation */
1586 if (size_a > (PY_SSIZE_T_MAX - i) / PyLong_SHIFT) {
Armin Rigo7ccbca92006-10-04 12:17:45 +00001587 PyErr_SetString(PyExc_OverflowError,
1588 "long is too large to format");
1589 return NULL;
1590 }
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001591 sz = i + 1 + (size_a * PyLong_SHIFT - 1) / bits;
1592 assert(sz >= 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001593 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001594 if (str == NULL)
1595 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001596 p = PyString_AS_STRING(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001597 *p = '\0';
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001598 if (addL)
1599 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001600 if (a->ob_size < 0)
1601 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001602
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001603 if (a->ob_size == 0) {
1604 *--p = '0';
1605 }
1606 else if ((base & (base - 1)) == 0) {
1607 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001608 twodigits accum = 0;
1609 int accumbits = 0; /* # of bits in accum */
1610 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001611 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001612 while ((i >>= 1) > 1)
1613 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001614
1615 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001616 accum |= (twodigits)a->ob_digit[i] << accumbits;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001617 accumbits += PyLong_SHIFT;
Tim Peters586b2e32001-07-15 09:11:14 +00001618 assert(accumbits >= basebits);
1619 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001620 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001621 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001622 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001623 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001624 accumbits -= basebits;
1625 accum >>= basebits;
1626 } while (i < size_a-1 ? accumbits >= basebits :
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001627 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001628 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001629 }
1630 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001631 /* Not 0, and base not a power of 2. Divide repeatedly by
1632 base, but for speed use the highest power of base that
1633 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001634 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001635 digit *pin = a->ob_digit;
1636 PyLongObject *scratch;
1637 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001638 digit powbase = base; /* powbase == base ** power */
1639 int power = 1;
1640 for (;;) {
Mark Dickinsonb6464872009-02-25 20:29:50 +00001641 twodigits newpow = powbase * (twodigits)base;
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001642 if (newpow >> PyLong_SHIFT)
1643 /* doesn't fit in a digit */
Tim Petersfad225f2001-07-13 02:59:26 +00001644 break;
1645 powbase = (digit)newpow;
1646 ++power;
1647 }
Tim Peters212e6142001-07-14 12:23:19 +00001648
1649 /* Get a scratch area for repeated division. */
1650 scratch = _PyLong_New(size);
1651 if (scratch == NULL) {
1652 Py_DECREF(str);
1653 return NULL;
1654 }
1655
1656 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001657 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001658 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001659 digit rem = inplace_divrem1(scratch->ob_digit,
1660 pin, size, powbase);
1661 pin = scratch->ob_digit; /* no need to use a again */
1662 if (pin[size - 1] == 0)
1663 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001664 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001665 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001666 Py_DECREF(str);
1667 return NULL;
1668 })
Tim Peters212e6142001-07-14 12:23:19 +00001669
1670 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001671 assert(ntostore > 0);
1672 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001673 digit nextrem = (digit)(rem / base);
1674 char c = (char)(rem - nextrem * base);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001675 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001676 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001677 *--p = c;
1678 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001679 --ntostore;
1680 /* Termination is a bit delicate: must not
1681 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001682 remaining quotient and rem are both 0. */
1683 } while (ntostore && (size || rem));
1684 } while (size != 0);
1685 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001686 }
1687
Eric Smith5e527eb2008-02-10 01:36:53 +00001688 if (base == 2) {
1689 *--p = 'b';
1690 *--p = '0';
1691 }
1692 else if (base == 8) {
Mark Dickinson1f4fc092009-09-13 11:56:13 +00001693 if (newstyle) {
Eric Smith5e527eb2008-02-10 01:36:53 +00001694 *--p = 'o';
Guido van Rossum2c475421992-08-14 15:13:07 +00001695 *--p = '0';
Eric Smith5e527eb2008-02-10 01:36:53 +00001696 }
1697 else
1698 if (size_a != 0)
1699 *--p = '0';
Guido van Rossum2c475421992-08-14 15:13:07 +00001700 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001701 else if (base == 16) {
1702 *--p = 'x';
1703 *--p = '0';
1704 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001705 else if (base != 10) {
1706 *--p = '#';
1707 *--p = '0' + base%10;
1708 if (base > 10)
1709 *--p = '0' + base/10;
1710 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001711 if (sign)
1712 *--p = sign;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001713 if (p != PyString_AS_STRING(str)) {
1714 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001715 assert(p > q);
1716 do {
1717 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001718 q--;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001719 _PyString_Resize((PyObject **)&str,
1720 (Py_ssize_t) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001721 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001722 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001723}
1724
Tim Petersda53afa2006-05-25 17:34:03 +00001725/* Table of digit values for 8-bit string -> integer conversion.
1726 * '0' maps to 0, ..., '9' maps to 9.
1727 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1728 * All other indices map to 37.
1729 * Note that when converting a base B string, a char c is a legitimate
1730 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1731 */
1732int _PyLong_DigitValue[256] = {
Tim Peters696cf432006-05-24 21:10:40 +00001733 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1734 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1735 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1736 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1737 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1738 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1739 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1740 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1741 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1742 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1743 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1744 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1745 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1746 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1747 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1748 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Tim Peters696cf432006-05-24 21:10:40 +00001749};
1750
1751/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001752 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1753 * non-digit (which may be *str!). A normalized long is returned.
1754 * The point to this routine is that it takes time linear in the number of
1755 * string characters.
1756 */
1757static PyLongObject *
1758long_from_binary_base(char **str, int base)
1759{
1760 char *p = *str;
1761 char *start = p;
1762 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001763 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001764 PyLongObject *z;
1765 twodigits accum;
1766 int bits_in_accum;
1767 digit *pdigit;
1768
1769 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1770 n = base;
1771 for (bits_per_char = -1; n; ++bits_per_char)
1772 n >>= 1;
1773 /* n <- total # of bits needed, while setting p to end-of-string */
Neal Norwitzd183bdd2008-03-28 04:58:51 +00001774 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001775 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001776 *str = p;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001777 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1778 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001779 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001780 PyErr_SetString(PyExc_ValueError,
1781 "long string too large to convert");
1782 return NULL;
1783 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001784 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001785 z = _PyLong_New(n);
1786 if (z == NULL)
1787 return NULL;
1788 /* Read string from right, and fill in long from left; i.e.,
1789 * from least to most significant in both.
1790 */
1791 accum = 0;
1792 bits_in_accum = 0;
1793 pdigit = z->ob_digit;
1794 while (--p >= start) {
Neal Norwitzd183bdd2008-03-28 04:58:51 +00001795 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001796 assert(k >= 0 && k < base);
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001797 accum |= (twodigits)k << bits_in_accum;
Tim Petersbf2674b2003-02-02 07:51:32 +00001798 bits_in_accum += bits_per_char;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001799 if (bits_in_accum >= PyLong_SHIFT) {
1800 *pdigit++ = (digit)(accum & PyLong_MASK);
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001801 assert(pdigit - z->ob_digit <= n);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001802 accum >>= PyLong_SHIFT;
1803 bits_in_accum -= PyLong_SHIFT;
1804 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001805 }
1806 }
1807 if (bits_in_accum) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001808 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001809 *pdigit++ = (digit)accum;
Mark Dickinsonff84aa82009-01-24 15:27:44 +00001810 assert(pdigit - z->ob_digit <= n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001811 }
1812 while (pdigit - z->ob_digit < n)
1813 *pdigit++ = 0;
1814 return long_normalize(z);
1815}
1816
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001817PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001818PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001819{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001820 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001821 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001822 PyLongObject *z;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00001823 PyObject *strobj, *strrepr;
1824 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001825
Guido van Rossum472c04f1996-12-05 21:57:21 +00001826 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001827 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001828 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001829 return NULL;
1830 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001831 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001832 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001833 if (*str == '+')
1834 ++str;
1835 else if (*str == '-') {
1836 ++str;
1837 sign = -1;
1838 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001839 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001840 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001841 if (base == 0) {
Eric Smith9ff19b52008-03-17 17:32:20 +00001842 /* No base given. Deduce the base from the contents
1843 of the string */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001844 if (str[0] != '0')
1845 base = 10;
1846 else if (str[1] == 'x' || str[1] == 'X')
1847 base = 16;
Eric Smith9ff19b52008-03-17 17:32:20 +00001848 else if (str[1] == 'o' || str[1] == 'O')
1849 base = 8;
1850 else if (str[1] == 'b' || str[1] == 'B')
1851 base = 2;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001852 else
Eric Smith9ff19b52008-03-17 17:32:20 +00001853 /* "old" (C-style) octal literal, still valid in
1854 2.x, although illegal in 3.x */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001855 base = 8;
1856 }
Eric Smith9ff19b52008-03-17 17:32:20 +00001857 /* Whether or not we were deducing the base, skip leading chars
1858 as needed */
1859 if (str[0] == '0' &&
1860 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1861 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
1862 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001863 str += 2;
Tim Peters696cf432006-05-24 21:10:40 +00001864
Guido van Rossume6762971998-06-22 03:54:46 +00001865 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001866 if ((base & (base - 1)) == 0)
1867 z = long_from_binary_base(&str, base);
1868 else {
Tim Peters696cf432006-05-24 21:10:40 +00001869/***
1870Binary bases can be converted in time linear in the number of digits, because
1871Python's representation base is binary. Other bases (including decimal!) use
1872the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001873
Tim Peters696cf432006-05-24 21:10:40 +00001874First some math: the largest integer that can be expressed in N base-B digits
1875is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1876case number of Python digits needed to hold it is the smallest integer n s.t.
1877
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001878 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1879 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
1880 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001881
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001882The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
Tim Peters696cf432006-05-24 21:10:40 +00001883this quickly. A Python long with that much space is reserved near the start,
1884and the result is computed into it.
1885
1886The input string is actually treated as being in base base**i (i.e., i digits
1887are processed at a time), where two more static arrays hold:
1888
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001889 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
Tim Peters696cf432006-05-24 21:10:40 +00001890 convmultmax_base[base] = base ** convwidth_base[base]
1891
1892The first of these is the largest i such that i consecutive input digits
1893must fit in a single Python digit. The second is effectively the input
1894base we're really using.
1895
1896Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1897convmultmax_base[base], the result is "simply"
1898
1899 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1900
1901where B = convmultmax_base[base].
Tim Peters9faa3ed2006-05-30 15:53:34 +00001902
1903Error analysis: as above, the number of Python digits `n` needed is worst-
1904case
1905
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001906 n >= N * log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001907
1908where `N` is the number of input digits in base `B`. This is computed via
1909
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001910 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001911
1912below. Two numeric concerns are how much space this can waste, and whether
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001913the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
Tim Peters9faa3ed2006-05-30 15:53:34 +00001914which is the default (and it's unlikely anyone changes that).
1915
1916Waste isn't a problem: provided the first input digit isn't 0, the difference
1917between the worst-case input with N digits and the smallest input with N
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001918digits is about a factor of B, but B is small compared to PyLong_BASE so at most
Tim Peters9faa3ed2006-05-30 15:53:34 +00001919one allocated Python digit can remain unused on that count. If
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001920N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001921and adding 1 returns a result 1 larger than necessary. However, that can't
1922happen: whenever B is a power of 2, long_from_binary_base() is called
1923instead, and it's impossible for B**i to be an integer power of 2**15 when
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001924B 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 +00001925an exact integer when B is not a power of 2, since B**i has a prime factor
1926other than 2 in that case, but (2**15)**j's only prime factor is 2).
1927
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001928The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001929is a little bit larger than an exact integer, but due to roundoff errors (in
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001930computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
Tim Peters9faa3ed2006-05-30 15:53:34 +00001931yields a numeric result a little less than that integer. Unfortunately, "how
1932close can a transcendental function get to an integer over some range?"
1933questions are generally theoretically intractable. Computer analysis via
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001934continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
Tim Peters9faa3ed2006-05-30 15:53:34 +00001935fractions, giving a sequence i/j of "the best" rational approximations. Then
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001936j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
Tim Peters9faa3ed2006-05-30 15:53:34 +00001937we can get very close to being in trouble, but very rarely. For example,
193876573 is a denominator in one of the continued-fraction approximations to
1939log(10)/log(2**15), and indeed:
1940
1941 >>> log(10)/log(2**15)*76573
1942 16958.000000654003
1943
1944is very close to an integer. If we were working with IEEE single-precision,
1945rounding errors could kill us. Finding worst cases in IEEE double-precision
1946requires better-than-double-precision log() functions, and Tim didn't bother.
1947Instead the code checks to see whether the allocated space is enough as each
1948new Python digit is added, and copies the whole thing to a larger long if not.
1949This should happen extremely rarely, and in fact I don't have a test case
1950that triggers it(!). Instead the code was tested by artificially allocating
1951just 1 digit at the start, so that the copying code was exercised for every
1952digit beyond the first.
Tim Peters696cf432006-05-24 21:10:40 +00001953***/
1954 register twodigits c; /* current input character */
1955 Py_ssize_t size_z;
1956 int i;
1957 int convwidth;
1958 twodigits convmultmax, convmult;
1959 digit *pz, *pzstop;
1960 char* scan;
1961
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001962 static double log_base_PyLong_BASE[37] = {0.0e0,};
Tim Peters696cf432006-05-24 21:10:40 +00001963 static int convwidth_base[37] = {0,};
1964 static twodigits convmultmax_base[37] = {0,};
1965
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001966 if (log_base_PyLong_BASE[base] == 0.0) {
Tim Peters696cf432006-05-24 21:10:40 +00001967 twodigits convmax = base;
1968 int i = 1;
1969
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001970 log_base_PyLong_BASE[base] = log((double)base) /
1971 log((double)PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00001972 for (;;) {
1973 twodigits next = convmax * base;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001974 if (next > PyLong_BASE)
Tim Peters696cf432006-05-24 21:10:40 +00001975 break;
1976 convmax = next;
1977 ++i;
1978 }
1979 convmultmax_base[base] = convmax;
1980 assert(i > 0);
1981 convwidth_base[base] = i;
1982 }
1983
1984 /* Find length of the string of numeric characters. */
1985 scan = str;
Neal Norwitzd183bdd2008-03-28 04:58:51 +00001986 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Tim Peters696cf432006-05-24 21:10:40 +00001987 ++scan;
1988
1989 /* Create a long object that can contain the largest possible
1990 * integer with this base and length. Note that there's no
1991 * need to initialize z->ob_digit -- no slot is read up before
1992 * being stored into.
1993 */
Christian Heimes7f39c9f2008-01-25 12:18:43 +00001994 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
Tim Peters9faa3ed2006-05-30 15:53:34 +00001995 /* Uncomment next line to test exceedingly rare copy code */
1996 /* size_z = 1; */
Tim Peters696cf432006-05-24 21:10:40 +00001997 assert(size_z > 0);
1998 z = _PyLong_New(size_z);
1999 if (z == NULL)
2000 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002001 Py_SIZE(z) = 0;
Tim Peters696cf432006-05-24 21:10:40 +00002002
2003 /* `convwidth` consecutive input digits are treated as a single
2004 * digit in base `convmultmax`.
2005 */
2006 convwidth = convwidth_base[base];
2007 convmultmax = convmultmax_base[base];
2008
2009 /* Work ;-) */
2010 while (str < scan) {
2011 /* grab up to convwidth digits from the input string */
Neal Norwitzd183bdd2008-03-28 04:58:51 +00002012 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Tim Peters696cf432006-05-24 21:10:40 +00002013 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
2014 c = (twodigits)(c * base +
Neal Norwitzd183bdd2008-03-28 04:58:51 +00002015 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002016 assert(c < PyLong_BASE);
Tim Peters696cf432006-05-24 21:10:40 +00002017 }
2018
2019 convmult = convmultmax;
2020 /* Calculate the shift only if we couldn't get
2021 * convwidth digits.
2022 */
2023 if (i != convwidth) {
2024 convmult = base;
2025 for ( ; i > 1; --i)
2026 convmult *= base;
2027 }
2028
2029 /* Multiply z by convmult, and add c. */
2030 pz = z->ob_digit;
Christian Heimese93237d2007-12-19 02:37:44 +00002031 pzstop = pz + Py_SIZE(z);
Tim Peters696cf432006-05-24 21:10:40 +00002032 for (; pz < pzstop; ++pz) {
2033 c += (twodigits)*pz * convmult;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002034 *pz = (digit)(c & PyLong_MASK);
2035 c >>= PyLong_SHIFT;
Tim Peters696cf432006-05-24 21:10:40 +00002036 }
2037 /* carry off the current end? */
2038 if (c) {
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002039 assert(c < PyLong_BASE);
Christian Heimese93237d2007-12-19 02:37:44 +00002040 if (Py_SIZE(z) < size_z) {
Tim Peters9faa3ed2006-05-30 15:53:34 +00002041 *pz = (digit)c;
Christian Heimese93237d2007-12-19 02:37:44 +00002042 ++Py_SIZE(z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00002043 }
2044 else {
2045 PyLongObject *tmp;
2046 /* Extremely rare. Get more space. */
Christian Heimese93237d2007-12-19 02:37:44 +00002047 assert(Py_SIZE(z) == size_z);
Tim Peters9faa3ed2006-05-30 15:53:34 +00002048 tmp = _PyLong_New(size_z + 1);
2049 if (tmp == NULL) {
2050 Py_DECREF(z);
2051 return NULL;
2052 }
2053 memcpy(tmp->ob_digit,
2054 z->ob_digit,
2055 sizeof(digit) * size_z);
2056 Py_DECREF(z);
2057 z = tmp;
2058 z->ob_digit[size_z] = (digit)c;
2059 ++size_z;
2060 }
Tim Peters696cf432006-05-24 21:10:40 +00002061 }
Tim Petersbf2674b2003-02-02 07:51:32 +00002062 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002063 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00002064 if (z == NULL)
2065 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00002066 if (str == start)
2067 goto onError;
Tim Peters696cf432006-05-24 21:10:40 +00002068 if (sign < 0)
Christian Heimese93237d2007-12-19 02:37:44 +00002069 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00002070 if (*str == 'L' || *str == 'l')
2071 str++;
2072 while (*str && isspace(Py_CHARMASK(*str)))
2073 str++;
2074 if (*str != '\0')
2075 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002076 if (pend)
2077 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002078 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00002079
2080 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00002081 Py_XDECREF(z);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00002082 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002083 strobj = PyString_FromStringAndSize(orig_str, slen);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00002084 if (strobj == NULL)
2085 return NULL;
2086 strrepr = PyObject_Repr(strobj);
2087 Py_DECREF(strobj);
2088 if (strrepr == NULL)
2089 return NULL;
2090 PyErr_Format(PyExc_ValueError,
2091 "invalid literal for long() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002092 base, PyString_AS_STRING(strrepr));
Thomas Wouters9cb28bea2006-04-11 23:50:33 +00002093 Py_DECREF(strrepr);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002094 return NULL;
2095}
2096
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002097#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00002098PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002099PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00002100{
Walter Dörwald07e14762002-11-06 16:15:14 +00002101 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00002102 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002103
Walter Dörwald07e14762002-11-06 16:15:14 +00002104 if (buffer == NULL)
2105 return NULL;
2106
2107 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
2108 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002109 return NULL;
2110 }
Walter Dörwald07e14762002-11-06 16:15:14 +00002111 result = PyLong_FromString(buffer, NULL, base);
2112 PyMem_FREE(buffer);
2113 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002114}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002115#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002116
Tim Peters9f688bf2000-07-07 15:53:28 +00002117/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002118static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00002119 (PyLongObject *, PyLongObject *, PyLongObject **);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002120static PyObject *long_long(PyObject *v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002121
2122/* Long division with remainder, top-level routine */
2123
Guido van Rossume32e0141992-01-19 16:31:05 +00002124static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002125long_divrem(PyLongObject *a, PyLongObject *b,
2126 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002127{
Christian Heimese93237d2007-12-19 02:37:44 +00002128 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002129 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002130
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002131 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002132 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00002133 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00002134 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002135 }
2136 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00002137 (size_a == size_b &&
2138 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002139 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002140 *pdiv = _PyLong_New(0);
Georg Brandlc02e1312007-04-11 17:16:24 +00002141 if (*pdiv == NULL)
2142 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002143 Py_INCREF(a);
2144 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00002145 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002146 }
2147 if (size_b == 1) {
2148 digit rem = 0;
2149 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002150 if (z == NULL)
2151 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002152 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Georg Brandlc02e1312007-04-11 17:16:24 +00002153 if (*prem == NULL) {
2154 Py_DECREF(z);
2155 return -1;
2156 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002157 }
Guido van Rossume32e0141992-01-19 16:31:05 +00002158 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002159 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002160 if (z == NULL)
2161 return -1;
2162 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002163 /* Set the signs.
2164 The quotient z has the sign of a*b;
2165 the remainder r has the sign of a,
2166 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00002167 if ((a->ob_size < 0) != (b->ob_size < 0))
2168 z->ob_size = -(z->ob_size);
2169 if (a->ob_size < 0 && (*prem)->ob_size != 0)
2170 (*prem)->ob_size = -((*prem)->ob_size);
2171 *pdiv = z;
2172 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002173}
2174
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002175/* Unsigned long division with remainder -- the algorithm. The arguments v1
2176 and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002178static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002179x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002180{
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002181 PyLongObject *v, *w, *a;
2182 Py_ssize_t i, k, size_v, size_w;
2183 int d;
2184 digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
2185 twodigits vv;
2186 sdigit zhi;
2187 stwodigits z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002188
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002189 /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
2190 edn.), section 4.3.1, Algorithm D], except that we don't explicitly
2191 handle the special case when the initial estimate q for a quotient
2192 digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
2193 that won't overflow a digit. */
2194
2195 /* allocate space; w will also be used to hold the final remainder */
2196 size_v = ABS(Py_SIZE(v1));
2197 size_w = ABS(Py_SIZE(w1));
2198 assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
2199 v = _PyLong_New(size_v+1);
2200 if (v == NULL) {
2201 *prem = NULL;
2202 return NULL;
2203 }
2204 w = _PyLong_New(size_w);
2205 if (w == NULL) {
2206 Py_DECREF(v);
2207 *prem = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002208 return NULL;
2209 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002210
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002211 /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
2212 shift v1 left by the same amount. Results go into w and v. */
2213 d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
2214 carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
2215 assert(carry == 0);
2216 carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
2217 if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
2218 v->ob_digit[size_v] = carry;
2219 size_v++;
2220 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002221
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002222 /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
2223 at most (and usually exactly) k = size_v - size_w digits. */
Neal Norwitzc6a989a2006-05-10 06:57:58 +00002224 k = size_v - size_w;
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002225 assert(k >= 0);
2226 a = _PyLong_New(k);
2227 if (a == NULL) {
2228 Py_DECREF(w);
2229 Py_DECREF(v);
2230 *prem = NULL;
2231 return NULL;
2232 }
2233 v0 = v->ob_digit;
2234 w0 = w->ob_digit;
2235 wm1 = w0[size_w-1];
2236 wm2 = w0[size_w-2];
2237 for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
2238 /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
2239 single-digit quotient q, remainder in vk[0:size_w]. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002240
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002241 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002242 Py_DECREF(a);
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002243 Py_DECREF(w);
2244 Py_DECREF(v);
2245 *prem = NULL;
2246 return NULL;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002247 })
Tim Peters5af4e6c2002-08-12 02:31:19 +00002248
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002249 /* estimate quotient digit q; may overestimate by 1 (rare) */
2250 vtop = vk[size_w];
2251 assert(vtop <= wm1);
2252 vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
2253 q = (digit)(vv / wm1);
2254 r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
2255 while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
2256 | vk[size_w-2])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002257 --q;
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002258 r += wm1;
2259 if (r >= PyLong_BASE)
2260 break;
2261 }
2262 assert(q <= PyLong_BASE);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002263
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002264 /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
2265 zhi = 0;
2266 for (i = 0; i < size_w; ++i) {
2267 /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
2268 -PyLong_BASE * q <= z < PyLong_BASE */
2269 z = (sdigit)vk[i] + zhi -
2270 (stwodigits)q * (stwodigits)w0[i];
2271 vk[i] = (digit)z & PyLong_MASK;
2272 zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
2273 z, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002274 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002275
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002276 /* add w back if q was too large (this branch taken rarely) */
2277 assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
2278 if ((sdigit)vtop + zhi < 0) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002279 carry = 0;
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002280 for (i = 0; i < size_w; ++i) {
2281 carry += vk[i] + w0[i];
2282 vk[i] = carry & PyLong_MASK;
2283 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002284 }
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002285 --q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002286 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002287
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002288 /* store quotient digit */
2289 assert(q < PyLong_BASE);
2290 *--ak = q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002291 }
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002292
2293 /* unshift remainder; we reuse w to store the result */
2294 carry = v_rshift(w0, v0, size_w, d);
2295 assert(carry==0);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002296 Py_DECREF(v);
Mark Dickinson0b666bf2009-03-23 18:25:13 +00002297
2298 *prem = long_normalize(w);
2299 return long_normalize(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002300}
2301
2302/* Methods */
2303
2304static void
Tim Peters9f688bf2000-07-07 15:53:28 +00002305long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002306{
Christian Heimese93237d2007-12-19 02:37:44 +00002307 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002308}
2309
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002310static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002311long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002312{
Eric Smith5e527eb2008-02-10 01:36:53 +00002313 return _PyLong_Format(v, 10, 1, 0);
Fred Drake121ee271999-12-23 15:41:28 +00002314}
2315
2316static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002317long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00002318{
Eric Smith5e527eb2008-02-10 01:36:53 +00002319 return _PyLong_Format(v, 10, 0, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002320}
2321
2322static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002323long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002324{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002325 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002326
Christian Heimese93237d2007-12-19 02:37:44 +00002327 if (Py_SIZE(a) != Py_SIZE(b)) {
2328 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002329 sign = 0;
2330 else
Christian Heimese93237d2007-12-19 02:37:44 +00002331 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002332 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002333 else {
Christian Heimese93237d2007-12-19 02:37:44 +00002334 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002335 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2336 ;
2337 if (i < 0)
2338 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002339 else {
Mark Dickinsonbcf6b182009-02-15 15:48:39 +00002340 sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
Christian Heimese93237d2007-12-19 02:37:44 +00002341 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002342 sign = -sign;
2343 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002344 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00002345 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002346}
2347
Guido van Rossum9bfef441993-03-29 10:43:31 +00002348static long
Tim Peters9f688bf2000-07-07 15:53:28 +00002349long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002350{
Mark Dickinson6ffa4a22009-01-26 21:36:30 +00002351 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002352 Py_ssize_t i;
2353 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002354
2355 /* This is designed so that Python ints and longs with the
2356 same value hash to the same value, otherwise comparisons
2357 of mapping keys will turn out weird */
2358 i = v->ob_size;
2359 sign = 1;
2360 x = 0;
2361 if (i < 0) {
2362 sign = -1;
2363 i = -(i);
2364 }
Mark Dickinsona0eae032009-01-26 21:40:08 +00002365 /* The following loop produces a C unsigned long x such that x is
2366 congruent to the absolute value of v modulo ULONG_MAX. The
2367 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002368 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00002369 /* Force a native long #-bits (32 or 64) circular shift */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +00002370 x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002371 x += v->ob_digit[i];
Mark Dickinson6ffa4a22009-01-26 21:36:30 +00002372 /* If the addition above overflowed we compensate by
2373 incrementing. This preserves the value modulo
2374 ULONG_MAX. */
2375 if (x < v->ob_digit[i])
Facundo Batistad544df72007-09-19 15:10:06 +00002376 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002377 }
2378 x = x * sign;
Mark Dickinson2ffb26f2009-02-15 10:13:41 +00002379 if (x == (unsigned long)-1)
2380 x = (unsigned long)-2;
2381 return (long)x;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002382}
2383
2384
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002385/* Add the absolute values of two long integers. */
2386
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002387static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002388x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002389{
Christian Heimese93237d2007-12-19 02:37:44 +00002390 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002391 PyLongObject *z;
Mark Dickinsonff84aa82009-01-24 15:27:44 +00002392 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002393 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002394
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002395 /* Ensure a is the larger of the two: */
2396 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002397 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002398 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002399 size_a = size_b;
2400 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002401 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002402 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002403 if (z == NULL)
2404 return NULL;
2405 for (i = 0; i < size_b; ++i) {
2406 carry += a->ob_digit[i] + b->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002407 z->ob_digit[i] = carry & PyLong_MASK;
2408 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002409 }
2410 for (; i < size_a; ++i) {
2411 carry += a->ob_digit[i];
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002412 z->ob_digit[i] = carry & PyLong_MASK;
2413 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002414 }
2415 z->ob_digit[i] = carry;
2416 return long_normalize(z);
2417}
2418
2419/* Subtract the absolute values of two integers. */
2420
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002421static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002422x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002423{
Christian Heimese93237d2007-12-19 02:37:44 +00002424 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002425 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002426 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002427 int sign = 1;
2428 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002429
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002430 /* Ensure a is the larger of the two: */
2431 if (size_a < size_b) {
2432 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002433 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002434 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002435 size_a = size_b;
2436 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002437 }
2438 else if (size_a == size_b) {
2439 /* Find highest digit where a and b differ: */
2440 i = size_a;
2441 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2442 ;
2443 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002444 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002445 if (a->ob_digit[i] < b->ob_digit[i]) {
2446 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002447 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002448 }
2449 size_a = size_b = i+1;
2450 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002451 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002452 if (z == NULL)
2453 return NULL;
2454 for (i = 0; i < size_b; ++i) {
2455 /* The following assumes unsigned arithmetic
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002456 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002457 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002458 z->ob_digit[i] = borrow & PyLong_MASK;
2459 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002460 borrow &= 1; /* Keep only one sign bit */
2461 }
2462 for (; i < size_a; ++i) {
2463 borrow = a->ob_digit[i] - borrow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002464 z->ob_digit[i] = borrow & PyLong_MASK;
2465 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002466 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002467 }
2468 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002469 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002470 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002471 return long_normalize(z);
2472}
2473
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002474static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002475long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002476{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002477 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002478
Neil Schemenauerba872e22001-01-04 01:46:03 +00002479 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2480
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002481 if (a->ob_size < 0) {
2482 if (b->ob_size < 0) {
2483 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002484 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002485 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002486 }
2487 else
2488 z = x_sub(b, a);
2489 }
2490 else {
2491 if (b->ob_size < 0)
2492 z = x_sub(a, b);
2493 else
2494 z = x_add(a, b);
2495 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002496 Py_DECREF(a);
2497 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002498 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002499}
2500
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002501static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002502long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002503{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002504 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002505
Neil Schemenauerba872e22001-01-04 01:46:03 +00002506 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2507
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002508 if (a->ob_size < 0) {
2509 if (b->ob_size < 0)
2510 z = x_sub(a, b);
2511 else
2512 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002513 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002514 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002515 }
2516 else {
2517 if (b->ob_size < 0)
2518 z = x_add(a, b);
2519 else
2520 z = x_sub(a, b);
2521 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002522 Py_DECREF(a);
2523 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002524 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002525}
2526
Tim Peters5af4e6c2002-08-12 02:31:19 +00002527/* Grade school multiplication, ignoring the signs.
2528 * Returns the absolute value of the product, or NULL if error.
2529 */
2530static PyLongObject *
2531x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002532{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002533 PyLongObject *z;
Christian Heimese93237d2007-12-19 02:37:44 +00002534 Py_ssize_t size_a = ABS(Py_SIZE(a));
2535 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002536 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002537
Tim Peters5af4e6c2002-08-12 02:31:19 +00002538 z = _PyLong_New(size_a + size_b);
2539 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002540 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002541
Christian Heimese93237d2007-12-19 02:37:44 +00002542 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002543 if (a == b) {
2544 /* Efficient squaring per HAC, Algorithm 14.16:
2545 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2546 * Gives slightly less than a 2x speedup when a == b,
2547 * via exploiting that each entry in the multiplication
2548 * pyramid appears twice (except for the size_a squares).
2549 */
2550 for (i = 0; i < size_a; ++i) {
2551 twodigits carry;
2552 twodigits f = a->ob_digit[i];
2553 digit *pz = z->ob_digit + (i << 1);
2554 digit *pa = a->ob_digit + i + 1;
2555 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002556
Tim Peters0973b992004-08-29 22:16:50 +00002557 SIGCHECK({
2558 Py_DECREF(z);
2559 return NULL;
2560 })
2561
2562 carry = *pz + f * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002563 *pz++ = (digit)(carry & PyLong_MASK);
2564 carry >>= PyLong_SHIFT;
2565 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002566
2567 /* Now f is added in twice in each column of the
2568 * pyramid it appears. Same as adding f<<1 once.
2569 */
2570 f <<= 1;
2571 while (pa < paend) {
2572 carry += *pz + *pa++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002573 *pz++ = (digit)(carry & PyLong_MASK);
2574 carry >>= PyLong_SHIFT;
2575 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002576 }
2577 if (carry) {
2578 carry += *pz;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002579 *pz++ = (digit)(carry & PyLong_MASK);
2580 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002581 }
2582 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002583 *pz += (digit)(carry & PyLong_MASK);
2584 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002585 }
Tim Peters0973b992004-08-29 22:16:50 +00002586 }
2587 else { /* a is not the same as b -- gradeschool long mult */
2588 for (i = 0; i < size_a; ++i) {
2589 twodigits carry = 0;
2590 twodigits f = a->ob_digit[i];
2591 digit *pz = z->ob_digit + i;
2592 digit *pb = b->ob_digit;
2593 digit *pbend = b->ob_digit + size_b;
2594
2595 SIGCHECK({
2596 Py_DECREF(z);
2597 return NULL;
2598 })
2599
2600 while (pb < pbend) {
2601 carry += *pz + *pb++ * f;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002602 *pz++ = (digit)(carry & PyLong_MASK);
2603 carry >>= PyLong_SHIFT;
2604 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002605 }
2606 if (carry)
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002607 *pz += (digit)(carry & PyLong_MASK);
2608 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002609 }
2610 }
Tim Peters44121a62002-08-12 06:17:58 +00002611 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002612}
2613
2614/* A helper for Karatsuba multiplication (k_mul).
2615 Takes a long "n" and an integer "size" representing the place to
2616 split, and sets low and high such that abs(n) == (high << size) + low,
2617 viewing the shift as being by digits. The sign bit is ignored, and
2618 the return values are >= 0.
2619 Returns 0 on success, -1 on failure.
2620*/
2621static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002622kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002623{
2624 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002625 Py_ssize_t size_lo, size_hi;
Christian Heimese93237d2007-12-19 02:37:44 +00002626 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002627
2628 size_lo = MIN(size_n, size);
2629 size_hi = size_n - size_lo;
2630
2631 if ((hi = _PyLong_New(size_hi)) == NULL)
2632 return -1;
2633 if ((lo = _PyLong_New(size_lo)) == NULL) {
2634 Py_DECREF(hi);
2635 return -1;
2636 }
2637
2638 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2639 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2640
2641 *high = long_normalize(hi);
2642 *low = long_normalize(lo);
2643 return 0;
2644}
2645
Tim Peters60004642002-08-12 22:01:34 +00002646static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2647
Tim Peters5af4e6c2002-08-12 02:31:19 +00002648/* Karatsuba multiplication. Ignores the input signs, and returns the
2649 * absolute value of the product (or NULL if error).
2650 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2651 */
2652static PyLongObject *
2653k_mul(PyLongObject *a, PyLongObject *b)
2654{
Christian Heimese93237d2007-12-19 02:37:44 +00002655 Py_ssize_t asize = ABS(Py_SIZE(a));
2656 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002657 PyLongObject *ah = NULL;
2658 PyLongObject *al = NULL;
2659 PyLongObject *bh = NULL;
2660 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002661 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002662 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002663 Py_ssize_t shift; /* the number of digits we split off */
2664 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002665
Tim Peters5af4e6c2002-08-12 02:31:19 +00002666 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2667 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2668 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002669 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002670 * By picking X to be a power of 2, "*X" is just shifting, and it's
2671 * been reduced to 3 multiplies on numbers half the size.
2672 */
2673
Tim Petersfc07e562002-08-12 02:54:10 +00002674 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002675 * is largest.
2676 */
Tim Peters738eda72002-08-12 15:08:20 +00002677 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002678 t1 = a;
2679 a = b;
2680 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002681
2682 i = asize;
2683 asize = bsize;
2684 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002685 }
2686
2687 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002688 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2689 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002690 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002691 return _PyLong_New(0);
2692 else
2693 return x_mul(a, b);
2694 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002695
Tim Peters60004642002-08-12 22:01:34 +00002696 /* If a is small compared to b, splitting on b gives a degenerate
2697 * case with ah==0, and Karatsuba may be (even much) less efficient
2698 * than "grade school" then. However, we can still win, by viewing
2699 * b as a string of "big digits", each of width a->ob_size. That
2700 * leads to a sequence of balanced calls to k_mul.
2701 */
2702 if (2 * asize <= bsize)
2703 return k_lopsided_mul(a, b);
2704
Tim Petersd6974a52002-08-13 20:37:51 +00002705 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002706 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002707 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002708 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002709
Tim Peters0973b992004-08-29 22:16:50 +00002710 if (a == b) {
2711 bh = ah;
2712 bl = al;
2713 Py_INCREF(bh);
2714 Py_INCREF(bl);
2715 }
2716 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002717
Tim Petersd64c1de2002-08-12 17:36:03 +00002718 /* The plan:
2719 * 1. Allocate result space (asize + bsize digits: that's always
2720 * enough).
2721 * 2. Compute ah*bh, and copy into result at 2*shift.
2722 * 3. Compute al*bl, and copy into result at 0. Note that this
2723 * can't overlap with #2.
2724 * 4. Subtract al*bl from the result, starting at shift. This may
2725 * underflow (borrow out of the high digit), but we don't care:
2726 * we're effectively doing unsigned arithmetic mod
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002727 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
Tim Petersd64c1de2002-08-12 17:36:03 +00002728 * borrows and carries out of the high digit can be ignored.
2729 * 5. Subtract ah*bh from the result, starting at shift.
2730 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2731 * at shift.
2732 */
2733
2734 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002735 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002736 if (ret == NULL) goto fail;
2737#ifdef Py_DEBUG
2738 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002739 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002740#endif
Tim Peters44121a62002-08-12 06:17:58 +00002741
Tim Petersd64c1de2002-08-12 17:36:03 +00002742 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002743 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002744 assert(Py_SIZE(t1) >= 0);
2745 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002746 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimese93237d2007-12-19 02:37:44 +00002747 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002748
2749 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimese93237d2007-12-19 02:37:44 +00002750 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002751 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002752 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002753 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002754
Tim Petersd64c1de2002-08-12 17:36:03 +00002755 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002756 if ((t2 = k_mul(al, bl)) == NULL) {
2757 Py_DECREF(t1);
2758 goto fail;
2759 }
Christian Heimese93237d2007-12-19 02:37:44 +00002760 assert(Py_SIZE(t2) >= 0);
2761 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2762 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002763
2764 /* Zero out remaining digits. */
Christian Heimese93237d2007-12-19 02:37:44 +00002765 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002766 if (i)
Christian Heimese93237d2007-12-19 02:37:44 +00002767 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002768
Tim Petersd64c1de2002-08-12 17:36:03 +00002769 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2770 * because it's fresher in cache.
2771 */
Christian Heimese93237d2007-12-19 02:37:44 +00002772 i = Py_SIZE(ret) - shift; /* # digits after shift */
2773 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002774 Py_DECREF(t2);
2775
Christian Heimese93237d2007-12-19 02:37:44 +00002776 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002777 Py_DECREF(t1);
2778
Tim Petersd64c1de2002-08-12 17:36:03 +00002779 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002780 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2781 Py_DECREF(ah);
2782 Py_DECREF(al);
2783 ah = al = NULL;
2784
Tim Peters0973b992004-08-29 22:16:50 +00002785 if (a == b) {
2786 t2 = t1;
2787 Py_INCREF(t2);
2788 }
2789 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002790 Py_DECREF(t1);
2791 goto fail;
2792 }
2793 Py_DECREF(bh);
2794 Py_DECREF(bl);
2795 bh = bl = NULL;
2796
Tim Peters738eda72002-08-12 15:08:20 +00002797 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002798 Py_DECREF(t1);
2799 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002800 if (t3 == NULL) goto fail;
Christian Heimese93237d2007-12-19 02:37:44 +00002801 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002802
Tim Petersd6974a52002-08-13 20:37:51 +00002803 /* Add t3. It's not obvious why we can't run out of room here.
2804 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002805 */
Christian Heimese93237d2007-12-19 02:37:44 +00002806 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002807 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002808
Tim Peters5af4e6c2002-08-12 02:31:19 +00002809 return long_normalize(ret);
2810
2811 fail:
2812 Py_XDECREF(ret);
2813 Py_XDECREF(ah);
2814 Py_XDECREF(al);
2815 Py_XDECREF(bh);
2816 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002817 return NULL;
2818}
2819
Tim Petersd6974a52002-08-13 20:37:51 +00002820/* (*) Why adding t3 can't "run out of room" above.
2821
Tim Petersab86c2b2002-08-15 20:06:00 +00002822Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2823to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002824
Tim Petersab86c2b2002-08-15 20:06:00 +000028251. For any integer i, i = c(i/2) + f(i/2). In particular,
2826 bsize = c(bsize/2) + f(bsize/2).
28272. shift = f(bsize/2)
28283. asize <= bsize
28294. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2830 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002831
Tim Petersab86c2b2002-08-15 20:06:00 +00002832We allocated asize + bsize result digits, and add t3 into them at an offset
2833of shift. This leaves asize+bsize-shift allocated digit positions for t3
2834to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2835asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002836
Tim Petersab86c2b2002-08-15 20:06:00 +00002837bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2838at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002839
Tim Petersab86c2b2002-08-15 20:06:00 +00002840If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2841digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2842most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002843
Tim Petersab86c2b2002-08-15 20:06:00 +00002844The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002845
Tim Petersab86c2b2002-08-15 20:06:00 +00002846 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002847
Tim Petersab86c2b2002-08-15 20:06:00 +00002848and we have asize + c(bsize/2) available digit positions. We need to show
2849this is always enough. An instance of c(bsize/2) cancels out in both, so
2850the question reduces to whether asize digits is enough to hold
2851(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2852then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2853asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Christian Heimes7f39c9f2008-01-25 12:18:43 +00002854digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002855asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002856c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2857is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2858bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002859
Tim Peters48d52c02002-08-14 17:07:32 +00002860Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2861clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2862ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002863*/
2864
Tim Peters60004642002-08-12 22:01:34 +00002865/* b has at least twice the digits of a, and a is big enough that Karatsuba
2866 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2867 * of slices, each with a->ob_size digits, and multiply the slices by a,
2868 * one at a time. This gives k_mul balanced inputs to work with, and is
2869 * also cache-friendly (we compute one double-width slice of the result
2870 * at a time, then move on, never bactracking except for the helpful
2871 * single-width slice overlap between successive partial sums).
2872 */
2873static PyLongObject *
2874k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2875{
Christian Heimese93237d2007-12-19 02:37:44 +00002876 const Py_ssize_t asize = ABS(Py_SIZE(a));
2877 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002878 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002879 PyLongObject *ret;
2880 PyLongObject *bslice = NULL;
2881
2882 assert(asize > KARATSUBA_CUTOFF);
2883 assert(2 * asize <= bsize);
2884
2885 /* Allocate result space, and zero it out. */
2886 ret = _PyLong_New(asize + bsize);
2887 if (ret == NULL)
2888 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00002889 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002890
2891 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002892 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002893 if (bslice == NULL)
2894 goto fail;
2895
2896 nbdone = 0;
2897 while (bsize > 0) {
2898 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002899 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002900
2901 /* Multiply the next slice of b by a. */
2902 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2903 nbtouse * sizeof(digit));
Christian Heimese93237d2007-12-19 02:37:44 +00002904 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002905 product = k_mul(a, bslice);
2906 if (product == NULL)
2907 goto fail;
2908
2909 /* Add into result. */
Christian Heimese93237d2007-12-19 02:37:44 +00002910 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2911 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002912 Py_DECREF(product);
2913
2914 bsize -= nbtouse;
2915 nbdone += nbtouse;
2916 }
2917
2918 Py_DECREF(bslice);
2919 return long_normalize(ret);
2920
2921 fail:
2922 Py_DECREF(ret);
2923 Py_XDECREF(bslice);
2924 return NULL;
2925}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002926
2927static PyObject *
2928long_mul(PyLongObject *v, PyLongObject *w)
2929{
2930 PyLongObject *a, *b, *z;
2931
2932 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002933 Py_INCREF(Py_NotImplemented);
2934 return Py_NotImplemented;
2935 }
2936
Tim Petersd64c1de2002-08-12 17:36:03 +00002937 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002938 /* Negate if exactly one of the inputs is negative. */
2939 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002940 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002941 Py_DECREF(a);
2942 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002943 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002944}
2945
Guido van Rossume32e0141992-01-19 16:31:05 +00002946/* The / and % operators are now defined in terms of divmod().
2947 The expression a mod b has the value a - b*floor(a/b).
2948 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002949 |a| by |b|, with the sign of a. This is also expressed
2950 as a - b*trunc(a/b), if trunc truncates towards zero.
2951 Some examples:
2952 a b a rem b a mod b
2953 13 10 3 3
2954 -13 10 -3 7
2955 13 -10 3 -7
2956 -13 -10 -3 -3
2957 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002958 have different signs. We then subtract one from the 'div'
2959 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002960
Tim Peters47e52ee2004-08-30 02:44:38 +00002961/* Compute
2962 * *pdiv, *pmod = divmod(v, w)
2963 * NULL can be passed for pdiv or pmod, in which case that part of
2964 * the result is simply thrown away. The caller owns a reference to
2965 * each of these it requests (does not pass NULL for).
2966 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002967static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002968l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002969 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002970{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002971 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002972
Guido van Rossume32e0141992-01-19 16:31:05 +00002973 if (long_divrem(v, w, &div, &mod) < 0)
2974 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00002975 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2976 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002977 PyLongObject *temp;
2978 PyLongObject *one;
2979 temp = (PyLongObject *) long_add(mod, w);
2980 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002981 mod = temp;
2982 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002983 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002984 return -1;
2985 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002986 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002987 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002988 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2989 Py_DECREF(mod);
2990 Py_DECREF(div);
2991 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002992 return -1;
2993 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002994 Py_DECREF(one);
2995 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002996 div = temp;
2997 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002998 if (pdiv != NULL)
2999 *pdiv = div;
3000 else
3001 Py_DECREF(div);
3002
3003 if (pmod != NULL)
3004 *pmod = mod;
3005 else
3006 Py_DECREF(mod);
3007
Guido van Rossume32e0141992-01-19 16:31:05 +00003008 return 0;
3009}
3010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003011static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003012long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00003013{
Tim Peters47e52ee2004-08-30 02:44:38 +00003014 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003015
3016 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00003017 if (l_divmod(a, b, &div, NULL) < 0)
3018 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003019 Py_DECREF(a);
3020 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003021 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00003022}
3023
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003024static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00003025long_classic_div(PyObject *v, PyObject *w)
3026{
Tim Peters47e52ee2004-08-30 02:44:38 +00003027 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00003028
3029 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00003030 if (Py_DivisionWarningFlag &&
3031 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
3032 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00003033 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00003034 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00003035 Py_DECREF(a);
3036 Py_DECREF(b);
3037 return (PyObject *)div;
3038}
3039
3040static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00003041long_true_divide(PyObject *v, PyObject *w)
3042{
Tim Peterse2a60002001-09-04 06:17:36 +00003043 PyLongObject *a, *b;
3044 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00003045 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00003046
3047 CONVERT_BINOP(v, w, &a, &b);
3048 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
3049 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00003050 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
3051 Py_DECREF(a);
3052 Py_DECREF(b);
3053 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00003054 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00003055 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
3056 but should really be set correctly after sucessful calls to
3057 _PyLong_AsScaledDouble() */
3058 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00003059
3060 if (bd == 0.0) {
3061 PyErr_SetString(PyExc_ZeroDivisionError,
3062 "long division or modulo by zero");
3063 return NULL;
3064 }
3065
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003066 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00003067 ad /= bd; /* overflow/underflow impossible here */
3068 aexp -= bexp;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003069 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00003070 goto overflow;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003071 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00003072 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00003073 errno = 0;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003074 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00003075 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00003076 goto overflow;
3077 return PyFloat_FromDouble(ad);
3078
3079overflow:
3080 PyErr_SetString(PyExc_OverflowError,
3081 "long/long too large for a float");
3082 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003083
Tim Peters20dab9f2001-09-04 05:31:47 +00003084}
3085
3086static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003087long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00003088{
Tim Peters47e52ee2004-08-30 02:44:38 +00003089 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003090
3091 CONVERT_BINOP(v, w, &a, &b);
3092
Tim Peters47e52ee2004-08-30 02:44:38 +00003093 if (l_divmod(a, b, NULL, &mod) < 0)
3094 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003095 Py_DECREF(a);
3096 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003097 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00003098}
3099
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003100static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003101long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003102{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003103 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003104 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003105
3106 CONVERT_BINOP(v, w, &a, &b);
3107
3108 if (l_divmod(a, b, &div, &mod) < 0) {
3109 Py_DECREF(a);
3110 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003111 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003112 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003113 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003114 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003115 PyTuple_SetItem(z, 0, (PyObject *) div);
3116 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003117 }
3118 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003119 Py_DECREF(div);
3120 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003121 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003122 Py_DECREF(a);
3123 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003124 return z;
3125}
3126
Tim Peters47e52ee2004-08-30 02:44:38 +00003127/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003128static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003129long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003130{
Tim Peters47e52ee2004-08-30 02:44:38 +00003131 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
3132 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003133
Tim Peters47e52ee2004-08-30 02:44:38 +00003134 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00003135 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00003136 PyLongObject *temp = NULL;
3137
3138 /* 5-ary values. If the exponent is large enough, table is
3139 * precomputed so that table[i] == a**i % c for i in range(32).
3140 */
3141 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3142 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
3143
3144 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003145 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00003146 if (PyLong_Check(x)) {
3147 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003148 Py_INCREF(x);
3149 }
Tim Petersde7990b2005-07-17 23:45:23 +00003150 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003151 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00003152 if (c == NULL)
3153 goto Error;
3154 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003155 else if (x == Py_None)
3156 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003157 else {
3158 Py_DECREF(a);
3159 Py_DECREF(b);
3160 Py_INCREF(Py_NotImplemented);
3161 return Py_NotImplemented;
3162 }
Tim Peters4c483c42001-09-05 06:24:58 +00003163
Christian Heimese93237d2007-12-19 02:37:44 +00003164 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00003165 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00003166 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00003167 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00003168 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00003169 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003170 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00003171 /* else return a float. This works because we know
3172 that this calls float_pow() which converts its
3173 arguments to double. */
3174 Py_DECREF(a);
3175 Py_DECREF(b);
3176 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003177 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003178 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003179
3180 if (c) {
3181 /* if modulus == 0:
3182 raise ValueError() */
Christian Heimese93237d2007-12-19 02:37:44 +00003183 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003184 PyErr_SetString(PyExc_ValueError,
3185 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00003186 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003187 }
3188
3189 /* if modulus < 0:
3190 negativeOutput = True
3191 modulus = -modulus */
Christian Heimese93237d2007-12-19 02:37:44 +00003192 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003193 negativeOutput = 1;
3194 temp = (PyLongObject *)_PyLong_Copy(c);
3195 if (temp == NULL)
3196 goto Error;
3197 Py_DECREF(c);
3198 c = temp;
3199 temp = NULL;
3200 c->ob_size = - c->ob_size;
3201 }
3202
3203 /* if modulus == 1:
3204 return 0 */
Christian Heimese93237d2007-12-19 02:37:44 +00003205 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003206 z = (PyLongObject *)PyLong_FromLong(0L);
3207 goto Done;
3208 }
3209
3210 /* if base < 0:
3211 base = base % modulus
3212 Having the base positive just makes things easier. */
Christian Heimese93237d2007-12-19 02:37:44 +00003213 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003214 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00003215 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003216 Py_DECREF(a);
3217 a = temp;
3218 temp = NULL;
3219 }
3220 }
3221
3222 /* At this point a, b, and c are guaranteed non-negative UNLESS
3223 c is NULL, in which case a may be negative. */
3224
3225 z = (PyLongObject *)PyLong_FromLong(1L);
3226 if (z == NULL)
3227 goto Error;
3228
3229 /* Perform a modular reduction, X = X % c, but leave X alone if c
3230 * is NULL.
3231 */
3232#define REDUCE(X) \
3233 if (c != NULL) { \
3234 if (l_divmod(X, c, NULL, &temp) < 0) \
3235 goto Error; \
3236 Py_XDECREF(X); \
3237 X = temp; \
3238 temp = NULL; \
3239 }
3240
3241 /* Multiply two values, then reduce the result:
3242 result = X*Y % c. If c is NULL, skip the mod. */
3243#define MULT(X, Y, result) \
3244{ \
3245 temp = (PyLongObject *)long_mul(X, Y); \
3246 if (temp == NULL) \
3247 goto Error; \
3248 Py_XDECREF(result); \
3249 result = temp; \
3250 temp = NULL; \
3251 REDUCE(result) \
3252}
3253
Christian Heimese93237d2007-12-19 02:37:44 +00003254 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003255 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
3256 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimese93237d2007-12-19 02:37:44 +00003257 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003258 digit bi = b->ob_digit[i];
3259
Mark Dickinsonbcf6b182009-02-15 15:48:39 +00003260 for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003261 MULT(z, z, z)
3262 if (bi & j)
3263 MULT(z, a, z)
3264 }
3265 }
3266 }
3267 else {
3268 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
3269 Py_INCREF(z); /* still holds 1L */
3270 table[0] = z;
3271 for (i = 1; i < 32; ++i)
3272 MULT(table[i-1], a, table[i])
3273
Christian Heimese93237d2007-12-19 02:37:44 +00003274 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003275 const digit bi = b->ob_digit[i];
3276
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003277 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003278 const int index = (bi >> j) & 0x1f;
3279 for (k = 0; k < 5; ++k)
3280 MULT(z, z, z)
3281 if (index)
3282 MULT(z, table[index], z)
3283 }
3284 }
3285 }
3286
Christian Heimese93237d2007-12-19 02:37:44 +00003287 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003288 temp = (PyLongObject *)long_sub(z, c);
3289 if (temp == NULL)
3290 goto Error;
3291 Py_DECREF(z);
3292 z = temp;
3293 temp = NULL;
3294 }
3295 goto Done;
3296
3297 Error:
3298 if (z != NULL) {
3299 Py_DECREF(z);
3300 z = NULL;
3301 }
3302 /* fall through */
3303 Done:
Christian Heimese93237d2007-12-19 02:37:44 +00003304 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003305 for (i = 0; i < 32; ++i)
3306 Py_XDECREF(table[i]);
3307 }
Tim Petersde7990b2005-07-17 23:45:23 +00003308 Py_DECREF(a);
3309 Py_DECREF(b);
3310 Py_XDECREF(c);
3311 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003312 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003313}
3314
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003315static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003316long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003317{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003318 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003319 PyLongObject *x;
3320 PyLongObject *w;
3321 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003322 if (w == NULL)
3323 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003324 x = (PyLongObject *) long_add(v, w);
3325 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003326 if (x == NULL)
3327 return NULL;
Christian Heimese93237d2007-12-19 02:37:44 +00003328 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003329 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003330}
3331
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003332static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003333long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003334{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003335 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00003336 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003337 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003338 Py_INCREF(v);
3339 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003340 }
Tim Peters69c2de32001-09-11 22:31:33 +00003341 z = (PyLongObject *)_PyLong_Copy(v);
3342 if (z != NULL)
3343 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003344 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003345}
3346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003347static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003348long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003349{
3350 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003351 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00003352 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003353 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003354}
3355
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003356static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003357long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003358{
Christian Heimese93237d2007-12-19 02:37:44 +00003359 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003360}
3361
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003362static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003363long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003364{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003365 PyLongObject *a, *b;
3366 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003367 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003368 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003369 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003370
Neil Schemenauerba872e22001-01-04 01:46:03 +00003371 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
3372
Christian Heimese93237d2007-12-19 02:37:44 +00003373 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003374 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003375 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003376 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003377 if (a1 == NULL)
3378 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003379 a2 = (PyLongObject *) long_rshift(a1, b);
3380 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003381 if (a2 == NULL)
3382 goto rshift_error;
3383 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003384 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003385 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003386 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00003387
Neil Schemenauerba872e22001-01-04 01:46:03 +00003388 shiftby = PyLong_AsLong((PyObject *)b);
3389 if (shiftby == -1L && PyErr_Occurred())
3390 goto rshift_error;
3391 if (shiftby < 0) {
3392 PyErr_SetString(PyExc_ValueError,
3393 "negative shift count");
3394 goto rshift_error;
3395 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003396 wordshift = shiftby / PyLong_SHIFT;
Christian Heimese93237d2007-12-19 02:37:44 +00003397 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003398 if (newsize <= 0) {
3399 z = _PyLong_New(0);
3400 Py_DECREF(a);
3401 Py_DECREF(b);
3402 return (PyObject *)z;
3403 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003404 loshift = shiftby % PyLong_SHIFT;
3405 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003406 lomask = ((digit)1 << hishift) - 1;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003407 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003408 z = _PyLong_New(newsize);
3409 if (z == NULL)
3410 goto rshift_error;
Christian Heimese93237d2007-12-19 02:37:44 +00003411 if (Py_SIZE(a) < 0)
3412 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003413 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3414 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3415 if (i+1 < newsize)
3416 z->ob_digit[i] |=
3417 (a->ob_digit[j+1] << hishift) & himask;
3418 }
3419 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003420 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003421rshift_error:
3422 Py_DECREF(a);
3423 Py_DECREF(b);
3424 return (PyObject *) z;
3425
Guido van Rossumc6913e71991-11-19 20:26:46 +00003426}
3427
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003428static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003429long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003430{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003431 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003432 PyLongObject *a, *b;
3433 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003434 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003435 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003436 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003437
Neil Schemenauerba872e22001-01-04 01:46:03 +00003438 CONVERT_BINOP(v, w, &a, &b);
3439
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003440 shiftby = PyLong_AsLong((PyObject *)b);
3441 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003442 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003443 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003444 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003445 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003446 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003447 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003448 PyErr_SetString(PyExc_ValueError,
3449 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003450 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003451 }
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003452 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3453 wordshift = (int)shiftby / PyLong_SHIFT;
3454 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003455
3456 oldsize = ABS(a->ob_size);
3457 newsize = oldsize + wordshift;
3458 if (remshift)
3459 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003460 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003461 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003462 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003463 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003464 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003465 for (i = 0; i < wordshift; i++)
3466 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003467 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003468 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003469 accum |= (twodigits)a->ob_digit[j] << remshift;
Christian Heimes7f39c9f2008-01-25 12:18:43 +00003470 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3471 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003472 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003473 if (remshift)
3474 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003475 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003476 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003477 z = long_normalize(z);
3478lshift_error:
3479 Py_DECREF(a);
3480 Py_DECREF(b);
3481 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003482}
3483
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003484/* Compute two's complement of digit vector a[0:m], writing result to
3485 z[0:m]. The digit vector a need not be normalized, but should not
3486 be entirely zero. a and z may point to the same digit vector. */
3487
3488static void
3489v_complement(digit *z, digit *a, Py_ssize_t m)
3490{
3491 Py_ssize_t i;
3492 digit carry = 1;
3493 for (i = 0; i < m; ++i) {
3494 carry += a[i] ^ PyLong_MASK;
3495 z[i] = carry & PyLong_MASK;
3496 carry >>= PyLong_SHIFT;
3497 }
3498 assert(carry == 0);
3499}
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003500
3501/* Bitwise and/xor/or operations */
3502
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003503static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003504long_bitwise(PyLongObject *a,
3505 int op, /* '&', '|', '^' */
3506 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003507{
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003508 int nega, negb, negz;
Mark Dickinsonbcf6b182009-02-15 15:48:39 +00003509 Py_ssize_t size_a, size_b, size_z, i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003510 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003511
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003512 /* Bitwise operations for negative numbers operate as though
3513 on a two's complement representation. So convert arguments
3514 from sign-magnitude to two's complement, and convert the
3515 result back to sign-magnitude at the end. */
3516
3517 /* If a is negative, replace it by its two's complement. */
3518 size_a = ABS(Py_SIZE(a));
3519 nega = Py_SIZE(a) < 0;
3520 if (nega) {
3521 z = _PyLong_New(size_a);
3522 if (z == NULL)
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003523 return NULL;
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003524 v_complement(z->ob_digit, a->ob_digit, size_a);
3525 a = z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003526 }
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003527 else
3528 /* Keep reference count consistent. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003529 Py_INCREF(a);
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003530
3531 /* Same for b. */
3532 size_b = ABS(Py_SIZE(b));
3533 negb = Py_SIZE(b) < 0;
3534 if (negb) {
3535 z = _PyLong_New(size_b);
3536 if (z == NULL) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003537 Py_DECREF(a);
3538 return NULL;
3539 }
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003540 v_complement(z->ob_digit, b->ob_digit, size_b);
3541 b = z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003542 }
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003543 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003544 Py_INCREF(b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003545
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003546 /* Swap a and b if necessary to ensure size_a >= size_b. */
3547 if (size_a < size_b) {
3548 z = a; a = b; b = z;
3549 size_z = size_a; size_a = size_b; size_b = size_z;
3550 negz = nega; nega = negb; negb = negz;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003551 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003552
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003553 /* JRH: The original logic here was to allocate the result value (z)
3554 as the longer of the two operands. However, there are some cases
3555 where the result is guaranteed to be shorter than that: AND of two
3556 positives, OR of two negatives: use the shorter number. AND with
3557 mixed signs: use the positive number. OR with mixed signs: use the
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003558 negative number.
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003559 */
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003560 switch (op) {
3561 case '^':
3562 negz = nega ^ negb;
3563 size_z = size_a;
3564 break;
3565 case '&':
3566 negz = nega & negb;
3567 size_z = negb ? size_a : size_b;
3568 break;
3569 case '|':
3570 negz = nega | negb;
3571 size_z = negb ? size_b : size_a;
3572 break;
3573 default:
3574 PyErr_BadArgument();
3575 return NULL;
3576 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003577
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003578 /* We allow an extra digit if z is negative, to make sure that
3579 the final two's complement of z doesn't overflow. */
3580 z = _PyLong_New(size_z + negz);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003581 if (z == NULL) {
Neal Norwitzef02b9e2006-07-16 02:00:32 +00003582 Py_DECREF(a);
3583 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003584 return NULL;
3585 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003586
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003587 /* Compute digits for overlap of a and b. */
3588 switch(op) {
3589 case '&':
3590 for (i = 0; i < size_b; ++i)
3591 z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i];
3592 break;
3593 case '|':
3594 for (i = 0; i < size_b; ++i)
3595 z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i];
3596 break;
3597 case '^':
3598 for (i = 0; i < size_b; ++i)
3599 z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i];
3600 break;
3601 default:
3602 PyErr_BadArgument();
3603 return NULL;
3604 }
3605
3606 /* Copy any remaining digits of a, inverting if necessary. */
3607 if (op == '^' && negb)
3608 for (; i < size_z; ++i)
3609 z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK;
3610 else if (i < size_z)
3611 memcpy(&z->ob_digit[i], &a->ob_digit[i],
3612 (size_z-i)*sizeof(digit));
3613
3614 /* Complement result if negative. */
3615 if (negz) {
3616 Py_SIZE(z) = -(Py_SIZE(z));
3617 z->ob_digit[size_z] = PyLong_MASK;
3618 v_complement(z->ob_digit, z->ob_digit, size_z+1);
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003619 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003620
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003621 Py_DECREF(a);
3622 Py_DECREF(b);
Mark Dickinson8d87dc02009-10-25 20:39:06 +00003623 return (PyObject *)long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003624}
3625
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003626static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003627long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003628{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003629 PyLongObject *a, *b;
3630 PyObject *c;
3631 CONVERT_BINOP(v, w, &a, &b);
3632 c = long_bitwise(a, '&', b);
3633 Py_DECREF(a);
3634 Py_DECREF(b);
3635 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003636}
3637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003638static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003639long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003640{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003641 PyLongObject *a, *b;
3642 PyObject *c;
3643 CONVERT_BINOP(v, w, &a, &b);
3644 c = long_bitwise(a, '^', b);
3645 Py_DECREF(a);
3646 Py_DECREF(b);
3647 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003648}
3649
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003650static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003651long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003652{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003653 PyLongObject *a, *b;
3654 PyObject *c;
3655 CONVERT_BINOP(v, w, &a, &b);
3656 c = long_bitwise(a, '|', b);
3657 Py_DECREF(a);
3658 Py_DECREF(b);
3659 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003660}
3661
Guido van Rossum234f9421993-06-17 12:35:49 +00003662static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003663long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00003664{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003665 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00003666 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Georg Brandlc02e1312007-04-11 17:16:24 +00003667 if (*pw == NULL)
3668 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003669 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00003670 return 0;
3671 }
Guido van Rossum1952e382001-09-19 01:25:16 +00003672 else if (PyLong_Check(*pw)) {
3673 Py_INCREF(*pv);
3674 Py_INCREF(*pw);
3675 return 0;
3676 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00003677 return 1; /* Can't do it */
3678}
3679
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003680static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003681long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003682{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003683 if (PyLong_CheckExact(v))
3684 Py_INCREF(v);
3685 else
3686 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003687 return v;
3688}
3689
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003690static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003691long_int(PyObject *v)
3692{
3693 long x;
3694 x = PyLong_AsLong(v);
3695 if (PyErr_Occurred()) {
3696 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3697 PyErr_Clear();
3698 if (PyLong_CheckExact(v)) {
3699 Py_INCREF(v);
3700 return v;
3701 }
3702 else
3703 return _PyLong_Copy((PyLongObject *)v);
3704 }
3705 else
3706 return NULL;
3707 }
3708 return PyInt_FromLong(x);
3709}
3710
3711static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003712long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003713{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003714 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003715 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003716 if (result == -1.0 && PyErr_Occurred())
3717 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003718 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003719}
3720
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003721static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003722long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003723{
Eric Smith5e527eb2008-02-10 01:36:53 +00003724 return _PyLong_Format(v, 8, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003725}
3726
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003727static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003728long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003729{
Eric Smith5e527eb2008-02-10 01:36:53 +00003730 return _PyLong_Format(v, 16, 1, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003731}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003732
3733static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003734long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003735
Tim Peters6d6c1a32001-08-02 04:15:00 +00003736static PyObject *
3737long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3738{
3739 PyObject *x = NULL;
3740 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003741 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003742
Guido van Rossumbef14172001-08-29 15:47:46 +00003743 if (type != &PyLong_Type)
3744 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003745 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3746 &x, &base))
3747 return NULL;
3748 if (x == NULL)
3749 return PyLong_FromLong(0L);
3750 if (base == -909)
3751 return PyNumber_Long(x);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003752 else if (PyString_Check(x)) {
Georg Brandl00cd8182007-03-06 18:41:12 +00003753 /* Since PyLong_FromString doesn't have a length parameter,
3754 * check here for possible NULs in the string. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003755 char *string = PyString_AS_STRING(x);
Mark Dickinson2ffb26f2009-02-15 10:13:41 +00003756 if (strlen(string) != (size_t)PyString_Size(x)) {
Georg Brandl00cd8182007-03-06 18:41:12 +00003757 /* create a repr() of the input string,
3758 * just like PyLong_FromString does. */
3759 PyObject *srepr;
3760 srepr = PyObject_Repr(x);
3761 if (srepr == NULL)
3762 return NULL;
3763 PyErr_Format(PyExc_ValueError,
3764 "invalid literal for long() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003765 base, PyString_AS_STRING(srepr));
Georg Brandl00cd8182007-03-06 18:41:12 +00003766 Py_DECREF(srepr);
3767 return NULL;
3768 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003769 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Georg Brandl00cd8182007-03-06 18:41:12 +00003770 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003771#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003772 else if (PyUnicode_Check(x))
3773 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3774 PyUnicode_GET_SIZE(x),
3775 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003776#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 else {
3778 PyErr_SetString(PyExc_TypeError,
3779 "long() can't convert non-string with explicit base");
3780 return NULL;
3781 }
3782}
3783
Guido van Rossumbef14172001-08-29 15:47:46 +00003784/* Wimpy, slow approach to tp_new calls for subtypes of long:
3785 first create a regular long from whatever arguments we got,
3786 then allocate a subtype instance and initialize it from
3787 the regular long. The regular long is then thrown away.
3788*/
3789static PyObject *
3790long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3791{
Anthony Baxter377be112006-04-11 06:54:30 +00003792 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003793 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003794
3795 assert(PyType_IsSubtype(type, &PyLong_Type));
3796 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3797 if (tmp == NULL)
3798 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003799 assert(PyLong_CheckExact(tmp));
Christian Heimese93237d2007-12-19 02:37:44 +00003800 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003801 if (n < 0)
3802 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003803 newobj = (PyLongObject *)type->tp_alloc(type, n);
3804 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003805 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003806 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003807 }
Anthony Baxter377be112006-04-11 06:54:30 +00003808 assert(PyLong_Check(newobj));
Christian Heimese93237d2007-12-19 02:37:44 +00003809 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003810 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003811 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003812 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003813 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003814}
3815
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003816static PyObject *
3817long_getnewargs(PyLongObject *v)
3818{
3819 return Py_BuildValue("(N)", _PyLong_Copy(v));
3820}
3821
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003822static PyObject *
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003823long_get0(PyLongObject *v, void *context) {
3824 return PyLong_FromLong(0L);
3825}
3826
3827static PyObject *
3828long_get1(PyLongObject *v, void *context) {
3829 return PyLong_FromLong(1L);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003830}
3831
Eric Smitha9f7d622008-02-17 19:46:49 +00003832static PyObject *
3833long__format__(PyObject *self, PyObject *args)
3834{
3835 PyObject *format_spec;
3836
3837 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3838 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00003839 if (PyBytes_Check(format_spec))
Eric Smithdc13b792008-05-30 18:10:04 +00003840 return _PyLong_FormatAdvanced(self,
3841 PyBytes_AS_STRING(format_spec),
3842 PyBytes_GET_SIZE(format_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00003843 if (PyUnicode_Check(format_spec)) {
3844 /* Convert format_spec to a str */
Eric Smithdc13b792008-05-30 18:10:04 +00003845 PyObject *result;
3846 PyObject *str_spec = PyObject_Str(format_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00003847
Eric Smithdc13b792008-05-30 18:10:04 +00003848 if (str_spec == NULL)
3849 return NULL;
Eric Smitha9f7d622008-02-17 19:46:49 +00003850
Eric Smithdc13b792008-05-30 18:10:04 +00003851 result = _PyLong_FormatAdvanced(self,
3852 PyBytes_AS_STRING(str_spec),
3853 PyBytes_GET_SIZE(str_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00003854
Eric Smithdc13b792008-05-30 18:10:04 +00003855 Py_DECREF(str_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00003856 return result;
3857 }
3858 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
3859 return NULL;
3860}
3861
Robert Schuppenies51df0642008-06-01 16:16:17 +00003862static PyObject *
3863long_sizeof(PyLongObject *v)
3864{
3865 Py_ssize_t res;
3866
Mark Dickinson2ffb26f2009-02-15 10:13:41 +00003867 res = v->ob_type->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit);
Robert Schuppenies51df0642008-06-01 16:16:17 +00003868 return PyInt_FromSsize_t(res);
3869}
3870
Mark Dickinson1a707982008-12-17 16:14:37 +00003871static PyObject *
3872long_bit_length(PyLongObject *v)
3873{
3874 PyLongObject *result, *x, *y;
3875 Py_ssize_t ndigits, msd_bits = 0;
3876 digit msd;
3877
3878 assert(v != NULL);
3879 assert(PyLong_Check(v));
3880
3881 ndigits = ABS(Py_SIZE(v));
3882 if (ndigits == 0)
3883 return PyInt_FromLong(0);
3884
3885 msd = v->ob_digit[ndigits-1];
3886 while (msd >= 32) {
3887 msd_bits += 6;
3888 msd >>= 6;
3889 }
3890 msd_bits += (long)(BitLengthTable[msd]);
3891
3892 if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
3893 return PyInt_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
3894
3895 /* expression above may overflow; use Python integers instead */
3896 result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
3897 if (result == NULL)
3898 return NULL;
3899 x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
3900 if (x == NULL)
3901 goto error;
3902 y = (PyLongObject *)long_mul(result, x);
3903 Py_DECREF(x);
3904 if (y == NULL)
3905 goto error;
3906 Py_DECREF(result);
3907 result = y;
3908
3909 x = (PyLongObject *)PyLong_FromLong(msd_bits);
3910 if (x == NULL)
3911 goto error;
3912 y = (PyLongObject *)long_add(result, x);
3913 Py_DECREF(x);
3914 if (y == NULL)
3915 goto error;
3916 Py_DECREF(result);
3917 result = y;
3918
3919 return (PyObject *)result;
3920
3921error:
3922 Py_DECREF(result);
3923 return NULL;
3924}
3925
3926PyDoc_STRVAR(long_bit_length_doc,
3927"long.bit_length() -> int or long\n\
3928\n\
3929Number of bits necessary to represent self in binary.\n\
3930>>> bin(37L)\n\
3931'0b100101'\n\
3932>>> (37L).bit_length()\n\
39336");
3934
Christian Heimes6f341092008-04-18 23:13:07 +00003935#if 0
3936static PyObject *
3937long_is_finite(PyObject *v)
3938{
3939 Py_RETURN_TRUE;
3940}
3941#endif
3942
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003943static PyMethodDef long_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003944 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3945 "Returns self, the complex conjugate of any long."},
Mark Dickinson1a707982008-12-17 16:14:37 +00003946 {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS,
3947 long_bit_length_doc},
Christian Heimes6f341092008-04-18 23:13:07 +00003948#if 0
3949 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
3950 "Returns always True."},
3951#endif
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003952 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3953 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003954 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00003955 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
Robert Schuppenies51df0642008-06-01 16:16:17 +00003956 {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
Georg Brandl7a6de8b2008-06-01 16:42:16 +00003957 "Returns size in memory, in bytes"},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003958 {NULL, NULL} /* sentinel */
3959};
3960
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003961static PyGetSetDef long_getset[] = {
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003962 {"real",
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003963 (getter)long_long, (setter)NULL,
3964 "the real part of a complex number",
3965 NULL},
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003966 {"imag",
3967 (getter)long_get0, (setter)NULL,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003968 "the imaginary part of a complex number",
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003969 NULL},
3970 {"numerator",
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003971 (getter)long_long, (setter)NULL,
3972 "the numerator of a rational number in lowest terms",
3973 NULL},
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003974 {"denominator",
3975 (getter)long_get1, (setter)NULL,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003976 "the denominator of a rational number in lowest terms",
Mark Dickinsond4b5c982009-05-02 17:55:01 +00003977 NULL},
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003978 {NULL} /* Sentinel */
3979};
3980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003981PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003982"long(x[, base]) -> integer\n\
3983\n\
3984Convert a string or number to a long integer, if possible. A floating\n\
3985point argument will be truncated towards zero (this does not include a\n\
3986string representation of a floating point number!) When converting a\n\
3987string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003989
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003990static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003991 (binaryfunc) long_add, /*nb_add*/
3992 (binaryfunc) long_sub, /*nb_subtract*/
3993 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003994 long_classic_div, /*nb_divide*/
3995 long_mod, /*nb_remainder*/
3996 long_divmod, /*nb_divmod*/
3997 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003998 (unaryfunc) long_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00003999 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00004000 (unaryfunc) long_abs, /*tp_absolute*/
4001 (inquiry) long_nonzero, /*tp_nonzero*/
4002 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00004003 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00004004 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00004005 long_and, /*nb_and*/
4006 long_xor, /*nb_xor*/
4007 long_or, /*nb_or*/
4008 long_coerce, /*nb_coerce*/
4009 long_int, /*nb_int*/
4010 long_long, /*nb_long*/
4011 long_float, /*nb_float*/
4012 long_oct, /*nb_oct*/
4013 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00004014 0, /* nb_inplace_add */
4015 0, /* nb_inplace_subtract */
4016 0, /* nb_inplace_multiply */
4017 0, /* nb_inplace_divide */
4018 0, /* nb_inplace_remainder */
4019 0, /* nb_inplace_power */
4020 0, /* nb_inplace_lshift */
4021 0, /* nb_inplace_rshift */
4022 0, /* nb_inplace_and */
4023 0, /* nb_inplace_xor */
4024 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00004025 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00004026 long_true_divide, /* nb_true_divide */
4027 0, /* nb_inplace_floor_divide */
4028 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004029 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004030};
4031
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004032PyTypeObject PyLong_Type = {
4033 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004034 0, /* ob_size */
4035 "long", /* tp_name */
Mark Dickinson2ffb26f2009-02-15 10:13:41 +00004036 offsetof(PyLongObject, ob_digit), /* tp_basicsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004037 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00004038 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004039 0, /* tp_print */
4040 0, /* tp_getattr */
4041 0, /* tp_setattr */
4042 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00004043 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004044 &long_as_number, /* tp_as_number */
4045 0, /* tp_as_sequence */
4046 0, /* tp_as_mapping */
4047 (hashfunc)long_hash, /* tp_hash */
4048 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00004049 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004050 PyObject_GenericGetAttr, /* tp_getattro */
4051 0, /* tp_setattro */
4052 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00004053 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00004054 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004055 long_doc, /* tp_doc */
4056 0, /* tp_traverse */
4057 0, /* tp_clear */
4058 0, /* tp_richcompare */
4059 0, /* tp_weaklistoffset */
4060 0, /* tp_iter */
4061 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00004062 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004063 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00004064 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004065 0, /* tp_base */
4066 0, /* tp_dict */
4067 0, /* tp_descr_get */
4068 0, /* tp_descr_set */
4069 0, /* tp_dictoffset */
4070 0, /* tp_init */
4071 0, /* tp_alloc */
4072 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00004073 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004074};
Mark Dickinsonefc82f72009-03-20 15:51:55 +00004075
4076static PyTypeObject Long_InfoType;
4077
4078PyDoc_STRVAR(long_info__doc__,
4079"sys.long_info\n\
4080\n\
4081A struct sequence that holds information about Python's\n\
4082internal representation of integers. The attributes are read only.");
4083
4084static PyStructSequence_Field long_info_fields[] = {
4085 {"bits_per_digit", "size of a digit in bits"},
4086 {"sizeof_digit", "size in bytes of the C type used to "
4087 "represent a digit"},
4088 {NULL, NULL}
4089};
4090
4091static PyStructSequence_Desc long_info_desc = {
4092 "sys.long_info", /* name */
4093 long_info__doc__, /* doc */
4094 long_info_fields, /* fields */
4095 2 /* number of fields */
4096};
4097
4098PyObject *
4099PyLong_GetInfo(void)
4100{
4101 PyObject* long_info;
4102 int field = 0;
4103 long_info = PyStructSequence_New(&Long_InfoType);
4104 if (long_info == NULL)
4105 return NULL;
Mark Dickinson48e3fd22009-04-02 18:39:37 +00004106 PyStructSequence_SET_ITEM(long_info, field++,
4107 PyInt_FromLong(PyLong_SHIFT));
4108 PyStructSequence_SET_ITEM(long_info, field++,
4109 PyInt_FromLong(sizeof(digit)));
Mark Dickinsonefc82f72009-03-20 15:51:55 +00004110 if (PyErr_Occurred()) {
4111 Py_CLEAR(long_info);
4112 return NULL;
4113 }
4114 return long_info;
4115}
4116
4117int
4118_PyLong_Init(void)
4119{
4120 /* initialize long_info */
4121 if (Long_InfoType.tp_name == 0)
4122 PyStructSequence_InitType(&Long_InfoType, &long_info_desc);
4123 return 1;
4124}