blob: dc84583c393ac09a2bfb7e654c8588e8e54f6bbe [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 Rossumedcc38a1991-05-05 20:09:44 +000036#include "allobjects.h"
37#include "longintrepr.h"
Guido van Rossum687ec181995-03-04 22:43:47 +000038#include "mymath.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +000039#include <assert.h>
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000040#include <ctype.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000041
Guido van Rossume32e0141992-01-19 16:31:05 +000042#define ABS(x) ((x) < 0 ? -(x) : (x))
43
44/* Forward */
45static longobject *long_normalize PROTO((longobject *));
46static longobject *mul1 PROTO((longobject *, wdigit));
47static longobject *muladd1 PROTO((longobject *, wdigit, wdigit));
48static longobject *divrem1 PROTO((longobject *, wdigit, digit *));
Guido van Rossumb73cc041993-11-01 16:28:59 +000049static object *long_format PROTO((object *aa, int base));
Guido van Rossume32e0141992-01-19 16:31:05 +000050
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000051static int ticker; /* XXX Could be shared with ceval? */
52
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000053#define SIGCHECK(block) \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000054 if (--ticker < 0) { \
55 ticker = 100; \
Guido van Rossumeb1fafc1994-08-29 12:47:19 +000056 if (sigcheck()) { block; } \
Guido van Rossum23d6f0e1991-05-14 12:06:49 +000057 }
58
Guido van Rossumedcc38a1991-05-05 20:09:44 +000059/* Normalize (remove leading zeros from) a long int object.
60 Doesn't attempt to free the storage--in most cases, due to the nature
61 of the algorithms used, this could save at most be one word anyway. */
62
Guido van Rossum4c260ff1992-01-14 18:36:43 +000063static longobject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +000064long_normalize(v)
65 register longobject *v;
66{
Guido van Rossum4c260ff1992-01-14 18:36:43 +000067 int j = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000068 register int i = j;
69
70 while (i > 0 && v->ob_digit[i-1] == 0)
71 --i;
72 if (i != j)
Guido van Rossum4c260ff1992-01-14 18:36:43 +000073 v->ob_size = (v->ob_size < 0) ? -(i) : i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000074 return v;
75}
76
77/* Allocate a new long int object with size digits.
78 Return NULL and set exception if we run out of memory. */
79
80longobject *
81alloclongobject(size)
82 int size;
83{
84 return NEWVAROBJ(longobject, &Longtype, size);
85}
86
87/* Create a new long int object from a C long int */
88
89object *
90newlongobject(ival)
91 long ival;
92{
Guido van Rossum472c04f1996-12-05 21:57:21 +000093 /* Assume a C long fits in at most 5 'digits' */
94 /* Works on both 32- and 64-bit machines */
95 longobject *v = alloclongobject(5);
Guido van Rossumedcc38a1991-05-05 20:09:44 +000096 if (v != NULL) {
Guido van Rossum472c04f1996-12-05 21:57:21 +000097 unsigned long t = ival;
98 int i;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000099 if (ival < 0) {
Guido van Rossum472c04f1996-12-05 21:57:21 +0000100 t = -ival;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000101 v->ob_size = -(v->ob_size);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000102 }
103 for (i = 0; i < 5; i++) {
Guido van Rossum2095d241997-04-09 19:41:24 +0000104 v->ob_digit[i] = (digit) (t & MASK);
Guido van Rossum472c04f1996-12-05 21:57:21 +0000105 t >>= SHIFT;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000106 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000107 v = long_normalize(v);
108 }
109 return (object *)v;
110}
111
Guido van Rossum53756b11997-01-03 17:14:46 +0000112/* Create a new long int object from a C unsigned long int */
113
114object *
115PyLong_FromUnsignedLong(ival)
116 unsigned long ival;
117{
118 /* Assume a C long fits in at most 5 'digits' */
119 /* Works on both 32- and 64-bit machines */
120 longobject *v = alloclongobject(5);
121 if (v != NULL) {
122 unsigned long t = ival;
123 int i;
124 for (i = 0; i < 5; i++) {
Guido van Rossum2095d241997-04-09 19:41:24 +0000125 v->ob_digit[i] = (digit) (t & MASK);
Guido van Rossum53756b11997-01-03 17:14:46 +0000126 t >>= SHIFT;
127 }
128 v = long_normalize(v);
129 }
130 return (object *)v;
131}
132
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000133/* Create a new long int object from a C double */
134
135object *
Guido van Rossum687ec181995-03-04 22:43:47 +0000136#ifdef MPW
137dnewlongobject(double dval)
138#else
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000139dnewlongobject(dval)
140 double dval;
Guido van Rossum687ec181995-03-04 22:43:47 +0000141#endif /* MPW */
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000142{
143 longobject *v;
144 double frac;
145 int i, ndig, expo, neg;
146 neg = 0;
147 if (dval < 0.0) {
148 neg = 1;
149 dval = -dval;
150 }
151 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
152 if (expo <= 0)
153 return newlongobject(0L);
154 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
155 v = alloclongobject(ndig);
156 if (v == NULL)
157 return NULL;
158 frac = ldexp(frac, (expo-1) % SHIFT + 1);
159 for (i = ndig; --i >= 0; ) {
160 long bits = (long)frac;
Guido van Rossum2095d241997-04-09 19:41:24 +0000161 v->ob_digit[i] = (digit) bits;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000162 frac = frac - (double)bits;
163 frac = ldexp(frac, SHIFT);
164 }
165 if (neg)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000166 v->ob_size = -(v->ob_size);
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000167 return (object *)v;
168}
169
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000170/* Get a C long int from a long int object.
171 Returns -1 and sets an error condition if overflow occurs. */
172
173long
174getlongvalue(vv)
175 object *vv;
176{
177 register longobject *v;
178 long x, prev;
179 int i, sign;
180
181 if (vv == NULL || !is_longobject(vv)) {
182 err_badcall();
183 return -1;
184 }
185 v = (longobject *)vv;
186 i = v->ob_size;
187 sign = 1;
188 x = 0;
189 if (i < 0) {
190 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000191 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000192 }
193 while (--i >= 0) {
194 prev = x;
195 x = (x << SHIFT) + v->ob_digit[i];
196 if ((x >> SHIFT) != prev) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000197 err_setstr(OverflowError,
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000198 "long int too long to convert");
199 return -1;
200 }
201 }
202 return x * sign;
203}
204
Guido van Rossum53756b11997-01-03 17:14:46 +0000205/* Get a C long int from a long int object.
206 Returns -1 and sets an error condition if overflow occurs. */
207
208unsigned long
209PyLong_AsUnsignedLong(vv)
210 object *vv;
211{
212 register longobject *v;
213 unsigned long x, prev;
214 int i;
215
216 if (vv == NULL || !is_longobject(vv)) {
217 err_badcall();
218 return (unsigned long) -1;
219 }
220 v = (longobject *)vv;
221 i = v->ob_size;
222 x = 0;
223 if (i < 0) {
224 err_setstr(OverflowError,
225 "can't convert negative value to unsigned long");
226 return (unsigned long) -1;
227 }
228 while (--i >= 0) {
229 prev = x;
230 x = (x << SHIFT) + v->ob_digit[i];
231 if ((x >> SHIFT) != prev) {
232 err_setstr(OverflowError,
233 "long int too long to convert");
234 return (unsigned long) -1;
235 }
236 }
237 return x;
238}
239
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000240/* Get a C double from a long int object. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000241
242double
243dgetlongvalue(vv)
244 object *vv;
245{
246 register longobject *v;
247 double x;
248 double multiplier = (double) (1L << SHIFT);
249 int i, sign;
250
251 if (vv == NULL || !is_longobject(vv)) {
252 err_badcall();
253 return -1;
254 }
255 v = (longobject *)vv;
256 i = v->ob_size;
257 sign = 1;
258 x = 0.0;
259 if (i < 0) {
260 sign = -1;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000261 i = -(i);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000262 }
263 while (--i >= 0) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000264 x = x*multiplier + (double)v->ob_digit[i];
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000265 }
266 return x * sign;
267}
268
269/* Multiply by a single digit, ignoring the sign. */
270
Guido van Rossume32e0141992-01-19 16:31:05 +0000271static longobject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000272mul1(a, n)
273 longobject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000274 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000275{
276 return muladd1(a, n, (digit)0);
277}
278
279/* Multiply by a single digit and add a single digit, ignoring the sign. */
280
Guido van Rossume32e0141992-01-19 16:31:05 +0000281static longobject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000282muladd1(a, n, extra)
283 longobject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000284 wdigit n;
285 wdigit extra;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000286{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000287 int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000288 longobject *z = alloclongobject(size_a+1);
289 twodigits carry = extra;
290 int i;
291
292 if (z == NULL)
293 return NULL;
294 for (i = 0; i < size_a; ++i) {
295 carry += (twodigits)a->ob_digit[i] * n;
Guido van Rossum2095d241997-04-09 19:41:24 +0000296 z->ob_digit[i] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000297 carry >>= SHIFT;
298 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000299 z->ob_digit[i] = (digit) carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000300 return long_normalize(z);
301}
302
303/* Divide a long integer by a digit, returning both the quotient
304 (as function result) and the remainder (through *prem).
305 The sign of a is ignored; n should not be zero. */
306
Guido van Rossume32e0141992-01-19 16:31:05 +0000307static longobject *
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000308divrem1(a, n, prem)
309 longobject *a;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000310 wdigit n;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000311 digit *prem;
312{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000313 int size = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000314 longobject *z;
315 int i;
316 twodigits rem = 0;
317
318 assert(n > 0 && n <= MASK);
319 z = alloclongobject(size);
320 if (z == NULL)
321 return NULL;
322 for (i = size; --i >= 0; ) {
323 rem = (rem << SHIFT) + a->ob_digit[i];
Guido van Rossum2095d241997-04-09 19:41:24 +0000324 z->ob_digit[i] = (digit) (rem/n);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000325 rem %= n;
326 }
Guido van Rossum2095d241997-04-09 19:41:24 +0000327 *prem = (digit) rem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000328 return long_normalize(z);
329}
330
331/* Convert a long int object to a string, using a given conversion base.
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000332 Return a string object.
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000333 If base is 8 or 16, add the proper prefix '0' or '0x'.
334 External linkage: used in bltinmodule.c by hex() and oct(). */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000335
Guido van Rossum234f9421993-06-17 12:35:49 +0000336static object *
Guido van Rossume32e0141992-01-19 16:31:05 +0000337long_format(aa, base)
338 object *aa;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000339 int base;
340{
Guido van Rossume32e0141992-01-19 16:31:05 +0000341 register longobject *a = (longobject *)aa;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000342 stringobject *str;
343 int i;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000344 int size_a = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000345 char *p;
346 int bits;
347 char sign = '\0';
Guido van Rossume32e0141992-01-19 16:31:05 +0000348
349 if (a == NULL || !is_longobject(a)) {
350 err_badcall();
351 return NULL;
352 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000353 assert(base >= 2 && base <= 36);
354
355 /* Compute a rough upper bound for the length of the string */
356 i = base;
357 bits = 0;
358 while (i > 1) {
359 ++bits;
360 i >>= 1;
361 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000362 i = 6 + (size_a*SHIFT + bits-1) / bits;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000363 str = (stringobject *) newsizedstringobject((char *)0, i);
364 if (str == NULL)
365 return NULL;
366 p = GETSTRINGVALUE(str) + i;
367 *p = '\0';
Guido van Rossumc6913e71991-11-19 20:26:46 +0000368 *--p = 'L';
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000369 if (a->ob_size < 0)
370 sign = '-';
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000371
372 INCREF(a);
373 do {
374 digit rem;
375 longobject *temp = divrem1(a, (digit)base, &rem);
376 if (temp == NULL) {
377 DECREF(a);
378 DECREF(str);
379 return NULL;
380 }
381 if (rem < 10)
382 rem += '0';
383 else
384 rem += 'A'-10;
385 assert(p > GETSTRINGVALUE(str));
Guido van Rossum2095d241997-04-09 19:41:24 +0000386 *--p = (char) rem;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000387 DECREF(a);
388 a = temp;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000389 SIGCHECK({
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000390 DECREF(a);
391 DECREF(str);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000392 return NULL;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000393 })
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000394 } while (ABS(a->ob_size) != 0);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000395 DECREF(a);
Guido van Rossum2c475421992-08-14 15:13:07 +0000396 if (base == 8) {
397 if (size_a != 0)
398 *--p = '0';
399 }
Guido van Rossum3d3037d1991-10-24 14:55:57 +0000400 else if (base == 16) {
401 *--p = 'x';
402 *--p = '0';
403 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000404 else if (base != 10) {
405 *--p = '#';
406 *--p = '0' + base%10;
407 if (base > 10)
408 *--p = '0' + base/10;
409 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000410 if (sign)
411 *--p = sign;
412 if (p != GETSTRINGVALUE(str)) {
413 char *q = GETSTRINGVALUE(str);
414 assert(p > q);
415 do {
416 } while ((*q++ = *p++) != '\0');
Guido van Rossumc7ec9c91991-05-28 21:58:16 +0000417 q--;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000418 resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str)));
419 }
Guido van Rossume32e0141992-01-19 16:31:05 +0000420 return (object *)str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000421}
422
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000423#if 0
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000424/* Convert a string to a long int object, in a given base.
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000425 Base zero implies a default depending on the number.
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000426 External linkage: used in compile.c and stropmodule.c. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000427
428object *
429long_scan(str, base)
430 char *str;
431 int base;
432{
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000433 return long_escan(str, (char **)NULL, base);
434}
Guido van Rossum3535f6e1995-01-17 16:34:13 +0000435#endif
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000436
437object *
438long_escan(str, pend, base)
439 char *str;
440 char **pend;
441 int base;
442{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000443 int sign = 1;
444 longobject *z;
445
Guido van Rossum472c04f1996-12-05 21:57:21 +0000446 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000447 err_setstr(ValueError, "invalid base for long literal");
448 return NULL;
449 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000450 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000451 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000452 if (*str == '+')
453 ++str;
454 else if (*str == '-') {
455 ++str;
456 sign = -1;
457 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +0000458 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000459 str++;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000460 if (base == 0) {
461 if (str[0] != '0')
462 base = 10;
463 else if (str[1] == 'x' || str[1] == 'X')
464 base = 16;
465 else
466 base = 8;
467 }
468 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
469 str += 2;
470 z = alloclongobject(0);
471 for ( ; z != NULL; ++str) {
472 int k = -1;
473 longobject *temp;
474
475 if (*str <= '9')
476 k = *str - '0';
477 else if (*str >= 'a')
478 k = *str - 'a' + 10;
479 else if (*str >= 'A')
480 k = *str - 'A' + 10;
481 if (k < 0 || k >= base)
482 break;
483 temp = muladd1(z, (digit)base, (digit)k);
484 DECREF(z);
485 z = temp;
486 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000487 if (sign < 0 && z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000488 z->ob_size = -(z->ob_size);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000489 if (pend)
490 *pend = str;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000491 return (object *) z;
492}
493
494static longobject *x_divrem PROTO((longobject *, longobject *, longobject **));
Guido van Rossumc6913e71991-11-19 20:26:46 +0000495static object *long_pos PROTO((longobject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000496static long_divrem PROTO((longobject *, longobject *,
497 longobject **, longobject **));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000498
499/* Long division with remainder, top-level routine */
500
Guido van Rossume32e0141992-01-19 16:31:05 +0000501static int
502long_divrem(a, b, pdiv, prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000503 longobject *a, *b;
Guido van Rossume32e0141992-01-19 16:31:05 +0000504 longobject **pdiv;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000505 longobject **prem;
506{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000507 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000508 longobject *z;
509
510 if (size_b == 0) {
Guido van Rossume32e0141992-01-19 16:31:05 +0000511 err_setstr(ZeroDivisionError, "long division or modulo");
512 return -1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000513 }
514 if (size_a < size_b ||
Guido van Rossum472c04f1996-12-05 21:57:21 +0000515 (size_a == size_b &&
516 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000517 /* |a| < |b|. */
Guido van Rossume32e0141992-01-19 16:31:05 +0000518 *pdiv = alloclongobject(0);
519 INCREF(a);
520 *prem = (longobject *) a;
521 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000522 }
523 if (size_b == 1) {
524 digit rem = 0;
525 z = divrem1(a, b->ob_digit[0], &rem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000526 if (z == NULL)
527 return -1;
528 *prem = (longobject *) newlongobject((long)rem);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000529 }
Guido van Rossume32e0141992-01-19 16:31:05 +0000530 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000531 z = x_divrem(a, b, prem);
Guido van Rossume32e0141992-01-19 16:31:05 +0000532 if (z == NULL)
533 return -1;
534 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000535 /* Set the signs.
536 The quotient z has the sign of a*b;
537 the remainder r has the sign of a,
538 so a = b*z + r. */
Guido van Rossume32e0141992-01-19 16:31:05 +0000539 if ((a->ob_size < 0) != (b->ob_size < 0))
540 z->ob_size = -(z->ob_size);
541 if (a->ob_size < 0 && (*prem)->ob_size != 0)
542 (*prem)->ob_size = -((*prem)->ob_size);
543 *pdiv = z;
544 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000545}
546
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000547/* Unsigned long division with remainder -- the algorithm */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000548
549static longobject *
550x_divrem(v1, w1, prem)
551 longobject *v1, *w1;
552 longobject **prem;
553{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000554 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
Guido van Rossum2095d241997-04-09 19:41:24 +0000555 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000556 longobject *v = mul1(v1, d);
557 longobject *w = mul1(w1, d);
558 longobject *a;
559 int j, k;
560
561 if (v == NULL || w == NULL) {
562 XDECREF(v);
563 XDECREF(w);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000564 return NULL;
565 }
566
567 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000568 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000569 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000570
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000571 size_v = ABS(v->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000572 a = alloclongobject(size_v - size_w + 1);
573
574 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
575 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
576 twodigits q;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000577 stwodigits carry = 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000578 int i;
579
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000580 SIGCHECK({
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000581 DECREF(a);
582 a = NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000583 break;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000584 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000585 if (vj == w->ob_digit[size_w-1])
586 q = MASK;
587 else
588 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
589 w->ob_digit[size_w-1];
590
591 while (w->ob_digit[size_w-2]*q >
592 ((
593 ((twodigits)vj << SHIFT)
594 + v->ob_digit[j-1]
595 - q*w->ob_digit[size_w-1]
596 ) << SHIFT)
597 + v->ob_digit[j-2])
598 --q;
599
600 for (i = 0; i < size_w && i+k < size_v; ++i) {
601 twodigits z = w->ob_digit[i] * q;
Guido van Rossum2095d241997-04-09 19:41:24 +0000602 digit zz = (digit) (z >> SHIFT);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000603 carry += v->ob_digit[i+k] - z + ((twodigits)zz << SHIFT);
604 v->ob_digit[i+k] = carry & MASK;
605 carry = (carry >> SHIFT) - zz;
606 }
607
608 if (i+k < size_v) {
609 carry += v->ob_digit[i+k];
610 v->ob_digit[i+k] = 0;
611 }
612
613 if (carry == 0)
Guido van Rossum2095d241997-04-09 19:41:24 +0000614 a->ob_digit[k] = (digit) q;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000615 else {
616 assert(carry == -1);
Guido van Rossum2095d241997-04-09 19:41:24 +0000617 a->ob_digit[k] = (digit) q-1;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000618 carry = 0;
619 for (i = 0; i < size_w && i+k < size_v; ++i) {
620 carry += v->ob_digit[i+k] + w->ob_digit[i];
621 v->ob_digit[i+k] = carry & MASK;
622 carry >>= SHIFT;
623 }
624 }
625 } /* for j, k */
626
Guido van Rossumc206c761995-01-10 15:23:19 +0000627 if (a == NULL)
628 *prem = NULL;
629 else {
Guido van Rossumc6913e71991-11-19 20:26:46 +0000630 a = long_normalize(a);
Guido van Rossume32e0141992-01-19 16:31:05 +0000631 *prem = divrem1(v, d, &d);
632 /* d receives the (unused) remainder */
633 if (*prem == NULL) {
634 DECREF(a);
635 a = NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +0000636 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000637 }
638 DECREF(v);
639 DECREF(w);
640 return a;
641}
642
643/* Methods */
644
Guido van Rossume32e0141992-01-19 16:31:05 +0000645/* Forward */
Guido van Rossum8b27d921992-03-27 17:27:05 +0000646static void long_dealloc PROTO((object *));
Guido van Rossum8b27d921992-03-27 17:27:05 +0000647static object *long_repr PROTO((object *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000648static int long_compare PROTO((longobject *, longobject *));
Guido van Rossum9bfef441993-03-29 10:43:31 +0000649static long long_hash PROTO((longobject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000650
651static object *long_add PROTO((longobject *, longobject *));
652static object *long_sub PROTO((longobject *, longobject *));
653static object *long_mul PROTO((longobject *, longobject *));
654static object *long_div PROTO((longobject *, longobject *));
655static object *long_mod PROTO((longobject *, longobject *));
656static object *long_divmod PROTO((longobject *, longobject *));
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000657static object *long_pow PROTO((longobject *, longobject *, longobject *));
Guido van Rossume32e0141992-01-19 16:31:05 +0000658static object *long_neg PROTO((longobject *));
659static object *long_pos PROTO((longobject *));
660static object *long_abs PROTO((longobject *));
661static int long_nonzero PROTO((longobject *));
662static object *long_invert PROTO((longobject *));
663static object *long_lshift PROTO((longobject *, longobject *));
664static object *long_rshift PROTO((longobject *, longobject *));
665static object *long_and PROTO((longobject *, longobject *));
666static object *long_xor PROTO((longobject *, longobject *));
667static object *long_or PROTO((longobject *, longobject *));
668
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000669static void
670long_dealloc(v)
Guido van Rossum8b27d921992-03-27 17:27:05 +0000671 object *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000672{
673 DEL(v);
674}
675
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000676static object *
677long_repr(v)
Guido van Rossum8b27d921992-03-27 17:27:05 +0000678 object *v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000679{
Guido van Rossum8b27d921992-03-27 17:27:05 +0000680 return long_format(v, 10);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000681}
682
683static int
684long_compare(a, b)
685 longobject *a, *b;
686{
687 int sign;
688
Guido van Rossumc6913e71991-11-19 20:26:46 +0000689 if (a->ob_size != b->ob_size) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000690 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
Guido van Rossumc6913e71991-11-19 20:26:46 +0000691 sign = 0;
692 else
693 sign = a->ob_size - b->ob_size;
694 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000695 else {
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000696 int i = ABS(a->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000697 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
698 ;
699 if (i < 0)
700 sign = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000701 else {
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000702 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000703 if (a->ob_size < 0)
704 sign = -sign;
705 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000706 }
Guido van Rossumc6913e71991-11-19 20:26:46 +0000707 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000708}
709
Guido van Rossum9bfef441993-03-29 10:43:31 +0000710static long
711long_hash(v)
712 longobject *v;
713{
714 long x;
715 int i, sign;
716
717 /* This is designed so that Python ints and longs with the
718 same value hash to the same value, otherwise comparisons
719 of mapping keys will turn out weird */
720 i = v->ob_size;
721 sign = 1;
722 x = 0;
723 if (i < 0) {
724 sign = -1;
725 i = -(i);
726 }
727 while (--i >= 0) {
728 /* Force a 32-bit circular shift */
729 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
730 x += v->ob_digit[i];
731 }
732 x = x * sign;
733 if (x == -1)
734 x = -2;
735 return x;
736}
737
738
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000739/* Add the absolute values of two long integers. */
740
741static longobject *x_add PROTO((longobject *, longobject *));
742static longobject *
743x_add(a, b)
744 longobject *a, *b;
745{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000746 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000747 longobject *z;
748 int i;
749 digit carry = 0;
750
751 /* Ensure a is the larger of the two: */
752 if (size_a < size_b) {
753 { longobject *temp = a; a = b; b = temp; }
754 { int size_temp = size_a; size_a = size_b; size_b = size_temp; }
755 }
756 z = alloclongobject(size_a+1);
757 if (z == NULL)
758 return NULL;
759 for (i = 0; i < size_b; ++i) {
760 carry += a->ob_digit[i] + b->ob_digit[i];
761 z->ob_digit[i] = carry & MASK;
762 /* The following assumes unsigned shifts don't
763 propagate the sign bit. */
764 carry >>= SHIFT;
765 }
766 for (; i < size_a; ++i) {
767 carry += a->ob_digit[i];
768 z->ob_digit[i] = carry & MASK;
769 carry >>= SHIFT;
770 }
771 z->ob_digit[i] = carry;
772 return long_normalize(z);
773}
774
775/* Subtract the absolute values of two integers. */
776
777static longobject *x_sub PROTO((longobject *, longobject *));
778static longobject *
779x_sub(a, b)
780 longobject *a, *b;
781{
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000782 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000783 longobject *z;
784 int i;
785 int sign = 1;
786 digit borrow = 0;
787
788 /* Ensure a is the larger of the two: */
789 if (size_a < size_b) {
790 sign = -1;
791 { longobject *temp = a; a = b; b = temp; }
792 { int size_temp = size_a; size_a = size_b; size_b = size_temp; }
793 }
794 else if (size_a == size_b) {
795 /* Find highest digit where a and b differ: */
796 i = size_a;
797 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
798 ;
799 if (i < 0)
800 return alloclongobject(0);
801 if (a->ob_digit[i] < b->ob_digit[i]) {
802 sign = -1;
803 { longobject *temp = a; a = b; b = temp; }
804 }
805 size_a = size_b = i+1;
806 }
807 z = alloclongobject(size_a);
808 if (z == NULL)
809 return NULL;
810 for (i = 0; i < size_b; ++i) {
811 /* The following assumes unsigned arithmetic
812 works module 2**N for some N>SHIFT. */
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000813 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000814 z->ob_digit[i] = borrow & MASK;
815 borrow >>= SHIFT;
816 borrow &= 1; /* Keep only one sign bit */
817 }
818 for (; i < size_a; ++i) {
819 borrow = a->ob_digit[i] - borrow;
820 z->ob_digit[i] = borrow & MASK;
821 borrow >>= SHIFT;
822 }
823 assert(borrow == 0);
Guido van Rossumc6913e71991-11-19 20:26:46 +0000824 if (sign < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000825 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000826 return long_normalize(z);
827}
828
829static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000830long_add(a, b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000831 longobject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000832 longobject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000833{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000834 longobject *z;
835
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000836 if (a->ob_size < 0) {
837 if (b->ob_size < 0) {
838 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +0000839 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000840 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000841 }
842 else
843 z = x_sub(b, a);
844 }
845 else {
846 if (b->ob_size < 0)
847 z = x_sub(a, b);
848 else
849 z = x_add(a, b);
850 }
851 return (object *)z;
852}
853
854static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000855long_sub(a, b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000856 longobject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000857 longobject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000858{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000859 longobject *z;
860
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000861 if (a->ob_size < 0) {
862 if (b->ob_size < 0)
863 z = x_sub(a, b);
864 else
865 z = x_add(a, b);
Guido van Rossumc6913e71991-11-19 20:26:46 +0000866 if (z != NULL && z->ob_size != 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000867 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000868 }
869 else {
870 if (b->ob_size < 0)
871 z = x_add(a, b);
872 else
873 z = x_sub(a, b);
874 }
875 return (object *)z;
876}
877
878static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000879long_mul(a, b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000880 longobject *a;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000881 longobject *b;
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000882{
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000883 int size_a;
884 int size_b;
885 longobject *z;
886 int i;
887
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000888 size_a = ABS(a->ob_size);
889 size_b = ABS(b->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000890 z = alloclongobject(size_a + size_b);
891 if (z == NULL)
892 return NULL;
893 for (i = 0; i < z->ob_size; ++i)
894 z->ob_digit[i] = 0;
895 for (i = 0; i < size_a; ++i) {
896 twodigits carry = 0;
897 twodigits f = a->ob_digit[i];
898 int j;
899
Guido van Rossumeb1fafc1994-08-29 12:47:19 +0000900 SIGCHECK({
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000901 DECREF(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000902 return NULL;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000903 })
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000904 for (j = 0; j < size_b; ++j) {
905 carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
Guido van Rossum2095d241997-04-09 19:41:24 +0000906 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000907 carry >>= SHIFT;
908 }
909 for (; carry != 0; ++j) {
910 assert(i+j < z->ob_size);
911 carry += z->ob_digit[i+j];
Guido van Rossum2095d241997-04-09 19:41:24 +0000912 z->ob_digit[i+j] = (digit) (carry & MASK);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000913 carry >>= SHIFT;
914 }
915 }
916 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000917 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000918 if (b->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +0000919 z->ob_size = -(z->ob_size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000920 return (object *) long_normalize(z);
921}
922
Guido van Rossume32e0141992-01-19 16:31:05 +0000923/* The / and % operators are now defined in terms of divmod().
924 The expression a mod b has the value a - b*floor(a/b).
925 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000926 |a| by |b|, with the sign of a. This is also expressed
927 as a - b*trunc(a/b), if trunc truncates towards zero.
928 Some examples:
929 a b a rem b a mod b
930 13 10 3 3
931 -13 10 -3 7
932 13 -10 3 -7
933 -13 -10 -3 -3
934 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000935 have different signs. We then subtract one from the 'div'
936 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000937
Guido van Rossume32e0141992-01-19 16:31:05 +0000938static int l_divmod PROTO((longobject *, longobject *,
939 longobject **, longobject **));
940static int
941l_divmod(v, w, pdiv, pmod)
942 longobject *v;
943 longobject *w;
944 longobject **pdiv;
945 longobject **pmod;
946{
947 longobject *div, *mod;
948
949 if (long_divrem(v, w, &div, &mod) < 0)
950 return -1;
Guido van Rossum472c04f1996-12-05 21:57:21 +0000951 if ((mod->ob_size < 0 && w->ob_size > 0) ||
952 (mod->ob_size > 0 && w->ob_size < 0)) {
Guido van Rossume32e0141992-01-19 16:31:05 +0000953 longobject *temp;
954 longobject *one;
955 temp = (longobject *) long_add(mod, w);
956 DECREF(mod);
957 mod = temp;
958 if (mod == NULL) {
959 DECREF(div);
960 return -1;
961 }
962 one = (longobject *) newlongobject(1L);
963 if (one == NULL ||
964 (temp = (longobject *) long_sub(div, one)) == NULL) {
965 DECREF(mod);
966 DECREF(div);
Guido van Rossume5372401993-03-16 12:15:04 +0000967 XDECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +0000968 return -1;
969 }
Guido van Rossume5372401993-03-16 12:15:04 +0000970 DECREF(one);
Guido van Rossume32e0141992-01-19 16:31:05 +0000971 DECREF(div);
972 div = temp;
973 }
974 *pdiv = div;
975 *pmod = mod;
976 return 0;
977}
978
979static object *
980long_div(v, w)
981 longobject *v;
982 longobject *w;
983{
984 longobject *div, *mod;
985 if (l_divmod(v, w, &div, &mod) < 0)
986 return NULL;
987 DECREF(mod);
988 return (object *)div;
989}
990
991static object *
992long_mod(v, w)
993 longobject *v;
994 longobject *w;
995{
996 longobject *div, *mod;
997 if (l_divmod(v, w, &div, &mod) < 0)
998 return NULL;
999 DECREF(div);
1000 return (object *)mod;
1001}
1002
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001003static object *
1004long_divmod(v, w)
1005 longobject *v;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001006 longobject *w;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001007{
1008 object *z;
Guido van Rossume32e0141992-01-19 16:31:05 +00001009 longobject *div, *mod;
1010 if (l_divmod(v, w, &div, &mod) < 0)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001011 return NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001012 z = newtupleobject(2);
1013 if (z != NULL) {
1014 settupleitem(z, 0, (object *) div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001015 settupleitem(z, 1, (object *) mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001016 }
1017 else {
1018 DECREF(div);
Guido van Rossume32e0141992-01-19 16:31:05 +00001019 DECREF(mod);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001020 }
1021 return z;
1022}
1023
1024static object *
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001025long_pow(a, b, c)
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001026 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001027 longobject *b;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001028 longobject *c;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001029{
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001030 longobject *z, *div, *mod;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001031 int size_b, i;
1032
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001033 size_b = b->ob_size;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001034 if (size_b < 0) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001035 err_setstr(ValueError, "long integer to the negative power");
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001036 return NULL;
1037 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001038 z = (longobject *)newlongobject(1L);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001039 INCREF(a);
1040 for (i = 0; i < size_b; ++i) {
1041 digit bi = b->ob_digit[i];
1042 int j;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001043
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001044 for (j = 0; j < SHIFT; ++j) {
1045 longobject *temp;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001046
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001047 if (bi & 1) {
Guido van Rossume32e0141992-01-19 16:31:05 +00001048 temp = (longobject *)long_mul(z, a);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001049 DECREF(z);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001050 if ((object*)c!=None && temp!=NULL) {
1051 l_divmod(temp, c, &div, &mod);
1052 XDECREF(div);
1053 DECREF(temp);
1054 temp = mod;
1055 }
1056 z = temp;
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001057 if (z == NULL)
1058 break;
1059 }
1060 bi >>= 1;
1061 if (bi == 0 && i+1 == size_b)
1062 break;
Guido van Rossume32e0141992-01-19 16:31:05 +00001063 temp = (longobject *)long_mul(a, a);
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001064 DECREF(a);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001065 if ((object*)c!=None && temp!=NULL) {
1066 l_divmod(temp, c, &div, &mod);
1067 XDECREF(div);
1068 DECREF(temp);
1069 temp = mod;
1070 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001071 a = temp;
1072 if (a == NULL) {
1073 DECREF(z);
1074 z = NULL;
1075 break;
1076 }
1077 }
Guido van Rossumc206c761995-01-10 15:23:19 +00001078 if (a == NULL || z == NULL)
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001079 break;
1080 }
1081 XDECREF(a);
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001082 if ((object*)c!=None && z!=NULL) {
1083 l_divmod(z, c, &div, &mod);
1084 XDECREF(div);
1085 DECREF(z);
1086 z=mod;
1087 }
Guido van Rossumc7ec9c91991-05-28 21:58:16 +00001088 return (object *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001089}
1090
1091static object *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001092long_invert(v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001093 longobject *v;
1094{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001095 /* Implement ~x as -(x+1) */
1096 longobject *x;
Guido van Rossume32e0141992-01-19 16:31:05 +00001097 longobject *w;
1098 w = (longobject *)newlongobject(1L);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001099 if (w == NULL)
1100 return NULL;
1101 x = (longobject *) long_add(v, w);
1102 DECREF(w);
1103 if (x == NULL)
1104 return NULL;
1105 if (x->ob_size != 0)
1106 x->ob_size = -(x->ob_size);
1107 return (object *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001108}
1109
1110static object *
Guido van Rossumc6913e71991-11-19 20:26:46 +00001111long_pos(v)
1112 longobject *v;
1113{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001114 INCREF(v);
1115 return (object *)v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001116}
1117
1118static object *
1119long_neg(v)
1120 longobject *v;
1121{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001122 longobject *z;
1123 int i, n;
1124 n = ABS(v->ob_size);
1125 if (n == 0) {
1126 /* -0 == 0 */
Guido van Rossumc6913e71991-11-19 20:26:46 +00001127 INCREF(v);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001128 return (object *) v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001129 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001130 z = alloclongobject(ABS(n));
1131 if (z == NULL)
1132 return NULL;
1133 for (i = 0; i < n; i++)
1134 z->ob_digit[i] = v->ob_digit[i];
1135 z->ob_size = -(v->ob_size);
1136 return (object *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001137}
1138
1139static object *
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001140long_abs(v)
1141 longobject *v;
1142{
1143 if (v->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001144 return long_neg(v);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001145 else {
1146 INCREF(v);
1147 return (object *)v;
1148 }
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001149}
1150
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001151static int
1152long_nonzero(v)
1153 longobject *v;
1154{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001155 return ABS(v->ob_size) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001156}
1157
1158static object *
1159long_rshift(a, b)
1160 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001161 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001162{
1163 longobject *z;
1164 long shiftby;
1165 int newsize, wordshift, loshift, hishift, i, j;
1166 digit lomask, himask;
1167
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001168 if (a->ob_size < 0) {
1169 /* Right shifting negative numbers is harder */
1170 longobject *a1, *a2, *a3;
1171 a1 = (longobject *) long_invert(a);
1172 if (a1 == NULL) return NULL;
1173 a2 = (longobject *) long_rshift(a1, b);
1174 DECREF(a1);
1175 if (a2 == NULL) return NULL;
1176 a3 = (longobject *) long_invert(a2);
1177 DECREF(a2);
1178 return (object *) a3;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001179 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001180
1181 shiftby = getlongvalue((object *)b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001182 if (shiftby == -1L && err_occurred())
1183 return NULL;
1184 if (shiftby < 0) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001185 err_setstr(ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001186 return NULL;
1187 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001188 wordshift = shiftby / SHIFT;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001189 newsize = ABS(a->ob_size) - wordshift;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001190 if (newsize <= 0) {
1191 z = alloclongobject(0);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001192 return (object *)z;
1193 }
1194 loshift = shiftby % SHIFT;
1195 hishift = SHIFT - loshift;
1196 lomask = ((digit)1 << hishift) - 1;
1197 himask = MASK ^ lomask;
1198 z = alloclongobject(newsize);
1199 if (z == NULL)
1200 return NULL;
1201 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001202 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001203 for (i = 0, j = wordshift; i < newsize; i++, j++) {
1204 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
1205 if (i+1 < newsize)
1206 z->ob_digit[i] |=
1207 (a->ob_digit[j+1] << hishift) & himask;
1208 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001209 return (object *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001210}
1211
1212static object *
1213long_lshift(a, b)
1214 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001215 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001216{
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001217 /* This version due to Tim Peters */
Guido van Rossumc6913e71991-11-19 20:26:46 +00001218 longobject *z;
1219 long shiftby;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001220 int oldsize, newsize, wordshift, remshift, i, j;
1221 twodigits accum;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001222
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001223 shiftby = getlongvalue((object *)b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001224 if (shiftby == -1L && err_occurred())
1225 return NULL;
1226 if (shiftby < 0) {
Guido van Rossum87e7ea71991-12-10 14:00:03 +00001227 err_setstr(ValueError, "negative shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001228 return NULL;
1229 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001230 if ((long)(int)shiftby != shiftby) {
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001231 err_setstr(ValueError, "outrageous left shift count");
Guido van Rossumc6913e71991-11-19 20:26:46 +00001232 return NULL;
1233 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001234 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1235 wordshift = (int)shiftby / SHIFT;
1236 remshift = (int)shiftby - wordshift * SHIFT;
1237
1238 oldsize = ABS(a->ob_size);
1239 newsize = oldsize + wordshift;
1240 if (remshift)
1241 ++newsize;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001242 z = alloclongobject(newsize);
1243 if (z == NULL)
1244 return NULL;
1245 if (a->ob_size < 0)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001246 z->ob_size = -(z->ob_size);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001247 for (i = 0; i < wordshift; i++)
1248 z->ob_digit[i] = 0;
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001249 accum = 0;
1250 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
1251 accum |= a->ob_digit[j] << remshift;
1252 z->ob_digit[i] = (digit)(accum & MASK);
1253 accum >>= SHIFT;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001254 }
Guido van Rossumf2e499b1997-03-16 00:37:59 +00001255 if (remshift)
1256 z->ob_digit[newsize-1] = (digit)accum;
1257 else
1258 assert(!accum);
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001259 return (object *) long_normalize(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001260}
1261
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001262
1263/* Bitwise and/xor/or operations */
1264
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001265#define MAX(x, y) ((x) < (y) ? (y) : (x))
1266#define MIN(x, y) ((x) > (y) ? (y) : (x))
1267
Guido van Rossume32e0141992-01-19 16:31:05 +00001268static object *long_bitwise PROTO((longobject *, int, longobject *));
1269static object *
1270long_bitwise(a, op, b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001271 longobject *a;
1272 int op; /* '&', '|', '^' */
1273 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001274{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001275 digit maska, maskb; /* 0 or MASK */
1276 int negz;
1277 int size_a, size_b, size_z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001278 longobject *z;
1279 int i;
Guido van Rossum8b27d921992-03-27 17:27:05 +00001280 digit diga, digb;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001281 object *v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001282
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001283 if (a->ob_size < 0) {
1284 a = (longobject *) long_invert(a);
1285 maska = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001286 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001287 else {
1288 INCREF(a);
1289 maska = 0;
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001290 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001291 if (b->ob_size < 0) {
1292 b = (longobject *) long_invert(b);
1293 maskb = MASK;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001294 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001295 else {
1296 INCREF(b);
1297 maskb = 0;
1298 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001299
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001300 size_a = a->ob_size;
1301 size_b = b->ob_size;
1302 size_z = MAX(size_a, size_b);
1303 z = alloclongobject(size_z);
1304 if (a == NULL || b == NULL || z == NULL) {
1305 XDECREF(a);
1306 XDECREF(b);
1307 XDECREF(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001308 return NULL;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001309 }
Guido van Rossumc6913e71991-11-19 20:26:46 +00001310
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001311 negz = 0;
1312 switch (op) {
1313 case '^':
1314 if (maska != maskb) {
1315 maska ^= MASK;
1316 negz = -1;
1317 }
1318 break;
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001319 case '&':
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001320 if (maska && maskb) {
1321 op = '|';
1322 maska ^= MASK;
1323 maskb ^= MASK;
1324 negz = -1;
1325 }
1326 break;
1327 case '|':
1328 if (maska || maskb) {
1329 op = '&';
1330 maska ^= MASK;
1331 maskb ^= MASK;
1332 negz = -1;
1333 }
1334 break;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001335 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001336
1337 for (i = 0; i < size_z; ++i) {
1338 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
1339 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
1340 switch (op) {
1341 case '&': z->ob_digit[i] = diga & digb; break;
1342 case '|': z->ob_digit[i] = diga | digb; break;
1343 case '^': z->ob_digit[i] = diga ^ digb; break;
1344 }
Guido van Rossumafbb8db1991-12-31 13:14:13 +00001345 }
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001346
1347 DECREF(a);
1348 DECREF(b);
1349 z = long_normalize(z);
1350 if (negz == 0)
1351 return (object *) z;
1352 v = long_invert(z);
1353 DECREF(z);
1354 return v;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001355}
1356
Guido van Rossumc6913e71991-11-19 20:26:46 +00001357static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001358long_and(a, b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001359 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001360 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001361{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001362 return long_bitwise(a, '&', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001363}
1364
1365static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001366long_xor(a, b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001367 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001368 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001369{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001370 return long_bitwise(a, '^', b);
Guido van Rossumc6913e71991-11-19 20:26:46 +00001371}
1372
1373static object *
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001374long_or(a, b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00001375 longobject *a;
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001376 longobject *b;
Guido van Rossumc6913e71991-11-19 20:26:46 +00001377{
Guido van Rossum4c260ff1992-01-14 18:36:43 +00001378 return long_bitwise(a, '|', b);
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00001379}
1380
Guido van Rossum234f9421993-06-17 12:35:49 +00001381static int
Guido van Rossume6eefc21992-08-14 12:06:52 +00001382long_coerce(pv, pw)
1383 object **pv;
1384 object **pw;
1385{
1386 if (is_intobject(*pw)) {
1387 *pw = newlongobject(getintvalue(*pw));
1388 INCREF(*pv);
1389 return 0;
1390 }
1391 return 1; /* Can't do it */
1392}
1393
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001394static object *
1395long_int(v)
1396 object *v;
1397{
1398 long x;
1399 x = getlongvalue(v);
1400 if (err_occurred())
1401 return NULL;
1402 return newintobject(x);
1403}
1404
1405static object *
1406long_long(v)
1407 object *v;
1408{
1409 INCREF(v);
1410 return v;
1411}
1412
1413static object *
1414long_float(v)
1415 object *v;
1416{
Guido van Rossum09e6ad01997-02-14 22:54:21 +00001417 double result;
1418 PyFPE_START_PROTECT("long_float", return 0)
1419 result = dgetlongvalue(v);
Guido van Rossum45b83911997-03-14 04:32:50 +00001420 PyFPE_END_PROTECT(result)
Guido van Rossum09e6ad01997-02-14 22:54:21 +00001421 return newfloatobject(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001422}
1423
1424static object *
1425long_oct(v)
1426 object *v;
1427{
1428 return long_format(v, 8);
1429}
1430
1431static object *
1432long_hex(v)
1433 object *v;
1434{
1435 return long_format(v, 16);
1436}
1437
1438
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001439#define UF (unaryfunc)
1440#define BF (binaryfunc)
1441#define TF (ternaryfunc)
1442#define IF (inquiry)
Guido van Rossum8b27d921992-03-27 17:27:05 +00001443
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001444static number_methods long_as_number = {
Guido van Rossum8b27d921992-03-27 17:27:05 +00001445 BF long_add, /*nb_add*/
1446 BF long_sub, /*nb_subtract*/
1447 BF long_mul, /*nb_multiply*/
1448 BF long_div, /*nb_divide*/
1449 BF long_mod, /*nb_remainder*/
1450 BF long_divmod, /*nb_divmod*/
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001451 TF long_pow, /*nb_power*/
Guido van Rossum8b27d921992-03-27 17:27:05 +00001452 UF long_neg, /*nb_negative*/
1453 UF long_pos, /*tp_positive*/
1454 UF long_abs, /*tp_absolute*/
1455 IF long_nonzero,/*tp_nonzero*/
1456 UF long_invert, /*nb_invert*/
1457 BF long_lshift, /*nb_lshift*/
1458 BF long_rshift, /*nb_rshift*/
1459 BF long_and, /*nb_and*/
1460 BF long_xor, /*nb_xor*/
1461 BF long_or, /*nb_or*/
Guido van Rossume6eefc21992-08-14 12:06:52 +00001462 (int (*) FPROTO((object **, object **)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001463 (coercion)long_coerce, /*nb_coerce*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001464 UF long_int, /*nb_int*/
1465 UF long_long, /*nb_long*/
1466 UF long_float, /*nb_float*/
1467 UF long_oct, /*nb_oct*/
1468 UF long_hex, /*nb_hex*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001469};
1470
1471typeobject Longtype = {
1472 OB_HEAD_INIT(&Typetype)
1473 0,
1474 "long int",
1475 sizeof(longobject) - sizeof(digit),
1476 sizeof(digit),
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001477 (destructor)long_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001478 0, /*tp_print*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001479 0, /*tp_getattr*/
1480 0, /*tp_setattr*/
Guido van Rossum8b27d921992-03-27 17:27:05 +00001481 (int (*) FPROTO((object *, object *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001482 (cmpfunc)long_compare, /*tp_compare*/
1483 (reprfunc)long_repr, /*tp_repr*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001484 &long_as_number,/*tp_as_number*/
1485 0, /*tp_as_sequence*/
1486 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001487 (long (*) FPROTO((object *)))
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00001488 (hashfunc)long_hash, /*tp_hash*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001489};