blob: f020453049f71efddddcd1c7d868623291bb6527 [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 }
526 ix=mod;
527 }
528 return newintobject(ix);
529#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000532 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000533 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000534 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000535 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000537 if ((object *)z != None) {
538 err_setstr(TypeError, "pow(int, int, int) not yet supported");
539 return NULL;
540 }
Guido van Rossum00466951991-05-05 20:08:27 +0000541 ix = 1;
542 while (--iw >= 0) {
543 long prev = ix;
544 ix = ix * iv;
545 if (iv == 0)
546 break; /* 0 to some power -- avoid ix / 0 */
547 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000548 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000549 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551#endif
552}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553
554static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000555int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556 intobject *v;
557{
558 register long a, x;
559 a = v->ob_ival;
560 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000561 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000562 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563 return newintobject(x);
564}
565
566static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000567int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 intobject *v;
569{
570 INCREF(v);
571 return (object *)v;
572}
573
Guido van Rossum00466951991-05-05 20:08:27 +0000574static object *
575int_abs(v)
576 intobject *v;
577{
578 if (v->ob_ival >= 0)
579 return int_pos(v);
580 else
581 return int_neg(v);
582}
583
Guido van Rossum0bff0151991-05-14 12:05:32 +0000584static int
585int_nonzero(v)
586 intobject *v;
587{
588 return v->ob_ival != 0;
589}
590
Guido van Rossum7928cd71991-10-24 14:59:31 +0000591static object *
592int_invert(v)
593 intobject *v;
594{
595 return newintobject(~v->ob_ival);
596}
597
598static object *
599int_lshift(v, w)
600 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000601 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000602{
603 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000604 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000605 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000606 if (b < 0) {
607 err_setstr(ValueError, "negative shift count");
608 return NULL;
609 }
610 if (a == 0 || b == 0) {
611 INCREF(v);
612 return (object *) v;
613 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000614 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000615 return newintobject(0L);
616 }
617 a = (unsigned long)a << b;
618 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000619}
620
621static object *
622int_rshift(v, w)
623 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000624 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000625{
626 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000627 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000628 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000629 if (b < 0) {
630 err_setstr(ValueError, "negative shift count");
631 return NULL;
632 }
633 if (a == 0 || b == 0) {
634 INCREF(v);
635 return (object *) v;
636 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000637 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000638 if (a < 0)
639 a = -1;
640 else
641 a = 0;
642 }
643 else {
644 if (a < 0)
645 a = ~( ~(unsigned long)a >> b );
646 else
647 a = (unsigned long)a >> b;
648 }
649 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000650}
651
652static object *
653int_and(v, w)
654 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000655 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000656{
657 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000659 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000660 return newintobject(a & b);
661}
662
663static object *
664int_xor(v, w)
665 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000666 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667{
668 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000669 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000670 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000671 return newintobject(a ^ b);
672}
673
674static object *
675int_or(v, w)
676 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000677 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000678{
679 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000680 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000681 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682 return newintobject(a | b);
683}
684
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000685static object *
686int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000687 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000688{
689 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000690 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000691}
692
693static object *
694int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000695 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000696{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000697 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000698}
699
700static object *
701int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000702 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000703{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000704 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000705}
706
707static object *
708int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000709 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000710{
711 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000712 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000713 if (x == 0)
714 strcpy(buf, "0");
715 else if (x > 0)
716 sprintf(buf, "0%lo", x);
717 else
718 sprintf(buf, "-0%lo", -x);
719 return newstringobject(buf);
720}
721
722static object *
723int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000724 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000725{
726 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000727 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000728 if (x >= 0)
729 sprintf(buf, "0x%lx", x);
730 else
731 sprintf(buf, "-0x%lx", -x);
732 return newstringobject(buf);
733}
734
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000736 (binaryfunc)int_add, /*nb_add*/
737 (binaryfunc)int_sub, /*nb_subtract*/
738 (binaryfunc)int_mul, /*nb_multiply*/
739 (binaryfunc)int_div, /*nb_divide*/
740 (binaryfunc)int_mod, /*nb_remainder*/
741 (binaryfunc)int_divmod, /*nb_divmod*/
742 (ternaryfunc)int_pow, /*nb_power*/
743 (unaryfunc)int_neg, /*nb_negative*/
744 (unaryfunc)int_pos, /*nb_positive*/
745 (unaryfunc)int_abs, /*nb_absolute*/
746 (inquiry)int_nonzero, /*nb_nonzero*/
747 (unaryfunc)int_invert, /*nb_invert*/
748 (binaryfunc)int_lshift, /*nb_lshift*/
749 (binaryfunc)int_rshift, /*nb_rshift*/
750 (binaryfunc)int_and, /*nb_and*/
751 (binaryfunc)int_xor, /*nb_xor*/
752 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000753 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000754 (unaryfunc)int_int, /*nb_int*/
755 (unaryfunc)int_long, /*nb_long*/
756 (unaryfunc)int_float, /*nb_float*/
757 (unaryfunc)int_oct, /*nb_oct*/
758 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759};
760
761typeobject Inttype = {
762 OB_HEAD_INIT(&Typetype)
763 0,
764 "int",
765 sizeof(intobject),
766 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000767 (destructor)int_dealloc, /*tp_dealloc*/
768 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769 0, /*tp_getattr*/
770 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000771 (cmpfunc)int_compare, /*tp_compare*/
772 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773 &int_as_number, /*tp_as_number*/
774 0, /*tp_as_sequence*/
775 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000776 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777};