blob: db6942be27ec819e68151ce731ed8db8341efcb2 [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 *));
50static PyObject *long_format Py_PROTO((PyObject *aa, int base));
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;
148 if (dval < 0.0) {
149 neg = 1;
150 dval = -dval;
151 }
152 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
153 if (expo <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 return PyLong_FromLong(0L);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000155 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 v = _PyLong_New(ndig);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000157 if (v == NULL)
158 return NULL;
159 frac = ldexp(frac, (expo-1) % SHIFT + 1);
160 for (i = ndig; --i >= 0; ) {
161 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000162 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000163 frac = frac - (double)bits;
164 frac = ldexp(frac, SHIFT);
165 }
166 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000167 v->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000169}
170
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000171/* Get a C long int from a long int object.
172 Returns -1 and sets an error condition if overflow occurs. */
173
174long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175PyLong_AsLong(vv)
176 PyObject *vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000177{
Guido van Rossumf7531811998-05-26 14:33:37 +0000178 /* This version by Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 register PyLongObject *v;
Guido van Rossumf7531811998-05-26 14:33:37 +0000180 unsigned long x, prev;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000181 int i, sign;
Guido van Rossumf7531811998-05-26 14:33:37 +0000182
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 if (vv == NULL || !PyLong_Check(vv)) {
184 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000185 return -1;
186 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000188 i = v->ob_size;
189 sign = 1;
190 x = 0;
191 if (i < 0) {
192 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000193 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000194 }
195 while (--i >= 0) {
196 prev = x;
197 x = (x << SHIFT) + v->ob_digit[i];
Guido van Rossumf7531811998-05-26 14:33:37 +0000198 if ((x >> SHIFT) != prev)
199 goto overflow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000200 }
Guido van Rossumf7531811998-05-26 14:33:37 +0000201 /* Haven't lost any bits, but if the sign bit is set we're in
202 * trouble *unless* this is the min negative number. So,
203 * trouble iff sign bit set && (positive || some bit set other
204 * than the sign bit).
205 */
206 if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
207 goto overflow;
208 return (long)x * sign;
209
210 overflow:
211 PyErr_SetString(PyExc_OverflowError,
212 "long int too long to convert");
213 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000214}
215
Guido van Rossum53756b11997-01-03 17:14:46 +0000216/* Get a C long int from a long int object.
217 Returns -1 and sets an error condition if overflow occurs. */
218
219unsigned long
220PyLong_AsUnsignedLong(vv)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221 PyObject *vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000222{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 register PyLongObject *v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000224 unsigned long x, prev;
225 int i;
226
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 if (vv == NULL || !PyLong_Check(vv)) {
228 PyErr_BadInternalCall();
Guido van Rossum53756b11997-01-03 17:14:46 +0000229 return (unsigned long) -1;
230 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000231 v = (PyLongObject *)vv;
Guido van Rossum53756b11997-01-03 17:14:46 +0000232 i = v->ob_size;
233 x = 0;
234 if (i < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000236 "can't convert negative value to unsigned long");
237 return (unsigned long) -1;
238 }
239 while (--i >= 0) {
240 prev = x;
241 x = (x << SHIFT) + v->ob_digit[i];
242 if ((x >> SHIFT) != prev) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum53756b11997-01-03 17:14:46 +0000244 "long int too long to convert");
245 return (unsigned long) -1;
246 }
247 }
248 return x;
249}
250
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000251/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000252
253double
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254PyLong_AsDouble(vv)
255 PyObject *vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000256{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000257 register PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000258 double x;
259 double multiplier = (double) (1L << SHIFT);
260 int i, sign;
261
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 if (vv == NULL || !PyLong_Check(vv)) {
263 PyErr_BadInternalCall();
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000264 return -1;
265 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 v = (PyLongObject *)vv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000267 i = v->ob_size;
268 sign = 1;
269 x = 0.0;
270 if (i < 0) {
271 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000272 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000273 }
274 while (--i >= 0) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000275 x = x*multiplier + (double)v->ob_digit[i];
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000276 }
277 return x * sign;
278}
279
Guido van Rossum78694d91998-09-18 14:14:13 +0000280/* Create a new long (or int) object from a C pointer */
281
282PyObject *
283PyLong_FromVoidPtr(p)
284 void *p;
285{
286#if SIZEOF_VOID_P == SIZEOF_LONG
287 return PyInt_FromLong((long)p);
288#else
289 /* optimize null pointers */
290 if ( p == NULL )
291 return PyInt_FromLong(0);
292
293 /* we can assume that HAVE_LONG_LONG is true. if not, then the
294 configuration process should have bailed (having big pointers
295 without long longs seems non-sensical) */
296 return PyLong_FromLongLong((LONG_LONG)p);
297#endif /* SIZEOF_VOID_P == SIZEOF_LONG */
298}
299
300/* Get a C pointer from a long object (or an int object in some cases) */
301
302void *
303PyLong_AsVoidPtr(vv)
304 PyObject *vv;
305{
306 /* This function will allow int or long objects. If vv is neither,
307 then the PyLong_AsLong*() functions will raise the exception:
308 PyExc_SystemError, "bad argument to internal function"
309 */
310
311#if SIZEOF_VOID_P == SIZEOF_LONG
312 long x;
313
314 if ( PyInt_Check(vv) )
315 x = PyInt_AS_LONG(vv);
316 else
317 x = PyLong_AsLong(vv);
318#else
319 /* we can assume that HAVE_LONG_LONG is true. if not, then the
320 configuration process should have bailed (having big pointers
321 without long longs seems non-sensical) */
322 LONG_LONG x;
323
324 if ( PyInt_Check(vv) )
325 x = PyInt_AS_LONG(vv);
326 else
327 x = PyLong_AsLongLong(vv);
328#endif /* SIZEOF_VOID_P == SIZEOF_LONG */
329
330 if (x == -1 && PyErr_Occurred())
331 return NULL;
332 return (void *)x;
333}
334
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000335#ifdef HAVE_LONG_LONG
336/*
Guido van Rossum3293b071998-08-25 16:07:15 +0000337 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000338 *
339 * For better or worse :-), I tried to follow the coding style already
340 * here.
341 */
342
343#ifdef HAVE_LIMITS_H
344#include <limits.h>
345#endif
346
347/* Hopefully this is portable... */
348#ifndef LONG_MAX
349#define LONG_MAX 2147483647L
350#endif
351#ifndef ULONG_MAX
352#define ULONG_MAX 4294967295U
353#endif
354#ifndef LONGLONG_MAX
355#define LONGLONG_MAX 9223372036854775807LL
356#endif
357#ifndef ULONGLONG_MAX
358#define ULONGLONG_MAX 0xffffffffffffffffULL
359#endif
360
Guido van Rossum3293b071998-08-25 16:07:15 +0000361/* Create a new long int object from a C LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000362
363PyObject *
364PyLong_FromLongLong(ival)
Guido van Rossum3293b071998-08-25 16:07:15 +0000365 LONG_LONG ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000366{
367#if SIZEOF_LONG_LONG == SIZEOF_LONG
368 /* In case the compiler is faking it. */
369 return PyLong_FromLong( (long)ival );
370#else
Guido van Rossum3293b071998-08-25 16:07:15 +0000371 if( ival <= (LONG_LONG)LONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000372 return PyLong_FromLong( (long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000373 }
Guido van Rossum3293b071998-08-25 16:07:15 +0000374 else if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000375 return PyLong_FromUnsignedLong( (unsigned long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000376 }
377 else {
Guido van Rossum3293b071998-08-25 16:07:15 +0000378 /* Assume a C LONG_LONG fits in at most 10 'digits'.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000379 * Should be OK if we're assuming long fits in 5.
380 */
381 PyLongObject *v = _PyLong_New(10);
382
383 if (v != NULL) {
Guido van Rossum3293b071998-08-25 16:07:15 +0000384 unsigned LONG_LONG t = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000385 int i;
386 if (ival < 0) {
387 t = -ival;
388 v->ob_size = -(v->ob_size);
389 }
390
391 for (i = 0; i < 10; i++) {
392 v->ob_digit[i] = (digit) (t & MASK);
393 t >>= SHIFT;
394 }
395
396 v = long_normalize(v);
397 }
398
399 return (PyObject *)v;
400 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000401#endif
402}
403
Guido van Rossum3293b071998-08-25 16:07:15 +0000404/* Create a new long int object from a C unsigned LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000405PyObject *
406PyLong_FromUnsignedLongLong(ival)
Guido van Rossum3293b071998-08-25 16:07:15 +0000407 unsigned LONG_LONG ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000408{
409#if SIZEOF_LONG_LONG == SIZEOF_LONG
410 /* In case the compiler is faking it. */
411 return PyLong_FromUnsignedLong( (unsigned long)ival );
412#else
Guido van Rossum3293b071998-08-25 16:07:15 +0000413 if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000414 return PyLong_FromUnsignedLong( (unsigned long)ival );
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000415 }
416 else {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000417 /* Assume a C long fits in at most 10 'digits'. */
418 PyLongObject *v = _PyLong_New(10);
419
420 if (v != NULL) {
Guido van Rossum3293b071998-08-25 16:07:15 +0000421 unsigned LONG_LONG t = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000422 int i;
423 for (i = 0; i < 10; i++) {
424 v->ob_digit[i] = (digit) (t & MASK);
425 t >>= SHIFT;
426 }
427
428 v = long_normalize(v);
429 }
430
431 return (PyObject *)v;
432 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000433#endif
434}
435
Guido van Rossum3293b071998-08-25 16:07:15 +0000436/* Get a C LONG_LONG int from a long int object.
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000437 Returns -1 and sets an error condition if overflow occurs. */
438
Guido van Rossum3293b071998-08-25 16:07:15 +0000439LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000440PyLong_AsLongLong(vv)
441 PyObject *vv;
442{
443#if SIZEOF_LONG_LONG == SIZEOF_LONG
444 /* In case the compiler is faking it. */
Guido van Rossum3293b071998-08-25 16:07:15 +0000445 return (LONG_LONG)PyLong_AsLong( vv );
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000446#else
447 register PyLongObject *v;
Guido van Rossum3293b071998-08-25 16:07:15 +0000448 LONG_LONG x, prev;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000449 int i, sign;
450
451 if (vv == NULL || !PyLong_Check(vv)) {
452 PyErr_BadInternalCall();
453 return -1;
454 }
455
456 v = (PyLongObject *)vv;
457 i = v->ob_size;
458 sign = 1;
459 x = 0;
460
461 if (i < 0) {
462 sign = -1;
463 i = -(i);
464 }
465
466 while (--i >= 0) {
467 prev = x;
468 x = (x << SHIFT) + v->ob_digit[i];
469 if ((x >> SHIFT) != prev) {
470 PyErr_SetString(PyExc_OverflowError,
471 "long int too long to convert");
472 return -1;
473 }
474 }
475
476 return x * sign;
477#endif
478}
479
Guido van Rossum3293b071998-08-25 16:07:15 +0000480unsigned LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000481PyLong_AsUnsignedLongLong(vv)
482 PyObject *vv;
483{
484#if SIZEOF_LONG_LONG == 4
485 /* In case the compiler is faking it. */
Guido van Rossum3293b071998-08-25 16:07:15 +0000486 return (unsigned LONG_LONG)PyLong_AsUnsignedLong( vv );
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000487#else
488 register PyLongObject *v;
Guido van Rossum3293b071998-08-25 16:07:15 +0000489 unsigned LONG_LONG x, prev;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000490 int i;
491
492 if (vv == NULL || !PyLong_Check(vv)) {
493 PyErr_BadInternalCall();
Guido van Rossum3293b071998-08-25 16:07:15 +0000494 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000495 }
496
497 v = (PyLongObject *)vv;
498 i = v->ob_size;
499 x = 0;
500
501 if (i < 0) {
502 PyErr_SetString(PyExc_OverflowError,
503 "can't convert negative value to unsigned long");
Guido van Rossum3293b071998-08-25 16:07:15 +0000504 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000505 }
506
507 while (--i >= 0) {
508 prev = x;
509 x = (x << SHIFT) + v->ob_digit[i];
510 if ((x >> SHIFT) != prev) {
511 PyErr_SetString(PyExc_OverflowError,
512 "long int too long to convert");
Guido van Rossum3293b071998-08-25 16:07:15 +0000513 return (unsigned LONG_LONG) -1;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000514 }
515 }
516
517 return x;
518#endif
519}
520#endif /* HAVE_LONG_LONG */
521
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000522/* Multiply by a single digit, ignoring the sign. */
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000525mul1(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000527 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000528{
529 return muladd1(a, n, (digit)0);
530}
531
532/* Multiply by a single digit and add a single digit, ignoring the sign. */
533
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000535muladd1(a, n, extra)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000537 wdigit n;
538 wdigit extra;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000539{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000540 int size_a = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 PyLongObject *z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000542 twodigits carry = extra;
543 int i;
544
545 if (z == NULL)
546 return NULL;
547 for (i = 0; i < size_a; ++i) {
548 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000549 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000550 carry >>= SHIFT;
551 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000552 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000553 return long_normalize(z);
554}
555
556/* Divide a long integer by a digit, returning both the quotient
557 (as function result) and the remainder (through *prem).
558 The sign of a is ignored; n should not be zero. */
559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000561divrem1(a, n, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 PyLongObject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000563 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000564 digit *prem;
565{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000566 int size = ABS(a->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000568 int i;
569 twodigits rem = 0;
570
571 assert(n > 0 && n <= MASK);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 z = _PyLong_New(size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000573 if (z == NULL)
574 return NULL;
575 for (i = size; --i >= 0; ) {
576 rem = (rem << SHIFT) + a->ob_digit[i];
Guido van Rossum2095d241997-04-09 19:41:24 +0000577 z->ob_digit[i] = (digit) (rem/n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000578 rem %= n;
579 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000580 *prem = (digit) rem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000581 return long_normalize(z);
582}
583
584/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000585 Return a string object.
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000586 If base is 8 or 16, add the proper prefix '0' or '0x'.
587 External linkage: used in bltinmodule.c by hex() and oct(). */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +0000590long_format(aa, base)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 PyObject *aa;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000592 int base;
593{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 register PyLongObject *a = (PyLongObject *)aa;
595 PyStringObject *str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000596 int i;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000597 int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000598 char *p;
599 int bits;
600 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +0000601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 if (a == NULL || !PyLong_Check(a)) {
603 PyErr_BadInternalCall();
Guido van Rossume32e0141992-01-19 16:31:05 +0000604 return NULL;
605 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000606 assert(base >= 2 && base <= 36);
607
608 /* Compute a rough upper bound for the length of the string */
609 i = base;
610 bits = 0;
611 while (i > 1) {
612 ++bits;
613 i >>= 1;
614 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000615 i = 6 + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000617 if (str == NULL)
618 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619 p = PyString_AS_STRING(str) + i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000620 *p = '\0';
Guido van Rossumc6913e71991-11-19 20:26:46 +0000621 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000622 if (a->ob_size < 0)
623 sign = '-';
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000624
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000625 if (a->ob_size == 0) {
626 *--p = '0';
627 }
628 else if ((base & (base - 1)) == 0) {
629 /* JRH: special case for power-of-2 bases */
630 twodigits temp = a->ob_digit[0];
631 int bitsleft = SHIFT;
632 int rem;
633 int last = abs(a->ob_size);
634 int basebits = 1;
635 i = base;
636 while ((i >>= 1) > 1) ++basebits;
637
638 i = 0;
639 for (;;) {
640 while (bitsleft >= basebits) {
641 if ((temp == 0) && (i >= last - 1)) break;
642 rem = temp & (base - 1);
643 if (rem < 10)
644 rem += '0';
645 else
646 rem += 'A' - 10;
647 assert(p > PyString_AS_STRING(str));
648 *--p = (char) rem;
649 bitsleft -= basebits;
650 temp >>= basebits;
651 }
652 if (++i >= last) {
653 if (temp == 0) break;
654 bitsleft = 99;
655 /* loop again to pick up final digits */
656 }
657 else {
658 temp = (a->ob_digit[i] << bitsleft) | temp;
659 bitsleft += SHIFT;
660 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000661 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000662 }
663 else {
664 Py_INCREF(a);
665 do {
666 digit rem;
667 PyLongObject *temp = divrem1(a, (digit)base, &rem);
668 if (temp == NULL) {
669 Py_DECREF(a);
670 Py_DECREF(str);
671 return NULL;
672 }
673 if (rem < 10)
674 rem += '0';
675 else
676 rem += 'A'-10;
677 assert(p > PyString_AS_STRING(str));
678 *--p = (char) rem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 Py_DECREF(a);
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000680 a = temp;
681 SIGCHECK({
682 Py_DECREF(a);
683 Py_DECREF(str);
684 return NULL;
685 })
686 } while (ABS(a->ob_size) != 0);
687 Py_DECREF(a);
688 }
689
Guido van Rossum2c475421992-08-14 15:13:07 +0000690 if (base == 8) {
691 if (size_a != 0)
692 *--p = '0';
693 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000694 else if (base == 16) {
695 *--p = 'x';
696 *--p = '0';
697 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000698 else if (base != 10) {
699 *--p = '#';
700 *--p = '0' + base%10;
701 if (base > 10)
702 *--p = '0' + base/10;
703 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000704 if (sign)
705 *--p = sign;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 if (p != PyString_AS_STRING(str)) {
707 char *q = PyString_AS_STRING(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000708 assert(p > q);
709 do {
710 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +0000711 q--;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 _PyString_Resize((PyObject **)&str,
713 (int) (q - PyString_AS_STRING(str)));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000714 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return (PyObject *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000716}
717
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000718#if 0
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000719/* Convert a string to a long int object, in a given base.
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000720 Base zero implies a default depending on the number.
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000721 External linkage: used in compile.c and stropmodule.c. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000722
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000724long_scan(str, base)
725 char *str;
726 int base;
727{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 return PyLong_FromString(str, (char **)NULL, base);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000729}
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000730#endif
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000731
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732PyObject *
733PyLong_FromString(str, pend, base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000734 char *str;
735 char **pend;
736 int base;
737{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000738 int sign = 1;
Guido van Rossume6762971998-06-22 03:54:46 +0000739 char *start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000741
Guido van Rossum472c04f1996-12-05 21:57:21 +0000742 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 PyErr_SetString(PyExc_ValueError,
744 "invalid base for long literal");
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000745 return NULL;
746 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000747 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000748 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000749 if (*str == '+')
750 ++str;
751 else if (*str == '-') {
752 ++str;
753 sign = -1;
754 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000755 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000756 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000757 if (base == 0) {
758 if (str[0] != '0')
759 base = 10;
760 else if (str[1] == 'x' || str[1] == 'X')
761 base = 16;
762 else
763 base = 8;
764 }
765 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
766 str += 2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 z = _PyLong_New(0);
Guido van Rossume6762971998-06-22 03:54:46 +0000768 start = str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000769 for ( ; z != NULL; ++str) {
770 int k = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771 PyLongObject *temp;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000772
773 if (*str <= '9')
774 k = *str - '0';
775 else if (*str >= 'a')
776 k = *str - 'a' + 10;
777 else if (*str >= 'A')
778 k = *str - 'A' + 10;
779 if (k < 0 || k >= base)
780 break;
781 temp = muladd1(z, (digit)base, (digit)k);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782 Py_DECREF(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000783 z = temp;
784 }
Guido van Rossumac6a37a1998-08-04 15:04:06 +0000785 if (z == NULL)
786 return NULL;
Guido van Rossume6762971998-06-22 03:54:46 +0000787 if (str == start) {
788 PyErr_SetString(PyExc_ValueError,
789 "no digits in long int constant");
790 return NULL;
791 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000792 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000793 z->ob_size = -(z->ob_size);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000794 if (pend)
795 *pend = str;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 return (PyObject *) z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000797}
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799static PyLongObject *x_divrem
800 Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject **));
801static PyObject *long_pos Py_PROTO((PyLongObject *));
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000802static int long_divrem Py_PROTO((PyLongObject *, PyLongObject *,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 PyLongObject **, PyLongObject **));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000804
805/* Long division with remainder, top-level routine */
806
Guido van Rossume32e0141992-01-19 16:31:05 +0000807static int
808long_divrem(a, b, pdiv, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 PyLongObject *a, *b;
810 PyLongObject **pdiv;
811 PyLongObject **prem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000812{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000813 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000815
816 if (size_b == 0) {
Guido van Rossumbd3a5271998-08-11 15:04:47 +0000817 PyErr_SetString(PyExc_ZeroDivisionError,
818 "long division or modulo");
Guido van Rossume32e0141992-01-19 16:31:05 +0000819 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000820 }
821 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +0000822 (size_a == size_b &&
823 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000824 /* |a| < |b|. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 *pdiv = _PyLong_New(0);
826 Py_INCREF(a);
827 *prem = (PyLongObject *) a;
Guido van Rossume32e0141992-01-19 16:31:05 +0000828 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000829 }
830 if (size_b == 1) {
831 digit rem = 0;
832 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000833 if (z == NULL)
834 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000836 }
Guido van Rossume32e0141992-01-19 16:31:05 +0000837 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000838 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000839 if (z == NULL)
840 return -1;
841 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000842 /* Set the signs.
843 The quotient z has the sign of a*b;
844 the remainder r has the sign of a,
845 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +0000846 if ((a->ob_size < 0) != (b->ob_size < 0))
847 z->ob_size = -(z->ob_size);
848 if (a->ob_size < 0 && (*prem)->ob_size != 0)
849 (*prem)->ob_size = -((*prem)->ob_size);
850 *pdiv = z;
851 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000852}
853
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000854/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000857x_divrem(v1, w1, prem)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 PyLongObject *v1, *w1;
859 PyLongObject **prem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000860{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000861 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +0000862 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 PyLongObject *v = mul1(v1, d);
864 PyLongObject *w = mul1(w1, d);
865 PyLongObject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000866 int j, k;
867
868 if (v == NULL || w == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869 Py_XDECREF(v);
870 Py_XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000871 return NULL;
872 }
873
874 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000875 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000876 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000877
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000878 size_v = ABS(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 a = _PyLong_New(size_v - size_w + 1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000880
881 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
882 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
883 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000884 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000885 int i;
886
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000887 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 Py_DECREF(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000889 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000890 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000891 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000892 if (vj == w->ob_digit[size_w-1])
893 q = MASK;
894 else
895 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
896 w->ob_digit[size_w-1];
897
898 while (w->ob_digit[size_w-2]*q >
899 ((
900 ((twodigits)vj << SHIFT)
901 + v->ob_digit[j-1]
902 - q*w->ob_digit[size_w-1]
903 ) << SHIFT)
904 + v->ob_digit[j-2])
905 --q;
906
907 for (i = 0; i < size_w && i+k < size_v; ++i) {
908 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +0000909 digit zz = (digit) (z >> SHIFT);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 carry += v->ob_digit[i+k] - z
911 + ((twodigits)zz << SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000912 v->ob_digit[i+k] = carry & MASK;
913 carry = (carry >> SHIFT) - zz;
914 }
915
916 if (i+k < size_v) {
917 carry += v->ob_digit[i+k];
918 v->ob_digit[i+k] = 0;
919 }
920
921 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +0000922 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000923 else {
924 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +0000925 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000926 carry = 0;
927 for (i = 0; i < size_w && i+k < size_v; ++i) {
928 carry += v->ob_digit[i+k] + w->ob_digit[i];
929 v->ob_digit[i+k] = carry & MASK;
930 carry >>= SHIFT;
931 }
932 }
933 } /* for j, k */
934
Guido van Rossumc206c761995-01-10 15:23:19 +0000935 if (a == NULL)
936 *prem = NULL;
937 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +0000938 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +0000939 *prem = divrem1(v, d, &d);
940 /* d receives the (unused) remainder */
941 if (*prem == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 Py_DECREF(a);
Guido van Rossume32e0141992-01-19 16:31:05 +0000943 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +0000944 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000945 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 Py_DECREF(v);
947 Py_DECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000948 return a;
949}
950
951/* Methods */
952
Guido van Rossume32e0141992-01-19 16:31:05 +0000953/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954static void long_dealloc Py_PROTO((PyObject *));
955static PyObject *long_repr Py_PROTO((PyObject *));
956static int long_compare Py_PROTO((PyLongObject *, PyLongObject *));
Guido van Rossum065ce5a1998-09-13 15:37:20 +0000957static long long_hash Py_PROTO((PyLongObject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000958
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959static PyObject *long_add Py_PROTO((PyLongObject *, PyLongObject *));
960static PyObject *long_sub Py_PROTO((PyLongObject *, PyLongObject *));
961static PyObject *long_mul Py_PROTO((PyLongObject *, PyLongObject *));
962static PyObject *long_div Py_PROTO((PyLongObject *, PyLongObject *));
963static PyObject *long_mod Py_PROTO((PyLongObject *, PyLongObject *));
964static PyObject *long_divmod Py_PROTO((PyLongObject *, PyLongObject *));
965static PyObject *long_pow
966 Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject *));
967static PyObject *long_neg Py_PROTO((PyLongObject *));
968static PyObject *long_pos Py_PROTO((PyLongObject *));
969static PyObject *long_abs Py_PROTO((PyLongObject *));
970static int long_nonzero Py_PROTO((PyLongObject *));
971static PyObject *long_invert Py_PROTO((PyLongObject *));
972static PyObject *long_lshift Py_PROTO((PyLongObject *, PyLongObject *));
973static PyObject *long_rshift Py_PROTO((PyLongObject *, PyLongObject *));
974static PyObject *long_and Py_PROTO((PyLongObject *, PyLongObject *));
975static PyObject *long_xor Py_PROTO((PyLongObject *, PyLongObject *));
976static PyObject *long_or Py_PROTO((PyLongObject *, PyLongObject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000977
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000978static void
979long_dealloc(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 PyObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000981{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 PyMem_DEL(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000983}
984
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000985static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000986long_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987 PyObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000988{
Guido van Rossum8b27d921992-03-27 17:27:05 +0000989 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000990}
991
992static int
993long_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000995{
996 int sign;
997
Guido van Rossumc6913e71991-11-19 20:26:46 +0000998 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000999 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001000 sign = 0;
1001 else
1002 sign = a->ob_size - b->ob_size;
1003 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001004 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001005 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001006 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1007 ;
1008 if (i < 0)
1009 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001010 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001011 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001012 if (a->ob_size < 0)
1013 sign = -sign;
1014 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001015 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001016 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001017}
1018
Guido van Rossum9bfef441993-03-29 10:43:31 +00001019static long
1020long_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001021 PyLongObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001022{
1023 long x;
1024 int i, sign;
1025
1026 /* This is designed so that Python ints and longs with the
1027 same value hash to the same value, otherwise comparisons
1028 of mapping keys will turn out weird */
1029 i = v->ob_size;
1030 sign = 1;
1031 x = 0;
1032 if (i < 0) {
1033 sign = -1;
1034 i = -(i);
1035 }
1036 while (--i >= 0) {
1037 /* Force a 32-bit circular shift */
1038 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
1039 x += v->ob_digit[i];
1040 }
1041 x = x * sign;
1042 if (x == -1)
1043 x = -2;
1044 return x;
1045}
1046
1047
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001048/* Add the absolute values of two long integers. */
1049
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050static PyLongObject *x_add Py_PROTO((PyLongObject *, PyLongObject *));
1051static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001052x_add(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001054{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001055 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001057 int i;
1058 digit carry = 0;
1059
1060 /* Ensure a is the larger of the two: */
1061 if (size_a < size_b) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001062 { PyLongObject *temp = a; a = b; b = temp; }
1063 { int size_temp = size_a;
1064 size_a = size_b;
1065 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001066 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067 z = _PyLong_New(size_a+1);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001068 if (z == NULL)
1069 return NULL;
1070 for (i = 0; i < size_b; ++i) {
1071 carry += a->ob_digit[i] + b->ob_digit[i];
1072 z->ob_digit[i] = carry & MASK;
1073 /* The following assumes unsigned shifts don't
1074 propagate the sign bit. */
1075 carry >>= SHIFT;
1076 }
1077 for (; i < size_a; ++i) {
1078 carry += a->ob_digit[i];
1079 z->ob_digit[i] = carry & MASK;
1080 carry >>= SHIFT;
1081 }
1082 z->ob_digit[i] = carry;
1083 return long_normalize(z);
1084}
1085
1086/* Subtract the absolute values of two integers. */
1087
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001088static PyLongObject *x_sub Py_PROTO((PyLongObject *, PyLongObject *));
1089static PyLongObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001090x_sub(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001091 PyLongObject *a, *b;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001092{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001093 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001095 int i;
1096 int sign = 1;
1097 digit borrow = 0;
1098
1099 /* Ensure a is the larger of the two: */
1100 if (size_a < size_b) {
1101 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001102 { PyLongObject *temp = a; a = b; b = temp; }
1103 { int size_temp = size_a;
1104 size_a = size_b;
1105 size_b = size_temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001106 }
1107 else if (size_a == size_b) {
1108 /* Find highest digit where a and b differ: */
1109 i = size_a;
1110 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
1111 ;
1112 if (i < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113 return _PyLong_New(0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001114 if (a->ob_digit[i] < b->ob_digit[i]) {
1115 sign = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 { PyLongObject *temp = a; a = b; b = temp; }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001117 }
1118 size_a = size_b = i+1;
1119 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001120 z = _PyLong_New(size_a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001121 if (z == NULL)
1122 return NULL;
1123 for (i = 0; i < size_b; ++i) {
1124 /* The following assumes unsigned arithmetic
1125 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001126 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001127 z->ob_digit[i] = borrow & MASK;
1128 borrow >>= SHIFT;
1129 borrow &= 1; /* Keep only one sign bit */
1130 }
1131 for (; i < size_a; ++i) {
1132 borrow = a->ob_digit[i] - borrow;
1133 z->ob_digit[i] = borrow & MASK;
1134 borrow >>= SHIFT;
1135 }
1136 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001137 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001138 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001139 return long_normalize(z);
1140}
1141
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001142static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001143long_add(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001144 PyLongObject *a;
1145 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001146{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001148
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001149 if (a->ob_size < 0) {
1150 if (b->ob_size < 0) {
1151 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001152 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001153 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001154 }
1155 else
1156 z = x_sub(b, a);
1157 }
1158 else {
1159 if (b->ob_size < 0)
1160 z = x_sub(a, b);
1161 else
1162 z = x_add(a, b);
1163 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001165}
1166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001168long_sub(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001169 PyLongObject *a;
1170 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001171{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001173
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001174 if (a->ob_size < 0) {
1175 if (b->ob_size < 0)
1176 z = x_sub(a, b);
1177 else
1178 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001179 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001180 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001181 }
1182 else {
1183 if (b->ob_size < 0)
1184 z = x_add(a, b);
1185 else
1186 z = x_sub(a, b);
1187 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001189}
1190
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001192long_mul(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001193 PyLongObject *a;
1194 PyLongObject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001195{
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001196 int size_a;
1197 int size_b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 PyLongObject *z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001199 int i;
1200
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001201 size_a = ABS(a->ob_size);
1202 size_b = ABS(b->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001203 z = _PyLong_New(size_a + size_b);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001204 if (z == NULL)
1205 return NULL;
1206 for (i = 0; i < z->ob_size; ++i)
1207 z->ob_digit[i] = 0;
1208 for (i = 0; i < size_a; ++i) {
1209 twodigits carry = 0;
1210 twodigits f = a->ob_digit[i];
1211 int j;
1212
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001213 SIGCHECK({
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001214 Py_DECREF(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001215 return NULL;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001216 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001217 for (j = 0; j < size_b; ++j) {
1218 carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
Guido van Rossum2095d241997-04-09 19:41:24 +00001219 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001220 carry >>= SHIFT;
1221 }
1222 for (; carry != 0; ++j) {
1223 assert(i+j < z->ob_size);
1224 carry += z->ob_digit[i+j];
Guido van Rossum2095d241997-04-09 19:41:24 +00001225 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001226 carry >>= SHIFT;
1227 }
1228 }
1229 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001230 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001231 if (b->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001232 z->ob_size = -(z->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001233 return (PyObject *) long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001234}
1235
Guido van Rossume32e0141992-01-19 16:31:05 +00001236/* The / and % operators are now defined in terms of divmod().
1237 The expression a mod b has the value a - b*floor(a/b).
1238 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001239 |a| by |b|, with the sign of a. This is also expressed
1240 as a - b*trunc(a/b), if trunc truncates towards zero.
1241 Some examples:
1242 a b a rem b a mod b
1243 13 10 3 3
1244 -13 10 -3 7
1245 13 -10 3 -7
1246 -13 -10 -3 -3
1247 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001248 have different signs. We then subtract one from the 'div'
1249 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001250
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251static int l_divmod Py_PROTO((PyLongObject *, PyLongObject *,
1252 PyLongObject **, PyLongObject **));
Guido van Rossume32e0141992-01-19 16:31:05 +00001253static int
1254l_divmod(v, w, pdiv, pmod)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255 PyLongObject *v;
1256 PyLongObject *w;
1257 PyLongObject **pdiv;
1258 PyLongObject **pmod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001259{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001261
1262 if (long_divrem(v, w, &div, &mod) < 0)
1263 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +00001264 if ((mod->ob_size < 0 && w->ob_size > 0) ||
1265 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266 PyLongObject *temp;
1267 PyLongObject *one;
1268 temp = (PyLongObject *) long_add(mod, w);
1269 Py_DECREF(mod);
Guido van Rossume32e0141992-01-19 16:31:05 +00001270 mod = temp;
1271 if (mod == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001272 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001273 return -1;
1274 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001275 one = (PyLongObject *) PyLong_FromLong(1L);
Guido van Rossume32e0141992-01-19 16:31:05 +00001276 if (one == NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001277 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
1278 Py_DECREF(mod);
1279 Py_DECREF(div);
1280 Py_XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +00001281 return -1;
1282 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001283 Py_DECREF(one);
1284 Py_DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001285 div = temp;
1286 }
1287 *pdiv = div;
1288 *pmod = mod;
1289 return 0;
1290}
1291
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001292static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001293long_div(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 PyLongObject *v;
1295 PyLongObject *w;
Guido van Rossume32e0141992-01-19 16:31:05 +00001296{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001298 if (l_divmod(v, w, &div, &mod) < 0)
1299 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300 Py_DECREF(mod);
1301 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00001302}
1303
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001304static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001305long_mod(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001306 PyLongObject *v;
1307 PyLongObject *w;
Guido van Rossume32e0141992-01-19 16:31:05 +00001308{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001310 if (l_divmod(v, w, &div, &mod) < 0)
1311 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 Py_DECREF(div);
1313 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001314}
1315
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001317long_divmod(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001318 PyLongObject *v;
1319 PyLongObject *w;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001320{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321 PyObject *z;
1322 PyLongObject *div, *mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00001323 if (l_divmod(v, w, &div, &mod) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001324 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325 z = PyTuple_New(2);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001326 if (z != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 PyTuple_SetItem(z, 0, (PyObject *) div);
1328 PyTuple_SetItem(z, 1, (PyObject *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001329 }
1330 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 Py_DECREF(div);
1332 Py_DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001333 }
1334 return z;
1335}
1336
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337static PyObject *
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001338long_pow(a, b, c)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 PyLongObject *a;
1340 PyLongObject *b;
1341 PyLongObject *c;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001342{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343 PyLongObject *z, *div, *mod;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001344 int size_b, i;
1345
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001346 size_b = b->ob_size;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001347 if (size_b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 PyErr_SetString(PyExc_ValueError,
1349 "long integer to the negative power");
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001350 return NULL;
1351 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 z = (PyLongObject *)PyLong_FromLong(1L);
1353 Py_INCREF(a);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001354 for (i = 0; i < size_b; ++i) {
1355 digit bi = b->ob_digit[i];
1356 int j;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001357
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001358 for (j = 0; j < SHIFT; ++j) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 PyLongObject *temp;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001360
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001361 if (bi & 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 temp = (PyLongObject *)long_mul(z, a);
1363 Py_DECREF(z);
1364 if ((PyObject*)c!=Py_None && temp!=NULL) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001365 l_divmod(temp, c, &div, &mod);
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 Rossumeb1fafc1994-08-29 12:47:19 +00001380 l_divmod(temp, c, &div, &mod);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381 Py_XDECREF(div);
1382 Py_DECREF(temp);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001383 temp = mod;
1384 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001385 a = temp;
1386 if (a == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 Py_DECREF(z);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001388 z = NULL;
1389 break;
1390 }
1391 }
Guido van Rossumc206c761995-01-10 15:23:19 +00001392 if (a == NULL || z == NULL)
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001393 break;
1394 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 Py_XDECREF(a);
1396 if ((PyObject*)c!=Py_None && z!=NULL) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001397 l_divmod(z, c, &div, &mod);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398 Py_XDECREF(div);
1399 Py_DECREF(z);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001400 z=mod;
1401 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001403}
1404
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001405static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001406long_invert(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407 PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001408{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001409 /* Implement ~x as -(x+1) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001410 PyLongObject *x;
1411 PyLongObject *w;
1412 w = (PyLongObject *)PyLong_FromLong(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001413 if (w == NULL)
1414 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415 x = (PyLongObject *) long_add(v, w);
1416 Py_DECREF(w);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001417 if (x == NULL)
1418 return NULL;
1419 if (x->ob_size != 0)
1420 x->ob_size = -(x->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001421 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001422}
1423
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001424static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001425long_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001426 PyLongObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001427{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001428 Py_INCREF(v);
1429 return (PyObject *)v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001430}
1431
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001433long_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434 PyLongObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001435{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436 PyLongObject *z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001437 int i, n;
1438 n = ABS(v->ob_size);
1439 if (n == 0) {
1440 /* -0 == 0 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001441 Py_INCREF(v);
1442 return (PyObject *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001443 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 z = _PyLong_New(ABS(n));
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001445 if (z == NULL)
1446 return NULL;
1447 for (i = 0; i < n; i++)
1448 z->ob_digit[i] = v->ob_digit[i];
1449 z->ob_size = -(v->ob_size);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001451}
1452
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001453static PyObject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001454long_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001455 PyLongObject *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001456{
1457 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001458 return long_neg(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001459 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460 Py_INCREF(v);
1461 return (PyObject *)v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001462 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001463}
1464
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001465static int
1466long_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001467 PyLongObject *v;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001468{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001469 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001470}
1471
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001473long_rshift(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001474 PyLongObject *a;
1475 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001476{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001478 long shiftby;
1479 int newsize, wordshift, loshift, hishift, i, j;
1480 digit lomask, himask;
1481
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001482 if (a->ob_size < 0) {
1483 /* Right shifting negative numbers is harder */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001484 PyLongObject *a1, *a2, *a3;
1485 a1 = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001486 if (a1 == NULL) return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001487 a2 = (PyLongObject *) long_rshift(a1, b);
1488 Py_DECREF(a1);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001489 if (a2 == NULL) return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490 a3 = (PyLongObject *) long_invert(a2);
1491 Py_DECREF(a2);
1492 return (PyObject *) a3;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001493 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001494
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001495 shiftby = PyLong_AsLong((PyObject *)b);
1496 if (shiftby == -1L && PyErr_Occurred())
Guido van Rossumc6913e71991-11-19 20:26:46 +00001497 return NULL;
1498 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001499 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001500 return NULL;
1501 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001502 wordshift = shiftby / SHIFT;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001503 newsize = ABS(a->ob_size) - wordshift;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001504 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001505 z = _PyLong_New(0);
1506 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001507 }
1508 loshift = shiftby % SHIFT;
1509 hishift = SHIFT - loshift;
1510 lomask = ((digit)1 << hishift) - 1;
1511 himask = MASK ^ lomask;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001512 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001513 if (z == NULL)
1514 return NULL;
1515 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001516 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001517 for (i = 0, j = wordshift; i < newsize; i++, j++) {
1518 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
1519 if (i+1 < newsize)
1520 z->ob_digit[i] |=
1521 (a->ob_digit[j+1] << hishift) & himask;
1522 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001523 return (PyObject *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001524}
1525
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001526static PyObject *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001527long_lshift(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001528 PyLongObject *a;
1529 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001530{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001531 /* This version due to Tim Peters */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001532 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001533 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001534 int oldsize, newsize, wordshift, remshift, i, j;
1535 twodigits accum;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001536
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537 shiftby = PyLong_AsLong((PyObject *)b);
1538 if (shiftby == -1L && PyErr_Occurred())
Guido van Rossumc6913e71991-11-19 20:26:46 +00001539 return NULL;
1540 if (shiftby < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001541 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001542 return NULL;
1543 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001544 if ((long)(int)shiftby != shiftby) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001545 PyErr_SetString(PyExc_ValueError,
1546 "outrageous left shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001547 return NULL;
1548 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001549 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1550 wordshift = (int)shiftby / SHIFT;
1551 remshift = (int)shiftby - wordshift * SHIFT;
1552
1553 oldsize = ABS(a->ob_size);
1554 newsize = oldsize + wordshift;
1555 if (remshift)
1556 ++newsize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001557 z = _PyLong_New(newsize);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001558 if (z == NULL)
1559 return NULL;
1560 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001561 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001562 for (i = 0; i < wordshift; i++)
1563 z->ob_digit[i] = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001564 accum = 0;
1565 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
1566 accum |= a->ob_digit[j] << remshift;
1567 z->ob_digit[i] = (digit)(accum & MASK);
1568 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001569 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001570 if (remshift)
1571 z->ob_digit[newsize-1] = (digit)accum;
1572 else
1573 assert(!accum);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001574 return (PyObject *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001575}
1576
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001577
1578/* Bitwise and/xor/or operations */
1579
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001580#define MAX(x, y) ((x) < (y) ? (y) : (x))
1581#define MIN(x, y) ((x) > (y) ? (y) : (x))
1582
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001583static PyObject *long_bitwise Py_PROTO((PyLongObject *, int, PyLongObject *));
1584static PyObject *
Guido van Rossume32e0141992-01-19 16:31:05 +00001585long_bitwise(a, op, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001586 PyLongObject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001587 int op; /* '&', '|', '^' */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001588 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001589{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001590 digit maska, maskb; /* 0 or MASK */
1591 int negz;
1592 int size_a, size_b, size_z;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001593 PyLongObject *z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001594 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00001595 digit diga, digb;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001596 PyObject *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001597
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001598 if (a->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001599 a = (PyLongObject *) long_invert(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001600 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001601 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001602 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603 Py_INCREF(a);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001604 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001605 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001606 if (b->ob_size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001607 b = (PyLongObject *) long_invert(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001608 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001609 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001610 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001611 Py_INCREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001612 maskb = 0;
1613 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001614
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001615 negz = 0;
1616 switch (op) {
1617 case '^':
1618 if (maska != maskb) {
1619 maska ^= MASK;
1620 negz = -1;
1621 }
1622 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001623 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001624 if (maska && maskb) {
1625 op = '|';
1626 maska ^= MASK;
1627 maskb ^= MASK;
1628 negz = -1;
1629 }
1630 break;
1631 case '|':
1632 if (maska || maskb) {
1633 op = '&';
1634 maska ^= MASK;
1635 maskb ^= MASK;
1636 negz = -1;
1637 }
1638 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001639 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001640
Guido van Rossumbd3a5271998-08-11 15:04:47 +00001641 /* JRH: The original logic here was to allocate the result value (z)
1642 as the longer of the two operands. However, there are some cases
1643 where the result is guaranteed to be shorter than that: AND of two
1644 positives, OR of two negatives: use the shorter number. AND with
1645 mixed signs: use the positive number. OR with mixed signs: use the
1646 negative number. After the transformations above, op will be '&'
1647 iff one of these cases applies, and mask will be non-0 for operands
1648 whose length should be ignored.
1649 */
1650
1651 size_a = a->ob_size;
1652 size_b = b->ob_size;
1653 size_z = op == '&'
1654 ? (maska
1655 ? size_b
1656 : (maskb ? size_a : MIN(size_a, size_b)))
1657 : MAX(size_a, size_b);
1658 z = _PyLong_New(size_z);
1659 if (a == NULL || b == NULL || z == NULL) {
1660 Py_XDECREF(a);
1661 Py_XDECREF(b);
1662 Py_XDECREF(z);
1663 return NULL;
1664 }
1665
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001666 for (i = 0; i < size_z; ++i) {
1667 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
1668 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
1669 switch (op) {
1670 case '&': z->ob_digit[i] = diga & digb; break;
1671 case '|': z->ob_digit[i] = diga | digb; break;
1672 case '^': z->ob_digit[i] = diga ^ digb; break;
1673 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001674 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001675
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001676 Py_DECREF(a);
1677 Py_DECREF(b);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001678 z = long_normalize(z);
1679 if (negz == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001680 return (PyObject *) z;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001681 v = long_invert(z);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001682 Py_DECREF(z);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001683 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001684}
1685
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001686static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001687long_and(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001688 PyLongObject *a;
1689 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001690{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001691 return long_bitwise(a, '&', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001692}
1693
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001694static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001695long_xor(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001696 PyLongObject *a;
1697 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001698{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001699 return long_bitwise(a, '^', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001700}
1701
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001702static PyObject *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001703long_or(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001704 PyLongObject *a;
1705 PyLongObject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001706{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001707 return long_bitwise(a, '|', b);
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001708}
1709
Guido van Rossum234f9421993-06-17 12:35:49 +00001710static int
Guido van Rossume6eefc21992-08-14 12:06:52 +00001711long_coerce(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712 PyObject **pv;
1713 PyObject **pw;
Guido van Rossume6eefc21992-08-14 12:06:52 +00001714{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001715 if (PyInt_Check(*pw)) {
1716 *pw = PyLong_FromLong(PyInt_AsLong(*pw));
1717 Py_INCREF(*pv);
Guido van Rossume6eefc21992-08-14 12:06:52 +00001718 return 0;
1719 }
1720 return 1; /* Can't do it */
1721}
1722
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001723static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001724long_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001725 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001726{
1727 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001728 x = PyLong_AsLong(v);
1729 if (PyErr_Occurred())
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001730 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001731 return PyInt_FromLong(x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001732}
1733
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001734static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001735long_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001736 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001737{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001738 Py_INCREF(v);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001739 return v;
1740}
1741
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001742static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001743long_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001744 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001745{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00001746 double result;
1747 PyFPE_START_PROTECT("long_float", return 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001748 result = PyLong_AsDouble(v);
Guido van Rossum45b83911997-03-14 04:32:50 +00001749 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001750 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001751}
1752
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001753static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001754long_oct(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001756{
1757 return long_format(v, 8);
1758}
1759
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001760static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001761long_hex(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001762 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001763{
1764 return long_format(v, 16);
1765}
1766
1767
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001768#define UF (unaryfunc)
1769#define BF (binaryfunc)
1770#define TF (ternaryfunc)
1771#define IF (inquiry)
Guido van Rossum8b27d921992-03-27 17:27:05 +00001772
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001773static PyNumberMethods long_as_number = {
Guido van Rossum8b27d921992-03-27 17:27:05 +00001774 BF long_add, /*nb_add*/
1775 BF long_sub, /*nb_subtract*/
1776 BF long_mul, /*nb_multiply*/
1777 BF long_div, /*nb_divide*/
1778 BF long_mod, /*nb_remainder*/
1779 BF long_divmod, /*nb_divmod*/
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001780 TF long_pow, /*nb_power*/
Guido van Rossum8b27d921992-03-27 17:27:05 +00001781 UF long_neg, /*nb_negative*/
1782 UF long_pos, /*tp_positive*/
1783 UF long_abs, /*tp_absolute*/
1784 IF long_nonzero,/*tp_nonzero*/
1785 UF long_invert, /*nb_invert*/
1786 BF long_lshift, /*nb_lshift*/
1787 BF long_rshift, /*nb_rshift*/
1788 BF long_and, /*nb_and*/
1789 BF long_xor, /*nb_xor*/
1790 BF long_or, /*nb_or*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001791 (int (*) Py_FPROTO((PyObject **, PyObject **)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001792 (coercion)long_coerce, /*nb_coerce*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001793 UF long_int, /*nb_int*/
1794 UF long_long, /*nb_long*/
1795 UF long_float, /*nb_float*/
1796 UF long_oct, /*nb_oct*/
1797 UF long_hex, /*nb_hex*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001798};
1799
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001800PyTypeObject PyLong_Type = {
1801 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001802 0,
1803 "long int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001804 sizeof(PyLongObject) - sizeof(digit),
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001805 sizeof(digit),
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001806 (destructor)long_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001807 0, /*tp_print*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001808 0, /*tp_getattr*/
1809 0, /*tp_setattr*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001810 (int (*) Py_FPROTO((PyObject *, PyObject *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001811 (cmpfunc)long_compare, /*tp_compare*/
1812 (reprfunc)long_repr, /*tp_repr*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001813 &long_as_number,/*tp_as_number*/
1814 0, /*tp_as_sequence*/
1815 0, /*tp_as_mapping*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001816 (long (*) Py_FPROTO((PyObject *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001817 (hashfunc)long_hash, /*tp_hash*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001818};