blob: d2557dfa7bb93368b426a58a90c3c730493591ac [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/* Long (arbitrary precision) integer object implementation */
2
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003/* XXX The functional organization of this file is terrible */
4
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00006#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Eric Smith8c663262007-08-25 02:26:07 +00008#include "formatter_unicode.h"
9
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Guido van Rossumddefaf32007-01-14 03:31:43 +000012#ifndef NSMALLPOSINTS
13#define NSMALLPOSINTS 257
14#endif
15#ifndef NSMALLNEGINTS
16#define NSMALLNEGINTS 5
17#endif
18#if NSMALLNEGINTS + NSMALLPOSINTS > 0
19/* Small integers are preallocated in this array so that they
20 can be shared.
21 The integers that are preallocated are those in the range
22 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
23*/
24static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
25#ifdef COUNT_ALLOCS
26int quick_int_allocs, quick_neg_int_allocs;
27#endif
28
Guido van Rossum7eaf8222007-06-18 17:58:50 +000029static PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +000030get_small_int(int ival)
31{
32 PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
33 Py_INCREF(v);
34#ifdef COUNT_ALLOCS
35 if (ival >= 0)
36 quick_int_allocs++;
37 else
38 quick_neg_int_allocs++;
39#endif
40 return v;
41}
42#define CHECK_SMALL_INT(ival) \
43 do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
44 return get_small_int(ival); \
45 } while(0)
46
47#else
48#define CHECK_SMALL_INT(ival)
49#endif
50
Christian Heimes90aa7642007-12-19 02:45:37 +000051#define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(x)->ob_digit[0] : (Py_SIZE(x) == 0 ? 0 : (x)->ob_digit[0]))
Guido van Rossumddefaf32007-01-14 03:31:43 +000052/* If a freshly-allocated long is already shared, it must
53 be a small integer, so negating it must go to PyLong_FromLong */
54#define NEGATE(x) \
Christian Heimes90aa7642007-12-19 02:45:37 +000055 do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \
Christian Heimes217cfd12007-12-02 14:31:20 +000056 else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \
Guido van Rossumddefaf32007-01-14 03:31:43 +000057 Py_DECREF(x); (x) = (PyLongObject*)tmp; } \
58 while(0)
Tim Peters5af4e6c2002-08-12 02:31:19 +000059/* For long multiplication, use the O(N**2) school algorithm unless
60 * both operands contain more than KARATSUBA_CUTOFF digits (this
61 * being an internal Python long digit, in base BASE).
62 */
Tim Peters0973b992004-08-29 22:16:50 +000063#define KARATSUBA_CUTOFF 70
64#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000065
Tim Peters47e52ee2004-08-30 02:44:38 +000066/* For exponentiation, use the binary left-to-right algorithm
67 * unless the exponent contains more than FIVEARY_CUTOFF digits.
68 * In that case, do 5 bits at a time. The potential drawback is that
69 * a table of 2**5 intermediate results is computed.
70 */
71#define FIVEARY_CUTOFF 8
72
Guido van Rossume32e0141992-01-19 16:31:05 +000073#define ABS(x) ((x) < 0 ? -(x) : (x))
74
Tim Peters5af4e6c2002-08-12 02:31:19 +000075#undef MIN
76#undef MAX
77#define MAX(x, y) ((x) < (y) ? (y) : (x))
78#define MIN(x, y) ((x) > (y) ? (y) : (x))
79
Guido van Rossume32e0141992-01-19 16:31:05 +000080/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000081static PyLongObject *long_normalize(PyLongObject *);
82static PyLongObject *mul1(PyLongObject *, wdigit);
83static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000084static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Guido van Rossume32e0141992-01-19 16:31:05 +000085
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000087 if (--_Py_Ticker < 0) { \
88 _Py_Ticker = _Py_CheckInterval; \
Thomas Wouters477c8d52006-05-27 19:21:47 +000089 if (PyErr_CheckSignals()) PyTryBlock \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000090 }
91
Guido van Rossumedcc38a1991-05-05 20:09:44 +000092/* Normalize (remove leading zeros from) a long int object.
93 Doesn't attempt to free the storage--in most cases, due to the nature
94 of the algorithms used, this could save at most be one word anyway. */
95
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000097long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000098{
Christian Heimes90aa7642007-12-19 02:45:37 +000099 Py_ssize_t j = ABS(Py_SIZE(v));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000100 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000101
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000102 while (i > 0 && v->ob_digit[i-1] == 0)
103 --i;
104 if (i != j)
Christian Heimes90aa7642007-12-19 02:45:37 +0000105 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000106 return v;
107}
108
109/* Allocate a new long int object with size digits.
110 Return NULL and set exception if we run out of memory. */
111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000114{
Guido van Rossumddefaf32007-01-14 03:31:43 +0000115 PyLongObject *result;
116 /* Can't use sizeof(PyLongObject) here, since the
117 compiler takes padding at the end into account.
118 As the consequence, this would waste 2 bytes on
119 a 32-bit system, and 6 bytes on a 64-bit system.
120 This computation would be incorrect on systems
121 which have padding before the digits; with 16-bit
122 digits this should not happen. */
123 result = PyObject_MALLOC(sizeof(PyVarObject) +
124 size*sizeof(digit));
125 if (!result) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000126 PyErr_NoMemory();
127 return NULL;
128 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000129 return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000130}
131
Tim Peters64b5ce32001-09-10 20:52:51 +0000132PyObject *
133_PyLong_Copy(PyLongObject *src)
134{
135 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +0000137
138 assert(src != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000139 i = Py_SIZE(src);
Tim Peters64b5ce32001-09-10 20:52:51 +0000140 if (i < 0)
141 i = -(i);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000142 if (i < 2) {
143 int ival = src->ob_digit[0];
Christian Heimes90aa7642007-12-19 02:45:37 +0000144 if (Py_SIZE(src) < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000145 ival = -ival;
146 CHECK_SMALL_INT(ival);
147 }
Tim Peters64b5ce32001-09-10 20:52:51 +0000148 result = _PyLong_New(i);
149 if (result != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000150 Py_SIZE(result) = Py_SIZE(src);
Tim Peters64b5ce32001-09-10 20:52:51 +0000151 while (--i >= 0)
152 result->ob_digit[i] = src->ob_digit[i];
153 }
154 return (PyObject *)result;
155}
156
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000157/* Create a new long int object from a C long int */
158
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000160PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000161{
Tim Petersce9de2f2001-06-14 04:56:19 +0000162 PyLongObject *v;
Christian Heimesdae2a892008-04-19 00:55:37 +0000163 unsigned long abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000164 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
165 int ndigits = 0;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000166 int sign = 1;
167
168 CHECK_SMALL_INT(ival);
Tim Petersce9de2f2001-06-14 04:56:19 +0000169
170 if (ival < 0) {
Christian Heimesdae2a892008-04-19 00:55:37 +0000171 /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
172 ANSI C says that the result of -ival is undefined when ival
173 == LONG_MIN. Hence the following workaround. */
174 abs_ival = (unsigned long)(-1-ival) + 1;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000175 sign = -1;
Tim Petersce9de2f2001-06-14 04:56:19 +0000176 }
Christian Heimesdae2a892008-04-19 00:55:37 +0000177 else {
178 abs_ival = (unsigned long)ival;
179 }
Tim Petersce9de2f2001-06-14 04:56:19 +0000180
Guido van Rossumddefaf32007-01-14 03:31:43 +0000181 /* Fast path for single-digits ints */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000182 if (!(ival>>PyLong_SHIFT)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000183 v = _PyLong_New(1);
184 if (v) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000185 Py_SIZE(v) = sign;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000186 v->ob_digit[0] = ival;
187 }
188 return (PyObject*)v;
189 }
190
191 /* 2 digits */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000192 if (!(ival >> 2*PyLong_SHIFT)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000193 v = _PyLong_New(2);
194 if (v) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000195 Py_SIZE(v) = 2*sign;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000196 v->ob_digit[0] = (digit)ival & PyLong_MASK;
197 v->ob_digit[1] = ival >> PyLong_SHIFT;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000198 }
199 return (PyObject*)v;
200 }
201
202 /* Larger numbers: loop to determine number of digits */
Christian Heimesdae2a892008-04-19 00:55:37 +0000203 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000204 while (t) {
205 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000206 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000207 }
208 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000209 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000210 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +0000211 Py_SIZE(v) = ndigits*sign;
Christian Heimesdae2a892008-04-19 00:55:37 +0000212 t = abs_ival;
Tim Petersce9de2f2001-06-14 04:56:19 +0000213 while (t) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000214 *p++ = (digit)(t & PyLong_MASK);
215 t >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000216 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000217 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000219}
220
Guido van Rossum53756b11997-01-03 17:14:46 +0000221/* Create a new long int object from a C unsigned long int */
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000224PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000225{
Tim Petersce9de2f2001-06-14 04:56:19 +0000226 PyLongObject *v;
227 unsigned long t;
228 int ndigits = 0;
229
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000230 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000231 return PyLong_FromLong(ival);
Tim Petersce9de2f2001-06-14 04:56:19 +0000232 /* Count the number of Python digits. */
233 t = (unsigned long)ival;
234 while (t) {
235 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000236 t >>= PyLong_SHIFT;
Tim Petersce9de2f2001-06-14 04:56:19 +0000237 }
238 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000239 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000240 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +0000241 Py_SIZE(v) = ndigits;
Tim Petersce9de2f2001-06-14 04:56:19 +0000242 while (ival) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000243 *p++ = (digit)(ival & PyLong_MASK);
244 ival >>= PyLong_SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000245 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000246 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000248}
249
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000250/* Create a new long int object from a C double */
251
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000254{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000256 double frac;
257 int i, ndig, expo, neg;
258 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000259 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000260 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000261 "cannot convert float infinity to int");
Guido van Rossum1a23c241999-09-27 17:11:52 +0000262 return NULL;
263 }
Christian Heimesa34706f2008-01-04 03:06:10 +0000264 if (Py_IS_NAN(dval)) {
Christian Heimes386cd1e2008-01-15 02:01:20 +0000265 PyErr_SetString(PyExc_OverflowError,
266 "cannot convert float NaN to int");
267 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +0000268 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000269 if (dval < 0.0) {
270 neg = 1;
271 dval = -dval;
272 }
273 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
274 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 return PyLong_FromLong(0L);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000276 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000278 if (v == NULL)
279 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000280 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000281 for (i = ndig; --i >= 0; ) {
282 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000283 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000284 frac = frac - (double)bits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000285 frac = ldexp(frac, PyLong_SHIFT);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000286 }
287 if (neg)
Christian Heimes90aa7642007-12-19 02:45:37 +0000288 Py_SIZE(v) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000290}
291
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
293 * anything about what happens when a signed integer operation overflows,
294 * and some compilers think they're doing you a favor by being "clever"
295 * then. The bit pattern for the largest postive signed long is
296 * (unsigned long)LONG_MAX, and for the smallest negative signed long
297 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
298 * However, some other compilers warn about applying unary minus to an
299 * unsigned operand. Hence the weird "0-".
300 */
301#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
302#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
303
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000304/* Get a C long int from a long int object.
305 Returns -1 and sets an error condition if overflow occurs. */
306
307long
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000308PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000309{
Guido van Rossumf7531811998-05-26 14:33:37 +0000310 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000312 unsigned long x, prev;
Georg Brandl61c31b02007-02-26 14:46:30 +0000313 long res;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000314 Py_ssize_t i;
315 int sign;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000316 int do_decref = 0; /* if nb_int was called */
Guido van Rossumf7531811998-05-26 14:33:37 +0000317
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000318 *overflow = 0;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000319 if (vv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000321 return -1;
322 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000323
324 if (!PyLong_Check(vv)) {
325 PyNumberMethods *nb;
326 if ((nb = vv->ob_type->tp_as_number) == NULL ||
327 nb->nb_int == NULL) {
328 PyErr_SetString(PyExc_TypeError, "an integer is required");
329 return -1;
330 }
331 vv = (*nb->nb_int) (vv);
332 if (vv == NULL)
333 return -1;
334 do_decref = 1;
335 if (!PyLong_Check(vv)) {
336 Py_DECREF(vv);
337 PyErr_SetString(PyExc_TypeError,
338 "nb_int should return int object");
339 return -1;
340 }
341 }
342
Georg Brandl61c31b02007-02-26 14:46:30 +0000343 res = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000345 i = Py_SIZE(v);
Guido van Rossumf7531811998-05-26 14:33:37 +0000346
Georg Brandl61c31b02007-02-26 14:46:30 +0000347 switch (i) {
348 case -1:
349 res = -v->ob_digit[0];
350 break;
351 case 0:
352 res = 0;
353 break;
354 case 1:
355 res = v->ob_digit[0];
356 break;
357 default:
358 sign = 1;
359 x = 0;
360 if (i < 0) {
361 sign = -1;
362 i = -(i);
363 }
364 while (--i >= 0) {
365 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000366 x = (x << PyLong_SHIFT) + v->ob_digit[i];
367 if ((x >> PyLong_SHIFT) != prev) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000368 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
Georg Brandl61c31b02007-02-26 14:46:30 +0000369 goto exit;
370 }
371 }
372 /* Haven't lost any bits, but casting to long requires extra care
373 * (see comment above).
374 */
375 if (x <= (unsigned long)LONG_MAX) {
376 res = (long)x * sign;
377 }
378 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
379 res = LONG_MIN;
380 }
381 else {
Christian Heimes90aa7642007-12-19 02:45:37 +0000382 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000383 /* res is already set to -1 */
Georg Brandl61c31b02007-02-26 14:46:30 +0000384 }
385 }
386 exit:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000387 if (do_decref) {
388 Py_DECREF(vv);
389 }
Georg Brandl61c31b02007-02-26 14:46:30 +0000390 return res;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000391}
392
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000393long
394PyLong_AsLong(PyObject *obj)
395{
396 int overflow;
397 long result = PyLong_AsLongAndOverflow(obj, &overflow);
398 if (overflow) {
399 /* XXX: could be cute and give a different
400 message for overflow == -1 */
401 PyErr_SetString(PyExc_OverflowError,
402 "Python int too large to convert to C long");
403 }
404 return result;
405}
406
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000407/* Get a Py_ssize_t from a long int object.
408 Returns -1 and sets an error condition if overflow occurs. */
409
410Py_ssize_t
Guido van Rossumddefaf32007-01-14 03:31:43 +0000411PyLong_AsSsize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000412 register PyLongObject *v;
413 size_t x, prev;
414 Py_ssize_t i;
415 int sign;
416
417 if (vv == NULL || !PyLong_Check(vv)) {
418 PyErr_BadInternalCall();
419 return -1;
420 }
421 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000422 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000423 switch (i) {
424 case -1: return -v->ob_digit[0];
425 case 0: return 0;
426 case 1: return v->ob_digit[0];
427 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 sign = 1;
429 x = 0;
430 if (i < 0) {
431 sign = -1;
432 i = -(i);
433 }
434 while (--i >= 0) {
435 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000436 x = (x << PyLong_SHIFT) + v->ob_digit[i];
437 if ((x >> PyLong_SHIFT) != prev)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000438 goto overflow;
439 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000440 /* Haven't lost any bits, but casting to a signed type requires
441 * extra care (see comment above).
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000443 if (x <= (size_t)PY_SSIZE_T_MAX) {
444 return (Py_ssize_t)x * sign;
445 }
446 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
447 return PY_SSIZE_T_MIN;
448 }
449 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000450
451 overflow:
452 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000453 "Python int too large to convert to C ssize_t");
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000454 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455}
456
Guido van Rossumd8c80482002-08-13 00:24:58 +0000457/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000458 Returns -1 and sets an error condition if overflow occurs. */
459
460unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000461PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000462{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000464 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467 if (vv == NULL || !PyLong_Check(vv)) {
468 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000469 return (unsigned long) -1;
470 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000472 i = Py_SIZE(v);
Guido van Rossum53756b11997-01-03 17:14:46 +0000473 x = 0;
474 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000476 "can't convert negative value to unsigned int");
Guido van Rossum53756b11997-01-03 17:14:46 +0000477 return (unsigned long) -1;
478 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000479 switch (i) {
480 case 0: return 0;
481 case 1: return v->ob_digit[0];
482 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000483 while (--i >= 0) {
484 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000485 x = (x << PyLong_SHIFT) + v->ob_digit[i];
486 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000488 "python int too large to convert to C unsigned long");
Guido van Rossumddefaf32007-01-14 03:31:43 +0000489 return (unsigned long) -1;
490 }
491 }
492 return x;
493}
494
495/* Get a C unsigned long int from a long int object.
496 Returns -1 and sets an error condition if overflow occurs. */
497
498size_t
499PyLong_AsSize_t(PyObject *vv)
500{
501 register PyLongObject *v;
502 size_t x, prev;
503 Py_ssize_t i;
504
505 if (vv == NULL || !PyLong_Check(vv)) {
506 PyErr_BadInternalCall();
507 return (unsigned long) -1;
508 }
509 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000510 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000511 x = 0;
512 if (i < 0) {
513 PyErr_SetString(PyExc_OverflowError,
514 "can't convert negative value to size_t");
515 return (size_t) -1;
516 }
517 switch (i) {
518 case 0: return 0;
519 case 1: return v->ob_digit[0];
520 }
521 while (--i >= 0) {
522 prev = x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000523 x = (x << PyLong_SHIFT) + v->ob_digit[i];
524 if ((x >> PyLong_SHIFT) != prev) {
Guido van Rossumddefaf32007-01-14 03:31:43 +0000525 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000526 "Python int too large to convert to C size_t");
Guido van Rossum53756b11997-01-03 17:14:46 +0000527 return (unsigned long) -1;
528 }
529 }
530 return x;
531}
532
Thomas Hellera4ea6032003-04-17 18:55:45 +0000533/* Get a C unsigned long int from a long int object, ignoring the high bits.
534 Returns -1 and sets an error condition if an error occurs. */
535
Guido van Rossumddefaf32007-01-14 03:31:43 +0000536static unsigned long
537_PyLong_AsUnsignedLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000538{
539 register PyLongObject *v;
540 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000541 Py_ssize_t i;
542 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000543
544 if (vv == NULL || !PyLong_Check(vv)) {
545 PyErr_BadInternalCall();
546 return (unsigned long) -1;
547 }
548 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000549 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000550 switch (i) {
551 case 0: return 0;
552 case 1: return v->ob_digit[0];
553 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000554 sign = 1;
555 x = 0;
556 if (i < 0) {
557 sign = -1;
558 i = -i;
559 }
560 while (--i >= 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000561 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +0000562 }
563 return x * sign;
564}
565
Guido van Rossumddefaf32007-01-14 03:31:43 +0000566unsigned long
567PyLong_AsUnsignedLongMask(register PyObject *op)
568{
569 PyNumberMethods *nb;
570 PyLongObject *lo;
571 unsigned long val;
572
573 if (op && PyLong_Check(op))
574 return _PyLong_AsUnsignedLongMask(op);
575
576 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
577 nb->nb_int == NULL) {
578 PyErr_SetString(PyExc_TypeError, "an integer is required");
579 return (unsigned long)-1;
580 }
581
582 lo = (PyLongObject*) (*nb->nb_int) (op);
583 if (lo == NULL)
584 return (unsigned long)-1;
585 if (PyLong_Check(lo)) {
586 val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
587 Py_DECREF(lo);
588 if (PyErr_Occurred())
589 return (unsigned long)-1;
590 return val;
591 }
592 else
593 {
594 Py_DECREF(lo);
595 PyErr_SetString(PyExc_TypeError,
596 "nb_int should return int object");
597 return (unsigned long)-1;
598 }
599}
600
Tim Peters5b8132f2003-01-31 15:52:05 +0000601int
602_PyLong_Sign(PyObject *vv)
603{
604 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000605
606 assert(v != NULL);
607 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000608
Christian Heimes90aa7642007-12-19 02:45:37 +0000609 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000610}
611
Tim Petersbaefd9e2003-01-28 20:37:45 +0000612size_t
613_PyLong_NumBits(PyObject *vv)
614{
615 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000616 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000617 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000618
619 assert(v != NULL);
620 assert(PyLong_Check(v));
Christian Heimes90aa7642007-12-19 02:45:37 +0000621 ndigits = ABS(Py_SIZE(v));
Tim Petersbaefd9e2003-01-28 20:37:45 +0000622 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
623 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000624 digit msd = v->ob_digit[ndigits - 1];
625
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000626 result = (ndigits - 1) * PyLong_SHIFT;
627 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
Tim Petersbaefd9e2003-01-28 20:37:45 +0000628 goto Overflow;
629 do {
630 ++result;
631 if (result == 0)
632 goto Overflow;
633 msd >>= 1;
634 } while (msd);
635 }
636 return result;
637
638Overflow:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000639 PyErr_SetString(PyExc_OverflowError, "int has too many bits "
Tim Petersbaefd9e2003-01-28 20:37:45 +0000640 "to express in a platform size_t");
641 return (size_t)-1;
642}
643
Tim Peters2a9b3672001-06-11 21:23:58 +0000644PyObject *
645_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
646 int little_endian, int is_signed)
647{
648 const unsigned char* pstartbyte;/* LSB of bytes */
649 int incr; /* direction to move pstartbyte */
650 const unsigned char* pendbyte; /* MSB of bytes */
651 size_t numsignificantbytes; /* number of bytes that matter */
652 size_t ndigits; /* number of Python long digits */
653 PyLongObject* v; /* result */
654 int idigit = 0; /* next free index in v->ob_digit */
655
656 if (n == 0)
657 return PyLong_FromLong(0L);
658
659 if (little_endian) {
660 pstartbyte = bytes;
661 pendbyte = bytes + n - 1;
662 incr = 1;
663 }
664 else {
665 pstartbyte = bytes + n - 1;
666 pendbyte = bytes;
667 incr = -1;
668 }
669
670 if (is_signed)
671 is_signed = *pendbyte >= 0x80;
672
673 /* Compute numsignificantbytes. This consists of finding the most
674 significant byte. Leading 0 bytes are insignficant if the number
675 is positive, and leading 0xff bytes if negative. */
676 {
677 size_t i;
678 const unsigned char* p = pendbyte;
679 const int pincr = -incr; /* search MSB to LSB */
680 const unsigned char insignficant = is_signed ? 0xff : 0x00;
681
682 for (i = 0; i < n; ++i, p += pincr) {
683 if (*p != insignficant)
684 break;
685 }
686 numsignificantbytes = n - i;
687 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
688 actually has 2 significant bytes. OTOH, 0xff0001 ==
689 -0x00ffff, so we wouldn't *need* to bump it there; but we
690 do for 0xffff = -0x0001. To be safe without bothering to
691 check every case, bump it regardless. */
692 if (is_signed && numsignificantbytes < n)
693 ++numsignificantbytes;
694 }
695
696 /* How many Python long digits do we need? We have
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000697 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
Tim Peters2a9b3672001-06-11 21:23:58 +0000698 bits, so it's the ceiling of the quotient. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000699 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
Tim Peters2a9b3672001-06-11 21:23:58 +0000700 if (ndigits > (size_t)INT_MAX)
701 return PyErr_NoMemory();
702 v = _PyLong_New((int)ndigits);
703 if (v == NULL)
704 return NULL;
705
706 /* Copy the bits over. The tricky parts are computing 2's-comp on
707 the fly for signed numbers, and dealing with the mismatch between
708 8-bit bytes and (probably) 15-bit Python digits.*/
709 {
710 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000711 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000712 twodigits accum = 0; /* sliding register */
713 unsigned int accumbits = 0; /* number of bits in accum */
714 const unsigned char* p = pstartbyte;
715
716 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000717 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000718 /* Compute correction for 2's comp, if needed. */
719 if (is_signed) {
720 thisbyte = (0xff ^ thisbyte) + carry;
721 carry = thisbyte >> 8;
722 thisbyte &= 0xff;
723 }
724 /* Because we're going LSB to MSB, thisbyte is
725 more significant than what's already in accum,
726 so needs to be prepended to accum. */
727 accum |= thisbyte << accumbits;
728 accumbits += 8;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000729 if (accumbits >= PyLong_SHIFT) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000730 /* There's enough to fill a Python digit. */
731 assert(idigit < (int)ndigits);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000732 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Tim Peters2a9b3672001-06-11 21:23:58 +0000733 ++idigit;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000734 accum >>= PyLong_SHIFT;
735 accumbits -= PyLong_SHIFT;
736 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000737 }
738 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000739 assert(accumbits < PyLong_SHIFT);
Tim Peters2a9b3672001-06-11 21:23:58 +0000740 if (accumbits) {
741 assert(idigit < (int)ndigits);
742 v->ob_digit[idigit] = (digit)accum;
743 ++idigit;
744 }
745 }
746
Christian Heimes90aa7642007-12-19 02:45:37 +0000747 Py_SIZE(v) = is_signed ? -idigit : idigit;
Tim Peters2a9b3672001-06-11 21:23:58 +0000748 return (PyObject *)long_normalize(v);
749}
750
751int
752_PyLong_AsByteArray(PyLongObject* v,
753 unsigned char* bytes, size_t n,
754 int little_endian, int is_signed)
755{
756 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000757 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000758 twodigits accum; /* sliding register */
759 unsigned int accumbits; /* # bits in accum */
760 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
761 twodigits carry; /* for computing 2's-comp */
762 size_t j; /* # bytes filled */
763 unsigned char* p; /* pointer to next byte in bytes */
764 int pincr; /* direction to move p */
765
766 assert(v != NULL && PyLong_Check(v));
767
Christian Heimes90aa7642007-12-19 02:45:37 +0000768 if (Py_SIZE(v) < 0) {
769 ndigits = -(Py_SIZE(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000770 if (!is_signed) {
771 PyErr_SetString(PyExc_TypeError,
Guido van Rossumddefaf32007-01-14 03:31:43 +0000772 "can't convert negative int to unsigned");
Tim Peters2a9b3672001-06-11 21:23:58 +0000773 return -1;
774 }
775 do_twos_comp = 1;
776 }
777 else {
Christian Heimes90aa7642007-12-19 02:45:37 +0000778 ndigits = Py_SIZE(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000779 do_twos_comp = 0;
780 }
781
782 if (little_endian) {
783 p = bytes;
784 pincr = 1;
785 }
786 else {
787 p = bytes + n - 1;
788 pincr = -1;
789 }
790
Tim Peters898cf852001-06-13 20:50:08 +0000791 /* Copy over all the Python digits.
792 It's crucial that every Python digit except for the MSD contribute
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000793 exactly PyLong_SHIFT bits to the total, so first assert that the long is
Tim Peters898cf852001-06-13 20:50:08 +0000794 normalized. */
795 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000796 j = 0;
797 accum = 0;
798 accumbits = 0;
799 carry = do_twos_comp ? 1 : 0;
800 for (i = 0; i < ndigits; ++i) {
801 twodigits thisdigit = v->ob_digit[i];
802 if (do_twos_comp) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000803 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
804 carry = thisdigit >> PyLong_SHIFT;
805 thisdigit &= PyLong_MASK;
Tim Peters2a9b3672001-06-11 21:23:58 +0000806 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000807 /* Because we're going LSB to MSB, thisdigit is more
808 significant than what's already in accum, so needs to be
809 prepended to accum. */
810 accum |= thisdigit << accumbits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000811 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000812
Tim Petersede05092001-06-14 08:53:38 +0000813 /* The most-significant digit may be (probably is) at least
814 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000815 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000816 /* Count # of sign bits -- they needn't be stored,
817 * although for signed conversion we need later to
818 * make sure at least one sign bit gets stored.
819 * First shift conceptual sign bit to real sign bit.
820 */
821 stwodigits s = (stwodigits)(thisdigit <<
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000822 (8*sizeof(stwodigits) - PyLong_SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000823 unsigned int nsignbits = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000824 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000825 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000826 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000827 }
Tim Petersede05092001-06-14 08:53:38 +0000828 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000829 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000830
Tim Peters2a9b3672001-06-11 21:23:58 +0000831 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000832 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000833 if (j >= n)
834 goto Overflow;
835 ++j;
836 *p = (unsigned char)(accum & 0xff);
837 p += pincr;
838 accumbits -= 8;
839 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000840 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000841 }
842
843 /* Store the straggler (if any). */
844 assert(accumbits < 8);
845 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000846 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000847 if (j >= n)
848 goto Overflow;
849 ++j;
850 if (do_twos_comp) {
851 /* Fill leading bits of the byte with sign bits
852 (appropriately pretending that the long had an
853 infinite supply of sign bits). */
854 accum |= (~(twodigits)0) << accumbits;
855 }
856 *p = (unsigned char)(accum & 0xff);
857 p += pincr;
858 }
Tim Peters05607ad2001-06-13 21:01:27 +0000859 else if (j == n && n > 0 && is_signed) {
860 /* The main loop filled the byte array exactly, so the code
861 just above didn't get to ensure there's a sign bit, and the
862 loop below wouldn't add one either. Make sure a sign bit
863 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000864 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000865 int sign_bit_set = msb >= 0x80;
866 assert(accumbits == 0);
867 if (sign_bit_set == do_twos_comp)
868 return 0;
869 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000870 goto Overflow;
871 }
Tim Peters05607ad2001-06-13 21:01:27 +0000872
873 /* Fill remaining bytes with copies of the sign bit. */
874 {
875 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
876 for ( ; j < n; ++j, p += pincr)
877 *p = signbyte;
878 }
879
Tim Peters2a9b3672001-06-11 21:23:58 +0000880 return 0;
881
882Overflow:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000883 PyErr_SetString(PyExc_OverflowError, "int too big to convert");
Tim Peters2a9b3672001-06-11 21:23:58 +0000884 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000885
Tim Peters2a9b3672001-06-11 21:23:58 +0000886}
887
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000888double
889_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
890{
891/* NBITS_WANTED should be > the number of bits in a double's precision,
892 but small enough so that 2**NBITS_WANTED is within the normal double
893 range. nbitsneeded is set to 1 less than that because the most-significant
894 Python digit contains at least 1 significant bit, but we don't want to
895 bother counting them (catering to the worst case cheaply).
896
897 57 is one more than VAX-D double precision; I (Tim) don't know of a double
898 format with more precision than that; it's 1 larger so that we add in at
899 least one round bit to stand in for the ignored least-significant bits.
900*/
901#define NBITS_WANTED 57
902 PyLongObject *v;
903 double x;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000904 const double multiplier = (double)(1L << PyLong_SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000905 Py_ssize_t i;
906 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000907 int nbitsneeded;
908
909 if (vv == NULL || !PyLong_Check(vv)) {
910 PyErr_BadInternalCall();
911 return -1;
912 }
913 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +0000914 i = Py_SIZE(v);
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000915 sign = 1;
916 if (i < 0) {
917 sign = -1;
918 i = -(i);
919 }
920 else if (i == 0) {
921 *exponent = 0;
922 return 0.0;
923 }
924 --i;
925 x = (double)v->ob_digit[i];
926 nbitsneeded = NBITS_WANTED - 1;
927 /* Invariant: i Python digits remain unaccounted for. */
928 while (i > 0 && nbitsneeded > 0) {
929 --i;
930 x = x * multiplier + (double)v->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000931 nbitsneeded -= PyLong_SHIFT;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000932 }
933 /* There are i digits we didn't shift in. Pretending they're all
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000934 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000935 *exponent = i;
936 assert(x > 0.0);
937 return x * sign;
938#undef NBITS_WANTED
939}
940
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000941/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000942
943double
Tim Peters9f688bf2000-07-07 15:53:28 +0000944PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000945{
Thomas Wouters553489a2006-02-01 21:32:04 +0000946 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000947 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000948
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 if (vv == NULL || !PyLong_Check(vv)) {
950 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000951 return -1;
952 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000953 x = _PyLong_AsScaledDouble(vv, &e);
954 if (x == -1.0 && PyErr_Occurred())
955 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000956 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
957 set correctly after a successful _PyLong_AsScaledDouble() call */
958 assert(e >= 0);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000959 if (e > INT_MAX / PyLong_SHIFT)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000960 goto overflow;
961 errno = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000962 x = ldexp(x, e * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000963 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000964 goto overflow;
965 return x;
966
967overflow:
968 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum523d4f92007-01-15 00:31:49 +0000969 "Python int too large to convert to C double");
Tim Peters9fffa3e2001-09-04 05:14:19 +0000970 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000971}
972
Guido van Rossum78694d91998-09-18 14:14:13 +0000973/* Create a new long (or int) object from a C pointer */
974
975PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000976PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000977{
Tim Peters70128a12001-06-16 08:48:40 +0000978#ifndef HAVE_LONG_LONG
979# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
980#endif
981#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000982# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000983#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +0000984 /* special-case null pointer */
985 if (!p)
Christian Heimes217cfd12007-12-02 14:31:20 +0000986 return PyLong_FromLong(0);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000987 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
Tim Peters70128a12001-06-16 08:48:40 +0000988
Guido van Rossum78694d91998-09-18 14:14:13 +0000989}
990
991/* Get a C pointer from a long object (or an int object in some cases) */
992
993void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000994PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000995{
996 /* This function will allow int or long objects. If vv is neither,
997 then the PyLong_AsLong*() functions will raise the exception:
998 PyExc_SystemError, "bad argument to internal function"
999 */
Tim Peters70128a12001-06-16 08:48:40 +00001000#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +00001001 long x;
1002
Guido van Rossumddefaf32007-01-14 03:31:43 +00001003 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001004 x = PyLong_AsLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001005 else
1006 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +00001007#else
Tim Peters70128a12001-06-16 08:48:40 +00001008
1009#ifndef HAVE_LONG_LONG
1010# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
1011#endif
1012#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001013# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +00001014#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001015 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001016
Guido van Rossumddefaf32007-01-14 03:31:43 +00001017 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +00001018 x = PyLong_AsLongLong(vv);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019 else
1020 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +00001021
1022#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +00001023
1024 if (x == -1 && PyErr_Occurred())
1025 return NULL;
1026 return (void *)x;
1027}
1028
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001029#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +00001030
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001031/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +00001032 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001033 */
1034
Tim Peterscf37dfc2001-06-14 18:42:50 +00001035#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +00001036
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001037/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001038
1039PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001040PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001041{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001042 PyLongObject *v;
Christian Heimesdae2a892008-04-19 00:55:37 +00001043 unsigned PY_LONG_LONG abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
1045 int ndigits = 0;
1046 int negative = 0;
1047
Guido van Rossumddefaf32007-01-14 03:31:43 +00001048 CHECK_SMALL_INT(ival);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 if (ival < 0) {
Christian Heimesdae2a892008-04-19 00:55:37 +00001050 /* avoid signed overflow on negation; see comments
1051 in PyLong_FromLong above. */
1052 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053 negative = 1;
1054 }
Christian Heimesdae2a892008-04-19 00:55:37 +00001055 else {
1056 abs_ival = (unsigned PY_LONG_LONG)ival;
1057 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058
1059 /* Count the number of Python digits.
1060 We used to pick 5 ("big enough for anything"), but that's a
1061 waste of time and space given that 5*15 = 75 bits are rarely
1062 needed. */
Christian Heimesdae2a892008-04-19 00:55:37 +00001063 t = abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064 while (t) {
1065 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001066 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 }
1068 v = _PyLong_New(ndigits);
1069 if (v != NULL) {
1070 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001071 Py_SIZE(v) = negative ? -ndigits : ndigits;
Christian Heimesdae2a892008-04-19 00:55:37 +00001072 t = abs_ival;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073 while (t) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001074 *p++ = (digit)(t & PyLong_MASK);
1075 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001076 }
1077 }
1078 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001079}
1080
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001081/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +00001082
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001083PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001084PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001085{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086 PyLongObject *v;
1087 unsigned PY_LONG_LONG t;
1088 int ndigits = 0;
1089
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001090 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +00001091 return PyLong_FromLong(ival);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092 /* Count the number of Python digits. */
1093 t = (unsigned PY_LONG_LONG)ival;
1094 while (t) {
1095 ++ndigits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001096 t >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001097 }
1098 v = _PyLong_New(ndigits);
1099 if (v != NULL) {
1100 digit *p = v->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001101 Py_SIZE(v) = ndigits;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001102 while (ival) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001103 *p++ = (digit)(ival & PyLong_MASK);
1104 ival >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001105 }
1106 }
1107 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001108}
1109
Martin v. Löwis18e16552006-02-15 17:27:45 +00001110/* Create a new long int object from a C Py_ssize_t. */
1111
1112PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001113PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001114{
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001115 PyLongObject *v;
1116 size_t abs_ival;
1117 size_t t; /* unsigned so >> doesn't propagate sign bit */
1118 int ndigits = 0;
1119 int negative = 0;
1120
1121 CHECK_SMALL_INT(ival);
1122 if (ival < 0) {
1123 /* avoid signed overflow when ival = SIZE_T_MIN */
1124 abs_ival = (size_t)(-1-ival)+1;
1125 negative = 1;
1126 }
1127 else {
1128 abs_ival = (size_t)ival;
1129 }
1130
1131 /* Count the number of Python digits. */
1132 t = abs_ival;
1133 while (t) {
1134 ++ndigits;
1135 t >>= PyLong_SHIFT;
1136 }
1137 v = _PyLong_New(ndigits);
1138 if (v != NULL) {
1139 digit *p = v->ob_digit;
1140 Py_SIZE(v) = negative ? -ndigits : ndigits;
1141 t = abs_ival;
1142 while (t) {
1143 *p++ = (digit)(t & PyLong_MASK);
1144 t >>= PyLong_SHIFT;
1145 }
1146 }
1147 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001148}
1149
1150/* Create a new long int object from a C size_t. */
1151
1152PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001153PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001154{
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001155 PyLongObject *v;
1156 size_t t;
1157 int ndigits = 0;
1158
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001159 if (ival < PyLong_BASE)
Guido van Rossumddefaf32007-01-14 03:31:43 +00001160 return PyLong_FromLong(ival);
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001161 /* Count the number of Python digits. */
1162 t = ival;
1163 while (t) {
1164 ++ndigits;
1165 t >>= PyLong_SHIFT;
1166 }
1167 v = _PyLong_New(ndigits);
1168 if (v != NULL) {
1169 digit *p = v->ob_digit;
1170 Py_SIZE(v) = ndigits;
1171 while (ival) {
1172 *p++ = (digit)(ival & PyLong_MASK);
1173 ival >>= PyLong_SHIFT;
1174 }
1175 }
1176 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001177}
1178
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001179/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001180 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001181
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001182PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001183PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001184{
Guido van Rossumddefaf32007-01-14 03:31:43 +00001185 PyLongObject *v;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001186 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001187 int one = 1;
1188 int res;
1189
Tim Petersd38b1c72001-09-30 05:09:37 +00001190 if (vv == NULL) {
1191 PyErr_BadInternalCall();
1192 return -1;
1193 }
1194 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001195 PyNumberMethods *nb;
1196 PyObject *io;
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001197 if ((nb = vv->ob_type->tp_as_number) == NULL ||
1198 nb->nb_int == NULL) {
1199 PyErr_SetString(PyExc_TypeError, "an integer is required");
1200 return -1;
1201 }
1202 io = (*nb->nb_int) (vv);
1203 if (io == NULL)
1204 return -1;
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00001205 if (PyLong_Check(io)) {
1206 bytes = PyLong_AsLongLong(io);
1207 Py_DECREF(io);
1208 return bytes;
1209 }
1210 Py_DECREF(io);
1211 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001212 return -1;
1213 }
1214
Guido van Rossumddefaf32007-01-14 03:31:43 +00001215 v = (PyLongObject*)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001216 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001217 case -1: return -v->ob_digit[0];
1218 case 0: return 0;
1219 case 1: return v->ob_digit[0];
1220 }
Tim Petersd1a7da62001-06-13 00:35:57 +00001221 res = _PyLong_AsByteArray(
1222 (PyLongObject *)vv, (unsigned char *)&bytes,
1223 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001224
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001225 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001226 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001227 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001228 else
1229 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001230}
1231
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001232/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001233 Return -1 and set an error if overflow occurs. */
1234
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001235unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +00001236PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001237{
Guido van Rossumddefaf32007-01-14 03:31:43 +00001238 PyLongObject *v;
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001239 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +00001240 int one = 1;
1241 int res;
1242
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001243 if (vv == NULL || !PyLong_Check(vv)) {
1244 PyErr_BadInternalCall();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245 return (unsigned PY_LONG_LONG)-1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001246 }
1247
Guido van Rossumddefaf32007-01-14 03:31:43 +00001248 v = (PyLongObject*)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001249 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001250 case 0: return 0;
1251 case 1: return v->ob_digit[0];
1252 }
1253
Tim Petersd1a7da62001-06-13 00:35:57 +00001254 res = _PyLong_AsByteArray(
1255 (PyLongObject *)vv, (unsigned char *)&bytes,
1256 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001257
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001258 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001259 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001260 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +00001261 else
1262 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001263}
Tim Petersd1a7da62001-06-13 00:35:57 +00001264
Thomas Hellera4ea6032003-04-17 18:55:45 +00001265/* Get a C unsigned long int from a long int object, ignoring the high bits.
1266 Returns -1 and sets an error condition if an error occurs. */
1267
Guido van Rossumddefaf32007-01-14 03:31:43 +00001268static unsigned PY_LONG_LONG
1269_PyLong_AsUnsignedLongLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001270{
1271 register PyLongObject *v;
1272 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001273 Py_ssize_t i;
1274 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001275
1276 if (vv == NULL || !PyLong_Check(vv)) {
1277 PyErr_BadInternalCall();
1278 return (unsigned long) -1;
1279 }
1280 v = (PyLongObject *)vv;
Christian Heimes90aa7642007-12-19 02:45:37 +00001281 switch(Py_SIZE(v)) {
Guido van Rossumddefaf32007-01-14 03:31:43 +00001282 case 0: return 0;
1283 case 1: return v->ob_digit[0];
1284 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001285 i = Py_SIZE(v);
Thomas Hellera4ea6032003-04-17 18:55:45 +00001286 sign = 1;
1287 x = 0;
1288 if (i < 0) {
1289 sign = -1;
1290 i = -i;
1291 }
1292 while (--i >= 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001293 x = (x << PyLong_SHIFT) + v->ob_digit[i];
Thomas Hellera4ea6032003-04-17 18:55:45 +00001294 }
1295 return x * sign;
1296}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001297
1298unsigned PY_LONG_LONG
1299PyLong_AsUnsignedLongLongMask(register PyObject *op)
1300{
1301 PyNumberMethods *nb;
1302 PyLongObject *lo;
1303 unsigned PY_LONG_LONG val;
1304
1305 if (op && PyLong_Check(op))
1306 return _PyLong_AsUnsignedLongLongMask(op);
1307
1308 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
1309 nb->nb_int == NULL) {
1310 PyErr_SetString(PyExc_TypeError, "an integer is required");
1311 return (unsigned PY_LONG_LONG)-1;
1312 }
1313
1314 lo = (PyLongObject*) (*nb->nb_int) (op);
1315 if (lo == NULL)
1316 return (unsigned PY_LONG_LONG)-1;
1317 if (PyLong_Check(lo)) {
1318 val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
1319 Py_DECREF(lo);
1320 if (PyErr_Occurred())
1321 return (unsigned PY_LONG_LONG)-1;
1322 return val;
1323 }
1324 else
1325 {
1326 Py_DECREF(lo);
1327 PyErr_SetString(PyExc_TypeError,
1328 "nb_int should return int object");
1329 return (unsigned PY_LONG_LONG)-1;
1330 }
1331}
Tim Petersd1a7da62001-06-13 00:35:57 +00001332#undef IS_LITTLE_ENDIAN
1333
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001334#endif /* HAVE_LONG_LONG */
1335
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00001336#define CHECK_BINOP(v,w) \
1337 if (!PyLong_Check(v) || !PyLong_Check(w)) { \
Neil Schemenauerba872e22001-01-04 01:46:03 +00001338 Py_INCREF(Py_NotImplemented); \
1339 return Py_NotImplemented; \
1340 }
1341
Tim Peters877a2122002-08-12 05:09:36 +00001342/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1343 * is modified in place, by adding y to it. Carries are propagated as far as
1344 * x[m-1], and the remaining carry (0 or 1) is returned.
1345 */
1346static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001347v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001348{
1349 int i;
1350 digit carry = 0;
1351
1352 assert(m >= n);
1353 for (i = 0; i < n; ++i) {
1354 carry += x[i] + y[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001355 x[i] = carry & PyLong_MASK;
1356 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001357 assert((carry & 1) == carry);
1358 }
1359 for (; carry && i < m; ++i) {
1360 carry += x[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001361 x[i] = carry & PyLong_MASK;
1362 carry >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001363 assert((carry & 1) == carry);
1364 }
1365 return carry;
1366}
1367
1368/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1369 * is modified in place, by subtracting y from it. Borrows are propagated as
1370 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1371 */
1372static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001373v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001374{
1375 int i;
1376 digit borrow = 0;
1377
1378 assert(m >= n);
1379 for (i = 0; i < n; ++i) {
1380 borrow = x[i] - y[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001381 x[i] = borrow & PyLong_MASK;
1382 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001383 borrow &= 1; /* keep only 1 sign bit */
1384 }
1385 for (; borrow && i < m; ++i) {
1386 borrow = x[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001387 x[i] = borrow & PyLong_MASK;
1388 borrow >>= PyLong_SHIFT;
Tim Peters877a2122002-08-12 05:09:36 +00001389 borrow &= 1;
1390 }
1391 return borrow;
1392}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001393
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001394/* Multiply by a single digit, ignoring the sign. */
1395
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001397mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001398{
1399 return muladd1(a, n, (digit)0);
1400}
1401
1402/* Multiply by a single digit and add a single digit, ignoring the sign. */
1403
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001404static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001405muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001406{
Christian Heimes90aa7642007-12-19 02:45:37 +00001407 Py_ssize_t size_a = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001409 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001410 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001411
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001412 if (z == NULL)
1413 return NULL;
1414 for (i = 0; i < size_a; ++i) {
1415 carry += (twodigits)a->ob_digit[i] * n;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001416 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1417 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001418 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001419 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001420 return long_normalize(z);
1421}
1422
Tim Peters212e6142001-07-14 12:23:19 +00001423/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1424 in pout, and returning the remainder. pin and pout point at the LSD.
1425 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001426 _PyLong_Format, but that should be done with great care since longs are
Tim Peters212e6142001-07-14 12:23:19 +00001427 immutable. */
1428
1429static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001430inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001431{
1432 twodigits rem = 0;
1433
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001434 assert(n > 0 && n <= PyLong_MASK);
Tim Peters212e6142001-07-14 12:23:19 +00001435 pin += size;
1436 pout += size;
1437 while (--size >= 0) {
1438 digit hi;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001439 rem = (rem << PyLong_SHIFT) + *--pin;
Tim Peters212e6142001-07-14 12:23:19 +00001440 *--pout = hi = (digit)(rem / n);
1441 rem -= hi * n;
1442 }
1443 return (digit)rem;
1444}
1445
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001446/* Divide a long integer by a digit, returning both the quotient
1447 (as function result) and the remainder (through *prem).
1448 The sign of a is ignored; n should not be zero. */
1449
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001451divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001452{
Christian Heimes90aa7642007-12-19 02:45:37 +00001453 const Py_ssize_t size = ABS(Py_SIZE(a));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001455
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001456 assert(n > 0 && n <= PyLong_MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001457 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001458 if (z == NULL)
1459 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001460 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001461 return long_normalize(z);
1462}
1463
1464/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001465 Return a string object.
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001466 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001467
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001468PyObject *
1469_PyLong_Format(PyObject *aa, int base)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001470{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001471 register PyLongObject *a = (PyLongObject *)aa;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001472 PyObject *str;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001473 Py_ssize_t i, j, sz;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001474 Py_ssize_t size_a;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001475 Py_UNICODE *p;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001476 int bits;
1477 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001478
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479 if (a == NULL || !PyLong_Check(a)) {
1480 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001481 return NULL;
1482 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001483 assert(base >= 2 && base <= 36);
Christian Heimes90aa7642007-12-19 02:45:37 +00001484 size_a = ABS(Py_SIZE(a));
Tim Peters5af4e6c2002-08-12 02:31:19 +00001485
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001486 /* Compute a rough upper bound for the length of the string */
1487 i = base;
1488 bits = 0;
1489 while (i > 1) {
1490 ++bits;
1491 i >>= 1;
1492 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001493 i = 5;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001494 j = size_a*PyLong_SHIFT + bits-1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001495 sz = i + j / bits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001496 if (j / PyLong_SHIFT < size_a || sz < i) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001497 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001498 "int is too large to format");
Thomas Wouters89f507f2006-12-13 04:49:30 +00001499 return NULL;
1500 }
Walter Dörwald1ab83302007-05-18 17:15:44 +00001501 str = PyUnicode_FromUnicode(NULL, sz);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001502 if (str == NULL)
1503 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001504 p = PyUnicode_AS_UNICODE(str) + sz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001505 *p = '\0';
Christian Heimes90aa7642007-12-19 02:45:37 +00001506 if (Py_SIZE(a) < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001507 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001508
Christian Heimes90aa7642007-12-19 02:45:37 +00001509 if (Py_SIZE(a) == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001510 *--p = '0';
1511 }
1512 else if ((base & (base - 1)) == 0) {
1513 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001514 twodigits accum = 0;
1515 int accumbits = 0; /* # of bits in accum */
1516 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001517 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001518 while ((i >>= 1) > 1)
1519 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001520
1521 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001522 accum |= (twodigits)a->ob_digit[i] << accumbits;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001523 accumbits += PyLong_SHIFT;
Tim Peters586b2e32001-07-15 09:11:14 +00001524 assert(accumbits >= basebits);
1525 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001526 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001527 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001528 assert(p > PyUnicode_AS_UNICODE(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001529 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001530 accumbits -= basebits;
1531 accum >>= basebits;
1532 } while (i < size_a-1 ? accumbits >= basebits :
1533 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001534 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001535 }
1536 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001537 /* Not 0, and base not a power of 2. Divide repeatedly by
1538 base, but for speed use the highest power of base that
1539 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001540 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001541 digit *pin = a->ob_digit;
1542 PyLongObject *scratch;
1543 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001544 digit powbase = base; /* powbase == base ** power */
1545 int power = 1;
1546 for (;;) {
1547 unsigned long newpow = powbase * (unsigned long)base;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001548 if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */
Tim Petersfad225f2001-07-13 02:59:26 +00001549 break;
1550 powbase = (digit)newpow;
1551 ++power;
1552 }
Tim Peters212e6142001-07-14 12:23:19 +00001553
1554 /* Get a scratch area for repeated division. */
1555 scratch = _PyLong_New(size);
1556 if (scratch == NULL) {
1557 Py_DECREF(str);
1558 return NULL;
1559 }
1560
1561 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001562 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001563 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001564 digit rem = inplace_divrem1(scratch->ob_digit,
1565 pin, size, powbase);
1566 pin = scratch->ob_digit; /* no need to use a again */
1567 if (pin[size - 1] == 0)
1568 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001569 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001570 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001571 Py_DECREF(str);
1572 return NULL;
1573 })
Tim Peters212e6142001-07-14 12:23:19 +00001574
1575 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001576 assert(ntostore > 0);
1577 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001578 digit nextrem = (digit)(rem / base);
1579 char c = (char)(rem - nextrem * base);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001580 assert(p > PyUnicode_AS_UNICODE(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001581 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001582 *--p = c;
1583 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001584 --ntostore;
1585 /* Termination is a bit delicate: must not
1586 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001587 remaining quotient and rem are both 0. */
1588 } while (ntostore && (size || rem));
1589 } while (size != 0);
1590 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001591 }
1592
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001593 if (base == 16) {
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001594 *--p = 'x';
1595 *--p = '0';
1596 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001597 else if (base == 8) {
1598 *--p = 'o';
1599 *--p = '0';
1600 }
1601 else if (base == 2) {
1602 *--p = 'b';
1603 *--p = '0';
1604 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001605 else if (base != 10) {
1606 *--p = '#';
1607 *--p = '0' + base%10;
1608 if (base > 10)
1609 *--p = '0' + base/10;
1610 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001611 if (sign)
1612 *--p = sign;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001613 if (p != PyUnicode_AS_UNICODE(str)) {
1614 Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001615 assert(p > q);
1616 do {
1617 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001618 q--;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001619 if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) {
1620 Py_DECREF(str);
1621 return NULL;
1622 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001623 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001624 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001625}
1626
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627/* Table of digit values for 8-bit string -> integer conversion.
1628 * '0' maps to 0, ..., '9' maps to 9.
1629 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1630 * All other indices map to 37.
1631 * Note that when converting a base B string, a char c is a legitimate
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001632 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633 */
1634int _PyLong_DigitValue[256] = {
1635 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1636 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1637 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1638 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
1639 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1640 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1641 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1642 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1643 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1644 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1645 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1646 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1647 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1648 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1649 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1650 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1651};
1652
1653/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00001654 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1655 * non-digit (which may be *str!). A normalized long is returned.
1656 * The point to this routine is that it takes time linear in the number of
1657 * string characters.
1658 */
1659static PyLongObject *
1660long_from_binary_base(char **str, int base)
1661{
1662 char *p = *str;
1663 char *start = p;
1664 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001665 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001666 PyLongObject *z;
1667 twodigits accum;
1668 int bits_in_accum;
1669 digit *pdigit;
1670
1671 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1672 n = base;
1673 for (bits_per_char = -1; n; ++bits_per_char)
1674 n >>= 1;
1675 /* n <- total # of bits needed, while setting p to end-of-string */
1676 n = 0;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001677 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
Tim Petersbf2674b2003-02-02 07:51:32 +00001678 ++p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001679 *str = p;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001680 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1681 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001682 if (n / bits_per_char < p - start) {
Tim Peters1a3b19a2003-02-02 17:33:53 +00001683 PyErr_SetString(PyExc_ValueError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001684 "int string too large to convert");
Tim Peters1a3b19a2003-02-02 17:33:53 +00001685 return NULL;
1686 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001687 n = n / PyLong_SHIFT;
Tim Petersbf2674b2003-02-02 07:51:32 +00001688 z = _PyLong_New(n);
1689 if (z == NULL)
1690 return NULL;
1691 /* Read string from right, and fill in long from left; i.e.,
1692 * from least to most significant in both.
1693 */
1694 accum = 0;
1695 bits_in_accum = 0;
1696 pdigit = z->ob_digit;
1697 while (--p >= start) {
Christian Heimesbbe741d2008-03-28 10:53:29 +00001698 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
Tim Petersc7bc0b92003-05-05 20:39:43 +00001699 assert(k >= 0 && k < base);
1700 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001701 bits_in_accum += bits_per_char;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001702 if (bits_in_accum >= PyLong_SHIFT) {
1703 *pdigit++ = (digit)(accum & PyLong_MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001704 assert(pdigit - z->ob_digit <= (int)n);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001705 accum >>= PyLong_SHIFT;
1706 bits_in_accum -= PyLong_SHIFT;
1707 assert(bits_in_accum < PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001708 }
1709 }
1710 if (bits_in_accum) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001711 assert(bits_in_accum <= PyLong_SHIFT);
Tim Petersbf2674b2003-02-02 07:51:32 +00001712 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001713 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001714 }
1715 while (pdigit - z->ob_digit < n)
1716 *pdigit++ = 0;
1717 return long_normalize(z);
1718}
1719
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001720PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001721PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001722{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001723 int sign = 1, error_if_nonzero = 0;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001724 char *start, *orig_str = str;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001725 PyLongObject *z = NULL;
Guido van Rossum25236212007-08-22 23:28:23 +00001726 PyObject *strobj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001727 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001728
Guido van Rossum472c04f1996-12-05 21:57:21 +00001729 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001730 PyErr_SetString(PyExc_ValueError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001731 "int() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001732 return NULL;
1733 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001734 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001735 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001736 if (*str == '+')
1737 ++str;
1738 else if (*str == '-') {
1739 ++str;
1740 sign = -1;
1741 }
1742 if (base == 0) {
1743 if (str[0] != '0')
1744 base = 10;
1745 else if (str[1] == 'x' || str[1] == 'X')
1746 base = 16;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001747 else if (str[1] == 'o' || str[1] == 'O')
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001748 base = 8;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001749 else if (str[1] == 'b' || str[1] == 'B')
1750 base = 2;
1751 else {
1752 /* "old" (C-style) octal literal, now invalid.
1753 it might still be zero though */
1754 error_if_nonzero = 1;
1755 base = 10;
1756 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001757 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001758 if (str[0] == '0' &&
1759 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1760 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
1761 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001762 str += 2;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001763
Guido van Rossume6762971998-06-22 03:54:46 +00001764 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001765 if ((base & (base - 1)) == 0)
1766 z = long_from_binary_base(&str, base);
1767 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001768/***
1769Binary bases can be converted in time linear in the number of digits, because
1770Python's representation base is binary. Other bases (including decimal!) use
1771the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00001772
Thomas Wouters477c8d52006-05-27 19:21:47 +00001773First some math: the largest integer that can be expressed in N base-B digits
1774is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
1775case number of Python digits needed to hold it is the smallest integer n s.t.
1776
1777 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
1778 BASE**n >= B**N [taking logs to base BASE]
1779 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
1780
1781The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
1782this quickly. A Python long with that much space is reserved near the start,
1783and the result is computed into it.
1784
1785The input string is actually treated as being in base base**i (i.e., i digits
1786are processed at a time), where two more static arrays hold:
1787
1788 convwidth_base[base] = the largest integer i such that base**i <= BASE
1789 convmultmax_base[base] = base ** convwidth_base[base]
1790
1791The first of these is the largest i such that i consecutive input digits
1792must fit in a single Python digit. The second is effectively the input
1793base we're really using.
1794
1795Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1796convmultmax_base[base], the result is "simply"
1797
1798 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1799
1800where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001801
1802Error analysis: as above, the number of Python digits `n` needed is worst-
1803case
1804
1805 n >= N * log(B)/log(BASE)
1806
1807where `N` is the number of input digits in base `B`. This is computed via
1808
1809 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
1810
1811below. Two numeric concerns are how much space this can waste, and whether
1812the computed result can be too small. To be concrete, assume BASE = 2**15,
1813which is the default (and it's unlikely anyone changes that).
1814
1815Waste isn't a problem: provided the first input digit isn't 0, the difference
1816between the worst-case input with N digits and the smallest input with N
1817digits is about a factor of B, but B is small compared to BASE so at most
1818one allocated Python digit can remain unused on that count. If
1819N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
1820and adding 1 returns a result 1 larger than necessary. However, that can't
1821happen: whenever B is a power of 2, long_from_binary_base() is called
1822instead, and it's impossible for B**i to be an integer power of 2**15 when
1823B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
1824an exact integer when B is not a power of 2, since B**i has a prime factor
1825other than 2 in that case, but (2**15)**j's only prime factor is 2).
1826
1827The computed result can be too small if the true value of N*log(B)/log(BASE)
1828is a little bit larger than an exact integer, but due to roundoff errors (in
1829computing log(B), log(BASE), their quotient, and/or multiplying that by N)
1830yields a numeric result a little less than that integer. Unfortunately, "how
1831close can a transcendental function get to an integer over some range?"
1832questions are generally theoretically intractable. Computer analysis via
1833continued fractions is practical: expand log(B)/log(BASE) via continued
1834fractions, giving a sequence i/j of "the best" rational approximations. Then
1835j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
1836we can get very close to being in trouble, but very rarely. For example,
183776573 is a denominator in one of the continued-fraction approximations to
1838log(10)/log(2**15), and indeed:
1839
1840 >>> log(10)/log(2**15)*76573
1841 16958.000000654003
1842
1843is very close to an integer. If we were working with IEEE single-precision,
1844rounding errors could kill us. Finding worst cases in IEEE double-precision
1845requires better-than-double-precision log() functions, and Tim didn't bother.
1846Instead the code checks to see whether the allocated space is enough as each
1847new Python digit is added, and copies the whole thing to a larger long if not.
1848This should happen extremely rarely, and in fact I don't have a test case
1849that triggers it(!). Instead the code was tested by artificially allocating
1850just 1 digit at the start, so that the copying code was exercised for every
1851digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001852***/
1853 register twodigits c; /* current input character */
1854 Py_ssize_t size_z;
1855 int i;
1856 int convwidth;
1857 twodigits convmultmax, convmult;
1858 digit *pz, *pzstop;
1859 char* scan;
1860
1861 static double log_base_BASE[37] = {0.0e0,};
1862 static int convwidth_base[37] = {0,};
1863 static twodigits convmultmax_base[37] = {0,};
1864
1865 if (log_base_BASE[base] == 0.0) {
1866 twodigits convmax = base;
1867 int i = 1;
1868
1869 log_base_BASE[base] = log((double)base) /
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001870 log((double)PyLong_BASE);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871 for (;;) {
1872 twodigits next = convmax * base;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001873 if (next > PyLong_BASE)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001874 break;
1875 convmax = next;
1876 ++i;
1877 }
1878 convmultmax_base[base] = convmax;
1879 assert(i > 0);
1880 convwidth_base[base] = i;
1881 }
1882
1883 /* Find length of the string of numeric characters. */
1884 scan = str;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001885 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886 ++scan;
1887
1888 /* Create a long object that can contain the largest possible
1889 * integer with this base and length. Note that there's no
1890 * need to initialize z->ob_digit -- no slot is read up before
1891 * being stored into.
1892 */
1893 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001894 /* Uncomment next line to test exceedingly rare copy code */
1895 /* size_z = 1; */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896 assert(size_z > 0);
1897 z = _PyLong_New(size_z);
1898 if (z == NULL)
1899 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00001900 Py_SIZE(z) = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001901
1902 /* `convwidth` consecutive input digits are treated as a single
1903 * digit in base `convmultmax`.
1904 */
1905 convwidth = convwidth_base[base];
1906 convmultmax = convmultmax_base[base];
1907
1908 /* Work ;-) */
1909 while (str < scan) {
1910 /* grab up to convwidth digits from the input string */
Christian Heimesbbe741d2008-03-28 10:53:29 +00001911 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001912 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1913 c = (twodigits)(c * base +
Christian Heimesbbe741d2008-03-28 10:53:29 +00001914 _PyLong_DigitValue[Py_CHARMASK(*str)]);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001915 assert(c < PyLong_BASE);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916 }
1917
1918 convmult = convmultmax;
1919 /* Calculate the shift only if we couldn't get
1920 * convwidth digits.
1921 */
1922 if (i != convwidth) {
1923 convmult = base;
1924 for ( ; i > 1; --i)
1925 convmult *= base;
1926 }
1927
1928 /* Multiply z by convmult, and add c. */
1929 pz = z->ob_digit;
Christian Heimes90aa7642007-12-19 02:45:37 +00001930 pzstop = pz + Py_SIZE(z);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001931 for (; pz < pzstop; ++pz) {
1932 c += (twodigits)*pz * convmult;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001933 *pz = (digit)(c & PyLong_MASK);
1934 c >>= PyLong_SHIFT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001935 }
1936 /* carry off the current end? */
1937 if (c) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001938 assert(c < PyLong_BASE);
Christian Heimes90aa7642007-12-19 02:45:37 +00001939 if (Py_SIZE(z) < size_z) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001940 *pz = (digit)c;
Christian Heimes90aa7642007-12-19 02:45:37 +00001941 ++Py_SIZE(z);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001942 }
1943 else {
1944 PyLongObject *tmp;
1945 /* Extremely rare. Get more space. */
Christian Heimes90aa7642007-12-19 02:45:37 +00001946 assert(Py_SIZE(z) == size_z);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001947 tmp = _PyLong_New(size_z + 1);
1948 if (tmp == NULL) {
1949 Py_DECREF(z);
1950 return NULL;
1951 }
1952 memcpy(tmp->ob_digit,
1953 z->ob_digit,
1954 sizeof(digit) * size_z);
1955 Py_DECREF(z);
1956 z = tmp;
1957 z->ob_digit[size_z] = (digit)c;
1958 ++size_z;
1959 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001960 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001961 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001962 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001963 if (z == NULL)
1964 return NULL;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001965 if (error_if_nonzero) {
1966 /* reset the base to 0, else the exception message
1967 doesn't make too much sense */
1968 base = 0;
Christian Heimes90aa7642007-12-19 02:45:37 +00001969 if (Py_SIZE(z) != 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001970 goto onError;
1971 /* there might still be other problems, therefore base
1972 remains zero here for the same reason */
1973 }
Guido van Rossum9e896b32000-04-05 20:11:21 +00001974 if (str == start)
1975 goto onError;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001976 if (sign < 0)
Christian Heimes90aa7642007-12-19 02:45:37 +00001977 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001978 if (*str == 'L' || *str == 'l')
1979 str++;
1980 while (*str && isspace(Py_CHARMASK(*str)))
1981 str++;
1982 if (*str != '\0')
1983 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001984 if (pend)
1985 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001986 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001987
1988 onError:
Guido van Rossum9e896b32000-04-05 20:11:21 +00001989 Py_XDECREF(z);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001990 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
Guido van Rossum25236212007-08-22 23:28:23 +00001991 strobj = PyUnicode_FromStringAndSize(orig_str, slen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001992 if (strobj == NULL)
1993 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001994 PyErr_Format(PyExc_ValueError,
Guido van Rossum25236212007-08-22 23:28:23 +00001995 "invalid literal for int() with base %d: %R",
1996 base, strobj);
1997 Py_DECREF(strobj);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001998 return NULL;
1999}
2000
2001PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00002003{
Walter Dörwald07e14762002-11-06 16:15:14 +00002004 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002005 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002006
Walter Dörwald07e14762002-11-06 16:15:14 +00002007 if (buffer == NULL)
2008 return NULL;
2009
2010 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
2011 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00002012 return NULL;
2013 }
Walter Dörwald07e14762002-11-06 16:15:14 +00002014 result = PyLong_FromString(buffer, NULL, base);
2015 PyMem_FREE(buffer);
2016 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002017}
2018
Tim Peters9f688bf2000-07-07 15:53:28 +00002019/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002020static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00002021 (PyLongObject *, PyLongObject *, PyLongObject **);
Guido van Rossumb43daf72007-08-01 18:08:08 +00002022static PyObject *long_long(PyObject *v);
Tim Peters9f688bf2000-07-07 15:53:28 +00002023static int long_divrem(PyLongObject *, PyLongObject *,
2024 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002025
2026/* Long division with remainder, top-level routine */
2027
Guido van Rossume32e0141992-01-19 16:31:05 +00002028static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002029long_divrem(PyLongObject *a, PyLongObject *b,
2030 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002031{
Christian Heimes90aa7642007-12-19 02:45:37 +00002032 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002033 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002034
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002035 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002036 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002037 "integer division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00002038 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002039 }
2040 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00002041 (size_a == size_b &&
2042 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002043 /* |a| < |b|. */
Guido van Rossumddefaf32007-01-14 03:31:43 +00002044 *pdiv = (PyLongObject*)PyLong_FromLong(0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002045 if (*pdiv == NULL)
2046 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047 Py_INCREF(a);
2048 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00002049 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002050 }
2051 if (size_b == 1) {
2052 digit rem = 0;
2053 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002054 if (z == NULL)
2055 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002057 if (*prem == NULL) {
2058 Py_DECREF(z);
2059 return -1;
2060 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002061 }
Guido van Rossume32e0141992-01-19 16:31:05 +00002062 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002063 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002064 if (z == NULL)
2065 return -1;
2066 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002067 /* Set the signs.
2068 The quotient z has the sign of a*b;
2069 the remainder r has the sign of a,
2070 so a = b*z + r. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002071 if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
Guido van Rossumddefaf32007-01-14 03:31:43 +00002072 NEGATE(z);
Christian Heimes90aa7642007-12-19 02:45:37 +00002073 if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002074 NEGATE(*prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00002075 *pdiv = z;
2076 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002077}
2078
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002079/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002081static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002082x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002083{
Christian Heimes90aa7642007-12-19 02:45:37 +00002084 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002085 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002086 PyLongObject *v = mul1(v1, d);
2087 PyLongObject *w = mul1(w1, d);
2088 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002089 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002090
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002091 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002092 Py_XDECREF(v);
2093 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002094 return NULL;
2095 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002096
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002097 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Christian Heimes90aa7642007-12-19 02:45:37 +00002098 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
2099 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002100
Christian Heimes90aa7642007-12-19 02:45:37 +00002101 size_v = ABS(Py_SIZE(v));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002102 k = size_v - size_w;
2103 a = _PyLong_New(k + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002104
Thomas Wouters477c8d52006-05-27 19:21:47 +00002105 for (j = size_v; a != NULL && k >= 0; --j, --k) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002106 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
2107 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002108 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002109 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002110
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002111 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002112 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002113 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002114 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002115 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002116 if (vj == w->ob_digit[size_w-1])
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002117 q = PyLong_MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002118 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002119 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002120 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00002121
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002122 while (w->ob_digit[size_w-2]*q >
2123 ((
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002124 ((twodigits)vj << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002125 + v->ob_digit[j-1]
2126 - q*w->ob_digit[size_w-1]
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002127 ) << PyLong_SHIFT)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002128 + v->ob_digit[j-2])
2129 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002130
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002131 for (i = 0; i < size_w && i+k < size_v; ++i) {
2132 twodigits z = w->ob_digit[i] * q;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002133 digit zz = (digit) (z >> PyLong_SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002134 carry += v->ob_digit[i+k] - z
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002135 + ((twodigits)zz << PyLong_SHIFT);
2136 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00002137 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002138 carry, PyLong_SHIFT);
Tim Peters7d3a5112000-07-08 04:17:21 +00002139 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002140 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002141
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002142 if (i+k < size_v) {
2143 carry += v->ob_digit[i+k];
2144 v->ob_digit[i+k] = 0;
2145 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002146
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002147 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00002148 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002149 else {
2150 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00002151 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002152 carry = 0;
2153 for (i = 0; i < size_w && i+k < size_v; ++i) {
2154 carry += v->ob_digit[i+k] + w->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002155 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00002156 carry = Py_ARITHMETIC_RIGHT_SHIFT(
2157 BASE_TWODIGITS_TYPE,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002158 carry, PyLong_SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002159 }
2160 }
2161 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002162
Guido van Rossumc206c761995-01-10 15:23:19 +00002163 if (a == NULL)
2164 *prem = NULL;
2165 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00002166 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00002167 *prem = divrem1(v, d, &d);
2168 /* d receives the (unused) remainder */
2169 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002170 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00002171 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002172 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002173 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002174 Py_DECREF(v);
2175 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002176 return a;
2177}
2178
2179/* Methods */
2180
2181static void
Tim Peters9f688bf2000-07-07 15:53:28 +00002182long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002183{
Christian Heimes90aa7642007-12-19 02:45:37 +00002184 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002185}
2186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002187static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002188long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002189{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002190 return _PyLong_Format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002191}
2192
2193static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002194long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002195{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002196 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002197
Christian Heimes90aa7642007-12-19 02:45:37 +00002198 if (Py_SIZE(a) != Py_SIZE(b)) {
2199 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002200 sign = 0;
2201 else
Christian Heimes90aa7642007-12-19 02:45:37 +00002202 sign = Py_SIZE(a) - Py_SIZE(b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002203 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002204 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002205 Py_ssize_t i = ABS(Py_SIZE(a));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002206 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2207 ;
2208 if (i < 0)
2209 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002210 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002211 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Christian Heimes90aa7642007-12-19 02:45:37 +00002212 if (Py_SIZE(a) < 0)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00002213 sign = -sign;
2214 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002215 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00002216 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002217}
2218
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002219static PyObject *
2220long_richcompare(PyObject *self, PyObject *other, int op)
2221{
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002222 PyObject *result;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002223 CHECK_BINOP(self, other);
2224 result = Py_CmpToRich(op, long_compare((PyLongObject*)self,
2225 (PyLongObject*)other));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002226 return result;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002227}
2228
Guido van Rossum9bfef441993-03-29 10:43:31 +00002229static long
Tim Peters9f688bf2000-07-07 15:53:28 +00002230long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002231{
2232 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002233 Py_ssize_t i;
2234 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002235
2236 /* This is designed so that Python ints and longs with the
2237 same value hash to the same value, otherwise comparisons
2238 of mapping keys will turn out weird */
Christian Heimes90aa7642007-12-19 02:45:37 +00002239 i = Py_SIZE(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +00002240 switch(i) {
2241 case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0];
2242 case 0: return 0;
2243 case 1: return v->ob_digit[0];
2244 }
Guido van Rossum9bfef441993-03-29 10:43:31 +00002245 sign = 1;
2246 x = 0;
2247 if (i < 0) {
2248 sign = -1;
2249 i = -(i);
2250 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002251#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
Thomas Woutersce272b62007-09-19 21:19:28 +00002252 /* The following loop produces a C long x such that (unsigned long)x
2253 is congruent to the absolute value of v modulo ULONG_MAX. The
2254 resulting x is nonzero if and only if v is. */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002255 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00002256 /* Force a native long #-bits (32 or 64) circular shift */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002257 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002258 x += v->ob_digit[i];
Thomas Woutersce272b62007-09-19 21:19:28 +00002259 /* If the addition above overflowed (thinking of x as
2260 unsigned), we compensate by incrementing. This preserves
2261 the value modulo ULONG_MAX. */
2262 if ((unsigned long)x < v->ob_digit[i])
2263 x++;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002264 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002265#undef LONG_BIT_PyLong_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00002266 x = x * sign;
2267 if (x == -1)
2268 x = -2;
2269 return x;
2270}
2271
2272
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002273/* Add the absolute values of two long integers. */
2274
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002275static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002276x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002277{
Christian Heimes90aa7642007-12-19 02:45:37 +00002278 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002279 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002280 int i;
2281 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002282
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002283 /* Ensure a is the larger of the two: */
2284 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002285 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002286 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002287 size_a = size_b;
2288 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002289 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002290 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002291 if (z == NULL)
2292 return NULL;
2293 for (i = 0; i < size_b; ++i) {
2294 carry += a->ob_digit[i] + b->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002295 z->ob_digit[i] = carry & PyLong_MASK;
2296 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002297 }
2298 for (; i < size_a; ++i) {
2299 carry += a->ob_digit[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002300 z->ob_digit[i] = carry & PyLong_MASK;
2301 carry >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002302 }
2303 z->ob_digit[i] = carry;
2304 return long_normalize(z);
2305}
2306
2307/* Subtract the absolute values of two integers. */
2308
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002309static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002310x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002311{
Christian Heimes90aa7642007-12-19 02:45:37 +00002312 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002313 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002314 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002315 int sign = 1;
2316 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00002317
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002318 /* Ensure a is the larger of the two: */
2319 if (size_a < size_b) {
2320 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002321 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002322 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002323 size_a = size_b;
2324 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002325 }
2326 else if (size_a == size_b) {
2327 /* Find highest digit where a and b differ: */
2328 i = size_a;
2329 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
2330 ;
2331 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002332 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002333 if (a->ob_digit[i] < b->ob_digit[i]) {
2334 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002335 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002336 }
2337 size_a = size_b = i+1;
2338 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002339 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002340 if (z == NULL)
2341 return NULL;
2342 for (i = 0; i < size_b; ++i) {
2343 /* The following assumes unsigned arithmetic
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002344 works module 2**N for some N>PyLong_SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002345 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002346 z->ob_digit[i] = borrow & PyLong_MASK;
2347 borrow >>= PyLong_SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002348 borrow &= 1; /* Keep only one sign bit */
2349 }
2350 for (; i < size_a; ++i) {
2351 borrow = a->ob_digit[i] - borrow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002352 z->ob_digit[i] = borrow & PyLong_MASK;
2353 borrow >>= PyLong_SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00002354 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002355 }
2356 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002357 if (sign < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002358 NEGATE(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002359 return long_normalize(z);
2360}
2361
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002362static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002363long_add(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002364{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002365 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002366
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002367 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002368
Christian Heimes90aa7642007-12-19 02:45:37 +00002369 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Christian Heimes217cfd12007-12-02 14:31:20 +00002370 PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) +
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002371 MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002372 return result;
2373 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002374 if (Py_SIZE(a) < 0) {
2375 if (Py_SIZE(b) < 0) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002376 z = x_add(a, b);
Christian Heimes90aa7642007-12-19 02:45:37 +00002377 if (z != NULL && Py_SIZE(z) != 0)
2378 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002379 }
2380 else
2381 z = x_sub(b, a);
2382 }
2383 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002384 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002385 z = x_sub(a, b);
2386 else
2387 z = x_add(a, b);
2388 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002389 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002390}
2391
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002392static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002393long_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002394{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002395 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002396
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002397 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002398
Christian Heimes90aa7642007-12-19 02:45:37 +00002399 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002400 PyObject* r;
2401 r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002402 return r;
2403 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002404 if (Py_SIZE(a) < 0) {
2405 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002406 z = x_sub(a, b);
2407 else
2408 z = x_add(a, b);
Christian Heimes90aa7642007-12-19 02:45:37 +00002409 if (z != NULL && Py_SIZE(z) != 0)
2410 Py_SIZE(z) = -(Py_SIZE(z));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002411 }
2412 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00002413 if (Py_SIZE(b) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002414 z = x_add(a, b);
2415 else
2416 z = x_sub(a, b);
2417 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002418 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002419}
2420
Tim Peters5af4e6c2002-08-12 02:31:19 +00002421/* Grade school multiplication, ignoring the signs.
2422 * Returns the absolute value of the product, or NULL if error.
2423 */
2424static PyLongObject *
2425x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002426{
Tim Peters5af4e6c2002-08-12 02:31:19 +00002427 PyLongObject *z;
Christian Heimes90aa7642007-12-19 02:45:37 +00002428 Py_ssize_t size_a = ABS(Py_SIZE(a));
2429 Py_ssize_t size_b = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002430 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002431
Tim Peters5af4e6c2002-08-12 02:31:19 +00002432 z = _PyLong_New(size_a + size_b);
2433 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002434 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002435
Christian Heimes90aa7642007-12-19 02:45:37 +00002436 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00002437 if (a == b) {
2438 /* Efficient squaring per HAC, Algorithm 14.16:
2439 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
2440 * Gives slightly less than a 2x speedup when a == b,
2441 * via exploiting that each entry in the multiplication
2442 * pyramid appears twice (except for the size_a squares).
2443 */
2444 for (i = 0; i < size_a; ++i) {
2445 twodigits carry;
2446 twodigits f = a->ob_digit[i];
2447 digit *pz = z->ob_digit + (i << 1);
2448 digit *pa = a->ob_digit + i + 1;
2449 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002450
Tim Peters0973b992004-08-29 22:16:50 +00002451 SIGCHECK({
2452 Py_DECREF(z);
2453 return NULL;
2454 })
2455
2456 carry = *pz + f * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002457 *pz++ = (digit)(carry & PyLong_MASK);
2458 carry >>= PyLong_SHIFT;
2459 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002460
2461 /* Now f is added in twice in each column of the
2462 * pyramid it appears. Same as adding f<<1 once.
2463 */
2464 f <<= 1;
2465 while (pa < paend) {
2466 carry += *pz + *pa++ * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002467 *pz++ = (digit)(carry & PyLong_MASK);
2468 carry >>= PyLong_SHIFT;
2469 assert(carry <= (PyLong_MASK << 1));
Tim Peters0973b992004-08-29 22:16:50 +00002470 }
2471 if (carry) {
2472 carry += *pz;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002473 *pz++ = (digit)(carry & PyLong_MASK);
2474 carry >>= PyLong_SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00002475 }
2476 if (carry)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002477 *pz += (digit)(carry & PyLong_MASK);
2478 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002479 }
Tim Peters0973b992004-08-29 22:16:50 +00002480 }
2481 else { /* a is not the same as b -- gradeschool long mult */
2482 for (i = 0; i < size_a; ++i) {
2483 twodigits carry = 0;
2484 twodigits f = a->ob_digit[i];
2485 digit *pz = z->ob_digit + i;
2486 digit *pb = b->ob_digit;
2487 digit *pbend = b->ob_digit + size_b;
2488
2489 SIGCHECK({
2490 Py_DECREF(z);
2491 return NULL;
2492 })
2493
2494 while (pb < pbend) {
2495 carry += *pz + *pb++ * f;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002496 *pz++ = (digit)(carry & PyLong_MASK);
2497 carry >>= PyLong_SHIFT;
2498 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00002499 }
2500 if (carry)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002501 *pz += (digit)(carry & PyLong_MASK);
2502 assert((carry >> PyLong_SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002503 }
2504 }
Tim Peters44121a62002-08-12 06:17:58 +00002505 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002506}
2507
2508/* A helper for Karatsuba multiplication (k_mul).
2509 Takes a long "n" and an integer "size" representing the place to
2510 split, and sets low and high such that abs(n) == (high << size) + low,
2511 viewing the shift as being by digits. The sign bit is ignored, and
2512 the return values are >= 0.
2513 Returns 0 on success, -1 on failure.
2514*/
2515static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002516kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002517{
2518 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002519 Py_ssize_t size_lo, size_hi;
Christian Heimes90aa7642007-12-19 02:45:37 +00002520 const Py_ssize_t size_n = ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002521
2522 size_lo = MIN(size_n, size);
2523 size_hi = size_n - size_lo;
2524
2525 if ((hi = _PyLong_New(size_hi)) == NULL)
2526 return -1;
2527 if ((lo = _PyLong_New(size_lo)) == NULL) {
2528 Py_DECREF(hi);
2529 return -1;
2530 }
2531
2532 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
2533 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
2534
2535 *high = long_normalize(hi);
2536 *low = long_normalize(lo);
2537 return 0;
2538}
2539
Tim Peters60004642002-08-12 22:01:34 +00002540static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
2541
Tim Peters5af4e6c2002-08-12 02:31:19 +00002542/* Karatsuba multiplication. Ignores the input signs, and returns the
2543 * absolute value of the product (or NULL if error).
2544 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
2545 */
2546static PyLongObject *
2547k_mul(PyLongObject *a, PyLongObject *b)
2548{
Christian Heimes90aa7642007-12-19 02:45:37 +00002549 Py_ssize_t asize = ABS(Py_SIZE(a));
2550 Py_ssize_t bsize = ABS(Py_SIZE(b));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002551 PyLongObject *ah = NULL;
2552 PyLongObject *al = NULL;
2553 PyLongObject *bh = NULL;
2554 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002555 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002556 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002557 Py_ssize_t shift; /* the number of digits we split off */
2558 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002559
Tim Peters5af4e6c2002-08-12 02:31:19 +00002560 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2561 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2562 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002563 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002564 * By picking X to be a power of 2, "*X" is just shifting, and it's
2565 * been reduced to 3 multiplies on numbers half the size.
2566 */
2567
Tim Petersfc07e562002-08-12 02:54:10 +00002568 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002569 * is largest.
2570 */
Tim Peters738eda72002-08-12 15:08:20 +00002571 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002572 t1 = a;
2573 a = b;
2574 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002575
2576 i = asize;
2577 asize = bsize;
2578 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002579 }
2580
2581 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002582 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2583 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002584 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002585 return _PyLong_New(0);
2586 else
2587 return x_mul(a, b);
2588 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002589
Tim Peters60004642002-08-12 22:01:34 +00002590 /* If a is small compared to b, splitting on b gives a degenerate
2591 * case with ah==0, and Karatsuba may be (even much) less efficient
2592 * than "grade school" then. However, we can still win, by viewing
2593 * b as a string of "big digits", each of width a->ob_size. That
2594 * leads to a sequence of balanced calls to k_mul.
2595 */
2596 if (2 * asize <= bsize)
2597 return k_lopsided_mul(a, b);
2598
Tim Petersd6974a52002-08-13 20:37:51 +00002599 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002600 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002601 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002602 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00002603
Tim Peters0973b992004-08-29 22:16:50 +00002604 if (a == b) {
2605 bh = ah;
2606 bl = al;
2607 Py_INCREF(bh);
2608 Py_INCREF(bl);
2609 }
2610 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002611
Tim Petersd64c1de2002-08-12 17:36:03 +00002612 /* The plan:
2613 * 1. Allocate result space (asize + bsize digits: that's always
2614 * enough).
2615 * 2. Compute ah*bh, and copy into result at 2*shift.
2616 * 3. Compute al*bl, and copy into result at 0. Note that this
2617 * can't overlap with #2.
2618 * 4. Subtract al*bl from the result, starting at shift. This may
2619 * underflow (borrow out of the high digit), but we don't care:
2620 * we're effectively doing unsigned arithmetic mod
2621 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2622 * borrows and carries out of the high digit can be ignored.
2623 * 5. Subtract ah*bh from the result, starting at shift.
2624 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2625 * at shift.
2626 */
2627
2628 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002629 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002630 if (ret == NULL) goto fail;
2631#ifdef Py_DEBUG
2632 /* Fill with trash, to catch reference to uninitialized digits. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002633 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002634#endif
Tim Peters44121a62002-08-12 06:17:58 +00002635
Tim Petersd64c1de2002-08-12 17:36:03 +00002636 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002637 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002638 assert(Py_SIZE(t1) >= 0);
2639 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
Tim Peters738eda72002-08-12 15:08:20 +00002640 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
Christian Heimes90aa7642007-12-19 02:45:37 +00002641 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00002642
2643 /* Zero-out the digits higher than the ah*bh copy. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002644 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
Tim Peters44121a62002-08-12 06:17:58 +00002645 if (i)
Christian Heimes90aa7642007-12-19 02:45:37 +00002646 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
Tim Peters44121a62002-08-12 06:17:58 +00002647 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002648
Tim Petersd64c1de2002-08-12 17:36:03 +00002649 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002650 if ((t2 = k_mul(al, bl)) == NULL) {
2651 Py_DECREF(t1);
2652 goto fail;
2653 }
Christian Heimes90aa7642007-12-19 02:45:37 +00002654 assert(Py_SIZE(t2) >= 0);
2655 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
2656 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002657
2658 /* Zero out remaining digits. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002659 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002660 if (i)
Christian Heimes90aa7642007-12-19 02:45:37 +00002661 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002662
Tim Petersd64c1de2002-08-12 17:36:03 +00002663 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2664 * because it's fresher in cache.
2665 */
Christian Heimes90aa7642007-12-19 02:45:37 +00002666 i = Py_SIZE(ret) - shift; /* # digits after shift */
2667 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Tim Peters738eda72002-08-12 15:08:20 +00002668 Py_DECREF(t2);
2669
Christian Heimes90aa7642007-12-19 02:45:37 +00002670 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Tim Peters738eda72002-08-12 15:08:20 +00002671 Py_DECREF(t1);
2672
Tim Petersd64c1de2002-08-12 17:36:03 +00002673 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002674 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2675 Py_DECREF(ah);
2676 Py_DECREF(al);
2677 ah = al = NULL;
2678
Tim Peters0973b992004-08-29 22:16:50 +00002679 if (a == b) {
2680 t2 = t1;
2681 Py_INCREF(t2);
2682 }
2683 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002684 Py_DECREF(t1);
2685 goto fail;
2686 }
2687 Py_DECREF(bh);
2688 Py_DECREF(bl);
2689 bh = bl = NULL;
2690
Tim Peters738eda72002-08-12 15:08:20 +00002691 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002692 Py_DECREF(t1);
2693 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002694 if (t3 == NULL) goto fail;
Christian Heimes90aa7642007-12-19 02:45:37 +00002695 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002696
Tim Petersd6974a52002-08-13 20:37:51 +00002697 /* Add t3. It's not obvious why we can't run out of room here.
2698 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002699 */
Christian Heimes90aa7642007-12-19 02:45:37 +00002700 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Tim Peters738eda72002-08-12 15:08:20 +00002701 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002702
Tim Peters5af4e6c2002-08-12 02:31:19 +00002703 return long_normalize(ret);
2704
2705 fail:
2706 Py_XDECREF(ret);
2707 Py_XDECREF(ah);
2708 Py_XDECREF(al);
2709 Py_XDECREF(bh);
2710 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002711 return NULL;
2712}
2713
Tim Petersd6974a52002-08-13 20:37:51 +00002714/* (*) Why adding t3 can't "run out of room" above.
2715
Tim Petersab86c2b2002-08-15 20:06:00 +00002716Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2717to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002718
Tim Petersab86c2b2002-08-15 20:06:00 +000027191. For any integer i, i = c(i/2) + f(i/2). In particular,
2720 bsize = c(bsize/2) + f(bsize/2).
27212. shift = f(bsize/2)
27223. asize <= bsize
27234. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2724 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002725
Tim Petersab86c2b2002-08-15 20:06:00 +00002726We allocated asize + bsize result digits, and add t3 into them at an offset
2727of shift. This leaves asize+bsize-shift allocated digit positions for t3
2728to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2729asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002730
Tim Petersab86c2b2002-08-15 20:06:00 +00002731bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2732at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002733
Tim Petersab86c2b2002-08-15 20:06:00 +00002734If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2735digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2736most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002737
Tim Petersab86c2b2002-08-15 20:06:00 +00002738The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002739
Tim Petersab86c2b2002-08-15 20:06:00 +00002740 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002741
Tim Petersab86c2b2002-08-15 20:06:00 +00002742and we have asize + c(bsize/2) available digit positions. We need to show
2743this is always enough. An instance of c(bsize/2) cancels out in both, so
2744the question reduces to whether asize digits is enough to hold
2745(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2746then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2747asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002748digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00002749asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002750c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2751is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2752bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002753
Tim Peters48d52c02002-08-14 17:07:32 +00002754Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2755clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2756ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002757*/
2758
Tim Peters60004642002-08-12 22:01:34 +00002759/* b has at least twice the digits of a, and a is big enough that Karatsuba
2760 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2761 * of slices, each with a->ob_size digits, and multiply the slices by a,
2762 * one at a time. This gives k_mul balanced inputs to work with, and is
2763 * also cache-friendly (we compute one double-width slice of the result
2764 * at a time, then move on, never bactracking except for the helpful
2765 * single-width slice overlap between successive partial sums).
2766 */
2767static PyLongObject *
2768k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2769{
Christian Heimes90aa7642007-12-19 02:45:37 +00002770 const Py_ssize_t asize = ABS(Py_SIZE(a));
2771 Py_ssize_t bsize = ABS(Py_SIZE(b));
Martin v. Löwis18e16552006-02-15 17:27:45 +00002772 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002773 PyLongObject *ret;
2774 PyLongObject *bslice = NULL;
2775
2776 assert(asize > KARATSUBA_CUTOFF);
2777 assert(2 * asize <= bsize);
2778
2779 /* Allocate result space, and zero it out. */
2780 ret = _PyLong_New(asize + bsize);
2781 if (ret == NULL)
2782 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00002783 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00002784
2785 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002786 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002787 if (bslice == NULL)
2788 goto fail;
2789
2790 nbdone = 0;
2791 while (bsize > 0) {
2792 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002793 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002794
2795 /* Multiply the next slice of b by a. */
2796 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2797 nbtouse * sizeof(digit));
Christian Heimes90aa7642007-12-19 02:45:37 +00002798 Py_SIZE(bslice) = nbtouse;
Tim Peters60004642002-08-12 22:01:34 +00002799 product = k_mul(a, bslice);
2800 if (product == NULL)
2801 goto fail;
2802
2803 /* Add into result. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002804 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
2805 product->ob_digit, Py_SIZE(product));
Tim Peters60004642002-08-12 22:01:34 +00002806 Py_DECREF(product);
2807
2808 bsize -= nbtouse;
2809 nbdone += nbtouse;
2810 }
2811
2812 Py_DECREF(bslice);
2813 return long_normalize(ret);
2814
2815 fail:
2816 Py_DECREF(ret);
2817 Py_XDECREF(bslice);
2818 return NULL;
2819}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002820
2821static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002822long_mul(PyLongObject *a, PyLongObject *b)
Tim Peters5af4e6c2002-08-12 02:31:19 +00002823{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002824 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002825
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002826 CHECK_BINOP(a, b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002827
Christian Heimes90aa7642007-12-19 02:45:37 +00002828 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002829 PyObject *r;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002830 r = PyLong_FromLong(MEDIUM_VALUE(a)*MEDIUM_VALUE(b));
Martin v. Löwis14b6d922007-02-06 21:05:30 +00002831 return r;
2832 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00002833
Tim Petersd64c1de2002-08-12 17:36:03 +00002834 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002835 /* Negate if exactly one of the inputs is negative. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002836 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
Guido van Rossumddefaf32007-01-14 03:31:43 +00002837 NEGATE(z);
Tim Peters9973d742002-08-15 19:41:06 +00002838 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002839}
2840
Guido van Rossume32e0141992-01-19 16:31:05 +00002841/* The / and % operators are now defined in terms of divmod().
2842 The expression a mod b has the value a - b*floor(a/b).
2843 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002844 |a| by |b|, with the sign of a. This is also expressed
2845 as a - b*trunc(a/b), if trunc truncates towards zero.
2846 Some examples:
2847 a b a rem b a mod b
2848 13 10 3 3
2849 -13 10 -3 7
2850 13 -10 3 -7
2851 -13 -10 -3 -3
2852 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002853 have different signs. We then subtract one from the 'div'
2854 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002855
Tim Peters47e52ee2004-08-30 02:44:38 +00002856/* Compute
2857 * *pdiv, *pmod = divmod(v, w)
2858 * NULL can be passed for pdiv or pmod, in which case that part of
2859 * the result is simply thrown away. The caller owns a reference to
2860 * each of these it requests (does not pass NULL for).
2861 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002862static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002863l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002864 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002865{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002866 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002867
Guido van Rossume32e0141992-01-19 16:31:05 +00002868 if (long_divrem(v, w, &div, &mod) < 0)
2869 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00002870 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
2871 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002872 PyLongObject *temp;
2873 PyLongObject *one;
2874 temp = (PyLongObject *) long_add(mod, w);
2875 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002876 mod = temp;
2877 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002878 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002879 return -1;
2880 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002881 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002882 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002883 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2884 Py_DECREF(mod);
2885 Py_DECREF(div);
2886 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002887 return -1;
2888 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002889 Py_DECREF(one);
2890 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002891 div = temp;
2892 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002893 if (pdiv != NULL)
2894 *pdiv = div;
2895 else
2896 Py_DECREF(div);
2897
2898 if (pmod != NULL)
2899 *pmod = mod;
2900 else
2901 Py_DECREF(mod);
2902
Guido van Rossume32e0141992-01-19 16:31:05 +00002903 return 0;
2904}
2905
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002906static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002907long_div(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00002908{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002909 PyLongObject *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002910
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002911 CHECK_BINOP(a, b);
2912 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
Tim Peters47e52ee2004-08-30 02:44:38 +00002913 div = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002914 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002915}
2916
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002917static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002918long_true_divide(PyObject *a, PyObject *b)
Tim Peters20dab9f2001-09-04 05:31:47 +00002919{
Tim Peterse2a60002001-09-04 06:17:36 +00002920 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002921 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002922
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002923 CHECK_BINOP(a, b);
Tim Peterse2a60002001-09-04 06:17:36 +00002924 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2925 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002926 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
Tim Peters6f97e492001-11-04 23:09:40 +00002927 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002928 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002929 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2930 but should really be set correctly after sucessful calls to
2931 _PyLong_AsScaledDouble() */
2932 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002933
2934 if (bd == 0.0) {
2935 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002936 "int division or modulo by zero");
Tim Peterse2a60002001-09-04 06:17:36 +00002937 return NULL;
2938 }
2939
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002940 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
Tim Peterse2a60002001-09-04 06:17:36 +00002941 ad /= bd; /* overflow/underflow impossible here */
2942 aexp -= bexp;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002943 if (aexp > INT_MAX / PyLong_SHIFT)
Tim Peterse2a60002001-09-04 06:17:36 +00002944 goto overflow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002945 else if (aexp < -(INT_MAX / PyLong_SHIFT))
Tim Peterse56ed9b2001-09-06 21:59:14 +00002946 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002947 errno = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002948 ad = ldexp(ad, aexp * PyLong_SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002949 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002950 goto overflow;
2951 return PyFloat_FromDouble(ad);
2952
2953overflow:
2954 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00002955 "int/int too large for a float");
Tim Peterse2a60002001-09-04 06:17:36 +00002956 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002957
Tim Peters20dab9f2001-09-04 05:31:47 +00002958}
2959
2960static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002961long_mod(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00002962{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002963 PyLongObject *mod;
2964
2965 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002966
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002967 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
Tim Peters47e52ee2004-08-30 02:44:38 +00002968 mod = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002969 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002970}
2971
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002972static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002973long_divmod(PyObject *a, PyObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002974{
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002975 PyLongObject *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002976 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002977
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002978 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002979
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00002980 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002981 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002982 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002983 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002984 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002985 PyTuple_SetItem(z, 0, (PyObject *) div);
2986 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002987 }
2988 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002989 Py_DECREF(div);
2990 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002991 }
2992 return z;
2993}
2994
Tim Peters47e52ee2004-08-30 02:44:38 +00002995/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002996static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002997long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002998{
Tim Peters47e52ee2004-08-30 02:44:38 +00002999 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
3000 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003001
Tim Peters47e52ee2004-08-30 02:44:38 +00003002 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00003003 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00003004 PyLongObject *temp = NULL;
3005
3006 /* 5-ary values. If the exponent is large enough, table is
3007 * precomputed so that table[i] == a**i % c for i in range(32).
3008 */
3009 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
3010 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
3011
3012 /* a, b, c = v, w, x */
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003013 CHECK_BINOP(v, w);
3014 a = (PyLongObject*)v; Py_INCREF(a);
3015 b = (PyLongObject*)w; Py_INCREF(b);
Tim Peters47e52ee2004-08-30 02:44:38 +00003016 if (PyLong_Check(x)) {
3017 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003018 Py_INCREF(x);
3019 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003020 else if (x == Py_None)
3021 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003022 else {
3023 Py_DECREF(a);
3024 Py_DECREF(b);
3025 Py_INCREF(Py_NotImplemented);
3026 return Py_NotImplemented;
3027 }
Tim Peters4c483c42001-09-05 06:24:58 +00003028
Christian Heimes90aa7642007-12-19 02:45:37 +00003029 if (Py_SIZE(b) < 0) { /* if exponent is negative */
Tim Peters47e52ee2004-08-30 02:44:38 +00003030 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00003031 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00003032 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00003033 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00003034 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003035 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00003036 /* else return a float. This works because we know
3037 that this calls float_pow() which converts its
3038 arguments to double. */
3039 Py_DECREF(a);
3040 Py_DECREF(b);
3041 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00003042 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003043 }
Tim Peters47e52ee2004-08-30 02:44:38 +00003044
3045 if (c) {
3046 /* if modulus == 0:
3047 raise ValueError() */
Christian Heimes90aa7642007-12-19 02:45:37 +00003048 if (Py_SIZE(c) == 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003049 PyErr_SetString(PyExc_ValueError,
3050 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00003051 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003052 }
3053
3054 /* if modulus < 0:
3055 negativeOutput = True
3056 modulus = -modulus */
Christian Heimes90aa7642007-12-19 02:45:37 +00003057 if (Py_SIZE(c) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003058 negativeOutput = 1;
3059 temp = (PyLongObject *)_PyLong_Copy(c);
3060 if (temp == NULL)
3061 goto Error;
3062 Py_DECREF(c);
3063 c = temp;
3064 temp = NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003065 NEGATE(c);
Tim Peters47e52ee2004-08-30 02:44:38 +00003066 }
3067
3068 /* if modulus == 1:
3069 return 0 */
Christian Heimes90aa7642007-12-19 02:45:37 +00003070 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003071 z = (PyLongObject *)PyLong_FromLong(0L);
3072 goto Done;
3073 }
3074
3075 /* if base < 0:
3076 base = base % modulus
3077 Having the base positive just makes things easier. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003078 if (Py_SIZE(a) < 0) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003079 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00003080 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00003081 Py_DECREF(a);
3082 a = temp;
3083 temp = NULL;
3084 }
3085 }
3086
3087 /* At this point a, b, and c are guaranteed non-negative UNLESS
3088 c is NULL, in which case a may be negative. */
3089
3090 z = (PyLongObject *)PyLong_FromLong(1L);
3091 if (z == NULL)
3092 goto Error;
3093
3094 /* Perform a modular reduction, X = X % c, but leave X alone if c
3095 * is NULL.
3096 */
3097#define REDUCE(X) \
3098 if (c != NULL) { \
3099 if (l_divmod(X, c, NULL, &temp) < 0) \
3100 goto Error; \
3101 Py_XDECREF(X); \
3102 X = temp; \
3103 temp = NULL; \
3104 }
3105
3106 /* Multiply two values, then reduce the result:
3107 result = X*Y % c. If c is NULL, skip the mod. */
3108#define MULT(X, Y, result) \
3109{ \
3110 temp = (PyLongObject *)long_mul(X, Y); \
3111 if (temp == NULL) \
3112 goto Error; \
3113 Py_XDECREF(result); \
3114 result = temp; \
3115 temp = NULL; \
3116 REDUCE(result) \
3117}
3118
Christian Heimes90aa7642007-12-19 02:45:37 +00003119 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003120 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
3121 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
Christian Heimes90aa7642007-12-19 02:45:37 +00003122 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003123 digit bi = b->ob_digit[i];
3124
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003125 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003126 MULT(z, z, z)
3127 if (bi & j)
3128 MULT(z, a, z)
3129 }
3130 }
3131 }
3132 else {
3133 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
3134 Py_INCREF(z); /* still holds 1L */
3135 table[0] = z;
3136 for (i = 1; i < 32; ++i)
3137 MULT(table[i-1], a, table[i])
3138
Christian Heimes90aa7642007-12-19 02:45:37 +00003139 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003140 const digit bi = b->ob_digit[i];
3141
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003142 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003143 const int index = (bi >> j) & 0x1f;
3144 for (k = 0; k < 5; ++k)
3145 MULT(z, z, z)
3146 if (index)
3147 MULT(z, table[index], z)
3148 }
3149 }
3150 }
3151
Christian Heimes90aa7642007-12-19 02:45:37 +00003152 if (negativeOutput && (Py_SIZE(z) != 0)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003153 temp = (PyLongObject *)long_sub(z, c);
3154 if (temp == NULL)
3155 goto Error;
3156 Py_DECREF(z);
3157 z = temp;
3158 temp = NULL;
3159 }
3160 goto Done;
3161
3162 Error:
3163 if (z != NULL) {
3164 Py_DECREF(z);
3165 z = NULL;
3166 }
3167 /* fall through */
3168 Done:
Christian Heimes90aa7642007-12-19 02:45:37 +00003169 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
Tim Peters47e52ee2004-08-30 02:44:38 +00003170 for (i = 0; i < 32; ++i)
3171 Py_XDECREF(table[i]);
3172 }
Tim Petersde7990b2005-07-17 23:45:23 +00003173 Py_DECREF(a);
3174 Py_DECREF(b);
3175 Py_XDECREF(c);
3176 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003177 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003178}
3179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003180static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003181long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003182{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003183 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003184 PyLongObject *x;
3185 PyLongObject *w;
Christian Heimes90aa7642007-12-19 02:45:37 +00003186 if (ABS(Py_SIZE(v)) <=1)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003187 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003188 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003189 if (w == NULL)
3190 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003191 x = (PyLongObject *) long_add(v, w);
3192 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003193 if (x == NULL)
3194 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00003195 Py_SIZE(x) = -(Py_SIZE(x));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003196 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003197}
3198
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003199static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003200long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003201{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003202 PyLongObject *z;
Christian Heimes90aa7642007-12-19 02:45:37 +00003203 if (ABS(Py_SIZE(v)) <= 1)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003204 return PyLong_FromLong(-MEDIUM_VALUE(v));
Tim Peters69c2de32001-09-11 22:31:33 +00003205 z = (PyLongObject *)_PyLong_Copy(v);
3206 if (z != NULL)
Christian Heimes90aa7642007-12-19 02:45:37 +00003207 Py_SIZE(z) = -(Py_SIZE(v));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003208 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003209}
3210
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003211static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003212long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003213{
Christian Heimes90aa7642007-12-19 02:45:37 +00003214 if (Py_SIZE(v) < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003215 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00003216 else
Guido van Rossumb43daf72007-08-01 18:08:08 +00003217 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003218}
3219
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003220static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00003221long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003222{
Christian Heimes90aa7642007-12-19 02:45:37 +00003223 return ABS(Py_SIZE(v)) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003224}
3225
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003226static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003227long_rshift(PyLongObject *a, PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003228{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003229 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003230 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003231 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003232 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003233
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003234 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003235
Christian Heimes90aa7642007-12-19 02:45:37 +00003236 if (Py_SIZE(a) < 0) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003237 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00003238 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003239 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003240 if (a1 == NULL)
3241 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003242 a2 = (PyLongObject *) long_rshift(a1, b);
3243 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003244 if (a2 == NULL)
3245 goto rshift_error;
3246 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003247 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003248 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003249 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00003250
Neil Schemenauerba872e22001-01-04 01:46:03 +00003251 shiftby = PyLong_AsLong((PyObject *)b);
3252 if (shiftby == -1L && PyErr_Occurred())
3253 goto rshift_error;
3254 if (shiftby < 0) {
3255 PyErr_SetString(PyExc_ValueError,
3256 "negative shift count");
3257 goto rshift_error;
3258 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003259 wordshift = shiftby / PyLong_SHIFT;
Christian Heimes90aa7642007-12-19 02:45:37 +00003260 newsize = ABS(Py_SIZE(a)) - wordshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003261 if (newsize <= 0) {
3262 z = _PyLong_New(0);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003263 return (PyObject *)z;
3264 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003265 loshift = shiftby % PyLong_SHIFT;
3266 hishift = PyLong_SHIFT - loshift;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003267 lomask = ((digit)1 << hishift) - 1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003268 himask = PyLong_MASK ^ lomask;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003269 z = _PyLong_New(newsize);
3270 if (z == NULL)
3271 goto rshift_error;
Christian Heimes90aa7642007-12-19 02:45:37 +00003272 if (Py_SIZE(a) < 0)
3273 Py_SIZE(z) = -(Py_SIZE(z));
Neil Schemenauerba872e22001-01-04 01:46:03 +00003274 for (i = 0, j = wordshift; i < newsize; i++, j++) {
3275 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
3276 if (i+1 < newsize)
3277 z->ob_digit[i] |=
3278 (a->ob_digit[j+1] << hishift) & himask;
3279 }
3280 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003281 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00003282rshift_error:
Neil Schemenauerba872e22001-01-04 01:46:03 +00003283 return (PyObject *) z;
3284
Guido van Rossumc6913e71991-11-19 20:26:46 +00003285}
3286
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003287static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00003288long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003289{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003290 /* This version due to Tim Peters */
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003291 PyLongObject *a = (PyLongObject*)v;
3292 PyLongObject *b = (PyLongObject*)w;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003293 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003294 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003295 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003296 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003297
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003298 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003299
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003300 shiftby = PyLong_AsLong((PyObject *)b);
3301 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00003302 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003303 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003304 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003305 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003306 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003307 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003308 PyErr_SetString(PyExc_ValueError,
3309 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00003310 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003311 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003312 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
3313 wordshift = (int)shiftby / PyLong_SHIFT;
3314 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003315
Christian Heimes90aa7642007-12-19 02:45:37 +00003316 oldsize = ABS(Py_SIZE(a));
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003317 newsize = oldsize + wordshift;
3318 if (remshift)
3319 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003320 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003321 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003322 goto lshift_error;
Christian Heimes90aa7642007-12-19 02:45:37 +00003323 if (Py_SIZE(a) < 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003324 NEGATE(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00003325 for (i = 0; i < wordshift; i++)
3326 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003327 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003328 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00003329 accum |= (twodigits)a->ob_digit[j] << remshift;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003330 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
3331 accum >>= PyLong_SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003332 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003333 if (remshift)
3334 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003335 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00003336 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003337 z = long_normalize(z);
3338lshift_error:
Neil Schemenauerba872e22001-01-04 01:46:03 +00003339 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003340}
3341
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003342
3343/* Bitwise and/xor/or operations */
3344
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003345static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003346long_bitwise(PyLongObject *a,
3347 int op, /* '&', '|', '^' */
3348 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003349{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003350 digit maska, maskb; /* 0 or PyLong_MASK */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003351 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003352 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003353 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003354 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00003355 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003356 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003357
Christian Heimes90aa7642007-12-19 02:45:37 +00003358 if (Py_SIZE(a) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003359 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003360 if (a == NULL)
3361 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003362 maska = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003363 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003364 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003365 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003366 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003367 }
Christian Heimes90aa7642007-12-19 02:45:37 +00003368 if (Py_SIZE(b) < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003369 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003370 if (b == NULL) {
3371 Py_DECREF(a);
3372 return NULL;
3373 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003374 maskb = PyLong_MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003375 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003376 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003377 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003378 maskb = 0;
3379 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003380
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003381 negz = 0;
3382 switch (op) {
3383 case '^':
3384 if (maska != maskb) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003385 maska ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003386 negz = -1;
3387 }
3388 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00003389 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003390 if (maska && maskb) {
3391 op = '|';
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003392 maska ^= PyLong_MASK;
3393 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003394 negz = -1;
3395 }
3396 break;
3397 case '|':
3398 if (maska || maskb) {
3399 op = '&';
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003400 maska ^= PyLong_MASK;
3401 maskb ^= PyLong_MASK;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003402 negz = -1;
3403 }
3404 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003405 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003406
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003407 /* JRH: The original logic here was to allocate the result value (z)
3408 as the longer of the two operands. However, there are some cases
3409 where the result is guaranteed to be shorter than that: AND of two
3410 positives, OR of two negatives: use the shorter number. AND with
3411 mixed signs: use the positive number. OR with mixed signs: use the
3412 negative number. After the transformations above, op will be '&'
3413 iff one of these cases applies, and mask will be non-0 for operands
3414 whose length should be ignored.
3415 */
3416
Christian Heimes90aa7642007-12-19 02:45:37 +00003417 size_a = Py_SIZE(a);
3418 size_b = Py_SIZE(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003419 size_z = op == '&'
3420 ? (maska
3421 ? size_b
3422 : (maskb ? size_a : MIN(size_a, size_b)))
3423 : MAX(size_a, size_b);
3424 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003425 if (z == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003426 Py_DECREF(a);
3427 Py_DECREF(b);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00003428 return NULL;
3429 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003430
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003431 for (i = 0; i < size_z; ++i) {
3432 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
3433 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
3434 switch (op) {
3435 case '&': z->ob_digit[i] = diga & digb; break;
3436 case '|': z->ob_digit[i] = diga | digb; break;
3437 case '^': z->ob_digit[i] = diga ^ digb; break;
3438 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00003439 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003440
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003441 Py_DECREF(a);
3442 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003443 z = long_normalize(z);
3444 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003445 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003446 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003447 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003448 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003449}
3450
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003451static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003452long_and(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003453{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003454 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003455 CHECK_BINOP(a, b);
3456 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003457 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003458}
3459
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003460static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003461long_xor(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003462{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003463 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003464 CHECK_BINOP(a, b);
3465 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003466 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00003467}
3468
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003469static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003470long_or(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00003471{
Neil Schemenauerba872e22001-01-04 01:46:03 +00003472 PyObject *c;
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003473 CHECK_BINOP(a, b);
3474 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003475 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003476}
3477
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003478static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003479long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003480{
Brett Cannonc3647ac2005-04-26 03:45:26 +00003481 if (PyLong_CheckExact(v))
3482 Py_INCREF(v);
3483 else
3484 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003485 return v;
3486}
3487
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003488static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003489long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003490{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003491 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003492 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003493 if (result == -1.0 && PyErr_Occurred())
3494 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003495 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003496}
3497
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003498static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003499long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003500
Tim Peters6d6c1a32001-08-02 04:15:00 +00003501static PyObject *
3502long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3503{
3504 PyObject *x = NULL;
3505 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003506 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003507
Guido van Rossumbef14172001-08-29 15:47:46 +00003508 if (type != &PyLong_Type)
3509 return long_subtype_new(type, args, kwds); /* Wimp out */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003510 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003511 &x, &base))
3512 return NULL;
3513 if (x == NULL)
3514 return PyLong_FromLong(0L);
3515 if (base == -909)
3516 return PyNumber_Long(x);
Guido van Rossum98297ee2007-11-06 21:34:58 +00003517 else if (PyUnicode_Check(x))
3518 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3519 PyUnicode_GET_SIZE(x),
3520 base);
Christian Heimes9c4756e2008-05-26 13:22:05 +00003521 else if (PyByteArray_Check(x) || PyString_Check(x)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003522 /* Since PyLong_FromString doesn't have a length parameter,
3523 * check here for possible NULs in the string. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003524 char *string;
Christian Heimes90aa7642007-12-19 02:45:37 +00003525 int size = Py_SIZE(x);
Christian Heimes9c4756e2008-05-26 13:22:05 +00003526 if (PyByteArray_Check(x))
3527 string = PyByteArray_AS_STRING(x);
Guido van Rossum98297ee2007-11-06 21:34:58 +00003528 else
3529 string = PyString_AS_STRING(x);
Guido van Rossum4581ae52007-05-22 21:56:47 +00003530 if (strlen(string) != size) {
Guido van Rossum25236212007-08-22 23:28:23 +00003531 /* We only see this if there's a null byte in x,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003532 x is a bytes or buffer, *and* a base is given. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003533 PyErr_Format(PyExc_ValueError,
Guido van Rossum25236212007-08-22 23:28:23 +00003534 "invalid literal for int() with base %d: %R",
3535 base, x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003536 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003537 }
Guido van Rossum4581ae52007-05-22 21:56:47 +00003538 return PyLong_FromString(string, NULL, base);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003539 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003540 else {
3541 PyErr_SetString(PyExc_TypeError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00003542 "int() can't convert non-string with explicit base");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003543 return NULL;
3544 }
3545}
3546
Guido van Rossumbef14172001-08-29 15:47:46 +00003547/* Wimpy, slow approach to tp_new calls for subtypes of long:
3548 first create a regular long from whatever arguments we got,
3549 then allocate a subtype instance and initialize it from
3550 the regular long. The regular long is then thrown away.
3551*/
3552static PyObject *
3553long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3554{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003555 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003556 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003557
3558 assert(PyType_IsSubtype(type, &PyLong_Type));
3559 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3560 if (tmp == NULL)
3561 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003562 assert(PyLong_CheckExact(tmp));
Christian Heimes90aa7642007-12-19 02:45:37 +00003563 n = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003564 if (n < 0)
3565 n = -n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003566 newobj = (PyLongObject *)type->tp_alloc(type, n);
3567 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003568 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003569 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003570 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003571 assert(PyLong_Check(newobj));
Christian Heimes90aa7642007-12-19 02:45:37 +00003572 Py_SIZE(newobj) = Py_SIZE(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003573 for (i = 0; i < n; i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003574 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003575 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003576 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003577}
3578
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003579static PyObject *
3580long_getnewargs(PyLongObject *v)
3581{
3582 return Py_BuildValue("(N)", _PyLong_Copy(v));
3583}
3584
Guido van Rossumb43daf72007-08-01 18:08:08 +00003585static PyObject *
3586long_getN(PyLongObject *v, void *context) {
Christian Heimesc36625b2008-01-04 13:33:00 +00003587 return PyLong_FromLong((Py_intptr_t)context);
Guido van Rossumb43daf72007-08-01 18:08:08 +00003588}
3589
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003590static PyObject *
Eric Smith8c663262007-08-25 02:26:07 +00003591long__format__(PyObject *self, PyObject *args)
3592{
3593 /* when back porting this to 2.6, check type of the format_spec
3594 and call either unicode_long__format__ or
3595 string_long__format__ */
3596 return unicode_long__format__(self, args);
3597}
3598
3599
3600static PyObject *
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003601long_round(PyObject *self, PyObject *args)
3602{
3603#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
3604 int ndigits = UNDEF_NDIGITS;
3605 double x;
3606 PyObject *res;
3607
3608 if (!PyArg_ParseTuple(args, "|i", &ndigits))
3609 return NULL;
3610
3611 if (ndigits == UNDEF_NDIGITS)
3612 return long_long(self);
3613
3614 /* If called with two args, defer to float.__round__(). */
3615 x = PyLong_AsDouble(self);
3616 if (x == -1.0 && PyErr_Occurred())
3617 return NULL;
3618 self = PyFloat_FromDouble(x);
3619 if (self == NULL)
3620 return NULL;
3621 res = PyObject_CallMethod(self, "__round__", "i", ndigits);
3622 Py_DECREF(self);
3623 return res;
3624#undef UNDEF_NDIGITS
3625}
3626
Christian Heimes53876d92008-04-19 00:31:39 +00003627#if 0
3628static PyObject *
3629long_is_finite(PyObject *v)
3630{
3631 Py_RETURN_TRUE;
3632}
3633#endif
3634
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003635static PyMethodDef long_methods[] = {
Guido van Rossumb43daf72007-08-01 18:08:08 +00003636 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
3637 "Returns self, the complex conjugate of any int."},
Christian Heimes53876d92008-04-19 00:31:39 +00003638#if 0
3639 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
3640 "Returns always True."},
3641#endif
Guido van Rossum2fa33db2007-08-23 22:07:24 +00003642 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
3643 "Truncating an Integral returns itself."},
3644 {"__floor__", (PyCFunction)long_long, METH_NOARGS,
3645 "Flooring an Integral returns itself."},
3646 {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
3647 "Ceiling of an Integral returns itself."},
3648 {"__round__", (PyCFunction)long_round, METH_VARARGS,
3649 "Rounding an Integral returns itself.\n"
3650 "Rounding with an ndigits arguments defers to float.__round__."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003651 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
Eric Smith8c663262007-08-25 02:26:07 +00003652 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003653 {NULL, NULL} /* sentinel */
3654};
3655
Guido van Rossumb43daf72007-08-01 18:08:08 +00003656static PyGetSetDef long_getset[] = {
3657 {"real",
3658 (getter)long_long, (setter)NULL,
3659 "the real part of a complex number",
3660 NULL},
3661 {"imag",
3662 (getter)long_getN, (setter)NULL,
3663 "the imaginary part of a complex number",
3664 (void*)0},
3665 {"numerator",
3666 (getter)long_long, (setter)NULL,
3667 "the numerator of a rational number in lowest terms",
3668 NULL},
3669 {"denominator",
3670 (getter)long_getN, (setter)NULL,
3671 "the denominator of a rational number in lowest terms",
3672 (void*)1},
3673 {NULL} /* Sentinel */
3674};
3675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676PyDoc_STRVAR(long_doc,
Guido van Rossumddefaf32007-01-14 03:31:43 +00003677"int(x[, base]) -> integer\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00003678\n\
Guido van Rossumddefaf32007-01-14 03:31:43 +00003679Convert a string or number to an integer, if possible. A floating\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00003680point argument will be truncated towards zero (this does not include a\n\
3681string representation of a floating point number!) When converting a\n\
3682string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003683converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003684
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003685static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003686 (binaryfunc) long_add, /*nb_add*/
3687 (binaryfunc) long_sub, /*nb_subtract*/
3688 (binaryfunc) long_mul, /*nb_multiply*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003689 long_mod, /*nb_remainder*/
3690 long_divmod, /*nb_divmod*/
3691 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003692 (unaryfunc) long_neg, /*nb_negative*/
Guido van Rossumb43daf72007-08-01 18:08:08 +00003693 (unaryfunc) long_long, /*tp_positive*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003694 (unaryfunc) long_abs, /*tp_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00003695 (inquiry) long_bool, /*tp_bool*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003696 (unaryfunc) long_invert, /*nb_invert*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003697 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003698 (binaryfunc) long_rshift, /*nb_rshift*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003699 long_and, /*nb_and*/
3700 long_xor, /*nb_xor*/
3701 long_or, /*nb_or*/
Neil Schemenauer16c70752007-09-21 20:19:23 +00003702 0, /*nb_reserved*/
Guido van Rossumb43daf72007-08-01 18:08:08 +00003703 long_long, /*nb_int*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003704 long_long, /*nb_long*/
3705 long_float, /*nb_float*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003706 0, /*nb_oct*/ /* not used */
3707 0, /*nb_hex*/ /* not used */
Guido van Rossum4668b002001-08-08 05:00:18 +00003708 0, /* nb_inplace_add */
3709 0, /* nb_inplace_subtract */
3710 0, /* nb_inplace_multiply */
Guido van Rossum4668b002001-08-08 05:00:18 +00003711 0, /* nb_inplace_remainder */
3712 0, /* nb_inplace_power */
3713 0, /* nb_inplace_lshift */
3714 0, /* nb_inplace_rshift */
3715 0, /* nb_inplace_and */
3716 0, /* nb_inplace_xor */
3717 0, /* nb_inplace_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003718 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003719 long_true_divide, /* nb_true_divide */
3720 0, /* nb_inplace_floor_divide */
3721 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003722 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003723};
3724
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003725PyTypeObject PyLong_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003726 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumddefaf32007-01-14 03:31:43 +00003727 "int", /* tp_name */
3728 /* See _PyLong_New for why this isn't
3729 sizeof(PyLongObject) - sizeof(digit) */
3730 sizeof(PyVarObject), /* tp_basicsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003731 sizeof(digit), /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003732 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003733 0, /* tp_print */
3734 0, /* tp_getattr */
3735 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003736 0, /* tp_compare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003737 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003738 &long_as_number, /* tp_as_number */
3739 0, /* tp_as_sequence */
3740 0, /* tp_as_mapping */
3741 (hashfunc)long_hash, /* tp_hash */
3742 0, /* tp_call */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003743 long_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003744 PyObject_GenericGetAttr, /* tp_getattro */
3745 0, /* tp_setattro */
3746 0, /* tp_as_buffer */
Thomas Wouters27d517b2007-02-25 20:39:11 +00003747 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3748 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003749 long_doc, /* tp_doc */
3750 0, /* tp_traverse */
3751 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003752 long_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003753 0, /* tp_weaklistoffset */
3754 0, /* tp_iter */
3755 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003756 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003757 0, /* tp_members */
Guido van Rossumb43daf72007-08-01 18:08:08 +00003758 long_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003759 0, /* tp_base */
3760 0, /* tp_dict */
3761 0, /* tp_descr_get */
3762 0, /* tp_descr_set */
3763 0, /* tp_dictoffset */
3764 0, /* tp_init */
3765 0, /* tp_alloc */
3766 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003767 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003768};
Guido van Rossumddefaf32007-01-14 03:31:43 +00003769
3770int
3771_PyLong_Init(void)
3772{
3773#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003774 int ival, size;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003775 PyLongObject *v = small_ints;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003776
3777 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
3778 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
3779 if (Py_TYPE(v) == &PyLong_Type) {
3780 /* The element is already initialized, most likely
3781 * the Python interpreter was initialized before.
3782 */
Christian Heimes48aa4b12008-02-01 16:56:30 +00003783 Py_ssize_t refcnt;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003784 PyObject* op = (PyObject*)v;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003785
Christian Heimes48aa4b12008-02-01 16:56:30 +00003786 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
3787 _Py_NewReference(op);
3788 /* _Py_NewReference sets the ref count to 1 but
3789 * the ref count might be larger. Set the refcnt
3790 * to the original refcnt + 1 */
3791 Py_REFCNT(op) = refcnt + 1;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003792 assert(Py_SIZE(op) == size);
3793 assert(v->ob_digit[0] == abs(ival));
3794 }
3795 else {
3796 PyObject_INIT(v, &PyLong_Type);
3797 }
3798 Py_SIZE(v) = size;
3799 v->ob_digit[0] = abs(ival);
Guido van Rossumddefaf32007-01-14 03:31:43 +00003800 }
3801#endif
3802 return 1;
3803}
3804
3805void
3806PyLong_Fini(void)
3807{
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003808 /* Integers are currently statically allocated. Py_DECREF is not
3809 needed, but Python must forget about the reference or multiple
3810 reinitializations will fail. */
Guido van Rossumddefaf32007-01-14 03:31:43 +00003811#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimesdfc12ed2008-01-31 15:16:38 +00003812 int i;
3813 PyLongObject *v = small_ints;
3814 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
3815 _Py_DEC_REFTOTAL;
3816 _Py_ForgetReference((PyObject*)v);
3817 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003818#endif
3819}