blob: 2d3dce67e4e3d3b21e9bd7a3cdd3627455580dee [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003/* Long (arbitrary precision) integer object implementation */
4
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00005/* XXX The functional organization of this file is terrible */
6
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00008#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000010#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Tim Peters5af4e6c2002-08-12 02:31:19 +000012/* For long multiplication, use the O(N**2) school algorithm unless
13 * both operands contain more than KARATSUBA_CUTOFF digits (this
14 * being an internal Python long digit, in base BASE).
15 */
Tim Peters0973b992004-08-29 22:16:50 +000016#define KARATSUBA_CUTOFF 70
17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +000018
Tim Peters47e52ee2004-08-30 02:44:38 +000019/* For exponentiation, use the binary left-to-right algorithm
20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
21 * In that case, do 5 bits at a time. The potential drawback is that
22 * a table of 2**5 intermediate results is computed.
23 */
24#define FIVEARY_CUTOFF 8
25
Guido van Rossume32e0141992-01-19 16:31:05 +000026#define ABS(x) ((x) < 0 ? -(x) : (x))
27
Tim Peters5af4e6c2002-08-12 02:31:19 +000028#undef MIN
29#undef MAX
30#define MAX(x, y) ((x) < (y) ? (y) : (x))
31#define MIN(x, y) ((x) > (y) ? (y) : (x))
32
Guido van Rossume32e0141992-01-19 16:31:05 +000033/* Forward */
Tim Peters9f688bf2000-07-07 15:53:28 +000034static PyLongObject *long_normalize(PyLongObject *);
35static PyLongObject *mul1(PyLongObject *, wdigit);
36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
Tim Peters212e6142001-07-14 12:23:19 +000037static PyLongObject *divrem1(PyLongObject *, digit, digit *);
Tim Peters9f688bf2000-07-07 15:53:28 +000038static PyObject *long_format(PyObject *aa, int base, int addL);
Guido van Rossume32e0141992-01-19 16:31:05 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040#define SIGCHECK(PyTryBlock) \
Skip Montanarod581d772002-09-03 20:10:45 +000041 if (--_Py_Ticker < 0) { \
42 _Py_Ticker = _Py_CheckInterval; \
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 if (PyErr_CheckSignals()) { PyTryBlock; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000044 }
45
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046/* Normalize (remove leading zeros from) a long int object.
47 Doesn't attempt to free the storage--in most cases, due to the nature
48 of the algorithms used, this could save at most be one word anyway. */
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000051long_normalize(register PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000052{
Martin v. Löwis18e16552006-02-15 17:27:45 +000053 Py_ssize_t j = ABS(v->ob_size);
54 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +000055
Guido van Rossumedcc38a1991-05-05 20:09:44 +000056 while (i > 0 && v->ob_digit[i-1] == 0)
57 --i;
58 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000059 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060 return v;
61}
62
63/* Allocate a new long int object with size digits.
64 Return NULL and set exception if we run out of memory. */
65
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000067_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068{
Martin v. Löwis18e16552006-02-15 17:27:45 +000069 if (size > INT_MAX) {
70 /* XXX: Fix this check when ob_size becomes ssize_t */
71 PyErr_NoMemory();
72 return NULL;
73 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000075}
76
Tim Peters64b5ce32001-09-10 20:52:51 +000077PyObject *
78_PyLong_Copy(PyLongObject *src)
79{
80 PyLongObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +000081 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +000082
83 assert(src != NULL);
84 i = src->ob_size;
85 if (i < 0)
86 i = -(i);
87 result = _PyLong_New(i);
88 if (result != NULL) {
Tim Peters5329cdb2002-03-02 04:18:04 +000089 result->ob_size = src->ob_size;
Tim Peters64b5ce32001-09-10 20:52:51 +000090 while (--i >= 0)
91 result->ob_digit[i] = src->ob_digit[i];
92 }
93 return (PyObject *)result;
94}
95
Guido van Rossumedcc38a1991-05-05 20:09:44 +000096/* Create a new long int object from a C long int */
97
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +000099PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000100{
Tim Petersce9de2f2001-06-14 04:56:19 +0000101 PyLongObject *v;
102 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
103 int ndigits = 0;
104 int negative = 0;
105
106 if (ival < 0) {
107 ival = -ival;
108 negative = 1;
109 }
110
111 /* Count the number of Python digits.
112 We used to pick 5 ("big enough for anything"), but that's a
113 waste of time and space given that 5*15 = 75 bits are rarely
114 needed. */
115 t = (unsigned long)ival;
116 while (t) {
117 ++ndigits;
118 t >>= SHIFT;
119 }
120 v = _PyLong_New(ndigits);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000121 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000122 digit *p = v->ob_digit;
123 v->ob_size = negative ? -ndigits : ndigits;
124 t = (unsigned long)ival;
125 while (t) {
126 *p++ = (digit)(t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000127 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000128 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000129 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000131}
132
Guido van Rossum53756b11997-01-03 17:14:46 +0000133/* Create a new long int object from a C unsigned long int */
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000136PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000137{
Tim Petersce9de2f2001-06-14 04:56:19 +0000138 PyLongObject *v;
139 unsigned long t;
140 int ndigits = 0;
141
142 /* Count the number of Python digits. */
143 t = (unsigned long)ival;
144 while (t) {
145 ++ndigits;
146 t >>= SHIFT;
147 }
148 v = _PyLong_New(ndigits);
Guido van Rossum53756b11997-01-03 17:14:46 +0000149 if (v != NULL) {
Tim Petersce9de2f2001-06-14 04:56:19 +0000150 digit *p = v->ob_digit;
151 v->ob_size = ndigits;
152 while (ival) {
153 *p++ = (digit)(ival & MASK);
154 ival >>= SHIFT;
Guido van Rossum53756b11997-01-03 17:14:46 +0000155 }
Guido van Rossum53756b11997-01-03 17:14:46 +0000156 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000158}
159
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000160/* Create a new long int object from a C double */
161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000164{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000166 double frac;
167 int i, ndig, expo, neg;
168 neg = 0;
Tim Peters39dce292000-08-15 03:34:48 +0000169 if (Py_IS_INFINITY(dval)) {
Guido van Rossum1a23c241999-09-27 17:11:52 +0000170 PyErr_SetString(PyExc_OverflowError,
171 "cannot convert float infinity to long");
172 return NULL;
173 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000174 if (dval < 0.0) {
175 neg = 1;
176 dval = -dval;
177 }
178 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
179 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000181 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000183 if (v == NULL)
184 return NULL;
185 frac = ldexp(frac, (expo-1) % SHIFT + 1);
186 for (i = ndig; --i >= 0; ) {
187 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000188 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000189 frac = frac - (double)bits;
190 frac = ldexp(frac, SHIFT);
191 }
192 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000193 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000195}
196
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000197/* Get a C long int from a long int object.
198 Returns -1 and sets an error condition if overflow occurs. */
199
200long
Tim Peters9f688bf2000-07-07 15:53:28 +0000201PyLong_AsLong(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000202{
Guido van Rossumf7531811998-05-26 14:33:37 +0000203 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000205 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206 Py_ssize_t i;
207 int sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 if (vv == NULL || !PyLong_Check(vv)) {
Guido van Rossum7e35d572001-09-15 03:14:32 +0000210 if (vv != NULL && PyInt_Check(vv))
211 return PyInt_AsLong(vv);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000213 return -1;
214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000216 i = v->ob_size;
217 sign = 1;
218 x = 0;
219 if (i < 0) {
220 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000221 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000222 }
223 while (--i >= 0) {
224 prev = x;
225 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000226 if ((x >> SHIFT) != prev)
227 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000228 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000229 /* Haven't lost any bits, but if the sign bit is set we're in
230 * trouble *unless* this is the min negative number. So,
231 * trouble iff sign bit set && (positive || some bit set other
232 * than the sign bit).
233 */
234 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
235 goto overflow;
236 return (long)x * sign;
237
238 overflow:
239 PyErr_SetString(PyExc_OverflowError,
Skip Montanarobafedec2001-09-13 19:05:30 +0000240 "long int too large to convert to int");
Guido van Rossumf7531811998-05-26 14:33:37 +0000241 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000242}
243
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000244static Py_ssize_t
245_long_as_ssize_t(PyObject *vv) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000246 register PyLongObject *v;
247 size_t x, prev;
248 Py_ssize_t i;
249 int sign;
250
251 if (vv == NULL || !PyLong_Check(vv)) {
252 PyErr_BadInternalCall();
253 return -1;
254 }
255 v = (PyLongObject *)vv;
256 i = v->ob_size;
257 sign = 1;
258 x = 0;
259 if (i < 0) {
260 sign = -1;
261 i = -(i);
262 }
263 while (--i >= 0) {
264 prev = x;
265 x = (x << SHIFT) + v->ob_digit[i];
266 if ((x >> SHIFT) != prev)
267 goto overflow;
268 }
269 /* Haven't lost any bits, but if the sign bit is set we're in
270 * trouble *unless* this is the min negative number. So,
271 * trouble iff sign bit set && (positive || some bit set other
272 * than the sign bit).
273 */
274 if ((Py_ssize_t)x < 0 && (sign > 0 || (x << 1) != 0))
275 goto overflow;
276 return (Py_ssize_t)x * sign;
277
278 overflow:
279 PyErr_SetString(PyExc_OverflowError,
280 "long int too large to convert to int");
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000281 if (sign > 0)
282 return PY_SSIZE_T_MAX;
283 else
Martin v. Löwisc48c8db2006-04-05 18:21:17 +0000284 return PY_SSIZE_T_MIN;
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000285}
286
287/* Get a Py_ssize_t from a long int object.
288 Returns -1 and sets an error condition if overflow occurs. */
289
290Py_ssize_t
291_PyLong_AsSsize_t(PyObject *vv)
292{
293 Py_ssize_t x;
294
295 x = _long_as_ssize_t(vv);
296 if (PyErr_Occurred()) return -1;
297 return x;
298}
299
300
301/* Get a Py_ssize_t from a long int object.
302 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
303 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Armin Rigo12bec1b2006-03-28 19:27:56 +0000304 On error, return -1 with an exception set.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000305*/
306
307static Py_ssize_t
308long_index(PyObject *vv)
309{
310 Py_ssize_t x;
311
312 x = _long_as_ssize_t(vv);
313 if (PyErr_Occurred()) {
314 /* If overflow error, ignore the error */
315 if (x != -1) {
316 PyErr_Clear();
317 }
318 }
319 return x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320}
321
Guido van Rossumd8c80482002-08-13 00:24:58 +0000322/* Get a C unsigned long int from a long int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000323 Returns -1 and sets an error condition if overflow occurs. */
324
325unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000326PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000327{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000329 unsigned long x, prev;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000331
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000333 if (vv != NULL && PyInt_Check(vv)) {
334 long val = PyInt_AsLong(vv);
335 if (val < 0) {
336 PyErr_SetString(PyExc_OverflowError,
337 "can't convert negative value to unsigned long");
338 return (unsigned long) -1;
339 }
340 return val;
341 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000343 return (unsigned long) -1;
344 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000346 i = v->ob_size;
347 x = 0;
348 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000350 "can't convert negative value to unsigned long");
351 return (unsigned long) -1;
352 }
353 while (--i >= 0) {
354 prev = x;
355 x = (x << SHIFT) + v->ob_digit[i];
356 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +0000358 "long int too large to convert");
Guido van Rossum53756b11997-01-03 17:14:46 +0000359 return (unsigned long) -1;
360 }
361 }
362 return x;
363}
364
Thomas Hellera4ea6032003-04-17 18:55:45 +0000365/* Get a C unsigned long int from a long int object, ignoring the high bits.
366 Returns -1 and sets an error condition if an error occurs. */
367
368unsigned long
369PyLong_AsUnsignedLongMask(PyObject *vv)
370{
371 register PyLongObject *v;
372 unsigned long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000373 Py_ssize_t i;
374 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000375
376 if (vv == NULL || !PyLong_Check(vv)) {
Martin v. Löwis729d47d2004-09-20 06:17:46 +0000377 if (vv != NULL && PyInt_Check(vv))
378 return PyInt_AsUnsignedLongMask(vv);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000379 PyErr_BadInternalCall();
380 return (unsigned long) -1;
381 }
382 v = (PyLongObject *)vv;
383 i = v->ob_size;
384 sign = 1;
385 x = 0;
386 if (i < 0) {
387 sign = -1;
388 i = -i;
389 }
390 while (--i >= 0) {
391 x = (x << SHIFT) + v->ob_digit[i];
392 }
393 return x * sign;
394}
395
Tim Peters5b8132f2003-01-31 15:52:05 +0000396int
397_PyLong_Sign(PyObject *vv)
398{
399 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000400
401 assert(v != NULL);
402 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000403
Tim Petersee1a53c2003-02-02 02:57:53 +0000404 return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000405}
406
Tim Petersbaefd9e2003-01-28 20:37:45 +0000407size_t
408_PyLong_NumBits(PyObject *vv)
409{
410 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000411 size_t result = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000412 Py_ssize_t ndigits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000413
414 assert(v != NULL);
415 assert(PyLong_Check(v));
Guido van Rossum004a65c2003-02-03 15:28:19 +0000416 ndigits = ABS(v->ob_size);
Tim Petersbaefd9e2003-01-28 20:37:45 +0000417 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
418 if (ndigits > 0) {
Tim Petersbaefd9e2003-01-28 20:37:45 +0000419 digit msd = v->ob_digit[ndigits - 1];
420
Tim Peters5b8132f2003-01-31 15:52:05 +0000421 result = (ndigits - 1) * SHIFT;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000422 if (result / SHIFT != ndigits - 1)
Tim Petersbaefd9e2003-01-28 20:37:45 +0000423 goto Overflow;
424 do {
425 ++result;
426 if (result == 0)
427 goto Overflow;
428 msd >>= 1;
429 } while (msd);
430 }
431 return result;
432
433Overflow:
434 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
435 "to express in a platform size_t");
436 return (size_t)-1;
437}
438
Tim Peters2a9b3672001-06-11 21:23:58 +0000439PyObject *
440_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
441 int little_endian, int is_signed)
442{
443 const unsigned char* pstartbyte;/* LSB of bytes */
444 int incr; /* direction to move pstartbyte */
445 const unsigned char* pendbyte; /* MSB of bytes */
446 size_t numsignificantbytes; /* number of bytes that matter */
447 size_t ndigits; /* number of Python long digits */
448 PyLongObject* v; /* result */
449 int idigit = 0; /* next free index in v->ob_digit */
450
451 if (n == 0)
452 return PyLong_FromLong(0L);
453
454 if (little_endian) {
455 pstartbyte = bytes;
456 pendbyte = bytes + n - 1;
457 incr = 1;
458 }
459 else {
460 pstartbyte = bytes + n - 1;
461 pendbyte = bytes;
462 incr = -1;
463 }
464
465 if (is_signed)
466 is_signed = *pendbyte >= 0x80;
467
468 /* Compute numsignificantbytes. This consists of finding the most
469 significant byte. Leading 0 bytes are insignficant if the number
470 is positive, and leading 0xff bytes if negative. */
471 {
472 size_t i;
473 const unsigned char* p = pendbyte;
474 const int pincr = -incr; /* search MSB to LSB */
475 const unsigned char insignficant = is_signed ? 0xff : 0x00;
476
477 for (i = 0; i < n; ++i, p += pincr) {
478 if (*p != insignficant)
479 break;
480 }
481 numsignificantbytes = n - i;
482 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
483 actually has 2 significant bytes. OTOH, 0xff0001 ==
484 -0x00ffff, so we wouldn't *need* to bump it there; but we
485 do for 0xffff = -0x0001. To be safe without bothering to
486 check every case, bump it regardless. */
487 if (is_signed && numsignificantbytes < n)
488 ++numsignificantbytes;
489 }
490
491 /* How many Python long digits do we need? We have
492 8*numsignificantbytes bits, and each Python long digit has SHIFT
493 bits, so it's the ceiling of the quotient. */
494 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
495 if (ndigits > (size_t)INT_MAX)
496 return PyErr_NoMemory();
497 v = _PyLong_New((int)ndigits);
498 if (v == NULL)
499 return NULL;
500
501 /* Copy the bits over. The tricky parts are computing 2's-comp on
502 the fly for signed numbers, and dealing with the mismatch between
503 8-bit bytes and (probably) 15-bit Python digits.*/
504 {
505 size_t i;
Tim Petersf251d062001-06-13 21:09:15 +0000506 twodigits carry = 1; /* for 2's-comp calculation */
Tim Peters2a9b3672001-06-11 21:23:58 +0000507 twodigits accum = 0; /* sliding register */
508 unsigned int accumbits = 0; /* number of bits in accum */
509 const unsigned char* p = pstartbyte;
510
511 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
Tim Peters8bc84b42001-06-12 19:17:03 +0000512 twodigits thisbyte = *p;
Tim Peters2a9b3672001-06-11 21:23:58 +0000513 /* Compute correction for 2's comp, if needed. */
514 if (is_signed) {
515 thisbyte = (0xff ^ thisbyte) + carry;
516 carry = thisbyte >> 8;
517 thisbyte &= 0xff;
518 }
519 /* Because we're going LSB to MSB, thisbyte is
520 more significant than what's already in accum,
521 so needs to be prepended to accum. */
522 accum |= thisbyte << accumbits;
523 accumbits += 8;
524 if (accumbits >= SHIFT) {
525 /* There's enough to fill a Python digit. */
526 assert(idigit < (int)ndigits);
527 v->ob_digit[idigit] = (digit)(accum & MASK);
528 ++idigit;
529 accum >>= SHIFT;
530 accumbits -= SHIFT;
531 assert(accumbits < SHIFT);
532 }
533 }
534 assert(accumbits < SHIFT);
535 if (accumbits) {
536 assert(idigit < (int)ndigits);
537 v->ob_digit[idigit] = (digit)accum;
538 ++idigit;
539 }
540 }
541
542 v->ob_size = is_signed ? -idigit : idigit;
543 return (PyObject *)long_normalize(v);
544}
545
546int
547_PyLong_AsByteArray(PyLongObject* v,
548 unsigned char* bytes, size_t n,
549 int little_endian, int is_signed)
550{
551 int i; /* index into v->ob_digit */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000552 Py_ssize_t ndigits; /* |v->ob_size| */
Tim Peters2a9b3672001-06-11 21:23:58 +0000553 twodigits accum; /* sliding register */
554 unsigned int accumbits; /* # bits in accum */
555 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
556 twodigits carry; /* for computing 2's-comp */
557 size_t j; /* # bytes filled */
558 unsigned char* p; /* pointer to next byte in bytes */
559 int pincr; /* direction to move p */
560
561 assert(v != NULL && PyLong_Check(v));
562
563 if (v->ob_size < 0) {
564 ndigits = -(v->ob_size);
565 if (!is_signed) {
566 PyErr_SetString(PyExc_TypeError,
567 "can't convert negative long to unsigned");
568 return -1;
569 }
570 do_twos_comp = 1;
571 }
572 else {
573 ndigits = v->ob_size;
574 do_twos_comp = 0;
575 }
576
577 if (little_endian) {
578 p = bytes;
579 pincr = 1;
580 }
581 else {
582 p = bytes + n - 1;
583 pincr = -1;
584 }
585
Tim Peters898cf852001-06-13 20:50:08 +0000586 /* Copy over all the Python digits.
587 It's crucial that every Python digit except for the MSD contribute
588 exactly SHIFT bits to the total, so first assert that the long is
589 normalized. */
590 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
Tim Peters2a9b3672001-06-11 21:23:58 +0000591 j = 0;
592 accum = 0;
593 accumbits = 0;
594 carry = do_twos_comp ? 1 : 0;
595 for (i = 0; i < ndigits; ++i) {
596 twodigits thisdigit = v->ob_digit[i];
597 if (do_twos_comp) {
598 thisdigit = (thisdigit ^ MASK) + carry;
599 carry = thisdigit >> SHIFT;
600 thisdigit &= MASK;
601 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000602 /* Because we're going LSB to MSB, thisdigit is more
603 significant than what's already in accum, so needs to be
604 prepended to accum. */
605 accum |= thisdigit << accumbits;
Tim Petersede05092001-06-14 08:53:38 +0000606 accumbits += SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +0000607
Tim Petersede05092001-06-14 08:53:38 +0000608 /* The most-significant digit may be (probably is) at least
609 partly empty. */
Tim Peters8bc84b42001-06-12 19:17:03 +0000610 if (i == ndigits - 1) {
Tim Petersede05092001-06-14 08:53:38 +0000611 /* Count # of sign bits -- they needn't be stored,
612 * although for signed conversion we need later to
613 * make sure at least one sign bit gets stored.
614 * First shift conceptual sign bit to real sign bit.
615 */
616 stwodigits s = (stwodigits)(thisdigit <<
617 (8*sizeof(stwodigits) - SHIFT));
Tim Peters7a3bfc32001-06-12 01:22:22 +0000618 unsigned int nsignbits = 0;
Tim Petersede05092001-06-14 08:53:38 +0000619 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
Tim Peters7a3bfc32001-06-12 01:22:22 +0000620 ++nsignbits;
Tim Petersede05092001-06-14 08:53:38 +0000621 s <<= 1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000622 }
Tim Petersede05092001-06-14 08:53:38 +0000623 accumbits -= nsignbits;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000624 }
Tim Peters8bc84b42001-06-12 19:17:03 +0000625
Tim Peters2a9b3672001-06-11 21:23:58 +0000626 /* Store as many bytes as possible. */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000627 while (accumbits >= 8) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000628 if (j >= n)
629 goto Overflow;
630 ++j;
631 *p = (unsigned char)(accum & 0xff);
632 p += pincr;
633 accumbits -= 8;
634 accum >>= 8;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000635 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000636 }
637
638 /* Store the straggler (if any). */
639 assert(accumbits < 8);
640 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
Tim Peters7a3bfc32001-06-12 01:22:22 +0000641 if (accumbits > 0) {
Tim Peters2a9b3672001-06-11 21:23:58 +0000642 if (j >= n)
643 goto Overflow;
644 ++j;
645 if (do_twos_comp) {
646 /* Fill leading bits of the byte with sign bits
647 (appropriately pretending that the long had an
648 infinite supply of sign bits). */
649 accum |= (~(twodigits)0) << accumbits;
650 }
651 *p = (unsigned char)(accum & 0xff);
652 p += pincr;
653 }
Tim Peters05607ad2001-06-13 21:01:27 +0000654 else if (j == n && n > 0 && is_signed) {
655 /* The main loop filled the byte array exactly, so the code
656 just above didn't get to ensure there's a sign bit, and the
657 loop below wouldn't add one either. Make sure a sign bit
658 exists. */
Tim Peters2a9b3672001-06-11 21:23:58 +0000659 unsigned char msb = *(p - pincr);
Tim Peters05607ad2001-06-13 21:01:27 +0000660 int sign_bit_set = msb >= 0x80;
661 assert(accumbits == 0);
662 if (sign_bit_set == do_twos_comp)
663 return 0;
664 else
Tim Peters2a9b3672001-06-11 21:23:58 +0000665 goto Overflow;
666 }
Tim Peters05607ad2001-06-13 21:01:27 +0000667
668 /* Fill remaining bytes with copies of the sign bit. */
669 {
670 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
671 for ( ; j < n; ++j, p += pincr)
672 *p = signbyte;
673 }
674
Tim Peters2a9b3672001-06-11 21:23:58 +0000675 return 0;
676
677Overflow:
678 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
679 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000680
Tim Peters2a9b3672001-06-11 21:23:58 +0000681}
682
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000683double
684_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
685{
686/* NBITS_WANTED should be > the number of bits in a double's precision,
687 but small enough so that 2**NBITS_WANTED is within the normal double
688 range. nbitsneeded is set to 1 less than that because the most-significant
689 Python digit contains at least 1 significant bit, but we don't want to
690 bother counting them (catering to the worst case cheaply).
691
692 57 is one more than VAX-D double precision; I (Tim) don't know of a double
693 format with more precision than that; it's 1 larger so that we add in at
694 least one round bit to stand in for the ignored least-significant bits.
695*/
696#define NBITS_WANTED 57
697 PyLongObject *v;
698 double x;
699 const double multiplier = (double)(1L << SHIFT);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000700 Py_ssize_t i;
701 int sign;
Tim Petersa1c1b0f2001-09-04 02:50:49 +0000702 int nbitsneeded;
703
704 if (vv == NULL || !PyLong_Check(vv)) {
705 PyErr_BadInternalCall();
706 return -1;
707 }
708 v = (PyLongObject *)vv;
709 i = v->ob_size;
710 sign = 1;
711 if (i < 0) {
712 sign = -1;
713 i = -(i);
714 }
715 else if (i == 0) {
716 *exponent = 0;
717 return 0.0;
718 }
719 --i;
720 x = (double)v->ob_digit[i];
721 nbitsneeded = NBITS_WANTED - 1;
722 /* Invariant: i Python digits remain unaccounted for. */
723 while (i > 0 && nbitsneeded > 0) {
724 --i;
725 x = x * multiplier + (double)v->ob_digit[i];
726 nbitsneeded -= SHIFT;
727 }
728 /* There are i digits we didn't shift in. Pretending they're all
729 zeroes, the true value is x * 2**(i*SHIFT). */
730 *exponent = i;
731 assert(x > 0.0);
732 return x * sign;
733#undef NBITS_WANTED
734}
735
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000736/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000737
738double
Tim Peters9f688bf2000-07-07 15:53:28 +0000739PyLong_AsDouble(PyObject *vv)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000740{
Thomas Wouters553489a2006-02-01 21:32:04 +0000741 int e = -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000742 double x;
Tim Peters9fffa3e2001-09-04 05:14:19 +0000743
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 if (vv == NULL || !PyLong_Check(vv)) {
745 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000746 return -1;
747 }
Tim Peters9fffa3e2001-09-04 05:14:19 +0000748 x = _PyLong_AsScaledDouble(vv, &e);
749 if (x == -1.0 && PyErr_Occurred())
750 return -1.0;
Thomas Wouters553489a2006-02-01 21:32:04 +0000751 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
752 set correctly after a successful _PyLong_AsScaledDouble() call */
753 assert(e >= 0);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000754 if (e > INT_MAX / SHIFT)
755 goto overflow;
756 errno = 0;
757 x = ldexp(x, e * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +0000758 if (Py_OVERFLOWED(x))
Tim Peters9fffa3e2001-09-04 05:14:19 +0000759 goto overflow;
760 return x;
761
762overflow:
763 PyErr_SetString(PyExc_OverflowError,
764 "long int too large to convert to float");
765 return -1.0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000766}
767
Guido van Rossum78694d91998-09-18 14:14:13 +0000768/* Create a new long (or int) object from a C pointer */
769
770PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000771PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +0000772{
Tim Peters70128a12001-06-16 08:48:40 +0000773#if SIZEOF_VOID_P <= SIZEOF_LONG
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000774 if ((long)p < 0)
775 return PyLong_FromUnsignedLong((unsigned long)p);
Guido van Rossum78694d91998-09-18 14:14:13 +0000776 return PyInt_FromLong((long)p);
777#else
Guido van Rossum78694d91998-09-18 14:14:13 +0000778
Tim Peters70128a12001-06-16 08:48:40 +0000779#ifndef HAVE_LONG_LONG
780# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
781#endif
782#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000783# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000784#endif
785 /* optimize null pointers */
786 if (p == NULL)
787 return PyInt_FromLong(0);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000788 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
Tim Peters70128a12001-06-16 08:48:40 +0000789
790#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000791}
792
793/* Get a C pointer from a long object (or an int object in some cases) */
794
795void *
Tim Peters9f688bf2000-07-07 15:53:28 +0000796PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +0000797{
798 /* This function will allow int or long objects. If vv is neither,
799 then the PyLong_AsLong*() functions will raise the exception:
800 PyExc_SystemError, "bad argument to internal function"
801 */
Tim Peters70128a12001-06-16 08:48:40 +0000802#if SIZEOF_VOID_P <= SIZEOF_LONG
Guido van Rossum78694d91998-09-18 14:14:13 +0000803 long x;
804
Tim Peters70128a12001-06-16 08:48:40 +0000805 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000806 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000807 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000808 x = PyLong_AsLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000809 else
810 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +0000811#else
Tim Peters70128a12001-06-16 08:48:40 +0000812
813#ifndef HAVE_LONG_LONG
814# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
815#endif
816#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000817# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +0000818#endif
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000819 PY_LONG_LONG x;
Guido van Rossum78694d91998-09-18 14:14:13 +0000820
Tim Peters70128a12001-06-16 08:48:40 +0000821 if (PyInt_Check(vv))
Guido van Rossum78694d91998-09-18 14:14:13 +0000822 x = PyInt_AS_LONG(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000823 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
Guido van Rossum78694d91998-09-18 14:14:13 +0000824 x = PyLong_AsLongLong(vv);
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000825 else
826 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +0000827
828#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +0000829
830 if (x == -1 && PyErr_Occurred())
831 return NULL;
832 return (void *)x;
833}
834
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000835#ifdef HAVE_LONG_LONG
Tim Petersd1a7da62001-06-13 00:35:57 +0000836
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000837/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +0000838 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000839 */
840
Tim Peterscf37dfc2001-06-14 18:42:50 +0000841#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
Tim Petersd1a7da62001-06-13 00:35:57 +0000842
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000843/* Create a new long int object from a C PY_LONG_LONG int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000844
845PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000846PyLong_FromLongLong(PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000847{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000848 PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000849 int one = 1;
850 return _PyLong_FromByteArray(
851 (unsigned char *)&bytes,
852 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000853}
854
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000855/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
Tim Petersd1a7da62001-06-13 00:35:57 +0000856
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000857PyObject *
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000858PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000859{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000860 unsigned PY_LONG_LONG bytes = ival;
Tim Petersd1a7da62001-06-13 00:35:57 +0000861 int one = 1;
862 return _PyLong_FromByteArray(
863 (unsigned char *)&bytes,
864 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000865}
866
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867/* Create a new long int object from a C Py_ssize_t. */
868
869PyObject *
870_PyLong_FromSsize_t(Py_ssize_t ival)
871{
872 Py_ssize_t bytes = ival;
873 int one = 1;
874 return _PyLong_FromByteArray(
875 (unsigned char *)&bytes,
876 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
877}
878
879/* Create a new long int object from a C size_t. */
880
881PyObject *
882_PyLong_FromSize_t(size_t ival)
883{
884 size_t bytes = ival;
885 int one = 1;
886 return _PyLong_FromByteArray(
887 (unsigned char *)&bytes,
888 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
889}
890
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000891/* Get a C PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000892 Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000893
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000894PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000895PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000896{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000897 PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000898 int one = 1;
899 int res;
900
Tim Petersd38b1c72001-09-30 05:09:37 +0000901 if (vv == NULL) {
902 PyErr_BadInternalCall();
903 return -1;
904 }
905 if (!PyLong_Check(vv)) {
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000906 PyNumberMethods *nb;
907 PyObject *io;
Tim Petersd38b1c72001-09-30 05:09:37 +0000908 if (PyInt_Check(vv))
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000909 return (PY_LONG_LONG)PyInt_AsLong(vv);
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000910 if ((nb = vv->ob_type->tp_as_number) == NULL ||
911 nb->nb_int == NULL) {
912 PyErr_SetString(PyExc_TypeError, "an integer is required");
913 return -1;
914 }
915 io = (*nb->nb_int) (vv);
916 if (io == NULL)
917 return -1;
918 if (PyInt_Check(io)) {
919 bytes = PyInt_AsLong(io);
920 Py_DECREF(io);
921 return bytes;
922 }
923 if (PyLong_Check(io)) {
924 bytes = PyLong_AsLongLong(io);
925 Py_DECREF(io);
926 return bytes;
927 }
928 Py_DECREF(io);
929 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000930 return -1;
931 }
932
Tim Petersd1a7da62001-06-13 00:35:57 +0000933 res = _PyLong_AsByteArray(
934 (PyLongObject *)vv, (unsigned char *)&bytes,
935 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000936
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000937 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000938 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000939 return (PY_LONG_LONG)-1;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000940 else
941 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000942}
943
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000944/* Get a C unsigned PY_LONG_LONG int from a long int object.
Tim Petersd1a7da62001-06-13 00:35:57 +0000945 Return -1 and set an error if overflow occurs. */
946
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000947unsigned PY_LONG_LONG
Tim Peters9f688bf2000-07-07 15:53:28 +0000948PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000949{
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000950 unsigned PY_LONG_LONG bytes;
Tim Petersd1a7da62001-06-13 00:35:57 +0000951 int one = 1;
952 int res;
953
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000954 if (vv == NULL || !PyLong_Check(vv)) {
955 PyErr_BadInternalCall();
Tim Petersd1a7da62001-06-13 00:35:57 +0000956 return -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000957 }
958
Tim Petersd1a7da62001-06-13 00:35:57 +0000959 res = _PyLong_AsByteArray(
960 (PyLongObject *)vv, (unsigned char *)&bytes,
961 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000962
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000963 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000964 if (res < 0)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000965 return (unsigned PY_LONG_LONG)res;
Martin v. Löwisc8bb9eb2002-03-09 12:02:59 +0000966 else
967 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000968}
Tim Petersd1a7da62001-06-13 00:35:57 +0000969
Thomas Hellera4ea6032003-04-17 18:55:45 +0000970/* Get a C unsigned long int from a long int object, ignoring the high bits.
971 Returns -1 and sets an error condition if an error occurs. */
972
973unsigned PY_LONG_LONG
974PyLong_AsUnsignedLongLongMask(PyObject *vv)
975{
976 register PyLongObject *v;
977 unsigned PY_LONG_LONG x;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000978 Py_ssize_t i;
979 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000980
981 if (vv == NULL || !PyLong_Check(vv)) {
982 PyErr_BadInternalCall();
983 return (unsigned long) -1;
984 }
985 v = (PyLongObject *)vv;
986 i = v->ob_size;
987 sign = 1;
988 x = 0;
989 if (i < 0) {
990 sign = -1;
991 i = -i;
992 }
993 while (--i >= 0) {
994 x = (x << SHIFT) + v->ob_digit[i];
995 }
996 return x * sign;
997}
Tim Petersd1a7da62001-06-13 00:35:57 +0000998#undef IS_LITTLE_ENDIAN
999
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001000#endif /* HAVE_LONG_LONG */
1001
Neil Schemenauerba872e22001-01-04 01:46:03 +00001002
1003static int
1004convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00001005 if (PyLong_Check(v)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001006 *a = (PyLongObject *) v;
1007 Py_INCREF(v);
1008 }
1009 else if (PyInt_Check(v)) {
1010 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1011 }
1012 else {
1013 return 0;
1014 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001015 if (PyLong_Check(w)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00001016 *b = (PyLongObject *) w;
1017 Py_INCREF(w);
1018 }
1019 else if (PyInt_Check(w)) {
1020 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1021 }
1022 else {
1023 Py_DECREF(*a);
1024 return 0;
1025 }
1026 return 1;
1027}
1028
1029#define CONVERT_BINOP(v, w, a, b) \
1030 if (!convert_binop(v, w, a, b)) { \
1031 Py_INCREF(Py_NotImplemented); \
1032 return Py_NotImplemented; \
1033 }
1034
Tim Peters877a2122002-08-12 05:09:36 +00001035/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1036 * is modified in place, by adding y to it. Carries are propagated as far as
1037 * x[m-1], and the remaining carry (0 or 1) is returned.
1038 */
1039static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001041{
1042 int i;
1043 digit carry = 0;
1044
1045 assert(m >= n);
1046 for (i = 0; i < n; ++i) {
1047 carry += x[i] + y[i];
1048 x[i] = carry & MASK;
1049 carry >>= SHIFT;
1050 assert((carry & 1) == carry);
1051 }
1052 for (; carry && i < m; ++i) {
1053 carry += x[i];
1054 x[i] = carry & MASK;
1055 carry >>= SHIFT;
1056 assert((carry & 1) == carry);
1057 }
1058 return carry;
1059}
1060
1061/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1062 * is modified in place, by subtracting y from it. Borrows are propagated as
1063 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1064 */
1065static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001066v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001067{
1068 int i;
1069 digit borrow = 0;
1070
1071 assert(m >= n);
1072 for (i = 0; i < n; ++i) {
1073 borrow = x[i] - y[i] - borrow;
1074 x[i] = borrow & MASK;
1075 borrow >>= SHIFT;
1076 borrow &= 1; /* keep only 1 sign bit */
1077 }
1078 for (; borrow && i < m; ++i) {
1079 borrow = x[i] - borrow;
1080 x[i] = borrow & MASK;
1081 borrow >>= SHIFT;
1082 borrow &= 1;
1083 }
1084 return borrow;
1085}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001086
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001087/* Multiply by a single digit, ignoring the sign. */
1088
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001089static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001090mul1(PyLongObject *a, wdigit n)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001091{
1092 return muladd1(a, n, (digit)0);
1093}
1094
1095/* Multiply by a single digit and add a single digit, ignoring the sign. */
1096
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001097static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001098muladd1(PyLongObject *a, wdigit n, wdigit extra)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001099{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001100 Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001102 twodigits carry = extra;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001103 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001104
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001105 if (z == NULL)
1106 return NULL;
1107 for (i = 0; i < size_a; ++i) {
1108 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +00001109 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001110 carry >>= SHIFT;
1111 }
Guido van Rossum2095d241997-04-09 19:41:24 +00001112 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001113 return long_normalize(z);
1114}
1115
Tim Peters212e6142001-07-14 12:23:19 +00001116/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1117 in pout, and returning the remainder. pin and pout point at the LSD.
1118 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1119 long_format, but that should be done with great care since longs are
1120 immutable. */
1121
1122static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001123inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001124{
1125 twodigits rem = 0;
1126
1127 assert(n > 0 && n <= MASK);
1128 pin += size;
1129 pout += size;
1130 while (--size >= 0) {
1131 digit hi;
1132 rem = (rem << SHIFT) + *--pin;
1133 *--pout = hi = (digit)(rem / n);
1134 rem -= hi * n;
1135 }
1136 return (digit)rem;
1137}
1138
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001139/* Divide a long integer by a digit, returning both the quotient
1140 (as function result) and the remainder (through *prem).
1141 The sign of a is ignored; n should not be zero. */
1142
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001143static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001144divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001145{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146 const Py_ssize_t size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001148
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001149 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001151 if (z == NULL)
1152 return NULL;
Tim Peters212e6142001-07-14 12:23:19 +00001153 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001154 return long_normalize(z);
1155}
1156
1157/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001158 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +00001159 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001162long_format(PyObject *aa, int base, int addL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001163{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 register PyLongObject *a = (PyLongObject *)aa;
1165 PyStringObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001166 Py_ssize_t i;
1167 const Py_ssize_t size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001168 char *p;
1169 int bits;
1170 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +00001171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 if (a == NULL || !PyLong_Check(a)) {
1173 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +00001174 return NULL;
1175 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001176 assert(base >= 2 && base <= 36);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001177
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001178 /* Compute a rough upper bound for the length of the string */
1179 i = base;
1180 bits = 0;
1181 while (i > 1) {
1182 ++bits;
1183 i >>= 1;
1184 }
Fred Drake121ee271999-12-23 15:41:28 +00001185 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001186 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001187 if (str == NULL)
1188 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001190 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +00001191 if (addL)
1192 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001193 if (a->ob_size < 0)
1194 sign = '-';
Tim Peters5af4e6c2002-08-12 02:31:19 +00001195
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001196 if (a->ob_size == 0) {
1197 *--p = '0';
1198 }
1199 else if ((base & (base - 1)) == 0) {
1200 /* JRH: special case for power-of-2 bases */
Tim Peters586b2e32001-07-15 09:11:14 +00001201 twodigits accum = 0;
1202 int accumbits = 0; /* # of bits in accum */
1203 int basebits = 1; /* # of bits in base-1 */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001204 i = base;
Tim Peters7d3a5112000-07-08 04:17:21 +00001205 while ((i >>= 1) > 1)
1206 ++basebits;
Tim Peters586b2e32001-07-15 09:11:14 +00001207
1208 for (i = 0; i < size_a; ++i) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00001209 accum |= (twodigits)a->ob_digit[i] << accumbits;
Tim Peters586b2e32001-07-15 09:11:14 +00001210 accumbits += SHIFT;
1211 assert(accumbits >= basebits);
1212 do {
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001213 char cdigit = (char)(accum & (base - 1));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001214 cdigit += (cdigit < 10) ? '0' : 'a'-10;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001215 assert(p > PyString_AS_STRING(str));
Martin v. Löwisa5854c22002-02-16 23:39:10 +00001216 *--p = cdigit;
Tim Peters586b2e32001-07-15 09:11:14 +00001217 accumbits -= basebits;
1218 accum >>= basebits;
1219 } while (i < size_a-1 ? accumbits >= basebits :
1220 accum > 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001221 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001222 }
1223 else {
Tim Petersfad225f2001-07-13 02:59:26 +00001224 /* Not 0, and base not a power of 2. Divide repeatedly by
1225 base, but for speed use the highest power of base that
1226 fits in a digit. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001227 Py_ssize_t size = size_a;
Tim Peters212e6142001-07-14 12:23:19 +00001228 digit *pin = a->ob_digit;
1229 PyLongObject *scratch;
1230 /* powbasw <- largest power of base that fits in a digit. */
Tim Petersfad225f2001-07-13 02:59:26 +00001231 digit powbase = base; /* powbase == base ** power */
1232 int power = 1;
1233 for (;;) {
1234 unsigned long newpow = powbase * (unsigned long)base;
1235 if (newpow >> SHIFT) /* doesn't fit in a digit */
1236 break;
1237 powbase = (digit)newpow;
1238 ++power;
1239 }
Tim Peters212e6142001-07-14 12:23:19 +00001240
1241 /* Get a scratch area for repeated division. */
1242 scratch = _PyLong_New(size);
1243 if (scratch == NULL) {
1244 Py_DECREF(str);
1245 return NULL;
1246 }
1247
1248 /* Repeatedly divide by powbase. */
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001249 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001250 int ntostore = power;
Tim Peters212e6142001-07-14 12:23:19 +00001251 digit rem = inplace_divrem1(scratch->ob_digit,
1252 pin, size, powbase);
1253 pin = scratch->ob_digit; /* no need to use a again */
1254 if (pin[size - 1] == 0)
1255 --size;
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001256 SIGCHECK({
Tim Peters212e6142001-07-14 12:23:19 +00001257 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001258 Py_DECREF(str);
1259 return NULL;
1260 })
Tim Peters212e6142001-07-14 12:23:19 +00001261
1262 /* Break rem into digits. */
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001263 assert(ntostore > 0);
1264 do {
Tim Petersfad225f2001-07-13 02:59:26 +00001265 digit nextrem = (digit)(rem / base);
1266 char c = (char)(rem - nextrem * base);
1267 assert(p > PyString_AS_STRING(str));
Raymond Hettinger3296e692005-06-29 23:29:56 +00001268 c += (c < 10) ? '0' : 'a'-10;
Tim Petersfad225f2001-07-13 02:59:26 +00001269 *--p = c;
1270 rem = nextrem;
Tim Petersc8a6b9b2001-07-14 11:01:28 +00001271 --ntostore;
1272 /* Termination is a bit delicate: must not
1273 store leading zeroes, so must get out if
Tim Peters212e6142001-07-14 12:23:19 +00001274 remaining quotient and rem are both 0. */
1275 } while (ntostore && (size || rem));
1276 } while (size != 0);
1277 Py_DECREF(scratch);
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001278 }
1279
Guido van Rossum2c475421992-08-14 15:13:07 +00001280 if (base == 8) {
1281 if (size_a != 0)
1282 *--p = '0';
1283 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +00001284 else if (base == 16) {
1285 *--p = 'x';
1286 *--p = '0';
1287 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001288 else if (base != 10) {
1289 *--p = '#';
1290 *--p = '0' + base%10;
1291 if (base > 10)
1292 *--p = '0' + base/10;
1293 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001294 if (sign)
1295 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 if (p != PyString_AS_STRING(str)) {
1297 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001298 assert(p > q);
1299 do {
1300 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001301 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001302 _PyString_Resize((PyObject **)&str,
1303 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001304 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001306}
1307
Tim Petersbf2674b2003-02-02 07:51:32 +00001308/* *str points to the first digit in a string of base base digits. base
1309 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
1310 * non-digit (which may be *str!). A normalized long is returned.
1311 * The point to this routine is that it takes time linear in the number of
1312 * string characters.
1313 */
1314static PyLongObject *
1315long_from_binary_base(char **str, int base)
1316{
1317 char *p = *str;
1318 char *start = p;
1319 int bits_per_char;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001320 Py_ssize_t n;
Tim Petersbf2674b2003-02-02 07:51:32 +00001321 PyLongObject *z;
1322 twodigits accum;
1323 int bits_in_accum;
1324 digit *pdigit;
1325
1326 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1327 n = base;
1328 for (bits_per_char = -1; n; ++bits_per_char)
1329 n >>= 1;
1330 /* n <- total # of bits needed, while setting p to end-of-string */
1331 n = 0;
1332 for (;;) {
1333 int k = -1;
1334 char ch = *p;
1335
1336 if (ch <= '9')
1337 k = ch - '0';
1338 else if (ch >= 'a')
1339 k = ch - 'a' + 10;
1340 else if (ch >= 'A')
1341 k = ch - 'A' + 10;
1342 if (k < 0 || k >= base)
1343 break;
Tim Petersbf2674b2003-02-02 07:51:32 +00001344 ++p;
1345 }
1346 *str = p;
Tim Peters1a3b19a2003-02-02 17:33:53 +00001347 n = (p - start) * bits_per_char;
1348 if (n / bits_per_char != p - start) {
1349 PyErr_SetString(PyExc_ValueError,
1350 "long string too large to convert");
1351 return NULL;
1352 }
Tim Petersbf2674b2003-02-02 07:51:32 +00001353 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */
1354 n = (n + SHIFT - 1) / SHIFT;
1355 z = _PyLong_New(n);
1356 if (z == NULL)
1357 return NULL;
1358 /* Read string from right, and fill in long from left; i.e.,
1359 * from least to most significant in both.
1360 */
1361 accum = 0;
1362 bits_in_accum = 0;
1363 pdigit = z->ob_digit;
1364 while (--p >= start) {
Tim Petersc7bc0b92003-05-05 20:39:43 +00001365 int k;
1366 char ch = *p;
Tim Petersbf2674b2003-02-02 07:51:32 +00001367
1368 if (ch <= '9')
1369 k = ch - '0';
1370 else if (ch >= 'a')
1371 k = ch - 'a' + 10;
1372 else {
1373 assert(ch >= 'A');
1374 k = ch - 'A' + 10;
1375 }
Tim Petersc7bc0b92003-05-05 20:39:43 +00001376 assert(k >= 0 && k < base);
1377 accum |= (twodigits)(k << bits_in_accum);
Tim Petersbf2674b2003-02-02 07:51:32 +00001378 bits_in_accum += bits_per_char;
1379 if (bits_in_accum >= SHIFT) {
1380 *pdigit++ = (digit)(accum & MASK);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001381 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001382 accum >>= SHIFT;
1383 bits_in_accum -= SHIFT;
1384 assert(bits_in_accum < SHIFT);
1385 }
1386 }
1387 if (bits_in_accum) {
1388 assert(bits_in_accum <= SHIFT);
1389 *pdigit++ = (digit)accum;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001390 assert(pdigit - z->ob_digit <= (int)n);
Tim Petersbf2674b2003-02-02 07:51:32 +00001391 }
1392 while (pdigit - z->ob_digit < n)
1393 *pdigit++ = 0;
1394 return long_normalize(z);
1395}
1396
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001398PyLong_FromString(char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001399{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001400 int sign = 1;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001401 char *start, *orig_str = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001403
Guido van Rossum472c04f1996-12-05 21:57:21 +00001404 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001405 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001406 "long() arg 2 must be >= 2 and <= 36");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001407 return NULL;
1408 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001409 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001410 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001411 if (*str == '+')
1412 ++str;
1413 else if (*str == '-') {
1414 ++str;
1415 sign = -1;
1416 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001417 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001418 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001419 if (base == 0) {
1420 if (str[0] != '0')
1421 base = 10;
1422 else if (str[1] == 'x' || str[1] == 'X')
1423 base = 16;
1424 else
1425 base = 8;
1426 }
1427 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1428 str += 2;
Guido van Rossume6762971998-06-22 03:54:46 +00001429 start = str;
Tim Petersbf2674b2003-02-02 07:51:32 +00001430 if ((base & (base - 1)) == 0)
1431 z = long_from_binary_base(&str, base);
1432 else {
1433 z = _PyLong_New(0);
1434 for ( ; z != NULL; ++str) {
1435 int k = -1;
1436 PyLongObject *temp;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001437
Tim Petersbf2674b2003-02-02 07:51:32 +00001438 if (*str <= '9')
1439 k = *str - '0';
1440 else if (*str >= 'a')
1441 k = *str - 'a' + 10;
1442 else if (*str >= 'A')
1443 k = *str - 'A' + 10;
1444 if (k < 0 || k >= base)
1445 break;
1446 temp = muladd1(z, (digit)base, (digit)k);
1447 Py_DECREF(z);
1448 z = temp;
1449 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001450 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +00001451 if (z == NULL)
1452 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001453 if (str == start)
1454 goto onError;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001455 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001456 z->ob_size = -(z->ob_size);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001457 if (*str == 'L' || *str == 'l')
1458 str++;
1459 while (*str && isspace(Py_CHARMASK(*str)))
1460 str++;
1461 if (*str != '\0')
1462 goto onError;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001463 if (pend)
1464 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001465 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001466
1467 onError:
Tim Peters5af4e6c2002-08-12 02:31:19 +00001468 PyErr_Format(PyExc_ValueError,
Guido van Rossum9e896b32000-04-05 20:11:21 +00001469 "invalid literal for long(): %.200s", orig_str);
1470 Py_XDECREF(z);
1471 return NULL;
1472}
1473
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001474#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001475PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001476PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001477{
Walter Dörwald07e14762002-11-06 16:15:14 +00001478 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +00001479 char *buffer = (char *)PyMem_MALLOC(length+1);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001480
Walter Dörwald07e14762002-11-06 16:15:14 +00001481 if (buffer == NULL)
1482 return NULL;
1483
1484 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1485 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001486 return NULL;
1487 }
Walter Dörwald07e14762002-11-06 16:15:14 +00001488 result = PyLong_FromString(buffer, NULL, base);
1489 PyMem_FREE(buffer);
1490 return result;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001491}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001492#endif
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001493
Tim Peters9f688bf2000-07-07 15:53:28 +00001494/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001495static PyLongObject *x_divrem
Tim Peters9f688bf2000-07-07 15:53:28 +00001496 (PyLongObject *, PyLongObject *, PyLongObject **);
1497static PyObject *long_pos(PyLongObject *);
1498static int long_divrem(PyLongObject *, PyLongObject *,
1499 PyLongObject **, PyLongObject **);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001500
1501/* Long division with remainder, top-level routine */
1502
Guido van Rossume32e0141992-01-19 16:31:05 +00001503static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001504long_divrem(PyLongObject *a, PyLongObject *b,
1505 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001506{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001507 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001508 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001509
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001510 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001511 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +00001512 "long division or modulo by zero");
Guido van Rossume32e0141992-01-19 16:31:05 +00001513 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001514 }
1515 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +00001516 (size_a == size_b &&
1517 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001518 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001519 *pdiv = _PyLong_New(0);
1520 Py_INCREF(a);
1521 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +00001522 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001523 }
1524 if (size_b == 1) {
1525 digit rem = 0;
1526 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001527 if (z == NULL)
1528 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001529 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001530 }
Guido van Rossume32e0141992-01-19 16:31:05 +00001531 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001532 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +00001533 if (z == NULL)
1534 return -1;
1535 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001536 /* Set the signs.
1537 The quotient z has the sign of a*b;
1538 the remainder r has the sign of a,
1539 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +00001540 if ((a->ob_size < 0) != (b->ob_size < 0))
1541 z->ob_size = -(z->ob_size);
1542 if (a->ob_size < 0 && (*prem)->ob_size != 0)
1543 (*prem)->ob_size = -((*prem)->ob_size);
1544 *pdiv = z;
1545 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001546}
1547
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001548/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001549
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001550static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001551x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001552{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001553 Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +00001554 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001555 PyLongObject *v = mul1(v1, d);
1556 PyLongObject *w = mul1(w1, d);
1557 PyLongObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001558 Py_ssize_t j, k;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001559
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001560 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001561 Py_XDECREF(v);
1562 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001563 return NULL;
1564 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001565
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001566 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001567 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001568 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001569
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001570 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001571 a = _PyLong_New(size_v - size_w + 1);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001572
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001573 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
1574 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1575 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001576 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001577 int i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001578
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001579 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001581 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001582 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001583 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001584 if (vj == w->ob_digit[size_w-1])
1585 q = MASK;
1586 else
1587 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
1588 w->ob_digit[size_w-1];
Tim Peters5af4e6c2002-08-12 02:31:19 +00001589
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001590 while (w->ob_digit[size_w-2]*q >
1591 ((
1592 ((twodigits)vj << SHIFT)
1593 + v->ob_digit[j-1]
1594 - q*w->ob_digit[size_w-1]
1595 ) << SHIFT)
1596 + v->ob_digit[j-2])
1597 --q;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001598
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001599 for (i = 0; i < size_w && i+k < size_v; ++i) {
1600 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +00001601 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001602 carry += v->ob_digit[i+k] - z
1603 + ((twodigits)zz << SHIFT);
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001604 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001605 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1606 carry, SHIFT);
1607 carry -= zz;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001608 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001609
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001610 if (i+k < size_v) {
1611 carry += v->ob_digit[i+k];
1612 v->ob_digit[i+k] = 0;
1613 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001614
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001615 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +00001616 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001617 else {
1618 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +00001619 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001620 carry = 0;
1621 for (i = 0; i < size_w && i+k < size_v; ++i) {
1622 carry += v->ob_digit[i+k] + w->ob_digit[i];
Jeremy Hyltonfc61f9a2003-05-01 21:31:53 +00001623 v->ob_digit[i+k] = (digit)(carry & MASK);
Tim Peters7d3a5112000-07-08 04:17:21 +00001624 carry = Py_ARITHMETIC_RIGHT_SHIFT(
1625 BASE_TWODIGITS_TYPE,
1626 carry, SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001627 }
1628 }
1629 } /* for j, k */
Tim Peters5af4e6c2002-08-12 02:31:19 +00001630
Guido van Rossumc206c761995-01-10 15:23:19 +00001631 if (a == NULL)
1632 *prem = NULL;
1633 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +00001634 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001635 *prem = divrem1(v, d, &d);
1636 /* d receives the (unused) remainder */
1637 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +00001639 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001640 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001641 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001642 Py_DECREF(v);
1643 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001644 return a;
1645}
1646
1647/* Methods */
1648
1649static void
Tim Peters9f688bf2000-07-07 15:53:28 +00001650long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001651{
Guido van Rossum9475a232001-10-05 20:51:39 +00001652 v->ob_type->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001653}
1654
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001655static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001656long_repr(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001657{
Fred Drake121ee271999-12-23 15:41:28 +00001658 return long_format(v, 10, 1);
1659}
1660
1661static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001662long_str(PyObject *v)
Fred Drake121ee271999-12-23 15:41:28 +00001663{
1664 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001665}
1666
1667static int
Tim Peters9f688bf2000-07-07 15:53:28 +00001668long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001669{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001670 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001671
Guido van Rossumc6913e71991-11-19 20:26:46 +00001672 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001673 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001674 sign = 0;
1675 else
1676 sign = a->ob_size - b->ob_size;
1677 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001678 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001679 Py_ssize_t i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001680 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1681 ;
1682 if (i < 0)
1683 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001684 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001685 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001686 if (a->ob_size < 0)
1687 sign = -sign;
1688 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001689 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001690 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001691}
1692
Guido van Rossum9bfef441993-03-29 10:43:31 +00001693static long
Tim Peters9f688bf2000-07-07 15:53:28 +00001694long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001695{
1696 long x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001697 Py_ssize_t i;
1698 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001699
1700 /* This is designed so that Python ints and longs with the
1701 same value hash to the same value, otherwise comparisons
1702 of mapping keys will turn out weird */
1703 i = v->ob_size;
1704 sign = 1;
1705 x = 0;
1706 if (i < 0) {
1707 sign = -1;
1708 i = -(i);
1709 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001710#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001711 while (--i >= 0) {
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001712 /* Force a native long #-bits (32 or 64) circular shift */
1713 x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001714 x += v->ob_digit[i];
1715 }
Neal Norwitzd5a65a72003-02-23 23:11:41 +00001716#undef LONG_BIT_SHIFT
Guido van Rossum9bfef441993-03-29 10:43:31 +00001717 x = x * sign;
1718 if (x == -1)
1719 x = -2;
1720 return x;
1721}
1722
1723
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001724/* Add the absolute values of two long integers. */
1725
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001726static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001727x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001728{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001729 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001730 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001731 int i;
1732 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001733
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001734 /* Ensure a is the larger of the two: */
1735 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001736 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001737 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001738 size_a = size_b;
1739 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001740 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001741 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001742 if (z == NULL)
1743 return NULL;
1744 for (i = 0; i < size_b; ++i) {
1745 carry += a->ob_digit[i] + b->ob_digit[i];
1746 z->ob_digit[i] = carry & MASK;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001747 carry >>= SHIFT;
1748 }
1749 for (; i < size_a; ++i) {
1750 carry += a->ob_digit[i];
1751 z->ob_digit[i] = carry & MASK;
1752 carry >>= SHIFT;
1753 }
1754 z->ob_digit[i] = carry;
1755 return long_normalize(z);
1756}
1757
1758/* Subtract the absolute values of two integers. */
1759
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001760static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001761x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001762{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001763 Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001764 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001765 Py_ssize_t i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001766 int sign = 1;
1767 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00001768
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001769 /* Ensure a is the larger of the two: */
1770 if (size_a < size_b) {
1771 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001772 { PyLongObject *temp = a; a = b; b = temp; }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001773 { Py_ssize_t size_temp = size_a;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001774 size_a = size_b;
1775 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001776 }
1777 else if (size_a == size_b) {
1778 /* Find highest digit where a and b differ: */
1779 i = size_a;
1780 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1781 ;
1782 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001783 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001784 if (a->ob_digit[i] < b->ob_digit[i]) {
1785 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001786 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001787 }
1788 size_a = size_b = i+1;
1789 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001790 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001791 if (z == NULL)
1792 return NULL;
1793 for (i = 0; i < size_b; ++i) {
1794 /* The following assumes unsigned arithmetic
1795 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001796 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001797 z->ob_digit[i] = borrow & MASK;
1798 borrow >>= SHIFT;
1799 borrow &= 1; /* Keep only one sign bit */
1800 }
1801 for (; i < size_a; ++i) {
1802 borrow = a->ob_digit[i] - borrow;
1803 z->ob_digit[i] = borrow & MASK;
1804 borrow >>= SHIFT;
Tim Peters43f04a32000-07-08 02:26:47 +00001805 borrow &= 1; /* Keep only one sign bit */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001806 }
1807 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001808 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001809 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001810 return long_normalize(z);
1811}
1812
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001813static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001814long_add(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001815{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001816 PyLongObject *a, *b, *z;
Tim Peters69c2de32001-09-11 22:31:33 +00001817
Neil Schemenauerba872e22001-01-04 01:46:03 +00001818 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1819
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001820 if (a->ob_size < 0) {
1821 if (b->ob_size < 0) {
1822 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001823 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001824 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001825 }
1826 else
1827 z = x_sub(b, a);
1828 }
1829 else {
1830 if (b->ob_size < 0)
1831 z = x_sub(a, b);
1832 else
1833 z = x_add(a, b);
1834 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001835 Py_DECREF(a);
1836 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001837 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001838}
1839
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001840static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00001841long_sub(PyLongObject *v, PyLongObject *w)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001842{
Neil Schemenauerba872e22001-01-04 01:46:03 +00001843 PyLongObject *a, *b, *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001844
Neil Schemenauerba872e22001-01-04 01:46:03 +00001845 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
1846
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001847 if (a->ob_size < 0) {
1848 if (b->ob_size < 0)
1849 z = x_sub(a, b);
1850 else
1851 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001852 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001853 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001854 }
1855 else {
1856 if (b->ob_size < 0)
1857 z = x_add(a, b);
1858 else
1859 z = x_sub(a, b);
1860 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00001861 Py_DECREF(a);
1862 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001863 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001864}
1865
Tim Peters5af4e6c2002-08-12 02:31:19 +00001866/* Grade school multiplication, ignoring the signs.
1867 * Returns the absolute value of the product, or NULL if error.
1868 */
1869static PyLongObject *
1870x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001871{
Tim Peters5af4e6c2002-08-12 02:31:19 +00001872 PyLongObject *z;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001873 Py_ssize_t size_a = ABS(a->ob_size);
1874 Py_ssize_t size_b = ABS(b->ob_size);
1875 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00001876
Tim Peters5af4e6c2002-08-12 02:31:19 +00001877 z = _PyLong_New(size_a + size_b);
1878 if (z == NULL)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001879 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001880
1881 memset(z->ob_digit, 0, z->ob_size * sizeof(digit));
Tim Peters0973b992004-08-29 22:16:50 +00001882 if (a == b) {
1883 /* Efficient squaring per HAC, Algorithm 14.16:
1884 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1885 * Gives slightly less than a 2x speedup when a == b,
1886 * via exploiting that each entry in the multiplication
1887 * pyramid appears twice (except for the size_a squares).
1888 */
1889 for (i = 0; i < size_a; ++i) {
1890 twodigits carry;
1891 twodigits f = a->ob_digit[i];
1892 digit *pz = z->ob_digit + (i << 1);
1893 digit *pa = a->ob_digit + i + 1;
1894 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001895
Tim Peters0973b992004-08-29 22:16:50 +00001896 SIGCHECK({
1897 Py_DECREF(z);
1898 return NULL;
1899 })
1900
1901 carry = *pz + f * f;
1902 *pz++ = (digit)(carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001903 carry >>= SHIFT;
Tim Peters0973b992004-08-29 22:16:50 +00001904 assert(carry <= MASK);
1905
1906 /* Now f is added in twice in each column of the
1907 * pyramid it appears. Same as adding f<<1 once.
1908 */
1909 f <<= 1;
1910 while (pa < paend) {
1911 carry += *pz + *pa++ * f;
1912 *pz++ = (digit)(carry & MASK);
1913 carry >>= SHIFT;
1914 assert(carry <= (MASK << 1));
1915 }
1916 if (carry) {
1917 carry += *pz;
1918 *pz++ = (digit)(carry & MASK);
1919 carry >>= SHIFT;
1920 }
1921 if (carry)
1922 *pz += (digit)(carry & MASK);
1923 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001924 }
Tim Peters0973b992004-08-29 22:16:50 +00001925 }
1926 else { /* a is not the same as b -- gradeschool long mult */
1927 for (i = 0; i < size_a; ++i) {
1928 twodigits carry = 0;
1929 twodigits f = a->ob_digit[i];
1930 digit *pz = z->ob_digit + i;
1931 digit *pb = b->ob_digit;
1932 digit *pbend = b->ob_digit + size_b;
1933
1934 SIGCHECK({
1935 Py_DECREF(z);
1936 return NULL;
1937 })
1938
1939 while (pb < pbend) {
1940 carry += *pz + *pb++ * f;
1941 *pz++ = (digit)(carry & MASK);
1942 carry >>= SHIFT;
1943 assert(carry <= MASK);
1944 }
1945 if (carry)
1946 *pz += (digit)(carry & MASK);
1947 assert((carry >> SHIFT) == 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001948 }
1949 }
Tim Peters44121a62002-08-12 06:17:58 +00001950 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001951}
1952
1953/* A helper for Karatsuba multiplication (k_mul).
1954 Takes a long "n" and an integer "size" representing the place to
1955 split, and sets low and high such that abs(n) == (high << size) + low,
1956 viewing the shift as being by digits. The sign bit is ignored, and
1957 the return values are >= 0.
1958 Returns 0 on success, -1 on failure.
1959*/
1960static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001961kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00001962{
1963 PyLongObject *hi, *lo;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001964 Py_ssize_t size_lo, size_hi;
1965 const Py_ssize_t size_n = ABS(n->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001966
1967 size_lo = MIN(size_n, size);
1968 size_hi = size_n - size_lo;
1969
1970 if ((hi = _PyLong_New(size_hi)) == NULL)
1971 return -1;
1972 if ((lo = _PyLong_New(size_lo)) == NULL) {
1973 Py_DECREF(hi);
1974 return -1;
1975 }
1976
1977 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
1978 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
1979
1980 *high = long_normalize(hi);
1981 *low = long_normalize(lo);
1982 return 0;
1983}
1984
Tim Peters60004642002-08-12 22:01:34 +00001985static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
1986
Tim Peters5af4e6c2002-08-12 02:31:19 +00001987/* Karatsuba multiplication. Ignores the input signs, and returns the
1988 * absolute value of the product (or NULL if error).
1989 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
1990 */
1991static PyLongObject *
1992k_mul(PyLongObject *a, PyLongObject *b)
1993{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001994 Py_ssize_t asize = ABS(a->ob_size);
1995 Py_ssize_t bsize = ABS(b->ob_size);
Tim Peters5af4e6c2002-08-12 02:31:19 +00001996 PyLongObject *ah = NULL;
1997 PyLongObject *al = NULL;
1998 PyLongObject *bh = NULL;
1999 PyLongObject *bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002000 PyLongObject *ret = NULL;
Tim Peters738eda72002-08-12 15:08:20 +00002001 PyLongObject *t1, *t2, *t3;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002 Py_ssize_t shift; /* the number of digits we split off */
2003 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00002004
Tim Peters5af4e6c2002-08-12 02:31:19 +00002005 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
2006 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
2007 * Then the original product is
Tim Peters18c15b92002-08-12 02:43:58 +00002008 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
Tim Peters5af4e6c2002-08-12 02:31:19 +00002009 * By picking X to be a power of 2, "*X" is just shifting, and it's
2010 * been reduced to 3 multiplies on numbers half the size.
2011 */
2012
Tim Petersfc07e562002-08-12 02:54:10 +00002013 /* We want to split based on the larger number; fiddle so that b
Tim Peters5af4e6c2002-08-12 02:31:19 +00002014 * is largest.
2015 */
Tim Peters738eda72002-08-12 15:08:20 +00002016 if (asize > bsize) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002017 t1 = a;
2018 a = b;
2019 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00002020
2021 i = asize;
2022 asize = bsize;
2023 bsize = i;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002024 }
2025
2026 /* Use gradeschool math when either number is too small. */
Tim Peters0973b992004-08-29 22:16:50 +00002027 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
2028 if (asize <= i) {
Tim Peters738eda72002-08-12 15:08:20 +00002029 if (asize == 0)
Tim Peters44121a62002-08-12 06:17:58 +00002030 return _PyLong_New(0);
2031 else
2032 return x_mul(a, b);
2033 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002034
Tim Peters60004642002-08-12 22:01:34 +00002035 /* If a is small compared to b, splitting on b gives a degenerate
2036 * case with ah==0, and Karatsuba may be (even much) less efficient
2037 * than "grade school" then. However, we can still win, by viewing
2038 * b as a string of "big digits", each of width a->ob_size. That
2039 * leads to a sequence of balanced calls to k_mul.
2040 */
2041 if (2 * asize <= bsize)
2042 return k_lopsided_mul(a, b);
2043
Tim Petersd6974a52002-08-13 20:37:51 +00002044 /* Split a & b into hi & lo pieces. */
Tim Peters738eda72002-08-12 15:08:20 +00002045 shift = bsize >> 1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002046 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
Tim Petersd6974a52002-08-13 20:37:51 +00002047 assert(ah->ob_size > 0); /* the split isn't degenerate */
2048
Tim Peters0973b992004-08-29 22:16:50 +00002049 if (a == b) {
2050 bh = ah;
2051 bl = al;
2052 Py_INCREF(bh);
2053 Py_INCREF(bl);
2054 }
2055 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002056
Tim Petersd64c1de2002-08-12 17:36:03 +00002057 /* The plan:
2058 * 1. Allocate result space (asize + bsize digits: that's always
2059 * enough).
2060 * 2. Compute ah*bh, and copy into result at 2*shift.
2061 * 3. Compute al*bl, and copy into result at 0. Note that this
2062 * can't overlap with #2.
2063 * 4. Subtract al*bl from the result, starting at shift. This may
2064 * underflow (borrow out of the high digit), but we don't care:
2065 * we're effectively doing unsigned arithmetic mod
2066 * BASE**(sizea + sizeb), and so long as the *final* result fits,
2067 * borrows and carries out of the high digit can be ignored.
2068 * 5. Subtract ah*bh from the result, starting at shift.
2069 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
2070 * at shift.
2071 */
2072
2073 /* 1. Allocate result space. */
Tim Peters70b041b2002-08-12 19:38:01 +00002074 ret = _PyLong_New(asize + bsize);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002075 if (ret == NULL) goto fail;
2076#ifdef Py_DEBUG
2077 /* Fill with trash, to catch reference to uninitialized digits. */
2078 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit));
2079#endif
Tim Peters44121a62002-08-12 06:17:58 +00002080
Tim Petersd64c1de2002-08-12 17:36:03 +00002081 /* 2. t1 <- ah*bh, and copy into high digits of result. */
Tim Peters738eda72002-08-12 15:08:20 +00002082 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
2083 assert(t1->ob_size >= 0);
2084 assert(2*shift + t1->ob_size <= ret->ob_size);
2085 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
2086 t1->ob_size * sizeof(digit));
2087
2088 /* Zero-out the digits higher than the ah*bh copy. */
2089 i = ret->ob_size - 2*shift - t1->ob_size;
Tim Peters44121a62002-08-12 06:17:58 +00002090 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002091 memset(ret->ob_digit + 2*shift + t1->ob_size, 0,
Tim Peters44121a62002-08-12 06:17:58 +00002092 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002093
Tim Petersd64c1de2002-08-12 17:36:03 +00002094 /* 3. t2 <- al*bl, and copy into the low digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002095 if ((t2 = k_mul(al, bl)) == NULL) {
2096 Py_DECREF(t1);
2097 goto fail;
2098 }
2099 assert(t2->ob_size >= 0);
2100 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */
2101 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002102
2103 /* Zero out remaining digits. */
Tim Peters738eda72002-08-12 15:08:20 +00002104 i = 2*shift - t2->ob_size; /* number of uninitialized digits */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002105 if (i)
Tim Peters738eda72002-08-12 15:08:20 +00002106 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00002107
Tim Petersd64c1de2002-08-12 17:36:03 +00002108 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
2109 * because it's fresher in cache.
2110 */
Tim Peters738eda72002-08-12 15:08:20 +00002111 i = ret->ob_size - shift; /* # digits after shift */
Tim Petersd64c1de2002-08-12 17:36:03 +00002112 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002113 Py_DECREF(t2);
2114
Tim Petersd64c1de2002-08-12 17:36:03 +00002115 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002116 Py_DECREF(t1);
2117
Tim Petersd64c1de2002-08-12 17:36:03 +00002118 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002119 if ((t1 = x_add(ah, al)) == NULL) goto fail;
2120 Py_DECREF(ah);
2121 Py_DECREF(al);
2122 ah = al = NULL;
2123
Tim Peters0973b992004-08-29 22:16:50 +00002124 if (a == b) {
2125 t2 = t1;
2126 Py_INCREF(t2);
2127 }
2128 else if ((t2 = x_add(bh, bl)) == NULL) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002129 Py_DECREF(t1);
2130 goto fail;
2131 }
2132 Py_DECREF(bh);
2133 Py_DECREF(bl);
2134 bh = bl = NULL;
2135
Tim Peters738eda72002-08-12 15:08:20 +00002136 t3 = k_mul(t1, t2);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002137 Py_DECREF(t1);
2138 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00002139 if (t3 == NULL) goto fail;
Tim Peters547607c2002-08-12 19:43:49 +00002140 assert(t3->ob_size >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002141
Tim Petersd6974a52002-08-13 20:37:51 +00002142 /* Add t3. It's not obvious why we can't run out of room here.
2143 * See the (*) comment after this function.
Tim Petersd8b21732002-08-12 19:30:26 +00002144 */
Tim Petersd64c1de2002-08-12 17:36:03 +00002145 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size);
Tim Peters738eda72002-08-12 15:08:20 +00002146 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002147
Tim Peters5af4e6c2002-08-12 02:31:19 +00002148 return long_normalize(ret);
2149
2150 fail:
2151 Py_XDECREF(ret);
2152 Py_XDECREF(ah);
2153 Py_XDECREF(al);
2154 Py_XDECREF(bh);
2155 Py_XDECREF(bl);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002156 return NULL;
2157}
2158
Tim Petersd6974a52002-08-13 20:37:51 +00002159/* (*) Why adding t3 can't "run out of room" above.
2160
Tim Petersab86c2b2002-08-15 20:06:00 +00002161Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
2162to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00002163
Tim Petersab86c2b2002-08-15 20:06:00 +000021641. For any integer i, i = c(i/2) + f(i/2). In particular,
2165 bsize = c(bsize/2) + f(bsize/2).
21662. shift = f(bsize/2)
21673. asize <= bsize
21684. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
2169 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00002170
Tim Petersab86c2b2002-08-15 20:06:00 +00002171We allocated asize + bsize result digits, and add t3 into them at an offset
2172of shift. This leaves asize+bsize-shift allocated digit positions for t3
2173to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
2174asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00002175
Tim Petersab86c2b2002-08-15 20:06:00 +00002176bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
2177at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002178
Tim Petersab86c2b2002-08-15 20:06:00 +00002179If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
2180digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
2181most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00002182
Tim Petersab86c2b2002-08-15 20:06:00 +00002183The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00002184
Tim Petersab86c2b2002-08-15 20:06:00 +00002185 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00002186
Tim Petersab86c2b2002-08-15 20:06:00 +00002187and we have asize + c(bsize/2) available digit positions. We need to show
2188this is always enough. An instance of c(bsize/2) cancels out in both, so
2189the question reduces to whether asize digits is enough to hold
2190(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
2191then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
2192asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
2193digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If
2194asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00002195c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
2196is enough to hold 2 bits. This is so if bsize >= 2, which holds because
2197bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00002198
Tim Peters48d52c02002-08-14 17:07:32 +00002199Note that since there's always enough room for (ah+al)*(bh+bl), and that's
2200clearly >= each of ah*bh and al*bl, there's always enough room to subtract
2201ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00002202*/
2203
Tim Peters60004642002-08-12 22:01:34 +00002204/* b has at least twice the digits of a, and a is big enough that Karatsuba
2205 * would pay off *if* the inputs had balanced sizes. View b as a sequence
2206 * of slices, each with a->ob_size digits, and multiply the slices by a,
2207 * one at a time. This gives k_mul balanced inputs to work with, and is
2208 * also cache-friendly (we compute one double-width slice of the result
2209 * at a time, then move on, never bactracking except for the helpful
2210 * single-width slice overlap between successive partial sums).
2211 */
2212static PyLongObject *
2213k_lopsided_mul(PyLongObject *a, PyLongObject *b)
2214{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002215 const Py_ssize_t asize = ABS(a->ob_size);
2216 Py_ssize_t bsize = ABS(b->ob_size);
2217 Py_ssize_t nbdone; /* # of b digits already multiplied */
Tim Peters60004642002-08-12 22:01:34 +00002218 PyLongObject *ret;
2219 PyLongObject *bslice = NULL;
2220
2221 assert(asize > KARATSUBA_CUTOFF);
2222 assert(2 * asize <= bsize);
2223
2224 /* Allocate result space, and zero it out. */
2225 ret = _PyLong_New(asize + bsize);
2226 if (ret == NULL)
2227 return NULL;
2228 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit));
2229
2230 /* Successive slices of b are copied into bslice. */
Tim Peters12034032002-08-12 22:10:00 +00002231 bslice = _PyLong_New(asize);
Tim Peters60004642002-08-12 22:01:34 +00002232 if (bslice == NULL)
2233 goto fail;
2234
2235 nbdone = 0;
2236 while (bsize > 0) {
2237 PyLongObject *product;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002238 const Py_ssize_t nbtouse = MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00002239
2240 /* Multiply the next slice of b by a. */
2241 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
2242 nbtouse * sizeof(digit));
2243 bslice->ob_size = nbtouse;
2244 product = k_mul(a, bslice);
2245 if (product == NULL)
2246 goto fail;
2247
2248 /* Add into result. */
2249 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone,
2250 product->ob_digit, product->ob_size);
2251 Py_DECREF(product);
2252
2253 bsize -= nbtouse;
2254 nbdone += nbtouse;
2255 }
2256
2257 Py_DECREF(bslice);
2258 return long_normalize(ret);
2259
2260 fail:
2261 Py_DECREF(ret);
2262 Py_XDECREF(bslice);
2263 return NULL;
2264}
Tim Peters5af4e6c2002-08-12 02:31:19 +00002265
2266static PyObject *
2267long_mul(PyLongObject *v, PyLongObject *w)
2268{
2269 PyLongObject *a, *b, *z;
2270
2271 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002272 Py_INCREF(Py_NotImplemented);
2273 return Py_NotImplemented;
2274 }
2275
Tim Petersd64c1de2002-08-12 17:36:03 +00002276 z = k_mul(a, b);
Tim Peters9973d742002-08-15 19:41:06 +00002277 /* Negate if exactly one of the inputs is negative. */
2278 if (((a->ob_size ^ b->ob_size) < 0) && z)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002279 z->ob_size = -(z->ob_size);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002280 Py_DECREF(a);
2281 Py_DECREF(b);
Tim Peters9973d742002-08-15 19:41:06 +00002282 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002283}
2284
Guido van Rossume32e0141992-01-19 16:31:05 +00002285/* The / and % operators are now defined in terms of divmod().
2286 The expression a mod b has the value a - b*floor(a/b).
2287 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002288 |a| by |b|, with the sign of a. This is also expressed
2289 as a - b*trunc(a/b), if trunc truncates towards zero.
2290 Some examples:
2291 a b a rem b a mod b
2292 13 10 3 3
2293 -13 10 -3 7
2294 13 -10 3 -7
2295 -13 -10 -3 -3
2296 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002297 have different signs. We then subtract one from the 'div'
2298 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002299
Tim Peters47e52ee2004-08-30 02:44:38 +00002300/* Compute
2301 * *pdiv, *pmod = divmod(v, w)
2302 * NULL can be passed for pdiv or pmod, in which case that part of
2303 * the result is simply thrown away. The caller owns a reference to
2304 * each of these it requests (does not pass NULL for).
2305 */
Guido van Rossume32e0141992-01-19 16:31:05 +00002306static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00002307l_divmod(PyLongObject *v, PyLongObject *w,
Tim Peters9f688bf2000-07-07 15:53:28 +00002308 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00002309{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002310 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002311
Guido van Rossume32e0141992-01-19 16:31:05 +00002312 if (long_divrem(v, w, &div, &mod) < 0)
2313 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00002314 if ((mod->ob_size < 0 && w->ob_size > 0) ||
2315 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002316 PyLongObject *temp;
2317 PyLongObject *one;
2318 temp = (PyLongObject *) long_add(mod, w);
2319 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00002320 mod = temp;
2321 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002322 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002323 return -1;
2324 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002325 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00002326 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002327 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
2328 Py_DECREF(mod);
2329 Py_DECREF(div);
2330 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00002331 return -1;
2332 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002333 Py_DECREF(one);
2334 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00002335 div = temp;
2336 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002337 if (pdiv != NULL)
2338 *pdiv = div;
2339 else
2340 Py_DECREF(div);
2341
2342 if (pmod != NULL)
2343 *pmod = mod;
2344 else
2345 Py_DECREF(mod);
2346
Guido van Rossume32e0141992-01-19 16:31:05 +00002347 return 0;
2348}
2349
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002350static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002351long_div(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002352{
Tim Peters47e52ee2004-08-30 02:44:38 +00002353 PyLongObject *a, *b, *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002354
2355 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002356 if (l_divmod(a, b, &div, NULL) < 0)
2357 div = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002358 Py_DECREF(a);
2359 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002360 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00002361}
2362
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002363static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +00002364long_classic_div(PyObject *v, PyObject *w)
2365{
Tim Peters47e52ee2004-08-30 02:44:38 +00002366 PyLongObject *a, *b, *div;
Guido van Rossum393661d2001-08-31 17:40:15 +00002367
2368 CONVERT_BINOP(v, w, &a, &b);
Guido van Rossum393661d2001-08-31 17:40:15 +00002369 if (Py_DivisionWarningFlag &&
2370 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
2371 div = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00002372 else if (l_divmod(a, b, &div, NULL) < 0)
Guido van Rossum393661d2001-08-31 17:40:15 +00002373 div = NULL;
Guido van Rossum393661d2001-08-31 17:40:15 +00002374 Py_DECREF(a);
2375 Py_DECREF(b);
2376 return (PyObject *)div;
2377}
2378
2379static PyObject *
Tim Peters20dab9f2001-09-04 05:31:47 +00002380long_true_divide(PyObject *v, PyObject *w)
2381{
Tim Peterse2a60002001-09-04 06:17:36 +00002382 PyLongObject *a, *b;
2383 double ad, bd;
Thomas Wouters553489a2006-02-01 21:32:04 +00002384 int failed, aexp = -1, bexp = -1;
Tim Peterse2a60002001-09-04 06:17:36 +00002385
2386 CONVERT_BINOP(v, w, &a, &b);
2387 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
2388 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
Tim Peters6f97e492001-11-04 23:09:40 +00002389 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2390 Py_DECREF(a);
2391 Py_DECREF(b);
2392 if (failed)
Tim Peterse2a60002001-09-04 06:17:36 +00002393 return NULL;
Thomas Wouters553489a2006-02-01 21:32:04 +00002394 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
2395 but should really be set correctly after sucessful calls to
2396 _PyLong_AsScaledDouble() */
2397 assert(aexp >= 0 && bexp >= 0);
Tim Peterse2a60002001-09-04 06:17:36 +00002398
2399 if (bd == 0.0) {
2400 PyErr_SetString(PyExc_ZeroDivisionError,
2401 "long division or modulo by zero");
2402 return NULL;
2403 }
2404
2405 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
2406 ad /= bd; /* overflow/underflow impossible here */
2407 aexp -= bexp;
2408 if (aexp > INT_MAX / SHIFT)
2409 goto overflow;
Tim Peterse56ed9b2001-09-06 21:59:14 +00002410 else if (aexp < -(INT_MAX / SHIFT))
2411 return PyFloat_FromDouble(0.0); /* underflow to 0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002412 errno = 0;
2413 ad = ldexp(ad, aexp * SHIFT);
Tim Peters57f282a2001-09-05 05:38:10 +00002414 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
Tim Peterse2a60002001-09-04 06:17:36 +00002415 goto overflow;
2416 return PyFloat_FromDouble(ad);
2417
2418overflow:
2419 PyErr_SetString(PyExc_OverflowError,
2420 "long/long too large for a float");
2421 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002422
Tim Peters20dab9f2001-09-04 05:31:47 +00002423}
2424
2425static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002426long_mod(PyObject *v, PyObject *w)
Guido van Rossume32e0141992-01-19 16:31:05 +00002427{
Tim Peters47e52ee2004-08-30 02:44:38 +00002428 PyLongObject *a, *b, *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002429
2430 CONVERT_BINOP(v, w, &a, &b);
2431
Tim Peters47e52ee2004-08-30 02:44:38 +00002432 if (l_divmod(a, b, NULL, &mod) < 0)
2433 mod = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002434 Py_DECREF(a);
2435 Py_DECREF(b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002436 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00002437}
2438
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002439static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002440long_divmod(PyObject *v, PyObject *w)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002441{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002442 PyLongObject *a, *b, *div, *mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002443 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002444
2445 CONVERT_BINOP(v, w, &a, &b);
2446
2447 if (l_divmod(a, b, &div, &mod) < 0) {
2448 Py_DECREF(a);
2449 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002450 return NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002451 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002452 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002453 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002454 PyTuple_SetItem(z, 0, (PyObject *) div);
2455 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002456 }
2457 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002458 Py_DECREF(div);
2459 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002460 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002461 Py_DECREF(a);
2462 Py_DECREF(b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002463 return z;
2464}
2465
Tim Peters47e52ee2004-08-30 02:44:38 +00002466/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002467static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002468long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002469{
Tim Peters47e52ee2004-08-30 02:44:38 +00002470 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
2471 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002472
Tim Peters47e52ee2004-08-30 02:44:38 +00002473 PyLongObject *z = NULL; /* accumulated result */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002474 Py_ssize_t i, j, k; /* counters */
Tim Peters47e52ee2004-08-30 02:44:38 +00002475 PyLongObject *temp = NULL;
2476
2477 /* 5-ary values. If the exponent is large enough, table is
2478 * precomputed so that table[i] == a**i % c for i in range(32).
2479 */
2480 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2481 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2482
2483 /* a, b, c = v, w, x */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002484 CONVERT_BINOP(v, w, &a, &b);
Tim Peters47e52ee2004-08-30 02:44:38 +00002485 if (PyLong_Check(x)) {
2486 c = (PyLongObject *)x;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002487 Py_INCREF(x);
2488 }
Tim Petersde7990b2005-07-17 23:45:23 +00002489 else if (PyInt_Check(x)) {
Tim Peters47e52ee2004-08-30 02:44:38 +00002490 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
Tim Petersde7990b2005-07-17 23:45:23 +00002491 if (c == NULL)
2492 goto Error;
2493 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002494 else if (x == Py_None)
2495 c = NULL;
Neil Schemenauerba872e22001-01-04 01:46:03 +00002496 else {
2497 Py_DECREF(a);
2498 Py_DECREF(b);
2499 Py_INCREF(Py_NotImplemented);
2500 return Py_NotImplemented;
2501 }
Tim Peters4c483c42001-09-05 06:24:58 +00002502
Tim Peters47e52ee2004-08-30 02:44:38 +00002503 if (b->ob_size < 0) { /* if exponent is negative */
2504 if (c) {
Tim Peters4c483c42001-09-05 06:24:58 +00002505 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
Tim Peters47e52ee2004-08-30 02:44:38 +00002506 "cannot be negative when 3rd argument specified");
Tim Peterscd97da32004-08-30 02:58:59 +00002507 goto Error;
Tim Peters32f453e2001-09-03 08:35:41 +00002508 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002509 else {
Tim Peters47e52ee2004-08-30 02:44:38 +00002510 /* else return a float. This works because we know
2511 that this calls float_pow() which converts its
2512 arguments to double. */
2513 Py_DECREF(a);
2514 Py_DECREF(b);
2515 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00002516 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002517 }
Tim Peters47e52ee2004-08-30 02:44:38 +00002518
2519 if (c) {
2520 /* if modulus == 0:
2521 raise ValueError() */
2522 if (c->ob_size == 0) {
2523 PyErr_SetString(PyExc_ValueError,
2524 "pow() 3rd argument cannot be 0");
Tim Peterscd97da32004-08-30 02:58:59 +00002525 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002526 }
2527
2528 /* if modulus < 0:
2529 negativeOutput = True
2530 modulus = -modulus */
2531 if (c->ob_size < 0) {
2532 negativeOutput = 1;
2533 temp = (PyLongObject *)_PyLong_Copy(c);
2534 if (temp == NULL)
2535 goto Error;
2536 Py_DECREF(c);
2537 c = temp;
2538 temp = NULL;
2539 c->ob_size = - c->ob_size;
2540 }
2541
2542 /* if modulus == 1:
2543 return 0 */
2544 if ((c->ob_size == 1) && (c->ob_digit[0] == 1)) {
2545 z = (PyLongObject *)PyLong_FromLong(0L);
2546 goto Done;
2547 }
2548
2549 /* if base < 0:
2550 base = base % modulus
2551 Having the base positive just makes things easier. */
2552 if (a->ob_size < 0) {
2553 if (l_divmod(a, c, NULL, &temp) < 0)
Tim Peterscd97da32004-08-30 02:58:59 +00002554 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00002555 Py_DECREF(a);
2556 a = temp;
2557 temp = NULL;
2558 }
2559 }
2560
2561 /* At this point a, b, and c are guaranteed non-negative UNLESS
2562 c is NULL, in which case a may be negative. */
2563
2564 z = (PyLongObject *)PyLong_FromLong(1L);
2565 if (z == NULL)
2566 goto Error;
2567
2568 /* Perform a modular reduction, X = X % c, but leave X alone if c
2569 * is NULL.
2570 */
2571#define REDUCE(X) \
2572 if (c != NULL) { \
2573 if (l_divmod(X, c, NULL, &temp) < 0) \
2574 goto Error; \
2575 Py_XDECREF(X); \
2576 X = temp; \
2577 temp = NULL; \
2578 }
2579
2580 /* Multiply two values, then reduce the result:
2581 result = X*Y % c. If c is NULL, skip the mod. */
2582#define MULT(X, Y, result) \
2583{ \
2584 temp = (PyLongObject *)long_mul(X, Y); \
2585 if (temp == NULL) \
2586 goto Error; \
2587 Py_XDECREF(result); \
2588 result = temp; \
2589 temp = NULL; \
2590 REDUCE(result) \
2591}
2592
2593 if (b->ob_size <= FIVEARY_CUTOFF) {
2594 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
2595 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
2596 for (i = b->ob_size - 1; i >= 0; --i) {
2597 digit bi = b->ob_digit[i];
2598
2599 for (j = 1 << (SHIFT-1); j != 0; j >>= 1) {
2600 MULT(z, z, z)
2601 if (bi & j)
2602 MULT(z, a, z)
2603 }
2604 }
2605 }
2606 else {
2607 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
2608 Py_INCREF(z); /* still holds 1L */
2609 table[0] = z;
2610 for (i = 1; i < 32; ++i)
2611 MULT(table[i-1], a, table[i])
2612
2613 for (i = b->ob_size - 1; i >= 0; --i) {
2614 const digit bi = b->ob_digit[i];
2615
2616 for (j = SHIFT - 5; j >= 0; j -= 5) {
2617 const int index = (bi >> j) & 0x1f;
2618 for (k = 0; k < 5; ++k)
2619 MULT(z, z, z)
2620 if (index)
2621 MULT(z, table[index], z)
2622 }
2623 }
2624 }
2625
2626 if (negativeOutput && (z->ob_size != 0)) {
2627 temp = (PyLongObject *)long_sub(z, c);
2628 if (temp == NULL)
2629 goto Error;
2630 Py_DECREF(z);
2631 z = temp;
2632 temp = NULL;
2633 }
2634 goto Done;
2635
2636 Error:
2637 if (z != NULL) {
2638 Py_DECREF(z);
2639 z = NULL;
2640 }
2641 /* fall through */
2642 Done:
Tim Peters47e52ee2004-08-30 02:44:38 +00002643 if (b->ob_size > FIVEARY_CUTOFF) {
2644 for (i = 0; i < 32; ++i)
2645 Py_XDECREF(table[i]);
2646 }
Tim Petersde7990b2005-07-17 23:45:23 +00002647 Py_DECREF(a);
2648 Py_DECREF(b);
2649 Py_XDECREF(c);
2650 Py_XDECREF(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002651 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002652}
2653
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002654static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002655long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002656{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002657 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002658 PyLongObject *x;
2659 PyLongObject *w;
2660 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002661 if (w == NULL)
2662 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002663 x = (PyLongObject *) long_add(v, w);
2664 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002665 if (x == NULL)
2666 return NULL;
Tim Peters40c397d2001-09-11 23:24:22 +00002667 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002668 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002669}
2670
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002671static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002672long_pos(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002673{
Tim Peters69c2de32001-09-11 22:31:33 +00002674 if (PyLong_CheckExact(v)) {
2675 Py_INCREF(v);
2676 return (PyObject *)v;
2677 }
2678 else
2679 return _PyLong_Copy(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002680}
2681
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002682static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002683long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002684{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002685 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00002686 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002687 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002688 Py_INCREF(v);
2689 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002690 }
Tim Peters69c2de32001-09-11 22:31:33 +00002691 z = (PyLongObject *)_PyLong_Copy(v);
2692 if (z != NULL)
2693 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002694 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002695}
2696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002697static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002698long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002699{
2700 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002701 return long_neg(v);
Tim Peters69c2de32001-09-11 22:31:33 +00002702 else
2703 return long_pos(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002704}
2705
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002706static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002707long_nonzero(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002708{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002709 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002710}
2711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002712static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002713long_rshift(PyLongObject *v, PyLongObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002714{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002715 PyLongObject *a, *b;
2716 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002717 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002718 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002719 digit lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002720
Neil Schemenauerba872e22001-01-04 01:46:03 +00002721 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
2722
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002723 if (a->ob_size < 0) {
2724 /* Right shifting negative numbers is harder */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002725 PyLongObject *a1, *a2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002726 a1 = (PyLongObject *) long_invert(a);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002727 if (a1 == NULL)
2728 goto rshift_error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002729 a2 = (PyLongObject *) long_rshift(a1, b);
2730 Py_DECREF(a1);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002731 if (a2 == NULL)
2732 goto rshift_error;
2733 z = (PyLongObject *) long_invert(a2);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002734 Py_DECREF(a2);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002735 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002736 else {
Tim Peters5af4e6c2002-08-12 02:31:19 +00002737
Neil Schemenauerba872e22001-01-04 01:46:03 +00002738 shiftby = PyLong_AsLong((PyObject *)b);
2739 if (shiftby == -1L && PyErr_Occurred())
2740 goto rshift_error;
2741 if (shiftby < 0) {
2742 PyErr_SetString(PyExc_ValueError,
2743 "negative shift count");
2744 goto rshift_error;
2745 }
2746 wordshift = shiftby / SHIFT;
2747 newsize = ABS(a->ob_size) - wordshift;
2748 if (newsize <= 0) {
2749 z = _PyLong_New(0);
2750 Py_DECREF(a);
2751 Py_DECREF(b);
2752 return (PyObject *)z;
2753 }
2754 loshift = shiftby % SHIFT;
2755 hishift = SHIFT - loshift;
2756 lomask = ((digit)1 << hishift) - 1;
2757 himask = MASK ^ lomask;
2758 z = _PyLong_New(newsize);
2759 if (z == NULL)
2760 goto rshift_error;
2761 if (a->ob_size < 0)
2762 z->ob_size = -(z->ob_size);
2763 for (i = 0, j = wordshift; i < newsize; i++, j++) {
2764 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
2765 if (i+1 < newsize)
2766 z->ob_digit[i] |=
2767 (a->ob_digit[j+1] << hishift) & himask;
2768 }
2769 z = long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002770 }
Neil Schemenauerba872e22001-01-04 01:46:03 +00002771rshift_error:
2772 Py_DECREF(a);
2773 Py_DECREF(b);
2774 return (PyObject *) z;
2775
Guido van Rossumc6913e71991-11-19 20:26:46 +00002776}
2777
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002778static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002779long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002780{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002781 /* This version due to Tim Peters */
Neil Schemenauerba872e22001-01-04 01:46:03 +00002782 PyLongObject *a, *b;
2783 PyLongObject *z = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002784 long shiftby;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002785 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002786 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002787
Neil Schemenauerba872e22001-01-04 01:46:03 +00002788 CONVERT_BINOP(v, w, &a, &b);
2789
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002790 shiftby = PyLong_AsLong((PyObject *)b);
2791 if (shiftby == -1L && PyErr_Occurred())
Neil Schemenauerba872e22001-01-04 01:46:03 +00002792 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002793 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002794 PyErr_SetString(PyExc_ValueError, "negative shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002795 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002796 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002797 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002798 PyErr_SetString(PyExc_ValueError,
2799 "outrageous left shift count");
Neil Schemenauerba872e22001-01-04 01:46:03 +00002800 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002801 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002802 /* wordshift, remshift = divmod(shiftby, SHIFT) */
2803 wordshift = (int)shiftby / SHIFT;
2804 remshift = (int)shiftby - wordshift * SHIFT;
2805
2806 oldsize = ABS(a->ob_size);
2807 newsize = oldsize + wordshift;
2808 if (remshift)
2809 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002810 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002811 if (z == NULL)
Neil Schemenauerba872e22001-01-04 01:46:03 +00002812 goto lshift_error;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002813 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002814 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00002815 for (i = 0; i < wordshift; i++)
2816 z->ob_digit[i] = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002817 accum = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002818 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
Tim Peters0d2d87d2002-08-20 19:00:22 +00002819 accum |= (twodigits)a->ob_digit[j] << remshift;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002820 z->ob_digit[i] = (digit)(accum & MASK);
2821 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002822 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002823 if (remshift)
2824 z->ob_digit[newsize-1] = (digit)accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002825 else
Guido van Rossumf2e499b1997-03-16 00:37:59 +00002826 assert(!accum);
Neil Schemenauerba872e22001-01-04 01:46:03 +00002827 z = long_normalize(z);
2828lshift_error:
2829 Py_DECREF(a);
2830 Py_DECREF(b);
2831 return (PyObject *) z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002832}
2833
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002834
2835/* Bitwise and/xor/or operations */
2836
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002837static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002838long_bitwise(PyLongObject *a,
2839 int op, /* '&', '|', '^' */
2840 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002841{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002842 digit maska, maskb; /* 0 or MASK */
2843 int negz;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002844 Py_ssize_t size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002845 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002846 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00002847 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002848 PyObject *v;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002849
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002850 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002851 a = (PyLongObject *) long_invert(a);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002852 if (a == NULL)
2853 return NULL;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002854 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002855 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002856 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002857 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002858 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002859 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002860 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002861 b = (PyLongObject *) long_invert(b);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002862 if (b == NULL) {
2863 Py_DECREF(a);
2864 return NULL;
2865 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002866 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002867 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002868 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002869 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002870 maskb = 0;
2871 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002872
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002873 negz = 0;
2874 switch (op) {
2875 case '^':
2876 if (maska != maskb) {
2877 maska ^= MASK;
2878 negz = -1;
2879 }
2880 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002881 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002882 if (maska && maskb) {
2883 op = '|';
2884 maska ^= MASK;
2885 maskb ^= MASK;
2886 negz = -1;
2887 }
2888 break;
2889 case '|':
2890 if (maska || maskb) {
2891 op = '&';
2892 maska ^= MASK;
2893 maskb ^= MASK;
2894 negz = -1;
2895 }
2896 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002897 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002898
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002899 /* JRH: The original logic here was to allocate the result value (z)
2900 as the longer of the two operands. However, there are some cases
2901 where the result is guaranteed to be shorter than that: AND of two
2902 positives, OR of two negatives: use the shorter number. AND with
2903 mixed signs: use the positive number. OR with mixed signs: use the
2904 negative number. After the transformations above, op will be '&'
2905 iff one of these cases applies, and mask will be non-0 for operands
2906 whose length should be ignored.
2907 */
2908
2909 size_a = a->ob_size;
2910 size_b = b->ob_size;
2911 size_z = op == '&'
2912 ? (maska
2913 ? size_b
2914 : (maskb ? size_a : MIN(size_a, size_b)))
2915 : MAX(size_a, size_b);
2916 z = _PyLong_New(size_z);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002917 if (z == NULL) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +00002918 Py_XDECREF(a);
2919 Py_XDECREF(b);
2920 Py_XDECREF(z);
2921 return NULL;
2922 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002923
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002924 for (i = 0; i < size_z; ++i) {
2925 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
2926 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
2927 switch (op) {
2928 case '&': z->ob_digit[i] = diga & digb; break;
2929 case '|': z->ob_digit[i] = diga | digb; break;
2930 case '^': z->ob_digit[i] = diga ^ digb; break;
2931 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00002932 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002933
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002934 Py_DECREF(a);
2935 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002936 z = long_normalize(z);
2937 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002938 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002939 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002940 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00002941 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002942}
2943
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002944static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002945long_and(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002946{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002947 PyLongObject *a, *b;
2948 PyObject *c;
2949 CONVERT_BINOP(v, w, &a, &b);
2950 c = long_bitwise(a, '&', b);
2951 Py_DECREF(a);
2952 Py_DECREF(b);
2953 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002954}
2955
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002956static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002957long_xor(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002958{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002959 PyLongObject *a, *b;
2960 PyObject *c;
2961 CONVERT_BINOP(v, w, &a, &b);
2962 c = long_bitwise(a, '^', b);
2963 Py_DECREF(a);
2964 Py_DECREF(b);
2965 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00002966}
2967
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002968static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00002969long_or(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00002970{
Neil Schemenauerba872e22001-01-04 01:46:03 +00002971 PyLongObject *a, *b;
2972 PyObject *c;
2973 CONVERT_BINOP(v, w, &a, &b);
2974 c = long_bitwise(a, '|', b);
2975 Py_DECREF(a);
2976 Py_DECREF(b);
2977 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00002978}
2979
Guido van Rossum234f9421993-06-17 12:35:49 +00002980static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002981long_coerce(PyObject **pv, PyObject **pw)
Guido van Rossume6eefc21992-08-14 12:06:52 +00002982{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002983 if (PyInt_Check(*pw)) {
Neil Schemenauerba872e22001-01-04 01:46:03 +00002984 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002985 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002986 return 0;
2987 }
Guido van Rossum1952e382001-09-19 01:25:16 +00002988 else if (PyLong_Check(*pw)) {
2989 Py_INCREF(*pv);
2990 Py_INCREF(*pw);
2991 return 0;
2992 }
Guido van Rossume6eefc21992-08-14 12:06:52 +00002993 return 1; /* Can't do it */
2994}
2995
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002996static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002997long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00002998{
Brett Cannonc3647ac2005-04-26 03:45:26 +00002999 if (PyLong_CheckExact(v))
3000 Py_INCREF(v);
3001 else
3002 v = _PyLong_Copy((PyLongObject *)v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003003 return v;
3004}
3005
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003006static PyObject *
Walter Dörwaldf1715402002-11-19 20:49:15 +00003007long_int(PyObject *v)
3008{
3009 long x;
3010 x = PyLong_AsLong(v);
3011 if (PyErr_Occurred()) {
3012 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
3013 PyErr_Clear();
3014 if (PyLong_CheckExact(v)) {
3015 Py_INCREF(v);
3016 return v;
3017 }
3018 else
3019 return _PyLong_Copy((PyLongObject *)v);
3020 }
3021 else
3022 return NULL;
3023 }
3024 return PyInt_FromLong(x);
3025}
3026
3027static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003028long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003029{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00003030 double result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003031 result = PyLong_AsDouble(v);
Tim Peters9fffa3e2001-09-04 05:14:19 +00003032 if (result == -1.0 && PyErr_Occurred())
3033 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003034 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003035}
3036
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003037static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003038long_oct(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003039{
Fred Drake121ee271999-12-23 15:41:28 +00003040 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003041}
3042
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003043static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003044long_hex(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003045{
Fred Drake121ee271999-12-23 15:41:28 +00003046 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003047}
Jeremy Hylton938ace62002-07-17 16:30:39 +00003048
3049static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00003050long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00003051
Tim Peters6d6c1a32001-08-02 04:15:00 +00003052static PyObject *
3053long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3054{
3055 PyObject *x = NULL;
3056 int base = -909; /* unlikely! */
Martin v. Löwis15e62742006-02-27 16:46:16 +00003057 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00003058
Guido van Rossumbef14172001-08-29 15:47:46 +00003059 if (type != &PyLong_Type)
3060 return long_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003061 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
3062 &x, &base))
3063 return NULL;
3064 if (x == NULL)
3065 return PyLong_FromLong(0L);
3066 if (base == -909)
3067 return PyNumber_Long(x);
3068 else if (PyString_Check(x))
3069 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003070#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00003071 else if (PyUnicode_Check(x))
3072 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
3073 PyUnicode_GET_SIZE(x),
3074 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003075#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003076 else {
3077 PyErr_SetString(PyExc_TypeError,
3078 "long() can't convert non-string with explicit base");
3079 return NULL;
3080 }
3081}
3082
Guido van Rossumbef14172001-08-29 15:47:46 +00003083/* Wimpy, slow approach to tp_new calls for subtypes of long:
3084 first create a regular long from whatever arguments we got,
3085 then allocate a subtype instance and initialize it from
3086 the regular long. The regular long is then thrown away.
3087*/
3088static PyObject *
3089long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3090{
Anthony Baxter377be112006-04-11 06:54:30 +00003091 PyLongObject *tmp, *newobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003092 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00003093
3094 assert(PyType_IsSubtype(type, &PyLong_Type));
3095 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
3096 if (tmp == NULL)
3097 return NULL;
Tim Peters64b5ce32001-09-10 20:52:51 +00003098 assert(PyLong_CheckExact(tmp));
Guido van Rossumbef14172001-08-29 15:47:46 +00003099 n = tmp->ob_size;
3100 if (n < 0)
3101 n = -n;
Anthony Baxter377be112006-04-11 06:54:30 +00003102 newobj = (PyLongObject *)type->tp_alloc(type, n);
3103 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00003104 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00003105 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00003106 }
Anthony Baxter377be112006-04-11 06:54:30 +00003107 assert(PyLong_Check(newobj));
3108 newobj->ob_size = tmp->ob_size;
Guido van Rossumbef14172001-08-29 15:47:46 +00003109 for (i = 0; i < n; i++)
Anthony Baxter377be112006-04-11 06:54:30 +00003110 newobj->ob_digit[i] = tmp->ob_digit[i];
Guido van Rossumbef14172001-08-29 15:47:46 +00003111 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00003112 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00003113}
3114
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003115static PyObject *
3116long_getnewargs(PyLongObject *v)
3117{
3118 return Py_BuildValue("(N)", _PyLong_Copy(v));
3119}
3120
3121static PyMethodDef long_methods[] = {
3122 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3123 {NULL, NULL} /* sentinel */
3124};
3125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003126PyDoc_STRVAR(long_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00003127"long(x[, base]) -> integer\n\
3128\n\
3129Convert a string or number to a long integer, if possible. A floating\n\
3130point argument will be truncated towards zero (this does not include a\n\
3131string representation of a floating point number!) When converting a\n\
3132string, use the optional base. It is an error to supply a base when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003133converting a non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003134
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003135static PyNumberMethods long_as_number = {
Tim Peters9f688bf2000-07-07 15:53:28 +00003136 (binaryfunc) long_add, /*nb_add*/
3137 (binaryfunc) long_sub, /*nb_subtract*/
3138 (binaryfunc) long_mul, /*nb_multiply*/
Georg Brandl347b3002006-03-30 11:57:00 +00003139 long_classic_div, /*nb_divide*/
3140 long_mod, /*nb_remainder*/
3141 long_divmod, /*nb_divmod*/
3142 long_pow, /*nb_power*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003143 (unaryfunc) long_neg, /*nb_negative*/
3144 (unaryfunc) long_pos, /*tp_positive*/
3145 (unaryfunc) long_abs, /*tp_absolute*/
3146 (inquiry) long_nonzero, /*tp_nonzero*/
3147 (unaryfunc) long_invert, /*nb_invert*/
Georg Brandl347b3002006-03-30 11:57:00 +00003148 long_lshift, /*nb_lshift*/
Tim Peters9f688bf2000-07-07 15:53:28 +00003149 (binaryfunc) long_rshift, /*nb_rshift*/
Georg Brandl347b3002006-03-30 11:57:00 +00003150 long_and, /*nb_and*/
3151 long_xor, /*nb_xor*/
3152 long_or, /*nb_or*/
3153 long_coerce, /*nb_coerce*/
3154 long_int, /*nb_int*/
3155 long_long, /*nb_long*/
3156 long_float, /*nb_float*/
3157 long_oct, /*nb_oct*/
3158 long_hex, /*nb_hex*/
Guido van Rossum4668b002001-08-08 05:00:18 +00003159 0, /* nb_inplace_add */
3160 0, /* nb_inplace_subtract */
3161 0, /* nb_inplace_multiply */
3162 0, /* nb_inplace_divide */
3163 0, /* nb_inplace_remainder */
3164 0, /* nb_inplace_power */
3165 0, /* nb_inplace_lshift */
3166 0, /* nb_inplace_rshift */
3167 0, /* nb_inplace_and */
3168 0, /* nb_inplace_xor */
3169 0, /* nb_inplace_or */
Georg Brandl347b3002006-03-30 11:57:00 +00003170 long_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00003171 long_true_divide, /* nb_true_divide */
3172 0, /* nb_inplace_floor_divide */
3173 0, /* nb_inplace_true_divide */
Georg Brandl347b3002006-03-30 11:57:00 +00003174 long_index, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003175};
3176
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003177PyTypeObject PyLong_Type = {
3178 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003179 0, /* ob_size */
3180 "long", /* tp_name */
3181 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
3182 sizeof(digit), /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003183 long_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003184 0, /* tp_print */
3185 0, /* tp_getattr */
3186 0, /* tp_setattr */
3187 (cmpfunc)long_compare, /* tp_compare */
Georg Brandl347b3002006-03-30 11:57:00 +00003188 long_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003189 &long_as_number, /* tp_as_number */
3190 0, /* tp_as_sequence */
3191 0, /* tp_as_mapping */
3192 (hashfunc)long_hash, /* tp_hash */
3193 0, /* tp_call */
Georg Brandl347b3002006-03-30 11:57:00 +00003194 long_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003195 PyObject_GenericGetAttr, /* tp_getattro */
3196 0, /* tp_setattro */
3197 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00003198 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
3199 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003200 long_doc, /* tp_doc */
3201 0, /* tp_traverse */
3202 0, /* tp_clear */
3203 0, /* tp_richcompare */
3204 0, /* tp_weaklistoffset */
3205 0, /* tp_iter */
3206 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00003207 long_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003208 0, /* tp_members */
3209 0, /* tp_getset */
3210 0, /* tp_base */
3211 0, /* tp_dict */
3212 0, /* tp_descr_get */
3213 0, /* tp_descr_set */
3214 0, /* tp_dictoffset */
3215 0, /* tp_init */
3216 0, /* tp_alloc */
3217 long_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00003218 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003219};