blob: ca2961e83578312568e338a76d28a975e38b3036 [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
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{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000459 long d, m;
460 if (i_divmod(x, y, &d, &m) < 0)
461 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000462 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000463}
464
465static object *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000466int_pow(v, w, z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000468 intobject *w;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000469 intobject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000471#if 1
472 register long iv, iw, iz, ix, temp, prev;
473 int zset = 0;
474 iv = v->ob_ival;
475 iw = w->ob_ival;
476 if (iw < 0) {
477 err_setstr(ValueError, "integer to the negative power");
478 return NULL;
479 }
480 if ((object *)z != None) {
481 iz = z->ob_ival;
482 zset = 1;
483 }
484 /*
485 * XXX: The original exponentiation code stopped looping
486 * when temp hit zero; this code will continue onwards
487 * unnecessarily, but at least it won't cause any errors.
488 * Hopefully the speed improvement from the fast exponentiation
489 * will compensate for the slight inefficiency.
490 * XXX: Better handling of overflows is desperately needed.
491 */
492 temp = iv;
493 ix = 1;
494 while (iw > 0) {
495 prev = ix; /* Save value for overflow check */
496 if (iw & 1) {
497 ix = ix*temp;
498 if (temp == 0)
499 break; /* Avoid ix / 0 */
500 if (ix / temp != prev)
501 return err_ovf("integer pow()");
502 }
503 iw >>= 1; /* Shift exponent down by 1 bit */
504 if (iw==0) break;
505 prev = temp;
506 temp *= temp; /* Square the value of temp */
507 if (prev!=0 && temp/prev!=prev)
508 return err_ovf("integer pow()");
509 if (zset) {
510 /* If we did a multiplication, perform a modulo */
511 ix = ix % iz;
512 temp = temp % iz;
513 }
514 }
515 if (zset) {
516 object *t1, *t2;
517 long int div, mod;
518 t1=newintobject(ix);
519 t2=newintobject(iz);
520 if (t1==NULL || t2==NULL ||
521 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
522 XDECREF(t1);
523 XDECREF(t2);
524 return(NULL);
525 }
Guido van Rossumc206c761995-01-10 15:23:19 +0000526 DECREF(t1);
527 DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528 ix=mod;
529 }
530 return newintobject(ix);
531#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000534 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000535 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000536 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000537 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000539 if ((object *)z != None) {
540 err_setstr(TypeError, "pow(int, int, int) not yet supported");
541 return NULL;
542 }
Guido van Rossum00466951991-05-05 20:08:27 +0000543 ix = 1;
544 while (--iw >= 0) {
545 long prev = ix;
546 ix = ix * iv;
547 if (iv == 0)
548 break; /* 0 to some power -- avoid ix / 0 */
549 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000550 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000551 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000553#endif
554}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555
556static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000557int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558 intobject *v;
559{
560 register long a, x;
561 a = v->ob_ival;
562 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000563 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000564 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 return newintobject(x);
566}
567
568static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570 intobject *v;
571{
572 INCREF(v);
573 return (object *)v;
574}
575
Guido van Rossum00466951991-05-05 20:08:27 +0000576static object *
577int_abs(v)
578 intobject *v;
579{
580 if (v->ob_ival >= 0)
581 return int_pos(v);
582 else
583 return int_neg(v);
584}
585
Guido van Rossum0bff0151991-05-14 12:05:32 +0000586static int
587int_nonzero(v)
588 intobject *v;
589{
590 return v->ob_ival != 0;
591}
592
Guido van Rossum7928cd71991-10-24 14:59:31 +0000593static object *
594int_invert(v)
595 intobject *v;
596{
597 return newintobject(~v->ob_ival);
598}
599
600static object *
601int_lshift(v, w)
602 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000603 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000604{
605 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000606 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000607 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000608 if (b < 0) {
609 err_setstr(ValueError, "negative shift count");
610 return NULL;
611 }
612 if (a == 0 || b == 0) {
613 INCREF(v);
614 return (object *) v;
615 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000616 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000617 return newintobject(0L);
618 }
619 a = (unsigned long)a << b;
620 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000621}
622
623static object *
624int_rshift(v, w)
625 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000626 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000627{
628 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000629 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000630 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000631 if (b < 0) {
632 err_setstr(ValueError, "negative shift count");
633 return NULL;
634 }
635 if (a == 0 || b == 0) {
636 INCREF(v);
637 return (object *) v;
638 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000639 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000640 if (a < 0)
641 a = -1;
642 else
643 a = 0;
644 }
645 else {
646 if (a < 0)
647 a = ~( ~(unsigned long)a >> b );
648 else
649 a = (unsigned long)a >> b;
650 }
651 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000652}
653
654static object *
655int_and(v, w)
656 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000657 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658{
659 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000660 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000661 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000662 return newintobject(a & b);
663}
664
665static object *
666int_xor(v, w)
667 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000669{
670 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000671 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000672 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000673 return newintobject(a ^ b);
674}
675
676static object *
677int_or(v, w)
678 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000679 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000680{
681 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000683 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000684 return newintobject(a | b);
685}
686
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000687static object *
688int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000689 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000690{
691 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000692 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000693}
694
695static object *
696int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000697 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000698{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000699 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000700}
701
702static object *
703int_float(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 newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000707}
708
709static object *
710int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000711 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000712{
713 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000714 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000715 if (x == 0)
716 strcpy(buf, "0");
717 else if (x > 0)
718 sprintf(buf, "0%lo", x);
719 else
720 sprintf(buf, "-0%lo", -x);
721 return newstringobject(buf);
722}
723
724static object *
725int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000726 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000727{
728 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000729 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000730 if (x >= 0)
731 sprintf(buf, "0x%lx", x);
732 else
733 sprintf(buf, "-0x%lx", -x);
734 return newstringobject(buf);
735}
736
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000737static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000738 (binaryfunc)int_add, /*nb_add*/
739 (binaryfunc)int_sub, /*nb_subtract*/
740 (binaryfunc)int_mul, /*nb_multiply*/
741 (binaryfunc)int_div, /*nb_divide*/
742 (binaryfunc)int_mod, /*nb_remainder*/
743 (binaryfunc)int_divmod, /*nb_divmod*/
744 (ternaryfunc)int_pow, /*nb_power*/
745 (unaryfunc)int_neg, /*nb_negative*/
746 (unaryfunc)int_pos, /*nb_positive*/
747 (unaryfunc)int_abs, /*nb_absolute*/
748 (inquiry)int_nonzero, /*nb_nonzero*/
749 (unaryfunc)int_invert, /*nb_invert*/
750 (binaryfunc)int_lshift, /*nb_lshift*/
751 (binaryfunc)int_rshift, /*nb_rshift*/
752 (binaryfunc)int_and, /*nb_and*/
753 (binaryfunc)int_xor, /*nb_xor*/
754 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000755 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000756 (unaryfunc)int_int, /*nb_int*/
757 (unaryfunc)int_long, /*nb_long*/
758 (unaryfunc)int_float, /*nb_float*/
759 (unaryfunc)int_oct, /*nb_oct*/
760 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761};
762
763typeobject Inttype = {
764 OB_HEAD_INIT(&Typetype)
765 0,
766 "int",
767 sizeof(intobject),
768 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000769 (destructor)int_dealloc, /*tp_dealloc*/
770 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 0, /*tp_getattr*/
772 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000773 (cmpfunc)int_compare, /*tp_compare*/
774 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775 &int_as_number, /*tp_as_number*/
776 0, /*tp_as_sequence*/
777 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000778 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779};