blob: 1f1298f0a266bd9ac9c60d77d4b9260395f87232 [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{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000240 /* XXX If this is changed, you also need to change the way
241 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000242 long x = v -> ob_ival;
243 if (x == -1)
244 x = -2;
245 return x;
246}
247
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000249int_add(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000251 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252{
253 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000255 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000257 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000258 return err_ovf("integer addition");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 return newintobject(x);
260}
261
262static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263int_sub(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000265 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
267 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000269 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000271 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000272 return err_ovf("integer subtraction");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 return newintobject(x);
274}
275
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000276/*
277Integer overflow checking used to be done using a double, but on 64
278bit machines (where both long and double are 64 bit) this fails
279because the double doesn't have enouvg precision. John Tromp suggests
280the following algorithm:
281
282Suppose again we normalize a and b to be nonnegative.
283Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
284Now we test ah and bh against zero and get essentially 3 possible outcomes.
285
2861) both ah and bh > 0 : then report overflow
287
2882) both ah and bh = 0 : then compute a*b and report overflow if it comes out
289 negative
290
2913) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
292 compute al*bl and report overflow if it's negative
293 add (ah*bl)<<32 to al*bl and report overflow if
294 it's negative
295
296In case of no overflow the result is then negated if necessary.
297
298The majority of cases will be 2), in which case this method is the same as
299what I suggested before. If multiplication is expensive enough, then the
300other method is faster on case 3), but also more work to program, so I
301guess the above is the preferred solution.
302
303*/
304
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306int_mul(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000308 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000310 long a, b, ah, bh, x, y;
311 int s = 1;
312
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000314 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000315 ah = a >> (LONG_BIT/2);
316 bh = b >> (LONG_BIT/2);
317
318 /* Quick test for common case: two small positive ints */
319
320 if (ah == 0 && bh == 0) {
321 x = a*b;
322 if (x < 0)
323 goto bad;
324 return newintobject(x);
325 }
326
327 /* Arrange that a >= b >= 0 */
328
329 if (a < 0) {
330 a = -a;
331 if (a < 0) {
332 /* Largest negative */
333 if (b == 0 || b == 1) {
334 x = a*b;
335 goto ok;
336 }
337 else
338 goto bad;
339 }
340 s = -s;
341 ah = a >> (LONG_BIT/2);
342 }
343 if (b < 0) {
344 b = -b;
345 if (b < 0) {
346 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000347 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000348 x = a*b;
349 goto ok;
350 }
351 else
352 goto bad;
353 }
354 s = -s;
355 bh = b >> (LONG_BIT/2);
356 }
357
358 /* 1) both ah and bh > 0 : then report overflow */
359
360 if (ah != 0 && bh != 0)
361 goto bad;
362
363 /* 2) both ah and bh = 0 : then compute a*b and report
364 overflow if it comes out negative */
365
366 if (ah == 0 && bh == 0) {
367 x = a*b;
368 if (x < 0)
369 goto bad;
370 return newintobject(x*s);
371 }
372
373 if (a < b) {
374 /* Swap */
375 x = a;
376 a = b;
377 b = x;
378 ah = bh;
379 /* bh not used beyond this point */
380 }
381
382 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
383 it's >= 2^31
384 compute al*bl and report overflow if it's negative
385 add (ah*bl)<<32 to al*bl and report overflow if
386 it's negative
387 (NB b == bl in this case, and we make a = al) */
388
389 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000390 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000391 goto bad;
392 a &= (1L << (LONG_BIT/2)) - 1;
393 x = a*b;
394 if (x < 0)
395 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000396 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000397 if (x < 0)
398 goto bad;
399 ok:
400 return newintobject(x * s);
401
402 bad:
403 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404}
405
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000406static int
407i_divmod(x, y, p_xdivy, p_xmody)
408 register intobject *x, *y;
409 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000411 long xi = x->ob_ival;
412 long yi = y->ob_ival;
413 long xdivy, xmody;
414
415 if (yi == 0) {
416 err_setstr(ZeroDivisionError, "integer division or modulo");
417 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418 }
Guido van Rossum00466951991-05-05 20:08:27 +0000419 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000420 if (xi < 0)
421 xdivy = -xi / -yi;
422 else
423 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000424 }
425 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000426 if (xi < 0)
427 xdivy = - (-xi / yi);
428 else
429 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000430 }
431 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000432 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000433 xmody += yi;
434 xdivy -= 1;
435 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000436 *p_xdivy = xdivy;
437 *p_xmody = xmody;
438 return 0;
439}
440
441static object *
442int_div(x, y)
443 intobject *x;
444 intobject *y;
445{
446 long d, m;
447 if (i_divmod(x, y, &d, &m) < 0)
448 return NULL;
449 return newintobject(d);
450}
451
452static object *
453int_mod(x, y)
454 intobject *x;
455 intobject *y;
456{
457 long d, m;
458 if (i_divmod(x, y, &d, &m) < 0)
459 return NULL;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000460 return newintobject(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000461}
462
463static object *
464int_divmod(x, y)
465 intobject *x;
466 intobject *y;
467{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000468 long d, m;
469 if (i_divmod(x, y, &d, &m) < 0)
470 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000471 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000472}
473
474static object *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000475int_pow(v, w, z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000477 intobject *w;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000478 intobject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000480#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000481 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000482 iv = v->ob_ival;
483 iw = w->ob_ival;
484 if (iw < 0) {
485 err_setstr(ValueError, "integer to the negative power");
486 return NULL;
487 }
488 if ((object *)z != None) {
489 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000490 if (iz == 0) {
491 err_setstr(ValueError, "pow(x, y, z) with z==0");
492 return NULL;
493 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000494 }
495 /*
496 * XXX: The original exponentiation code stopped looping
497 * when temp hit zero; this code will continue onwards
498 * unnecessarily, but at least it won't cause any errors.
499 * Hopefully the speed improvement from the fast exponentiation
500 * will compensate for the slight inefficiency.
501 * XXX: Better handling of overflows is desperately needed.
502 */
503 temp = iv;
504 ix = 1;
505 while (iw > 0) {
506 prev = ix; /* Save value for overflow check */
507 if (iw & 1) {
508 ix = ix*temp;
509 if (temp == 0)
510 break; /* Avoid ix / 0 */
511 if (ix / temp != prev)
512 return err_ovf("integer pow()");
513 }
514 iw >>= 1; /* Shift exponent down by 1 bit */
515 if (iw==0) break;
516 prev = temp;
517 temp *= temp; /* Square the value of temp */
518 if (prev!=0 && temp/prev!=prev)
519 return err_ovf("integer pow()");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000520 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000521 /* If we did a multiplication, perform a modulo */
522 ix = ix % iz;
523 temp = temp % iz;
524 }
525 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000526 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000527 object *t1, *t2;
528 long int div, mod;
529 t1=newintobject(ix);
530 t2=newintobject(iz);
531 if (t1==NULL || t2==NULL ||
532 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
533 XDECREF(t1);
534 XDECREF(t2);
535 return(NULL);
536 }
Guido van Rossumc206c761995-01-10 15:23:19 +0000537 DECREF(t1);
538 DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000539 ix=mod;
540 }
541 return newintobject(ix);
542#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000545 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000546 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000547 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000548 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000550 if ((object *)z != None) {
551 err_setstr(TypeError, "pow(int, int, int) not yet supported");
552 return NULL;
553 }
Guido van Rossum00466951991-05-05 20:08:27 +0000554 ix = 1;
555 while (--iw >= 0) {
556 long prev = ix;
557 ix = ix * iv;
558 if (iv == 0)
559 break; /* 0 to some power -- avoid ix / 0 */
560 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000561 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000562 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000564#endif
565}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566
567static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000568int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 intobject *v;
570{
571 register long a, x;
572 a = v->ob_ival;
573 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000574 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000575 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 return newintobject(x);
577}
578
579static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000580int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 intobject *v;
582{
583 INCREF(v);
584 return (object *)v;
585}
586
Guido van Rossum00466951991-05-05 20:08:27 +0000587static object *
588int_abs(v)
589 intobject *v;
590{
591 if (v->ob_ival >= 0)
592 return int_pos(v);
593 else
594 return int_neg(v);
595}
596
Guido van Rossum0bff0151991-05-14 12:05:32 +0000597static int
598int_nonzero(v)
599 intobject *v;
600{
601 return v->ob_ival != 0;
602}
603
Guido van Rossum7928cd71991-10-24 14:59:31 +0000604static object *
605int_invert(v)
606 intobject *v;
607{
608 return newintobject(~v->ob_ival);
609}
610
611static object *
612int_lshift(v, w)
613 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000614 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000615{
616 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000617 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000618 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000619 if (b < 0) {
620 err_setstr(ValueError, "negative shift count");
621 return NULL;
622 }
623 if (a == 0 || b == 0) {
624 INCREF(v);
625 return (object *) v;
626 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000627 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000628 return newintobject(0L);
629 }
630 a = (unsigned long)a << b;
631 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000632}
633
634static object *
635int_rshift(v, w)
636 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000637 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000638{
639 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000640 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000641 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000642 if (b < 0) {
643 err_setstr(ValueError, "negative shift count");
644 return NULL;
645 }
646 if (a == 0 || b == 0) {
647 INCREF(v);
648 return (object *) v;
649 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000650 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000651 if (a < 0)
652 a = -1;
653 else
654 a = 0;
655 }
656 else {
657 if (a < 0)
658 a = ~( ~(unsigned long)a >> b );
659 else
660 a = (unsigned long)a >> b;
661 }
662 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000663}
664
665static object *
666int_and(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_xor(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
687static object *
688int_or(v, w)
689 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000690 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691{
692 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000694 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000695 return newintobject(a | b);
696}
697
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000698static object *
699int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000700 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000701{
702 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000703 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000704}
705
706static object *
707int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000708 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000709{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000710 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000711}
712
713static object *
714int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000715 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000717 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000718}
719
720static object *
721int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000722 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000723{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000724 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000725 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000726 if (x == 0)
727 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000728 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000729 sprintf(buf, "0%lo", x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000730 return newstringobject(buf);
731}
732
733static object *
734int_hex(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000735 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000736{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000737 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000738 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000739 sprintf(buf, "0x%lx", x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000740 return newstringobject(buf);
741}
742
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000744 (binaryfunc)int_add, /*nb_add*/
745 (binaryfunc)int_sub, /*nb_subtract*/
746 (binaryfunc)int_mul, /*nb_multiply*/
747 (binaryfunc)int_div, /*nb_divide*/
748 (binaryfunc)int_mod, /*nb_remainder*/
749 (binaryfunc)int_divmod, /*nb_divmod*/
750 (ternaryfunc)int_pow, /*nb_power*/
751 (unaryfunc)int_neg, /*nb_negative*/
752 (unaryfunc)int_pos, /*nb_positive*/
753 (unaryfunc)int_abs, /*nb_absolute*/
754 (inquiry)int_nonzero, /*nb_nonzero*/
755 (unaryfunc)int_invert, /*nb_invert*/
756 (binaryfunc)int_lshift, /*nb_lshift*/
757 (binaryfunc)int_rshift, /*nb_rshift*/
758 (binaryfunc)int_and, /*nb_and*/
759 (binaryfunc)int_xor, /*nb_xor*/
760 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000761 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000762 (unaryfunc)int_int, /*nb_int*/
763 (unaryfunc)int_long, /*nb_long*/
764 (unaryfunc)int_float, /*nb_float*/
765 (unaryfunc)int_oct, /*nb_oct*/
766 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767};
768
769typeobject Inttype = {
770 OB_HEAD_INIT(&Typetype)
771 0,
772 "int",
773 sizeof(intobject),
774 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000775 (destructor)int_dealloc, /*tp_dealloc*/
776 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777 0, /*tp_getattr*/
778 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000779 (cmpfunc)int_compare, /*tp_compare*/
780 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781 &int_as_number, /*tp_as_number*/
782 0, /*tp_as_sequence*/
783 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000784 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785};