blob: bdf4774f9c9d4d611bfbae0e3af793a1914f0a71 [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumedcc38a1991-05-05 20:09:44 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumedcc38a1991-05-05 20:09:44 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumedcc38a1991-05-05 20:09:44 +000029
30******************************************************************/
31
32/* Long (arbitrary precision) integer object implementation */
33
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000034/* XXX The functional organization of this file is terrible */
35
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +000037#include "longintrepr.h"
Guido van Rossum687ec181995-03-04 22:43:47 +000038#include "mymath.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039
Guido van Rossumedcc38a1991-05-05 20:09:44 +000040#include <assert.h>
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000041#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000042
Guido van Rossume32e0141992-01-19 16:31:05 +000043#define ABS(x) ((x) < 0 ? -(x) : (x))
44
45/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046static PyLongObject *long_normalize Py_PROTO((PyLongObject *));
47static PyLongObject *mul1 Py_PROTO((PyLongObject *, wdigit));
48static PyLongObject *muladd1 Py_PROTO((PyLongObject *, wdigit, wdigit));
49static PyLongObject *divrem1 Py_PROTO((PyLongObject *, wdigit, digit *));
Fred Drake121ee271999-12-23 15:41:28 +000050static PyObject *long_format Py_PROTO((PyObject *aa, int base, int addL));
Guido van Rossume32e0141992-01-19 16:31:05 +000051
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000052static int ticker; /* XXX Could be shared with ceval? */
53
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054#define SIGCHECK(PyTryBlock) \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000055 if (--ticker < 0) { \
56 ticker = 100; \
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057 if (PyErr_CheckSignals()) { PyTryBlock; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000058 }
59
Guido van Rossumedcc38a1991-05-05 20:09:44 +000060/* Normalize (remove leading zeros from) a long int object.
61 Doesn't attempt to free the storage--in most cases, due to the nature
62 of the algorithms used, this could save at most be one word anyway. */
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +000065long_normalize(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 register PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000067{
Guido van Rossum4c260ff1992-01-14 18:36:43 +000068 int j = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000069 register int i = j;
70
71 while (i > 0 && v->ob_digit[i-1] == 0)
72 --i;
73 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000074 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000075 return v;
76}
77
78/* Allocate a new long int object with size digits.
79 Return NULL and set exception if we run out of memory. */
80
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081PyLongObject *
82_PyLong_New(size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000083 int size;
84{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000086}
87
88/* Create a new long int object from a C long int */
89
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
91PyLong_FromLong(ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +000092 long ival;
93{
Guido van Rossum472c04f1996-12-05 21:57:21 +000094 /* Assume a C long fits in at most 5 'digits' */
95 /* Works on both 32- and 64-bit machines */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 PyLongObject *v = _PyLong_New(5);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000097 if (v != NULL) {
Guido van Rossum472c04f1996-12-05 21:57:21 +000098 unsigned long t = ival;
99 int i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000100 if (ival < 0) {
Guido van Rossum472c04f1996-12-05 21:57:21 +0000101 t = -ival;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000102 v->ob_size = -(v->ob_size);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000103 }
104 for (i = 0; i < 5; i++) {
Guido van Rossum2095d241997-04-09 19:41:24 +0000105 v->ob_digit[i] = (digit) (t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000106 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000107 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000108 v = long_normalize(v);
109 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000111}
112
Guido van Rossum53756b11997-01-03 17:14:46 +0000113/* Create a new long int object from a C unsigned long int */
114
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115PyObject *
Guido van Rossum53756b11997-01-03 17:14:46 +0000116PyLong_FromUnsignedLong(ival)
117 unsigned long ival;
118{
119 /* Assume a C long fits in at most 5 'digits' */
120 /* Works on both 32- and 64-bit machines */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 PyLongObject *v = _PyLong_New(5);
Guido van Rossum53756b11997-01-03 17:14:46 +0000122 if (v != NULL) {
123 unsigned long t = ival;
124 int i;
125 for (i = 0; i < 5; i++) {
Guido van Rossum2095d241997-04-09 19:41:24 +0000126 v->ob_digit[i] = (digit) (t & MASK);
Guido van Rossum53756b11997-01-03 17:14:46 +0000127 t >>= SHIFT;
128 }
129 v = long_normalize(v);
130 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000132}
133
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000134/* Create a new long int object from a C double */
135
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136PyObject *
Guido van Rossum687ec181995-03-04 22:43:47 +0000137#ifdef MPW
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138PyLong_FromDouble(double dval)
Guido van Rossum687ec181995-03-04 22:43:47 +0000139#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140PyLong_FromDouble(dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000141 double dval;
Guido van Rossum687ec181995-03-04 22:43:47 +0000142#endif /* MPW */
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000143{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 PyLongObject *v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000145 double frac;
146 int i, ndig, expo, neg;
147 neg = 0;
Guido van Rossum1a23c241999-09-27 17:11:52 +0000148 if (dval && dval * 0.5 == dval) {
149 PyErr_SetString(PyExc_OverflowError,
150 "cannot convert float infinity to long");
151 return NULL;
152 }
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000153 if (dval < 0.0) {
154 neg = 1;
155 dval = -dval;
156 }
157 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
158 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000160 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000162 if (v == NULL)
163 return NULL;
164 frac = ldexp(frac, (expo-1) % SHIFT + 1);
165 for (i = ndig; --i >= 0; ) {
166 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000167 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000168 frac = frac - (double)bits;
169 frac = ldexp(frac, SHIFT);
170 }
171 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000172 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000174}
175
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000176/* Get a C long int from a long int object.
177 Returns -1 and sets an error condition if overflow occurs. */
178
179long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180PyLong_AsLong(vv)
181 PyObject *vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000182{
Guido van Rossumf7531811998-05-26 14:33:37 +0000183 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000185 unsigned long x, prev;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000186 int i, sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000187
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 if (vv == NULL || !PyLong_Check(vv)) {
189 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000190 return -1;
191 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000193 i = v->ob_size;
194 sign = 1;
195 x = 0;
196 if (i < 0) {
197 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000198 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000199 }
200 while (--i >= 0) {
201 prev = x;
202 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000203 if ((x >> SHIFT) != prev)
204 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000205 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000206 /* Haven't lost any bits, but if the sign bit is set we're in
207 * trouble *unless* this is the min negative number. So,
208 * trouble iff sign bit set && (positive || some bit set other
209 * than the sign bit).
210 */
211 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
212 goto overflow;
213 return (long)x * sign;
214
215 overflow:
216 PyErr_SetString(PyExc_OverflowError,
217 "long int too long to convert");
218 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000219}
220
Guido van Rossum53756b11997-01-03 17:14:46 +0000221/* Get a C long int from a long int object.
222 Returns -1 and sets an error condition if overflow occurs. */
223
224unsigned long
225PyLong_AsUnsignedLong(vv)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 PyObject *vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000227{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000229 unsigned long x, prev;
230 int i;
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232 if (vv == NULL || !PyLong_Check(vv)) {
233 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000234 return (unsigned long) -1;
235 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000237 i = v->ob_size;
238 x = 0;
239 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000241 "can't convert negative value to unsigned long");
242 return (unsigned long) -1;
243 }
244 while (--i >= 0) {
245 prev = x;
246 x = (x << SHIFT) + v->ob_digit[i];
247 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000249 "long int too long to convert");
250 return (unsigned long) -1;
251 }
252 }
253 return x;
254}
255
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000256/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000257
258double
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000259PyLong_AsDouble(vv)
260 PyObject *vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000261{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 register PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000263 double x;
264 double multiplier = (double) (1L << SHIFT);
265 int i, sign;
266
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 if (vv == NULL || !PyLong_Check(vv)) {
268 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000269 return -1;
270 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000272 i = v->ob_size;
273 sign = 1;
274 x = 0.0;
275 if (i < 0) {
276 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000277 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000278 }
279 while (--i >= 0) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000280 x = x*multiplier + (double)v->ob_digit[i];
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000281 }
282 return x * sign;
283}
284
Guido van Rossum78694d91998-09-18 14:14:13 +0000285/* Create a new long (or int) object from a C pointer */
286
287PyObject *
288PyLong_FromVoidPtr(p)
289 void *p;
290{
291#if SIZEOF_VOID_P == SIZEOF_LONG
292 return PyInt_FromLong((long)p);
293#else
294 /* optimize null pointers */
295 if ( p == NULL )
296 return PyInt_FromLong(0);
297
298 /* we can assume that HAVE_LONG_LONG is true. if not, then the
299 configuration process should have bailed (having big pointers
300 without long longs seems non-sensical) */
301 return PyLong_FromLongLong((LONG_LONG)p);
302#endif /* SIZEOF_VOID_P == SIZEOF_LONG */
303}
304
305/* Get a C pointer from a long object (or an int object in some cases) */
306
307void *
308PyLong_AsVoidPtr(vv)
309 PyObject *vv;
310{
311 /* This function will allow int or long objects. If vv is neither,
312 then the PyLong_AsLong*() functions will raise the exception:
313 PyExc_SystemError, "bad argument to internal function"
314 */
315
316#if SIZEOF_VOID_P == SIZEOF_LONG
317 long x;
318
319 if ( PyInt_Check(vv) )
320 x = PyInt_AS_LONG(vv);
321 else
322 x = PyLong_AsLong(vv);
323#else
324 /* we can assume that HAVE_LONG_LONG is true. if not, then the
325 configuration process should have bailed (having big pointers
326 without long longs seems non-sensical) */
327 LONG_LONG x;
328
329 if ( PyInt_Check(vv) )
330 x = PyInt_AS_LONG(vv);
331 else
332 x = PyLong_AsLongLong(vv);
333#endif /* SIZEOF_VOID_P == SIZEOF_LONG */
334
335 if (x == -1 && PyErr_Occurred())
336 return NULL;
337 return (void *)x;
338}
339
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000340#ifdef HAVE_LONG_LONG
341/*
Guido van Rossum3293b071998-08-25 16:07:15 +0000342 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000343 *
344 * For better or worse :-), I tried to follow the coding style already
345 * here.
346 */
347
Guido van Rossum3293b071998-08-25 16:07:15 +0000348/* Create a new long int object from a C LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000349
350PyObject *
351PyLong_FromLongLong(ival)
Guido van Rossum3293b071998-08-25 16:07:15 +0000352 LONG_LONG ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000353{
354#if SIZEOF_LONG_LONG == SIZEOF_LONG
355 /* In case the compiler is faking it. */
356 return PyLong_FromLong( (long)ival );
357#else
Guido van Rossum3293b071998-08-25 16:07:15 +0000358 if( ival <= (LONG_LONG)LONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000359 return PyLong_FromLong( (long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000360 }
Guido van Rossum3293b071998-08-25 16:07:15 +0000361 else if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000362 return PyLong_FromUnsignedLong( (unsigned long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000363 }
364 else {
Guido van Rossum3293b071998-08-25 16:07:15 +0000365 /* Assume a C LONG_LONG fits in at most 10 'digits'.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000366 * Should be OK if we're assuming long fits in 5.
367 */
368 PyLongObject *v = _PyLong_New(10);
369
370 if (v != NULL) {
Guido van Rossum3293b071998-08-25 16:07:15 +0000371 unsigned LONG_LONG t = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000372 int i;
373 if (ival < 0) {
374 t = -ival;
375 v->ob_size = -(v->ob_size);
376 }
377
378 for (i = 0; i < 10; i++) {
379 v->ob_digit[i] = (digit) (t & MASK);
380 t >>= SHIFT;
381 }
382
383 v = long_normalize(v);
384 }
385
386 return (PyObject *)v;
387 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000388#endif
389}
390
Guido van Rossum3293b071998-08-25 16:07:15 +0000391/* Create a new long int object from a C unsigned LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000392PyObject *
393PyLong_FromUnsignedLongLong(ival)
Guido van Rossum3293b071998-08-25 16:07:15 +0000394 unsigned LONG_LONG ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000395{
396#if SIZEOF_LONG_LONG == SIZEOF_LONG
397 /* In case the compiler is faking it. */
398 return PyLong_FromUnsignedLong( (unsigned long)ival );
399#else
Guido van Rossum3293b071998-08-25 16:07:15 +0000400 if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000401 return PyLong_FromUnsignedLong( (unsigned long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000402 }
403 else {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000404 /* Assume a C long fits in at most 10 'digits'. */
405 PyLongObject *v = _PyLong_New(10);
406
407 if (v != NULL) {
Guido van Rossum3293b071998-08-25 16:07:15 +0000408 unsigned LONG_LONG t = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000409 int i;
410 for (i = 0; i < 10; i++) {
411 v->ob_digit[i] = (digit) (t & MASK);
412 t >>= SHIFT;
413 }
414
415 v = long_normalize(v);
416 }
417
418 return (PyObject *)v;
419 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000420#endif
421}
422
Guido van Rossum3293b071998-08-25 16:07:15 +0000423/* Get a C LONG_LONG int from a long int object.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000424 Returns -1 and sets an error condition if overflow occurs. */
425
Guido van Rossum3293b071998-08-25 16:07:15 +0000426LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000427PyLong_AsLongLong(vv)
428 PyObject *vv;
429{
430#if SIZEOF_LONG_LONG == SIZEOF_LONG
431 /* In case the compiler is faking it. */
Guido van Rossum3293b071998-08-25 16:07:15 +0000432 return (LONG_LONG)PyLong_AsLong( vv );
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000433#else
434 register PyLongObject *v;
Guido van Rossum3293b071998-08-25 16:07:15 +0000435 LONG_LONG x, prev;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000436 int i, sign;
437
438 if (vv == NULL || !PyLong_Check(vv)) {
439 PyErr_BadInternalCall();
440 return -1;
441 }
442
443 v = (PyLongObject *)vv;
444 i = v->ob_size;
445 sign = 1;
446 x = 0;
447
448 if (i < 0) {
449 sign = -1;
450 i = -(i);
451 }
452
453 while (--i >= 0) {
454 prev = x;
455 x = (x << SHIFT) + v->ob_digit[i];
456 if ((x >> SHIFT) != prev) {
457 PyErr_SetString(PyExc_OverflowError,
458 "long int too long to convert");
459 return -1;
460 }
461 }
462
463 return x * sign;
464#endif
465}
466
Guido van Rossum3293b071998-08-25 16:07:15 +0000467unsigned LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000468PyLong_AsUnsignedLongLong(vv)
469 PyObject *vv;
470{
471#if SIZEOF_LONG_LONG == 4
472 /* In case the compiler is faking it. */
Guido van Rossum3293b071998-08-25 16:07:15 +0000473 return (unsigned LONG_LONG)PyLong_AsUnsignedLong( vv );
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000474#else
475 register PyLongObject *v;
Guido van Rossum3293b071998-08-25 16:07:15 +0000476 unsigned LONG_LONG x, prev;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000477 int i;
478
479 if (vv == NULL || !PyLong_Check(vv)) {
480 PyErr_BadInternalCall();
Guido van Rossum3293b071998-08-25 16:07:15 +0000481 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000482 }
483
484 v = (PyLongObject *)vv;
485 i = v->ob_size;
486 x = 0;
487
488 if (i < 0) {
489 PyErr_SetString(PyExc_OverflowError,
490 "can't convert negative value to unsigned long");
Guido van Rossum3293b071998-08-25 16:07:15 +0000491 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000492 }
493
494 while (--i >= 0) {
495 prev = x;
496 x = (x << SHIFT) + v->ob_digit[i];
497 if ((x >> SHIFT) != prev) {
498 PyErr_SetString(PyExc_OverflowError,
499 "long int too long to convert");
Guido van Rossum3293b071998-08-25 16:07:15 +0000500 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000501 }
502 }
503
504 return x;
505#endif
506}
507#endif /* HAVE_LONG_LONG */
508
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000509/* Multiply by a single digit, ignoring the sign. */
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000512mul1(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000514 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000515{
516 return muladd1(a, n, (digit)0);
517}
518
519/* Multiply by a single digit and add a single digit, ignoring the sign. */
520
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000522muladd1(a, n, extra)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000524 wdigit n;
525 wdigit extra;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000526{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000527 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000529 twodigits carry = extra;
530 int i;
531
532 if (z == NULL)
533 return NULL;
534 for (i = 0; i < size_a; ++i) {
535 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000536 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000537 carry >>= SHIFT;
538 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000539 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000540 return long_normalize(z);
541}
542
543/* Divide a long integer by a digit, returning both the quotient
544 (as function result) and the remainder (through *prem).
545 The sign of a is ignored; n should not be zero. */
546
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000548divrem1(a, n, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000550 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000551 digit *prem;
552{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000553 int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000555 int i;
556 twodigits rem = 0;
557
558 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000560 if (z == NULL)
561 return NULL;
562 for (i = size; --i >= 0; ) {
563 rem = (rem << SHIFT) + a->ob_digit[i];
Guido van Rossum2095d241997-04-09 19:41:24 +0000564 z->ob_digit[i] = (digit) (rem/n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000565 rem %= n;
566 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000567 *prem = (digit) rem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000568 return long_normalize(z);
569}
570
571/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000572 Return a string object.
Fred Drake121ee271999-12-23 15:41:28 +0000573 If base is 8 or 16, add the proper prefix '0' or '0x'. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000574
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575static PyObject *
Fred Drake121ee271999-12-23 15:41:28 +0000576long_format(aa, base, addL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 PyObject *aa;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000578 int base;
Fred Drake121ee271999-12-23 15:41:28 +0000579 int addL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000580{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 register PyLongObject *a = (PyLongObject *)aa;
582 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000583 int i;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000584 int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000585 char *p;
586 int bits;
587 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +0000588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 if (a == NULL || !PyLong_Check(a)) {
590 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +0000591 return NULL;
592 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000593 assert(base >= 2 && base <= 36);
594
595 /* Compute a rough upper bound for the length of the string */
596 i = base;
597 bits = 0;
598 while (i > 1) {
599 ++bits;
600 i >>= 1;
601 }
Fred Drake121ee271999-12-23 15:41:28 +0000602 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000604 if (str == NULL)
605 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000607 *p = '\0';
Fred Drake121ee271999-12-23 15:41:28 +0000608 if (addL)
609 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000610 if (a->ob_size < 0)
611 sign = '-';
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000612
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000613 if (a->ob_size == 0) {
614 *--p = '0';
615 }
616 else if ((base & (base - 1)) == 0) {
617 /* JRH: special case for power-of-2 bases */
618 twodigits temp = a->ob_digit[0];
619 int bitsleft = SHIFT;
620 int rem;
621 int last = abs(a->ob_size);
622 int basebits = 1;
623 i = base;
624 while ((i >>= 1) > 1) ++basebits;
625
626 i = 0;
627 for (;;) {
628 while (bitsleft >= basebits) {
629 if ((temp == 0) && (i >= last - 1)) break;
630 rem = temp & (base - 1);
631 if (rem < 10)
632 rem += '0';
633 else
634 rem += 'A' - 10;
635 assert(p > PyString_AS_STRING(str));
636 *--p = (char) rem;
637 bitsleft -= basebits;
638 temp >>= basebits;
639 }
640 if (++i >= last) {
641 if (temp == 0) break;
642 bitsleft = 99;
643 /* loop again to pick up final digits */
644 }
645 else {
646 temp = (a->ob_digit[i] << bitsleft) | temp;
647 bitsleft += SHIFT;
648 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000649 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000650 }
651 else {
652 Py_INCREF(a);
653 do {
654 digit rem;
655 PyLongObject *temp = divrem1(a, (digit)base, &rem);
656 if (temp == NULL) {
657 Py_DECREF(a);
658 Py_DECREF(str);
659 return NULL;
660 }
661 if (rem < 10)
662 rem += '0';
663 else
664 rem += 'A'-10;
665 assert(p > PyString_AS_STRING(str));
666 *--p = (char) rem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 Py_DECREF(a);
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000668 a = temp;
669 SIGCHECK({
670 Py_DECREF(a);
671 Py_DECREF(str);
672 return NULL;
673 })
674 } while (ABS(a->ob_size) != 0);
675 Py_DECREF(a);
676 }
677
Guido van Rossum2c475421992-08-14 15:13:07 +0000678 if (base == 8) {
679 if (size_a != 0)
680 *--p = '0';
681 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000682 else if (base == 16) {
683 *--p = 'x';
684 *--p = '0';
685 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000686 else if (base != 10) {
687 *--p = '#';
688 *--p = '0' + base%10;
689 if (base > 10)
690 *--p = '0' + base/10;
691 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000692 if (sign)
693 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 if (p != PyString_AS_STRING(str)) {
695 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000696 assert(p > q);
697 do {
698 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +0000699 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 _PyString_Resize((PyObject **)&str,
701 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000702 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000704}
705
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000706#if 0
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000707/* Convert a string to a long int object, in a given base.
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000708 Base zero implies a default depending on the number.
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000709 External linkage: used in compile.c and stropmodule.c. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000710
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000712long_scan(str, base)
713 char *str;
714 int base;
715{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 return PyLong_FromString(str, (char **)NULL, base);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000717}
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000718#endif
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000719
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720PyObject *
721PyLong_FromString(str, pend, base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000722 char *str;
723 char **pend;
724 int base;
725{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000726 int sign = 1;
Guido van Rossume6762971998-06-22 03:54:46 +0000727 char *start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000729
Guido van Rossum472c04f1996-12-05 21:57:21 +0000730 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 PyErr_SetString(PyExc_ValueError,
732 "invalid base for long literal");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000733 return NULL;
734 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000735 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000736 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000737 if (*str == '+')
738 ++str;
739 else if (*str == '-') {
740 ++str;
741 sign = -1;
742 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000743 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000744 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000745 if (base == 0) {
746 if (str[0] != '0')
747 base = 10;
748 else if (str[1] == 'x' || str[1] == 'X')
749 base = 16;
750 else
751 base = 8;
752 }
753 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
754 str += 2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 z = _PyLong_New(0);
Guido van Rossume6762971998-06-22 03:54:46 +0000756 start = str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000757 for ( ; z != NULL; ++str) {
758 int k = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759 PyLongObject *temp;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000760
761 if (*str <= '9')
762 k = *str - '0';
763 else if (*str >= 'a')
764 k = *str - 'a' + 10;
765 else if (*str >= 'A')
766 k = *str - 'A' + 10;
767 if (k < 0 || k >= base)
768 break;
769 temp = muladd1(z, (digit)base, (digit)k);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770 Py_DECREF(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000771 z = temp;
772 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +0000773 if (z == NULL)
774 return NULL;
Guido van Rossume6762971998-06-22 03:54:46 +0000775 if (str == start) {
776 PyErr_SetString(PyExc_ValueError,
777 "no digits in long int constant");
Barry Warsawb5cebfe1999-01-27 17:48:27 +0000778 Py_DECREF(z);
Guido van Rossume6762971998-06-22 03:54:46 +0000779 return NULL;
780 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000781 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000782 z->ob_size = -(z->ob_size);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000783 if (pend)
784 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785 return (PyObject *) z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000786}
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788static PyLongObject *x_divrem
789 Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject **));
790static PyObject *long_pos Py_PROTO((PyLongObject *));
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000791static int long_divrem Py_PROTO((PyLongObject *, PyLongObject *,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792 PyLongObject **, PyLongObject **));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000793
794/* Long division with remainder, top-level routine */
795
Guido van Rossume32e0141992-01-19 16:31:05 +0000796static int
797long_divrem(a, b, pdiv, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 PyLongObject *a, *b;
799 PyLongObject **pdiv;
800 PyLongObject **prem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000801{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000802 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000804
805 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000806 PyErr_SetString(PyExc_ZeroDivisionError,
807 "long division or modulo");
Guido van Rossume32e0141992-01-19 16:31:05 +0000808 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000809 }
810 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +0000811 (size_a == size_b &&
812 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000813 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 *pdiv = _PyLong_New(0);
815 Py_INCREF(a);
816 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +0000817 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000818 }
819 if (size_b == 1) {
820 digit rem = 0;
821 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000822 if (z == NULL)
823 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000825 }
Guido van Rossume32e0141992-01-19 16:31:05 +0000826 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000827 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000828 if (z == NULL)
829 return -1;
830 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000831 /* Set the signs.
832 The quotient z has the sign of a*b;
833 the remainder r has the sign of a,
834 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +0000835 if ((a->ob_size < 0) != (b->ob_size < 0))
836 z->ob_size = -(z->ob_size);
837 if (a->ob_size < 0 && (*prem)->ob_size != 0)
838 (*prem)->ob_size = -((*prem)->ob_size);
839 *pdiv = z;
840 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000841}
842
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000843/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000846x_divrem(v1, w1, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 PyLongObject *v1, *w1;
848 PyLongObject **prem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000849{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000850 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +0000851 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852 PyLongObject *v = mul1(v1, d);
853 PyLongObject *w = mul1(w1, d);
854 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000855 int j, k;
856
857 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 Py_XDECREF(v);
859 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000860 return NULL;
861 }
862
863 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000864 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000865 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000866
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000867 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 a = _PyLong_New(size_v - size_w + 1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000869
870 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
871 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
872 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000873 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000874 int i;
875
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000876 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000878 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000879 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000880 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000881 if (vj == w->ob_digit[size_w-1])
882 q = MASK;
883 else
884 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
885 w->ob_digit[size_w-1];
886
887 while (w->ob_digit[size_w-2]*q >
888 ((
889 ((twodigits)vj << SHIFT)
890 + v->ob_digit[j-1]
891 - q*w->ob_digit[size_w-1]
892 ) << SHIFT)
893 + v->ob_digit[j-2])
894 --q;
895
896 for (i = 0; i < size_w && i+k < size_v; ++i) {
897 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +0000898 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 carry += v->ob_digit[i+k] - z
900 + ((twodigits)zz << SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000901 v->ob_digit[i+k] = carry & MASK;
902 carry = (carry >> SHIFT) - zz;
903 }
904
905 if (i+k < size_v) {
906 carry += v->ob_digit[i+k];
907 v->ob_digit[i+k] = 0;
908 }
909
910 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +0000911 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000912 else {
913 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +0000914 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000915 carry = 0;
916 for (i = 0; i < size_w && i+k < size_v; ++i) {
917 carry += v->ob_digit[i+k] + w->ob_digit[i];
918 v->ob_digit[i+k] = carry & MASK;
919 carry >>= SHIFT;
920 }
921 }
922 } /* for j, k */
923
Guido van Rossumc206c761995-01-10 15:23:19 +0000924 if (a == NULL)
925 *prem = NULL;
926 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +0000927 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +0000928 *prem = divrem1(v, d, &d);
929 /* d receives the (unused) remainder */
930 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +0000932 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +0000933 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000934 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 Py_DECREF(v);
936 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000937 return a;
938}
939
940/* Methods */
941
Guido van Rossume32e0141992-01-19 16:31:05 +0000942/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943static void long_dealloc Py_PROTO((PyObject *));
944static PyObject *long_repr Py_PROTO((PyObject *));
945static int long_compare Py_PROTO((PyLongObject *, PyLongObject *));
Guido van Rossum065ce5a1998-09-13 15:37:20 +0000946static long long_hash Py_PROTO((PyLongObject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000947
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948static PyObject *long_add Py_PROTO((PyLongObject *, PyLongObject *));
949static PyObject *long_sub Py_PROTO((PyLongObject *, PyLongObject *));
950static PyObject *long_mul Py_PROTO((PyLongObject *, PyLongObject *));
951static PyObject *long_div Py_PROTO((PyLongObject *, PyLongObject *));
952static PyObject *long_mod Py_PROTO((PyLongObject *, PyLongObject *));
953static PyObject *long_divmod Py_PROTO((PyLongObject *, PyLongObject *));
954static PyObject *long_pow
955 Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject *));
956static PyObject *long_neg Py_PROTO((PyLongObject *));
957static PyObject *long_pos Py_PROTO((PyLongObject *));
958static PyObject *long_abs Py_PROTO((PyLongObject *));
959static int long_nonzero Py_PROTO((PyLongObject *));
960static PyObject *long_invert Py_PROTO((PyLongObject *));
961static PyObject *long_lshift Py_PROTO((PyLongObject *, PyLongObject *));
962static PyObject *long_rshift Py_PROTO((PyLongObject *, PyLongObject *));
963static PyObject *long_and Py_PROTO((PyLongObject *, PyLongObject *));
964static PyObject *long_xor Py_PROTO((PyLongObject *, PyLongObject *));
965static PyObject *long_or Py_PROTO((PyLongObject *, PyLongObject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000966
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000967static void
968long_dealloc(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 PyObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000970{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 PyMem_DEL(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000972}
973
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000975long_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 PyObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000977{
Fred Drake121ee271999-12-23 15:41:28 +0000978 return long_format(v, 10, 1);
979}
980
981static PyObject *
982long_str(v)
983 PyObject *v;
984{
985 return long_format(v, 10, 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000986}
987
988static int
989long_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000991{
992 int sign;
993
Guido van Rossumc6913e71991-11-19 20:26:46 +0000994 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000995 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +0000996 sign = 0;
997 else
998 sign = a->ob_size - b->ob_size;
999 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001000 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001001 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001002 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1003 ;
1004 if (i < 0)
1005 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001006 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001007 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001008 if (a->ob_size < 0)
1009 sign = -sign;
1010 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001011 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001012 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001013}
1014
Guido van Rossum9bfef441993-03-29 10:43:31 +00001015static long
1016long_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001017 PyLongObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001018{
1019 long x;
1020 int i, sign;
1021
1022 /* This is designed so that Python ints and longs with the
1023 same value hash to the same value, otherwise comparisons
1024 of mapping keys will turn out weird */
1025 i = v->ob_size;
1026 sign = 1;
1027 x = 0;
1028 if (i < 0) {
1029 sign = -1;
1030 i = -(i);
1031 }
1032 while (--i >= 0) {
1033 /* Force a 32-bit circular shift */
1034 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
1035 x += v->ob_digit[i];
1036 }
1037 x = x * sign;
1038 if (x == -1)
1039 x = -2;
1040 return x;
1041}
1042
1043
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001044/* Add the absolute values of two long integers. */
1045
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046static PyLongObject *x_add Py_PROTO((PyLongObject *, PyLongObject *));
1047static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001048x_add(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001050{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001051 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001052 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001053 int i;
1054 digit carry = 0;
1055
1056 /* Ensure a is the larger of the two: */
1057 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058 { PyLongObject *temp = a; a = b; b = temp; }
1059 { int size_temp = size_a;
1060 size_a = size_b;
1061 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001062 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001063 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001064 if (z == NULL)
1065 return NULL;
1066 for (i = 0; i < size_b; ++i) {
1067 carry += a->ob_digit[i] + b->ob_digit[i];
1068 z->ob_digit[i] = carry & MASK;
1069 /* The following assumes unsigned shifts don't
1070 propagate the sign bit. */
1071 carry >>= SHIFT;
1072 }
1073 for (; i < size_a; ++i) {
1074 carry += a->ob_digit[i];
1075 z->ob_digit[i] = carry & MASK;
1076 carry >>= SHIFT;
1077 }
1078 z->ob_digit[i] = carry;
1079 return long_normalize(z);
1080}
1081
1082/* Subtract the absolute values of two integers. */
1083
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084static PyLongObject *x_sub Py_PROTO((PyLongObject *, PyLongObject *));
1085static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001086x_sub(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001088{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001089 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001091 int i;
1092 int sign = 1;
1093 digit borrow = 0;
1094
1095 /* Ensure a is the larger of the two: */
1096 if (size_a < size_b) {
1097 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001098 { PyLongObject *temp = a; a = b; b = temp; }
1099 { int size_temp = size_a;
1100 size_a = size_b;
1101 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001102 }
1103 else if (size_a == size_b) {
1104 /* Find highest digit where a and b differ: */
1105 i = size_a;
1106 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1107 ;
1108 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001109 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001110 if (a->ob_digit[i] < b->ob_digit[i]) {
1111 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001113 }
1114 size_a = size_b = i+1;
1115 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001117 if (z == NULL)
1118 return NULL;
1119 for (i = 0; i < size_b; ++i) {
1120 /* The following assumes unsigned arithmetic
1121 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001122 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001123 z->ob_digit[i] = borrow & MASK;
1124 borrow >>= SHIFT;
1125 borrow &= 1; /* Keep only one sign bit */
1126 }
1127 for (; i < size_a; ++i) {
1128 borrow = a->ob_digit[i] - borrow;
1129 z->ob_digit[i] = borrow & MASK;
1130 borrow >>= SHIFT;
1131 }
1132 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001133 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001134 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001135 return long_normalize(z);
1136}
1137
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001139long_add(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001140 PyLongObject *a;
1141 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001142{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001143 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001144
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001145 if (a->ob_size < 0) {
1146 if (b->ob_size < 0) {
1147 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001148 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001149 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001150 }
1151 else
1152 z = x_sub(b, a);
1153 }
1154 else {
1155 if (b->ob_size < 0)
1156 z = x_sub(a, b);
1157 else
1158 z = x_add(a, b);
1159 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001160 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001161}
1162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001164long_sub(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165 PyLongObject *a;
1166 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001167{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001169
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001170 if (a->ob_size < 0) {
1171 if (b->ob_size < 0)
1172 z = x_sub(a, b);
1173 else
1174 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001175 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001176 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001177 }
1178 else {
1179 if (b->ob_size < 0)
1180 z = x_add(a, b);
1181 else
1182 z = x_sub(a, b);
1183 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001185}
1186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001188long_mul(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189 PyLongObject *a;
1190 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001191{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001192 int size_a;
1193 int size_b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001195 int i;
1196
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001197 size_a = ABS(a->ob_size);
1198 size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001199 z = _PyLong_New(size_a + size_b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001200 if (z == NULL)
1201 return NULL;
1202 for (i = 0; i < z->ob_size; ++i)
1203 z->ob_digit[i] = 0;
1204 for (i = 0; i < size_a; ++i) {
1205 twodigits carry = 0;
1206 twodigits f = a->ob_digit[i];
1207 int j;
1208
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001209 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001210 Py_DECREF(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001211 return NULL;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001212 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001213 for (j = 0; j < size_b; ++j) {
1214 carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
Guido van Rossum2095d241997-04-09 19:41:24 +00001215 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001216 carry >>= SHIFT;
1217 }
1218 for (; carry != 0; ++j) {
1219 assert(i+j < z->ob_size);
1220 carry += z->ob_digit[i+j];
Guido van Rossum2095d241997-04-09 19:41:24 +00001221 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001222 carry >>= SHIFT;
1223 }
1224 }
1225 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001226 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001227 if (b->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001228 z->ob_size = -(z->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229 return (PyObject *) long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001230}
1231
Guido van Rossume32e0141992-01-19 16:31:05 +00001232/* The / and % operators are now defined in terms of divmod().
1233 The expression a mod b has the value a - b*floor(a/b).
1234 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001235 |a| by |b|, with the sign of a. This is also expressed
1236 as a - b*trunc(a/b), if trunc truncates towards zero.
1237 Some examples:
1238 a b a rem b a mod b
1239 13 10 3 3
1240 -13 10 -3 7
1241 13 -10 3 -7
1242 -13 -10 -3 -3
1243 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001244 have different signs. We then subtract one from the 'div'
1245 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247static int l_divmod Py_PROTO((PyLongObject *, PyLongObject *,
1248 PyLongObject **, PyLongObject **));
Guido van Rossume32e0141992-01-19 16:31:05 +00001249static int
1250l_divmod(v, w, pdiv, pmod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 PyLongObject *v;
1252 PyLongObject *w;
1253 PyLongObject **pdiv;
1254 PyLongObject **pmod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001255{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001257
1258 if (long_divrem(v, w, &div, &mod) < 0)
1259 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00001260 if ((mod->ob_size < 0 && w->ob_size > 0) ||
1261 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001262 PyLongObject *temp;
1263 PyLongObject *one;
1264 temp = (PyLongObject *) long_add(mod, w);
1265 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00001266 mod = temp;
1267 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001269 return -1;
1270 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00001272 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001273 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
1274 Py_DECREF(mod);
1275 Py_DECREF(div);
1276 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00001277 return -1;
1278 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001279 Py_DECREF(one);
1280 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001281 div = temp;
1282 }
1283 *pdiv = div;
1284 *pmod = mod;
1285 return 0;
1286}
1287
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001289long_div(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 PyLongObject *v;
1291 PyLongObject *w;
Guido van Rossume32e0141992-01-19 16:31:05 +00001292{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001293 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001294 if (l_divmod(v, w, &div, &mod) < 0)
1295 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 Py_DECREF(mod);
1297 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00001298}
1299
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001301long_mod(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001302 PyLongObject *v;
1303 PyLongObject *w;
Guido van Rossume32e0141992-01-19 16:31:05 +00001304{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001306 if (l_divmod(v, w, &div, &mod) < 0)
1307 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308 Py_DECREF(div);
1309 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001310}
1311
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001313long_divmod(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001314 PyLongObject *v;
1315 PyLongObject *w;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001316{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317 PyObject *z;
1318 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001319 if (l_divmod(v, w, &div, &mod) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001320 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001322 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323 PyTuple_SetItem(z, 0, (PyObject *) div);
1324 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001325 }
1326 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 Py_DECREF(div);
1328 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001329 }
1330 return z;
1331}
1332
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333static PyObject *
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001334long_pow(a, b, c)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335 PyLongObject *a;
1336 PyLongObject *b;
1337 PyLongObject *c;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001338{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 PyLongObject *z, *div, *mod;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001340 int size_b, i;
1341
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001342 size_b = b->ob_size;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001343 if (size_b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 PyErr_SetString(PyExc_ValueError,
1345 "long integer to the negative power");
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001346 return NULL;
1347 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 z = (PyLongObject *)PyLong_FromLong(1L);
1349 Py_INCREF(a);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001350 for (i = 0; i < size_b; ++i) {
1351 digit bi = b->ob_digit[i];
1352 int j;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001353
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001354 for (j = 0; j < SHIFT; ++j) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyLongObject *temp;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001356
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001357 if (bi & 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 temp = (PyLongObject *)long_mul(z, a);
1359 Py_DECREF(z);
1360 if ((PyObject*)c!=Py_None && temp!=NULL) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00001361 if (l_divmod(temp,c,&div,&mod) < 0) {
1362 Py_DECREF(temp);
1363 z = NULL;
1364 goto error;
1365 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366 Py_XDECREF(div);
1367 Py_DECREF(temp);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001368 temp = mod;
1369 }
1370 z = temp;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001371 if (z == NULL)
1372 break;
1373 }
1374 bi >>= 1;
1375 if (bi == 0 && i+1 == size_b)
1376 break;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 temp = (PyLongObject *)long_mul(a, a);
1378 Py_DECREF(a);
1379 if ((PyObject*)c!=Py_None && temp!=NULL) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00001380 if (l_divmod(temp, c, &div, &mod) < 0) {
1381 Py_DECREF(temp);
1382 z = NULL;
1383 goto error;
1384 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 Py_XDECREF(div);
1386 Py_DECREF(temp);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001387 temp = mod;
1388 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001389 a = temp;
1390 if (a == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001391 Py_DECREF(z);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001392 z = NULL;
1393 break;
1394 }
1395 }
Guido van Rossumc206c761995-01-10 15:23:19 +00001396 if (a == NULL || z == NULL)
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001397 break;
1398 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399 Py_XDECREF(a);
1400 if ((PyObject*)c!=Py_None && z!=NULL) {
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00001401 if (l_divmod(z, c, &div, &mod) < 0) {
1402 Py_DECREF(z);
1403 z = NULL;
1404 }
1405 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001406 Py_XDECREF(div);
1407 Py_DECREF(z);
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00001408 z = mod;
1409 }
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001410 }
Guido van Rossum2c7b8fe1999-10-11 22:34:41 +00001411 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001413}
1414
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001416long_invert(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001417 PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001418{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001419 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001420 PyLongObject *x;
1421 PyLongObject *w;
1422 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001423 if (w == NULL)
1424 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001425 x = (PyLongObject *) long_add(v, w);
1426 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001427 if (x == NULL)
1428 return NULL;
1429 if (x->ob_size != 0)
1430 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001431 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001432}
1433
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001435long_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436 PyLongObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001437{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 Py_INCREF(v);
1439 return (PyObject *)v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001440}
1441
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001443long_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 PyLongObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001445{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001446 PyLongObject *z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001447 int i, n;
1448 n = ABS(v->ob_size);
1449 if (n == 0) {
1450 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001451 Py_INCREF(v);
1452 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001453 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 z = _PyLong_New(ABS(n));
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001455 if (z == NULL)
1456 return NULL;
1457 for (i = 0; i < n; i++)
1458 z->ob_digit[i] = v->ob_digit[i];
1459 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001461}
1462
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001464long_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001465 PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001466{
1467 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001468 return long_neg(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001469 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001470 Py_INCREF(v);
1471 return (PyObject *)v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001472 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001473}
1474
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001475static int
1476long_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477 PyLongObject *v;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001478{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001479 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001480}
1481
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001483long_rshift(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001484 PyLongObject *a;
1485 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001486{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001487 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001488 long shiftby;
1489 int newsize, wordshift, loshift, hishift, i, j;
1490 digit lomask, himask;
1491
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001492 if (a->ob_size < 0) {
1493 /* Right shifting negative numbers is harder */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001494 PyLongObject *a1, *a2, *a3;
1495 a1 = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001496 if (a1 == NULL) return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497 a2 = (PyLongObject *) long_rshift(a1, b);
1498 Py_DECREF(a1);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001499 if (a2 == NULL) return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500 a3 = (PyLongObject *) long_invert(a2);
1501 Py_DECREF(a2);
1502 return (PyObject *) a3;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001503 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001504
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001505 shiftby = PyLong_AsLong((PyObject *)b);
1506 if (shiftby == -1L && PyErr_Occurred())
Guido van Rossumc6913e71991-11-19 20:26:46 +00001507 return NULL;
1508 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001509 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001510 return NULL;
1511 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001512 wordshift = shiftby / SHIFT;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001513 newsize = ABS(a->ob_size) - wordshift;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001514 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001515 z = _PyLong_New(0);
1516 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001517 }
1518 loshift = shiftby % SHIFT;
1519 hishift = SHIFT - loshift;
1520 lomask = ((digit)1 << hishift) - 1;
1521 himask = MASK ^ lomask;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001522 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001523 if (z == NULL)
1524 return NULL;
1525 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001526 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001527 for (i = 0, j = wordshift; i < newsize; i++, j++) {
1528 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
1529 if (i+1 < newsize)
1530 z->ob_digit[i] |=
1531 (a->ob_digit[j+1] << hishift) & himask;
1532 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001533 return (PyObject *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001534}
1535
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001536static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001537long_lshift(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001538 PyLongObject *a;
1539 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001540{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001541 /* This version due to Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001542 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001543 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001544 int oldsize, newsize, wordshift, remshift, i, j;
1545 twodigits accum;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001546
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001547 shiftby = PyLong_AsLong((PyObject *)b);
1548 if (shiftby == -1L && PyErr_Occurred())
Guido van Rossumc6913e71991-11-19 20:26:46 +00001549 return NULL;
1550 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001551 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001552 return NULL;
1553 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001554 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001555 PyErr_SetString(PyExc_ValueError,
1556 "outrageous left shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001557 return NULL;
1558 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001559 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1560 wordshift = (int)shiftby / SHIFT;
1561 remshift = (int)shiftby - wordshift * SHIFT;
1562
1563 oldsize = ABS(a->ob_size);
1564 newsize = oldsize + wordshift;
1565 if (remshift)
1566 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001567 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001568 if (z == NULL)
1569 return NULL;
1570 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001571 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001572 for (i = 0; i < wordshift; i++)
1573 z->ob_digit[i] = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001574 accum = 0;
1575 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
1576 accum |= a->ob_digit[j] << remshift;
1577 z->ob_digit[i] = (digit)(accum & MASK);
1578 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001579 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001580 if (remshift)
1581 z->ob_digit[newsize-1] = (digit)accum;
1582 else
1583 assert(!accum);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001584 return (PyObject *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001585}
1586
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001587
1588/* Bitwise and/xor/or operations */
1589
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001590#define MAX(x, y) ((x) < (y) ? (y) : (x))
1591#define MIN(x, y) ((x) > (y) ? (y) : (x))
1592
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001593static PyObject *long_bitwise Py_PROTO((PyLongObject *, int, PyLongObject *));
1594static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001595long_bitwise(a, op, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001596 PyLongObject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001597 int op; /* '&', '|', '^' */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001598 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001599{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001600 digit maska, maskb; /* 0 or MASK */
1601 int negz;
1602 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001604 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00001605 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001606 PyObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001607
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001608 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001610 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001611 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001612 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001613 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001614 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001615 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001616 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001618 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001619 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001620 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001621 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001622 maskb = 0;
1623 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001624
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001625 negz = 0;
1626 switch (op) {
1627 case '^':
1628 if (maska != maskb) {
1629 maska ^= MASK;
1630 negz = -1;
1631 }
1632 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001633 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001634 if (maska && maskb) {
1635 op = '|';
1636 maska ^= MASK;
1637 maskb ^= MASK;
1638 negz = -1;
1639 }
1640 break;
1641 case '|':
1642 if (maska || maskb) {
1643 op = '&';
1644 maska ^= MASK;
1645 maskb ^= MASK;
1646 negz = -1;
1647 }
1648 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001649 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001650
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001651 /* JRH: The original logic here was to allocate the result value (z)
1652 as the longer of the two operands. However, there are some cases
1653 where the result is guaranteed to be shorter than that: AND of two
1654 positives, OR of two negatives: use the shorter number. AND with
1655 mixed signs: use the positive number. OR with mixed signs: use the
1656 negative number. After the transformations above, op will be '&'
1657 iff one of these cases applies, and mask will be non-0 for operands
1658 whose length should be ignored.
1659 */
1660
1661 size_a = a->ob_size;
1662 size_b = b->ob_size;
1663 size_z = op == '&'
1664 ? (maska
1665 ? size_b
1666 : (maskb ? size_a : MIN(size_a, size_b)))
1667 : MAX(size_a, size_b);
1668 z = _PyLong_New(size_z);
1669 if (a == NULL || b == NULL || z == NULL) {
1670 Py_XDECREF(a);
1671 Py_XDECREF(b);
1672 Py_XDECREF(z);
1673 return NULL;
1674 }
1675
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001676 for (i = 0; i < size_z; ++i) {
1677 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
1678 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
1679 switch (op) {
1680 case '&': z->ob_digit[i] = diga & digb; break;
1681 case '|': z->ob_digit[i] = diga | digb; break;
1682 case '^': z->ob_digit[i] = diga ^ digb; break;
1683 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001684 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001685
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001686 Py_DECREF(a);
1687 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001688 z = long_normalize(z);
1689 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001690 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001691 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001692 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001693 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001694}
1695
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001696static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001697long_and(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001698 PyLongObject *a;
1699 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001700{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001701 return long_bitwise(a, '&', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001702}
1703
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001704static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001705long_xor(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001706 PyLongObject *a;
1707 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001708{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001709 return long_bitwise(a, '^', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001710}
1711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001713long_or(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001714 PyLongObject *a;
1715 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001716{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001717 return long_bitwise(a, '|', b);
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001718}
1719
Guido van Rossum234f9421993-06-17 12:35:49 +00001720static int
Guido van Rossume6eefc21992-08-14 12:06:52 +00001721long_coerce(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001722 PyObject **pv;
1723 PyObject **pw;
Guido van Rossume6eefc21992-08-14 12:06:52 +00001724{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001725 if (PyInt_Check(*pw)) {
1726 *pw = PyLong_FromLong(PyInt_AsLong(*pw));
1727 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00001728 return 0;
1729 }
1730 return 1; /* Can't do it */
1731}
1732
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001733static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001734long_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001735 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001736{
1737 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001738 x = PyLong_AsLong(v);
1739 if (PyErr_Occurred())
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001740 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001741 return PyInt_FromLong(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001742}
1743
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001744static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001745long_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001746 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001747{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001748 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001749 return v;
1750}
1751
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001752static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001753long_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001754 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001755{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00001756 double result;
1757 PyFPE_START_PROTECT("long_float", return 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001758 result = PyLong_AsDouble(v);
Guido van Rossum45b83911997-03-14 04:32:50 +00001759 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001760 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001761}
1762
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001763static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001764long_oct(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001765 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001766{
Fred Drake121ee271999-12-23 15:41:28 +00001767 return long_format(v, 8, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001768}
1769
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001770static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001771long_hex(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001772 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001773{
Fred Drake121ee271999-12-23 15:41:28 +00001774 return long_format(v, 16, 1);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001775}
1776
1777
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001778#define UF (unaryfunc)
1779#define BF (binaryfunc)
1780#define TF (ternaryfunc)
1781#define IF (inquiry)
Guido van Rossum8b27d921992-03-27 17:27:05 +00001782
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001783static PyNumberMethods long_as_number = {
Guido van Rossum8b27d921992-03-27 17:27:05 +00001784 BF long_add, /*nb_add*/
1785 BF long_sub, /*nb_subtract*/
1786 BF long_mul, /*nb_multiply*/
1787 BF long_div, /*nb_divide*/
1788 BF long_mod, /*nb_remainder*/
1789 BF long_divmod, /*nb_divmod*/
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001790 TF long_pow, /*nb_power*/
Guido van Rossum8b27d921992-03-27 17:27:05 +00001791 UF long_neg, /*nb_negative*/
1792 UF long_pos, /*tp_positive*/
1793 UF long_abs, /*tp_absolute*/
1794 IF long_nonzero,/*tp_nonzero*/
1795 UF long_invert, /*nb_invert*/
1796 BF long_lshift, /*nb_lshift*/
1797 BF long_rshift, /*nb_rshift*/
1798 BF long_and, /*nb_and*/
1799 BF long_xor, /*nb_xor*/
1800 BF long_or, /*nb_or*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801 (int (*) Py_FPROTO((PyObject **, PyObject **)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001802 (coercion)long_coerce, /*nb_coerce*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001803 UF long_int, /*nb_int*/
1804 UF long_long, /*nb_long*/
1805 UF long_float, /*nb_float*/
1806 UF long_oct, /*nb_oct*/
1807 UF long_hex, /*nb_hex*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001808};
1809
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001810PyTypeObject PyLong_Type = {
1811 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001812 0,
1813 "long int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001814 sizeof(PyLongObject) - sizeof(digit),
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001815 sizeof(digit),
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001816 (destructor)long_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001817 0, /*tp_print*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001818 0, /*tp_getattr*/
1819 0, /*tp_setattr*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001820 (int (*) Py_FPROTO((PyObject *, PyObject *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001821 (cmpfunc)long_compare, /*tp_compare*/
1822 (reprfunc)long_repr, /*tp_repr*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001823 &long_as_number,/*tp_as_number*/
1824 0, /*tp_as_sequence*/
1825 0, /*tp_as_mapping*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001826 (long (*) Py_FPROTO((PyObject *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001827 (hashfunc)long_hash, /*tp_hash*/
Fred Drake121ee271999-12-23 15:41:28 +00001828 0, /*tp_call*/
1829 (reprfunc)long_str, /*tp_str*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001830};