blob: 7a4708e5f805d757eb63c7d83f6776f023b7d2d6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbf8c0e31994-08-29 12:48:32 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossume5372401993-03-16 12:15:04 +00003Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Integer object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossume5372401993-03-16 12:15:04 +000028#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossumbf8c0e31994-08-29 12:48:32 +000030#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +000031#include <limits.h>
32#endif
33
34#ifndef LONG_MAX
35#define LONG_MAX 0X7FFFFFFFL
36#endif
37
38#ifndef LONG_MIN
39#define LONG_MIN (-LONG_MAX-1)
40#endif
41
42#ifndef CHAR_BIT
43#define CHAR_BIT 8
44#endif
45
Guido van Rossumb376a4a1993-11-23 17:53:17 +000046#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000047#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000048#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000049
Guido van Rossum2e1d4331993-12-24 10:22:45 +000050long
51getmaxint()
52{
53 return LONG_MAX; /* To initialize sys.maxint */
54}
55
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000057
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058intobject FalseObject = {
59 OB_HEAD_INIT(&Inttype)
60 0
61};
Guido van Rossum3f5da241990-12-20 15:06:42 +000062
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063intobject TrueObject = {
64 OB_HEAD_INIT(&Inttype)
65 1
66};
67
Guido van Rossum165e67e1990-10-14 20:02:26 +000068static object *
Guido van Rossum3a628451991-12-10 13:57:36 +000069err_ovf(msg)
70 char *msg;
Guido van Rossum165e67e1990-10-14 20:02:26 +000071{
Guido van Rossum3a628451991-12-10 13:57:36 +000072 err_setstr(OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000073 return NULL;
74}
75
Guido van Rossum3f5da241990-12-20 15:06:42 +000076/* Integers are quite normal objects, to make object handling uniform.
77 (Using odd pointers to represent integers would save much space
78 but require extra checks for this special case throughout the code.)
79 Since, a typical Python program spends much of its time allocating
80 and deallocating integers, these operations should be very fast.
81 Therefore we use a dedicated allocation scheme with a much lower
82 overhead (in space and time) than straight malloc(): a simple
83 dedicated free list, filled when necessary with memory from malloc().
84*/
85
86#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
87#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject))
88
89static intobject *
90fill_free_list()
91{
92 intobject *p, *q;
93 p = NEW(intobject, N_INTOBJECTS);
94 if (p == NULL)
95 return (intobject *)err_nomem();
96 q = p + N_INTOBJECTS;
97 while (--q > p)
98 *(intobject **)q = q-1;
99 *(intobject **)q = NULL;
100 return p + N_INTOBJECTS - 1;
101}
102
103static intobject *free_list = NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000104#ifndef NSMALLPOSINTS
105#define NSMALLPOSINTS 100
106#endif
107#ifndef NSMALLNEGINTS
108#define NSMALLNEGINTS 1
109#endif
110#if NSMALLNEGINTS + NSMALLPOSINTS > 0
111/* References to small integers are saved in this array so that they
112 can be shared.
113 The integers that are saved are those in the range
114 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
115*/
116static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
117#endif
118#ifdef COUNT_ALLOCS
119int quick_int_allocs, quick_neg_int_allocs;
120#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000121
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122object *
123newintobject(ival)
124 long ival;
125{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126 register intobject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000127#if NSMALLNEGINTS + NSMALLPOSINTS > 0
128 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
129 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
130 INCREF(v);
131#ifdef COUNT_ALLOCS
132 if (ival >= 0)
133 quick_int_allocs++;
134 else
135 quick_neg_int_allocs++;
136#endif
137 return (object *) v;
138 }
139#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000140 if (free_list == NULL) {
141 if ((free_list = fill_free_list()) == NULL)
142 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144 v = free_list;
145 free_list = *(intobject **)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146 v->ob_type = &Inttype;
147 v->ob_ival = ival;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000148 NEWREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000149#if NSMALLNEGINTS + NSMALLPOSINTS > 0
150 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
151 /* save this one for a following allocation */
152 INCREF(v);
153 small_ints[ival + NSMALLNEGINTS] = v;
154 }
155#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000156 return (object *) v;
157}
158
159static void
160int_dealloc(v)
161 intobject *v;
162{
163 *(intobject **)v = free_list;
164 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
167long
168getintvalue(op)
169 register object *op;
170{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000171 number_methods *nb;
172 intobject *io;
173 long val;
174
175 if (op && is_intobject(op))
176 return GETINTVALUE((intobject*) op);
177
178 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
179 nb->nb_int == NULL) {
180 err_badarg();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 return -1;
182 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000183
184 io = (intobject*) (*nb->nb_int) (op);
185 if (io == NULL)
186 return -1;
187 if (!is_intobject(io)) {
188 err_setstr(TypeError, "nb_int should return int object");
189 return -1;
190 }
191
192 val = GETINTVALUE(io);
193 DECREF(io);
194
195 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196}
197
198/* Methods */
199
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000200/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000201static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000202int_print(v, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203 intobject *v;
204 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000205 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206{
207 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000208 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
211static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000212int_repr(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 intobject *v;
214{
215 char buf[20];
216 sprintf(buf, "%ld", v->ob_ival);
217 return newstringobject(buf);
218}
219
220static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000221int_compare(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 intobject *v, *w;
223{
224 register long i = v->ob_ival;
225 register long j = w->ob_ival;
226 return (i < j) ? -1 : (i > j) ? 1 : 0;
227}
228
Guido van Rossum9bfef441993-03-29 10:43:31 +0000229static long
230int_hash(v)
231 intobject *v;
232{
233 long x = v -> ob_ival;
234 if (x == -1)
235 x = -2;
236 return x;
237}
238
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240int_add(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000242 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243{
244 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000246 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000248 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000249 return err_ovf("integer addition");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 return newintobject(x);
251}
252
253static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000254int_sub(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000256 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257{
258 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000260 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000262 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000263 return err_ovf("integer subtraction");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 return newintobject(x);
265}
266
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000267/*
268Integer overflow checking used to be done using a double, but on 64
269bit machines (where both long and double are 64 bit) this fails
270because the double doesn't have enouvg precision. John Tromp suggests
271the following algorithm:
272
273Suppose again we normalize a and b to be nonnegative.
274Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
275Now we test ah and bh against zero and get essentially 3 possible outcomes.
276
2771) both ah and bh > 0 : then report overflow
278
2792) both ah and bh = 0 : then compute a*b and report overflow if it comes out
280 negative
281
2823) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
283 compute al*bl and report overflow if it's negative
284 add (ah*bl)<<32 to al*bl and report overflow if
285 it's negative
286
287In case of no overflow the result is then negated if necessary.
288
289The majority of cases will be 2), in which case this method is the same as
290what I suggested before. If multiplication is expensive enough, then the
291other method is faster on case 3), but also more work to program, so I
292guess the above is the preferred solution.
293
294*/
295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000297int_mul(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000299 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000301 long a, b, ah, bh, x, y;
302 int s = 1;
303
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000305 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000306 ah = a >> (LONG_BIT/2);
307 bh = b >> (LONG_BIT/2);
308
309 /* Quick test for common case: two small positive ints */
310
311 if (ah == 0 && bh == 0) {
312 x = a*b;
313 if (x < 0)
314 goto bad;
315 return newintobject(x);
316 }
317
318 /* Arrange that a >= b >= 0 */
319
320 if (a < 0) {
321 a = -a;
322 if (a < 0) {
323 /* Largest negative */
324 if (b == 0 || b == 1) {
325 x = a*b;
326 goto ok;
327 }
328 else
329 goto bad;
330 }
331 s = -s;
332 ah = a >> (LONG_BIT/2);
333 }
334 if (b < 0) {
335 b = -b;
336 if (b < 0) {
337 /* Largest negative */
338 if (a == 0 || a == 1 && s == 1) {
339 x = a*b;
340 goto ok;
341 }
342 else
343 goto bad;
344 }
345 s = -s;
346 bh = b >> (LONG_BIT/2);
347 }
348
349 /* 1) both ah and bh > 0 : then report overflow */
350
351 if (ah != 0 && bh != 0)
352 goto bad;
353
354 /* 2) both ah and bh = 0 : then compute a*b and report
355 overflow if it comes out negative */
356
357 if (ah == 0 && bh == 0) {
358 x = a*b;
359 if (x < 0)
360 goto bad;
361 return newintobject(x*s);
362 }
363
364 if (a < b) {
365 /* Swap */
366 x = a;
367 a = b;
368 b = x;
369 ah = bh;
370 /* bh not used beyond this point */
371 }
372
373 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
374 it's >= 2^31
375 compute al*bl and report overflow if it's negative
376 add (ah*bl)<<32 to al*bl and report overflow if
377 it's negative
378 (NB b == bl in this case, and we make a = al) */
379
380 y = ah*b;
381 if (y >= (1L << (LONG_BIT/2)))
382 goto bad;
383 a &= (1L << (LONG_BIT/2)) - 1;
384 x = a*b;
385 if (x < 0)
386 goto bad;
387 x += y << LONG_BIT/2;
388 if (x < 0)
389 goto bad;
390 ok:
391 return newintobject(x * s);
392
393 bad:
394 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395}
396
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000397static int
398i_divmod(x, y, p_xdivy, p_xmody)
399 register intobject *x, *y;
400 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000402 long xi = x->ob_ival;
403 long yi = y->ob_ival;
404 long xdivy, xmody;
405
406 if (yi == 0) {
407 err_setstr(ZeroDivisionError, "integer division or modulo");
408 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 }
Guido van Rossum00466951991-05-05 20:08:27 +0000410 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000411 if (xi < 0)
412 xdivy = -xi / -yi;
413 else
414 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000415 }
416 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000417 if (xi < 0)
418 xdivy = - (-xi / yi);
419 else
420 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000421 }
422 xmody = xi - xdivy*yi;
423 if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
424 xmody += yi;
425 xdivy -= 1;
426 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000427 *p_xdivy = xdivy;
428 *p_xmody = xmody;
429 return 0;
430}
431
432static object *
433int_div(x, y)
434 intobject *x;
435 intobject *y;
436{
437 long d, m;
438 if (i_divmod(x, y, &d, &m) < 0)
439 return NULL;
440 return newintobject(d);
441}
442
443static object *
444int_mod(x, y)
445 intobject *x;
446 intobject *y;
447{
448 long d, m;
449 if (i_divmod(x, y, &d, &m) < 0)
450 return NULL;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000451 return newintobject(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000452}
453
454static object *
455int_divmod(x, y)
456 intobject *x;
457 intobject *y;
458{
459 object *v, *v0, *v1;
460 long d, m;
461 if (i_divmod(x, y, &d, &m) < 0)
462 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000463 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000464}
465
466static object *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000467int_pow(v, w, z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000469 intobject *w;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000470 intobject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000472#if 1
473 register long iv, iw, iz, ix, temp, prev;
474 int zset = 0;
475 iv = v->ob_ival;
476 iw = w->ob_ival;
477 if (iw < 0) {
478 err_setstr(ValueError, "integer to the negative power");
479 return NULL;
480 }
481 if ((object *)z != None) {
482 iz = z->ob_ival;
483 zset = 1;
484 }
485 /*
486 * XXX: The original exponentiation code stopped looping
487 * when temp hit zero; this code will continue onwards
488 * unnecessarily, but at least it won't cause any errors.
489 * Hopefully the speed improvement from the fast exponentiation
490 * will compensate for the slight inefficiency.
491 * XXX: Better handling of overflows is desperately needed.
492 */
493 temp = iv;
494 ix = 1;
495 while (iw > 0) {
496 prev = ix; /* Save value for overflow check */
497 if (iw & 1) {
498 ix = ix*temp;
499 if (temp == 0)
500 break; /* Avoid ix / 0 */
501 if (ix / temp != prev)
502 return err_ovf("integer pow()");
503 }
504 iw >>= 1; /* Shift exponent down by 1 bit */
505 if (iw==0) break;
506 prev = temp;
507 temp *= temp; /* Square the value of temp */
508 if (prev!=0 && temp/prev!=prev)
509 return err_ovf("integer pow()");
510 if (zset) {
511 /* If we did a multiplication, perform a modulo */
512 ix = ix % iz;
513 temp = temp % iz;
514 }
515 }
516 if (zset) {
517 object *t1, *t2;
518 long int div, mod;
519 t1=newintobject(ix);
520 t2=newintobject(iz);
521 if (t1==NULL || t2==NULL ||
522 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
523 XDECREF(t1);
524 XDECREF(t2);
525 return(NULL);
526 }
527 ix=mod;
528 }
529 return newintobject(ix);
530#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000533 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000534 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000535 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000536 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000538 if ((object *)z != None) {
539 err_setstr(TypeError, "pow(int, int, int) not yet supported");
540 return NULL;
541 }
Guido van Rossum00466951991-05-05 20:08:27 +0000542 ix = 1;
543 while (--iw >= 0) {
544 long prev = ix;
545 ix = ix * iv;
546 if (iv == 0)
547 break; /* 0 to some power -- avoid ix / 0 */
548 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000549 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000550 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000552#endif
553}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554
555static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000556int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557 intobject *v;
558{
559 register long a, x;
560 a = v->ob_ival;
561 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000562 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000563 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 return newintobject(x);
565}
566
567static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000568int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 intobject *v;
570{
571 INCREF(v);
572 return (object *)v;
573}
574
Guido van Rossum00466951991-05-05 20:08:27 +0000575static object *
576int_abs(v)
577 intobject *v;
578{
579 if (v->ob_ival >= 0)
580 return int_pos(v);
581 else
582 return int_neg(v);
583}
584
Guido van Rossum0bff0151991-05-14 12:05:32 +0000585static int
586int_nonzero(v)
587 intobject *v;
588{
589 return v->ob_ival != 0;
590}
591
Guido van Rossum7928cd71991-10-24 14:59:31 +0000592static object *
593int_invert(v)
594 intobject *v;
595{
596 return newintobject(~v->ob_ival);
597}
598
599static object *
600int_lshift(v, w)
601 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000602 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000603{
604 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000605 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000606 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000607 if (b < 0) {
608 err_setstr(ValueError, "negative shift count");
609 return NULL;
610 }
611 if (a == 0 || b == 0) {
612 INCREF(v);
613 return (object *) v;
614 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000615 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000616 return newintobject(0L);
617 }
618 a = (unsigned long)a << b;
619 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000620}
621
622static object *
623int_rshift(v, w)
624 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000625 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000626{
627 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000628 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000629 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000630 if (b < 0) {
631 err_setstr(ValueError, "negative shift count");
632 return NULL;
633 }
634 if (a == 0 || b == 0) {
635 INCREF(v);
636 return (object *) v;
637 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000638 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000639 if (a < 0)
640 a = -1;
641 else
642 a = 0;
643 }
644 else {
645 if (a < 0)
646 a = ~( ~(unsigned long)a >> b );
647 else
648 a = (unsigned long)a >> b;
649 }
650 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000651}
652
653static object *
654int_and(v, w)
655 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000656 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000657{
658 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000659 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000660 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661 return newintobject(a & b);
662}
663
664static object *
665int_xor(v, w)
666 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000667 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000668{
669 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000670 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000671 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000672 return newintobject(a ^ b);
673}
674
675static object *
676int_or(v, w)
677 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000678 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000679{
680 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000681 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000682 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000683 return newintobject(a | b);
684}
685
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000686static object *
687int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000688 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000689{
690 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000691 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000692}
693
694static object *
695int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000696 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000697{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000698 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000699}
700
701static object *
702int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000703 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000704{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000705 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000706}
707
708static object *
709int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000710 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000711{
712 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000713 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000714 if (x == 0)
715 strcpy(buf, "0");
716 else if (x > 0)
717 sprintf(buf, "0%lo", x);
718 else
719 sprintf(buf, "-0%lo", -x);
720 return newstringobject(buf);
721}
722
723static object *
724int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000725 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000726{
727 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000728 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000729 if (x >= 0)
730 sprintf(buf, "0x%lx", x);
731 else
732 sprintf(buf, "-0x%lx", -x);
733 return newstringobject(buf);
734}
735
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000737 (binaryfunc)int_add, /*nb_add*/
738 (binaryfunc)int_sub, /*nb_subtract*/
739 (binaryfunc)int_mul, /*nb_multiply*/
740 (binaryfunc)int_div, /*nb_divide*/
741 (binaryfunc)int_mod, /*nb_remainder*/
742 (binaryfunc)int_divmod, /*nb_divmod*/
743 (ternaryfunc)int_pow, /*nb_power*/
744 (unaryfunc)int_neg, /*nb_negative*/
745 (unaryfunc)int_pos, /*nb_positive*/
746 (unaryfunc)int_abs, /*nb_absolute*/
747 (inquiry)int_nonzero, /*nb_nonzero*/
748 (unaryfunc)int_invert, /*nb_invert*/
749 (binaryfunc)int_lshift, /*nb_lshift*/
750 (binaryfunc)int_rshift, /*nb_rshift*/
751 (binaryfunc)int_and, /*nb_and*/
752 (binaryfunc)int_xor, /*nb_xor*/
753 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000754 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000755 (unaryfunc)int_int, /*nb_int*/
756 (unaryfunc)int_long, /*nb_long*/
757 (unaryfunc)int_float, /*nb_float*/
758 (unaryfunc)int_oct, /*nb_oct*/
759 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760};
761
762typeobject Inttype = {
763 OB_HEAD_INIT(&Typetype)
764 0,
765 "int",
766 sizeof(intobject),
767 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000768 (destructor)int_dealloc, /*tp_dealloc*/
769 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 0, /*tp_getattr*/
771 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000772 (cmpfunc)int_compare, /*tp_compare*/
773 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774 &int_as_number, /*tp_as_number*/
775 0, /*tp_as_sequence*/
776 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000777 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778};