blob: e1ad157999448c0760222e2ae0d2da6cda5ef0c2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +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 Rossumf70e43a1991-02-19 12:39:46 +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 Rossumf70e43a1991-02-19 12:39:46 +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 Rossumf70e43a1991-02-19 12:39:46 +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 Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Integer object implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossume5372401993-03-16 12:15:04 +000035#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossumbf8c0e31994-08-29 12:48:32 +000037#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +000038#include <limits.h>
39#endif
40
41#ifndef LONG_MAX
42#define LONG_MAX 0X7FFFFFFFL
43#endif
44
45#ifndef LONG_MIN
46#define LONG_MIN (-LONG_MAX-1)
47#endif
48
49#ifndef CHAR_BIT
50#define CHAR_BIT 8
51#endif
52
Guido van Rossumb376a4a1993-11-23 17:53:17 +000053#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000054#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000055#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000056
Guido van Rossum2e1d4331993-12-24 10:22:45 +000057long
58getmaxint()
59{
60 return LONG_MAX; /* To initialize sys.maxint */
61}
62
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000064
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065intobject FalseObject = {
66 OB_HEAD_INIT(&Inttype)
67 0
68};
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070intobject TrueObject = {
71 OB_HEAD_INIT(&Inttype)
72 1
73};
74
Guido van Rossum165e67e1990-10-14 20:02:26 +000075static object *
Guido van Rossum3a628451991-12-10 13:57:36 +000076err_ovf(msg)
77 char *msg;
Guido van Rossum165e67e1990-10-14 20:02:26 +000078{
Guido van Rossum3a628451991-12-10 13:57:36 +000079 err_setstr(OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000080 return NULL;
81}
82
Guido van Rossum3f5da241990-12-20 15:06:42 +000083/* Integers are quite normal objects, to make object handling uniform.
84 (Using odd pointers to represent integers would save much space
85 but require extra checks for this special case throughout the code.)
86 Since, a typical Python program spends much of its time allocating
87 and deallocating integers, these operations should be very fast.
88 Therefore we use a dedicated allocation scheme with a much lower
89 overhead (in space and time) than straight malloc(): a simple
90 dedicated free list, filled when necessary with memory from malloc().
91*/
92
93#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
94#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject))
95
96static intobject *
97fill_free_list()
98{
99 intobject *p, *q;
100 p = NEW(intobject, N_INTOBJECTS);
101 if (p == NULL)
102 return (intobject *)err_nomem();
103 q = p + N_INTOBJECTS;
104 while (--q > p)
105 *(intobject **)q = q-1;
106 *(intobject **)q = NULL;
107 return p + N_INTOBJECTS - 1;
108}
109
110static intobject *free_list = NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000111#ifndef NSMALLPOSINTS
112#define NSMALLPOSINTS 100
113#endif
114#ifndef NSMALLNEGINTS
115#define NSMALLNEGINTS 1
116#endif
117#if NSMALLNEGINTS + NSMALLPOSINTS > 0
118/* References to small integers are saved in this array so that they
119 can be shared.
120 The integers that are saved are those in the range
121 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
122*/
123static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
124#endif
125#ifdef COUNT_ALLOCS
126int quick_int_allocs, quick_neg_int_allocs;
127#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129object *
130newintobject(ival)
131 long ival;
132{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133 register intobject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000134#if NSMALLNEGINTS + NSMALLPOSINTS > 0
135 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
136 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
137 INCREF(v);
138#ifdef COUNT_ALLOCS
139 if (ival >= 0)
140 quick_int_allocs++;
141 else
142 quick_neg_int_allocs++;
143#endif
144 return (object *) v;
145 }
146#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 if (free_list == NULL) {
148 if ((free_list = fill_free_list()) == NULL)
149 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000151 v = free_list;
152 free_list = *(intobject **)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000153 v->ob_type = &Inttype;
154 v->ob_ival = ival;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000155 NEWREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000156#if NSMALLNEGINTS + NSMALLPOSINTS > 0
157 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
158 /* save this one for a following allocation */
159 INCREF(v);
160 small_ints[ival + NSMALLNEGINTS] = v;
161 }
162#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163 return (object *) v;
164}
165
166static void
167int_dealloc(v)
168 intobject *v;
169{
170 *(intobject **)v = free_list;
171 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
174long
175getintvalue(op)
176 register object *op;
177{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000178 number_methods *nb;
179 intobject *io;
180 long val;
181
182 if (op && is_intobject(op))
183 return GETINTVALUE((intobject*) op);
184
185 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
186 nb->nb_int == NULL) {
187 err_badarg();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 return -1;
189 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000190
191 io = (intobject*) (*nb->nb_int) (op);
192 if (io == NULL)
193 return -1;
194 if (!is_intobject(io)) {
195 err_setstr(TypeError, "nb_int should return int object");
196 return -1;
197 }
198
199 val = GETINTVALUE(io);
200 DECREF(io);
201
202 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
205/* Methods */
206
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000207/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000208static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000209int_print(v, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 intobject *v;
211 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000212 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
214 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000215 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
218static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219int_repr(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 intobject *v;
221{
222 char buf[20];
223 sprintf(buf, "%ld", v->ob_ival);
224 return newstringobject(buf);
225}
226
227static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228int_compare(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 intobject *v, *w;
230{
231 register long i = v->ob_ival;
232 register long j = w->ob_ival;
233 return (i < j) ? -1 : (i > j) ? 1 : 0;
234}
235
Guido van Rossum9bfef441993-03-29 10:43:31 +0000236static long
237int_hash(v)
238 intobject *v;
239{
240 long x = v -> ob_ival;
241 if (x == -1)
242 x = -2;
243 return x;
244}
245
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247int_add(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000249 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250{
251 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000253 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000255 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000256 return err_ovf("integer addition");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 return newintobject(x);
258}
259
260static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261int_sub(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000263 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264{
265 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000267 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000269 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000270 return err_ovf("integer subtraction");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 return newintobject(x);
272}
273
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000274/*
275Integer overflow checking used to be done using a double, but on 64
276bit machines (where both long and double are 64 bit) this fails
277because the double doesn't have enouvg precision. John Tromp suggests
278the following algorithm:
279
280Suppose again we normalize a and b to be nonnegative.
281Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
282Now we test ah and bh against zero and get essentially 3 possible outcomes.
283
2841) both ah and bh > 0 : then report overflow
285
2862) both ah and bh = 0 : then compute a*b and report overflow if it comes out
287 negative
288
2893) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
290 compute al*bl and report overflow if it's negative
291 add (ah*bl)<<32 to al*bl and report overflow if
292 it's negative
293
294In case of no overflow the result is then negated if necessary.
295
296The majority of cases will be 2), in which case this method is the same as
297what I suggested before. If multiplication is expensive enough, then the
298other method is faster on case 3), but also more work to program, so I
299guess the above is the preferred solution.
300
301*/
302
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000304int_mul(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000306 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000308 long a, b, ah, bh, x, y;
309 int s = 1;
310
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000312 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000313 ah = a >> (LONG_BIT/2);
314 bh = b >> (LONG_BIT/2);
315
316 /* Quick test for common case: two small positive ints */
317
318 if (ah == 0 && bh == 0) {
319 x = a*b;
320 if (x < 0)
321 goto bad;
322 return newintobject(x);
323 }
324
325 /* Arrange that a >= b >= 0 */
326
327 if (a < 0) {
328 a = -a;
329 if (a < 0) {
330 /* Largest negative */
331 if (b == 0 || b == 1) {
332 x = a*b;
333 goto ok;
334 }
335 else
336 goto bad;
337 }
338 s = -s;
339 ah = a >> (LONG_BIT/2);
340 }
341 if (b < 0) {
342 b = -b;
343 if (b < 0) {
344 /* Largest negative */
345 if (a == 0 || a == 1 && s == 1) {
346 x = a*b;
347 goto ok;
348 }
349 else
350 goto bad;
351 }
352 s = -s;
353 bh = b >> (LONG_BIT/2);
354 }
355
356 /* 1) both ah and bh > 0 : then report overflow */
357
358 if (ah != 0 && bh != 0)
359 goto bad;
360
361 /* 2) both ah and bh = 0 : then compute a*b and report
362 overflow if it comes out negative */
363
364 if (ah == 0 && bh == 0) {
365 x = a*b;
366 if (x < 0)
367 goto bad;
368 return newintobject(x*s);
369 }
370
371 if (a < b) {
372 /* Swap */
373 x = a;
374 a = b;
375 b = x;
376 ah = bh;
377 /* bh not used beyond this point */
378 }
379
380 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
381 it's >= 2^31
382 compute al*bl and report overflow if it's negative
383 add (ah*bl)<<32 to al*bl and report overflow if
384 it's negative
385 (NB b == bl in this case, and we make a = al) */
386
387 y = ah*b;
388 if (y >= (1L << (LONG_BIT/2)))
389 goto bad;
390 a &= (1L << (LONG_BIT/2)) - 1;
391 x = a*b;
392 if (x < 0)
393 goto bad;
394 x += y << LONG_BIT/2;
395 if (x < 0)
396 goto bad;
397 ok:
398 return newintobject(x * s);
399
400 bad:
401 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402}
403
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000404static int
405i_divmod(x, y, p_xdivy, p_xmody)
406 register intobject *x, *y;
407 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000409 long xi = x->ob_ival;
410 long yi = y->ob_ival;
411 long xdivy, xmody;
412
413 if (yi == 0) {
414 err_setstr(ZeroDivisionError, "integer division or modulo");
415 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 }
Guido van Rossum00466951991-05-05 20:08:27 +0000417 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000418 if (xi < 0)
419 xdivy = -xi / -yi;
420 else
421 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000422 }
423 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000424 if (xi < 0)
425 xdivy = - (-xi / yi);
426 else
427 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000428 }
429 xmody = xi - xdivy*yi;
430 if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
431 xmody += yi;
432 xdivy -= 1;
433 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000434 *p_xdivy = xdivy;
435 *p_xmody = xmody;
436 return 0;
437}
438
439static object *
440int_div(x, y)
441 intobject *x;
442 intobject *y;
443{
444 long d, m;
445 if (i_divmod(x, y, &d, &m) < 0)
446 return NULL;
447 return newintobject(d);
448}
449
450static object *
451int_mod(x, y)
452 intobject *x;
453 intobject *y;
454{
455 long d, m;
456 if (i_divmod(x, y, &d, &m) < 0)
457 return NULL;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000458 return newintobject(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000459}
460
461static object *
462int_divmod(x, y)
463 intobject *x;
464 intobject *y;
465{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 long d, m;
467 if (i_divmod(x, y, &d, &m) < 0)
468 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000469 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000470}
471
472static object *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000473int_pow(v, w, z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475 intobject *w;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000476 intobject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000478#if 1
479 register long iv, iw, iz, ix, temp, prev;
480 int zset = 0;
481 iv = v->ob_ival;
482 iw = w->ob_ival;
483 if (iw < 0) {
484 err_setstr(ValueError, "integer to the negative power");
485 return NULL;
486 }
487 if ((object *)z != None) {
488 iz = z->ob_ival;
489 zset = 1;
490 }
491 /*
492 * XXX: The original exponentiation code stopped looping
493 * when temp hit zero; this code will continue onwards
494 * unnecessarily, but at least it won't cause any errors.
495 * Hopefully the speed improvement from the fast exponentiation
496 * will compensate for the slight inefficiency.
497 * XXX: Better handling of overflows is desperately needed.
498 */
499 temp = iv;
500 ix = 1;
501 while (iw > 0) {
502 prev = ix; /* Save value for overflow check */
503 if (iw & 1) {
504 ix = ix*temp;
505 if (temp == 0)
506 break; /* Avoid ix / 0 */
507 if (ix / temp != prev)
508 return err_ovf("integer pow()");
509 }
510 iw >>= 1; /* Shift exponent down by 1 bit */
511 if (iw==0) break;
512 prev = temp;
513 temp *= temp; /* Square the value of temp */
514 if (prev!=0 && temp/prev!=prev)
515 return err_ovf("integer pow()");
516 if (zset) {
517 /* If we did a multiplication, perform a modulo */
518 ix = ix % iz;
519 temp = temp % iz;
520 }
521 }
522 if (zset) {
523 object *t1, *t2;
524 long int div, mod;
525 t1=newintobject(ix);
526 t2=newintobject(iz);
527 if (t1==NULL || t2==NULL ||
528 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
529 XDECREF(t1);
530 XDECREF(t2);
531 return(NULL);
532 }
Guido van Rossumc206c761995-01-10 15:23:19 +0000533 DECREF(t1);
534 DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000535 ix=mod;
536 }
537 return newintobject(ix);
538#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000541 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000542 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000543 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000544 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000546 if ((object *)z != None) {
547 err_setstr(TypeError, "pow(int, int, int) not yet supported");
548 return NULL;
549 }
Guido van Rossum00466951991-05-05 20:08:27 +0000550 ix = 1;
551 while (--iw >= 0) {
552 long prev = ix;
553 ix = ix * iv;
554 if (iv == 0)
555 break; /* 0 to some power -- avoid ix / 0 */
556 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000557 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000558 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000560#endif
561}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562
563static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000564int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 intobject *v;
566{
567 register long a, x;
568 a = v->ob_ival;
569 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000570 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000571 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572 return newintobject(x);
573}
574
575static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000576int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577 intobject *v;
578{
579 INCREF(v);
580 return (object *)v;
581}
582
Guido van Rossum00466951991-05-05 20:08:27 +0000583static object *
584int_abs(v)
585 intobject *v;
586{
587 if (v->ob_ival >= 0)
588 return int_pos(v);
589 else
590 return int_neg(v);
591}
592
Guido van Rossum0bff0151991-05-14 12:05:32 +0000593static int
594int_nonzero(v)
595 intobject *v;
596{
597 return v->ob_ival != 0;
598}
599
Guido van Rossum7928cd71991-10-24 14:59:31 +0000600static object *
601int_invert(v)
602 intobject *v;
603{
604 return newintobject(~v->ob_ival);
605}
606
607static object *
608int_lshift(v, w)
609 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000610 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000611{
612 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000613 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000614 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000615 if (b < 0) {
616 err_setstr(ValueError, "negative shift count");
617 return NULL;
618 }
619 if (a == 0 || b == 0) {
620 INCREF(v);
621 return (object *) v;
622 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000623 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000624 return newintobject(0L);
625 }
626 a = (unsigned long)a << b;
627 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000628}
629
630static object *
631int_rshift(v, w)
632 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000633 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000634{
635 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000636 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000637 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000638 if (b < 0) {
639 err_setstr(ValueError, "negative shift count");
640 return NULL;
641 }
642 if (a == 0 || b == 0) {
643 INCREF(v);
644 return (object *) v;
645 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000646 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000647 if (a < 0)
648 a = -1;
649 else
650 a = 0;
651 }
652 else {
653 if (a < 0)
654 a = ~( ~(unsigned long)a >> b );
655 else
656 a = (unsigned long)a >> b;
657 }
658 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000659}
660
661static object *
662int_and(v, w)
663 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000664 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000665{
666 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000669 return newintobject(a & b);
670}
671
672static object *
673int_xor(v, w)
674 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000675 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000676{
677 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000678 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000679 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000680 return newintobject(a ^ b);
681}
682
683static object *
684int_or(v, w)
685 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000686 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000687{
688 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000689 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000690 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691 return newintobject(a | b);
692}
693
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000694static object *
695int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000696 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000697{
698 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000699 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000700}
701
702static object *
703int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000704 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000705{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000706 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000707}
708
709static object *
710int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000711 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000712{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000713 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000714}
715
716static object *
717int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000718 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000719{
720 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000721 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000722 if (x == 0)
723 strcpy(buf, "0");
724 else if (x > 0)
725 sprintf(buf, "0%lo", x);
726 else
727 sprintf(buf, "-0%lo", -x);
728 return newstringobject(buf);
729}
730
731static object *
732int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000733 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000734{
735 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000736 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000737 if (x >= 0)
738 sprintf(buf, "0x%lx", x);
739 else
740 sprintf(buf, "-0x%lx", -x);
741 return newstringobject(buf);
742}
743
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000745 (binaryfunc)int_add, /*nb_add*/
746 (binaryfunc)int_sub, /*nb_subtract*/
747 (binaryfunc)int_mul, /*nb_multiply*/
748 (binaryfunc)int_div, /*nb_divide*/
749 (binaryfunc)int_mod, /*nb_remainder*/
750 (binaryfunc)int_divmod, /*nb_divmod*/
751 (ternaryfunc)int_pow, /*nb_power*/
752 (unaryfunc)int_neg, /*nb_negative*/
753 (unaryfunc)int_pos, /*nb_positive*/
754 (unaryfunc)int_abs, /*nb_absolute*/
755 (inquiry)int_nonzero, /*nb_nonzero*/
756 (unaryfunc)int_invert, /*nb_invert*/
757 (binaryfunc)int_lshift, /*nb_lshift*/
758 (binaryfunc)int_rshift, /*nb_rshift*/
759 (binaryfunc)int_and, /*nb_and*/
760 (binaryfunc)int_xor, /*nb_xor*/
761 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000762 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000763 (unaryfunc)int_int, /*nb_int*/
764 (unaryfunc)int_long, /*nb_long*/
765 (unaryfunc)int_float, /*nb_float*/
766 (unaryfunc)int_oct, /*nb_oct*/
767 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768};
769
770typeobject Inttype = {
771 OB_HEAD_INIT(&Typetype)
772 0,
773 "int",
774 sizeof(intobject),
775 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000776 (destructor)int_dealloc, /*tp_dealloc*/
777 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778 0, /*tp_getattr*/
779 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000780 (cmpfunc)int_compare, /*tp_compare*/
781 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782 &int_as_number, /*tp_as_number*/
783 0, /*tp_as_sequence*/
784 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000785 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786};