blob: 62649209bfc2513ea4792846dee8123d6b409bc7 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Integer object implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossume5372401993-03-16 12:15:04 +000035#include "modsupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossumbf8c0e31994-08-29 12:48:32 +000037#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +000038#include <limits.h>
39#endif
40
41#ifndef LONG_MAX
42#define LONG_MAX 0X7FFFFFFFL
43#endif
44
45#ifndef LONG_MIN
46#define LONG_MIN (-LONG_MAX-1)
47#endif
48
49#ifndef CHAR_BIT
50#define CHAR_BIT 8
51#endif
52
Guido van Rossumb376a4a1993-11-23 17:53:17 +000053#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000054#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000055#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000056
Guido van Rossum2e1d4331993-12-24 10:22:45 +000057long
58getmaxint()
59{
60 return LONG_MAX; /* To initialize sys.maxint */
61}
62
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000064
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065intobject FalseObject = {
66 OB_HEAD_INIT(&Inttype)
67 0
68};
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070intobject TrueObject = {
71 OB_HEAD_INIT(&Inttype)
72 1
73};
74
Guido van Rossum165e67e1990-10-14 20:02:26 +000075static object *
Guido van Rossum3a628451991-12-10 13:57:36 +000076err_ovf(msg)
77 char *msg;
Guido van Rossum165e67e1990-10-14 20:02:26 +000078{
Guido van Rossum3a628451991-12-10 13:57:36 +000079 err_setstr(OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000080 return NULL;
81}
82
Guido van Rossum3f5da241990-12-20 15:06:42 +000083/* Integers are quite normal objects, to make object handling uniform.
84 (Using odd pointers to represent integers would save much space
85 but require extra checks for this special case throughout the code.)
86 Since, a typical Python program spends much of its time allocating
87 and deallocating integers, these operations should be very fast.
88 Therefore we use a dedicated allocation scheme with a much lower
89 overhead (in space and time) than straight malloc(): a simple
90 dedicated free list, filled when necessary with memory from malloc().
91*/
92
93#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
94#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject))
95
96static intobject *
97fill_free_list()
98{
99 intobject *p, *q;
100 p = NEW(intobject, N_INTOBJECTS);
101 if (p == NULL)
102 return (intobject *)err_nomem();
103 q = p + N_INTOBJECTS;
104 while (--q > p)
105 *(intobject **)q = q-1;
106 *(intobject **)q = NULL;
107 return p + N_INTOBJECTS - 1;
108}
109
110static intobject *free_list = NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000111#ifndef NSMALLPOSINTS
112#define NSMALLPOSINTS 100
113#endif
114#ifndef NSMALLNEGINTS
115#define NSMALLNEGINTS 1
116#endif
117#if NSMALLNEGINTS + NSMALLPOSINTS > 0
118/* References to small integers are saved in this array so that they
119 can be shared.
120 The integers that are saved are those in the range
121 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
122*/
123static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
124#endif
125#ifdef COUNT_ALLOCS
126int quick_int_allocs, quick_neg_int_allocs;
127#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129object *
130newintobject(ival)
131 long ival;
132{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133 register intobject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000134#if NSMALLNEGINTS + NSMALLPOSINTS > 0
135 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
136 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
137 INCREF(v);
138#ifdef COUNT_ALLOCS
139 if (ival >= 0)
140 quick_int_allocs++;
141 else
142 quick_neg_int_allocs++;
143#endif
144 return (object *) v;
145 }
146#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 if (free_list == NULL) {
148 if ((free_list = fill_free_list()) == NULL)
149 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000151 v = free_list;
152 free_list = *(intobject **)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000153 v->ob_type = &Inttype;
154 v->ob_ival = ival;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000155 NEWREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000156#if NSMALLNEGINTS + NSMALLPOSINTS > 0
157 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
158 /* save this one for a following allocation */
159 INCREF(v);
160 small_ints[ival + NSMALLNEGINTS] = v;
161 }
162#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163 return (object *) v;
164}
165
166static void
167int_dealloc(v)
168 intobject *v;
169{
170 *(intobject **)v = free_list;
171 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
174long
175getintvalue(op)
176 register object *op;
177{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000178 number_methods *nb;
179 intobject *io;
180 long val;
181
182 if (op && is_intobject(op))
183 return GETINTVALUE((intobject*) op);
184
185 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
186 nb->nb_int == NULL) {
187 err_badarg();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 return -1;
189 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000190
191 io = (intobject*) (*nb->nb_int) (op);
192 if (io == NULL)
193 return -1;
194 if (!is_intobject(io)) {
195 err_setstr(TypeError, "nb_int should return int object");
196 return -1;
197 }
198
199 val = GETINTVALUE(io);
200 DECREF(io);
201
202 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
205/* Methods */
206
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000207/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000208static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000209int_print(v, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 intobject *v;
211 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000212 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
214 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000215 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
218static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219int_repr(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 intobject *v;
221{
222 char buf[20];
223 sprintf(buf, "%ld", v->ob_ival);
224 return newstringobject(buf);
225}
226
227static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228int_compare(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 intobject *v, *w;
230{
231 register long i = v->ob_ival;
232 register long j = w->ob_ival;
233 return (i < j) ? -1 : (i > j) ? 1 : 0;
234}
235
Guido van Rossum9bfef441993-03-29 10:43:31 +0000236static long
237int_hash(v)
238 intobject *v;
239{
240 long x = v -> ob_ival;
241 if (x == -1)
242 x = -2;
243 return x;
244}
245
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247int_add(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000249 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250{
251 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000253 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000255 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000256 return err_ovf("integer addition");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 return newintobject(x);
258}
259
260static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261int_sub(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000263 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264{
265 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000267 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000269 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000270 return err_ovf("integer subtraction");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 return newintobject(x);
272}
273
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000274/*
275Integer overflow checking used to be done using a double, but on 64
276bit machines (where both long and double are 64 bit) this fails
277because the double doesn't have enouvg precision. John Tromp suggests
278the following algorithm:
279
280Suppose again we normalize a and b to be nonnegative.
281Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
282Now we test ah and bh against zero and get essentially 3 possible outcomes.
283
2841) both ah and bh > 0 : then report overflow
285
2862) both ah and bh = 0 : then compute a*b and report overflow if it comes out
287 negative
288
2893) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
290 compute al*bl and report overflow if it's negative
291 add (ah*bl)<<32 to al*bl and report overflow if
292 it's negative
293
294In case of no overflow the result is then negated if necessary.
295
296The majority of cases will be 2), in which case this method is the same as
297what I suggested before. If multiplication is expensive enough, then the
298other method is faster on case 3), but also more work to program, so I
299guess the above is the preferred solution.
300
301*/
302
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000304int_mul(v, w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000306 intobject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000308 long a, b, ah, bh, x, y;
309 int s = 1;
310
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000312 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000313 ah = a >> (LONG_BIT/2);
314 bh = b >> (LONG_BIT/2);
315
316 /* Quick test for common case: two small positive ints */
317
318 if (ah == 0 && bh == 0) {
319 x = a*b;
320 if (x < 0)
321 goto bad;
322 return newintobject(x);
323 }
324
325 /* Arrange that a >= b >= 0 */
326
327 if (a < 0) {
328 a = -a;
329 if (a < 0) {
330 /* Largest negative */
331 if (b == 0 || b == 1) {
332 x = a*b;
333 goto ok;
334 }
335 else
336 goto bad;
337 }
338 s = -s;
339 ah = a >> (LONG_BIT/2);
340 }
341 if (b < 0) {
342 b = -b;
343 if (b < 0) {
344 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000345 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000346 x = a*b;
347 goto ok;
348 }
349 else
350 goto bad;
351 }
352 s = -s;
353 bh = b >> (LONG_BIT/2);
354 }
355
356 /* 1) both ah and bh > 0 : then report overflow */
357
358 if (ah != 0 && bh != 0)
359 goto bad;
360
361 /* 2) both ah and bh = 0 : then compute a*b and report
362 overflow if it comes out negative */
363
364 if (ah == 0 && bh == 0) {
365 x = a*b;
366 if (x < 0)
367 goto bad;
368 return newintobject(x*s);
369 }
370
371 if (a < b) {
372 /* Swap */
373 x = a;
374 a = b;
375 b = x;
376 ah = bh;
377 /* bh not used beyond this point */
378 }
379
380 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
381 it's >= 2^31
382 compute al*bl and report overflow if it's negative
383 add (ah*bl)<<32 to al*bl and report overflow if
384 it's negative
385 (NB b == bl in this case, and we make a = al) */
386
387 y = ah*b;
388 if (y >= (1L << (LONG_BIT/2)))
389 goto bad;
390 a &= (1L << (LONG_BIT/2)) - 1;
391 x = a*b;
392 if (x < 0)
393 goto bad;
394 x += y << LONG_BIT/2;
395 if (x < 0)
396 goto bad;
397 ok:
398 return newintobject(x * s);
399
400 bad:
401 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402}
403
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000404static int
405i_divmod(x, y, p_xdivy, p_xmody)
406 register intobject *x, *y;
407 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000409 long xi = x->ob_ival;
410 long yi = y->ob_ival;
411 long xdivy, xmody;
412
413 if (yi == 0) {
414 err_setstr(ZeroDivisionError, "integer division or modulo");
415 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 }
Guido van Rossum00466951991-05-05 20:08:27 +0000417 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000418 if (xi < 0)
419 xdivy = -xi / -yi;
420 else
421 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000422 }
423 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000424 if (xi < 0)
425 xdivy = - (-xi / yi);
426 else
427 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000428 }
429 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000430 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000431 xmody += yi;
432 xdivy -= 1;
433 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000434 *p_xdivy = xdivy;
435 *p_xmody = xmody;
436 return 0;
437}
438
439static object *
440int_div(x, y)
441 intobject *x;
442 intobject *y;
443{
444 long d, m;
445 if (i_divmod(x, y, &d, &m) < 0)
446 return NULL;
447 return newintobject(d);
448}
449
450static object *
451int_mod(x, y)
452 intobject *x;
453 intobject *y;
454{
455 long d, m;
456 if (i_divmod(x, y, &d, &m) < 0)
457 return NULL;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000458 return newintobject(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000459}
460
461static object *
462int_divmod(x, y)
463 intobject *x;
464 intobject *y;
465{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 long d, m;
467 if (i_divmod(x, y, &d, &m) < 0)
468 return NULL;
Guido van Rossume5372401993-03-16 12:15:04 +0000469 return mkvalue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000470}
471
472static object *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000473int_pow(v, w, z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475 intobject *w;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000476 intobject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000478#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000479 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000480 iv = v->ob_ival;
481 iw = w->ob_ival;
482 if (iw < 0) {
483 err_setstr(ValueError, "integer to the negative power");
484 return NULL;
485 }
486 if ((object *)z != None) {
487 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000488 if (iz == 0) {
489 err_setstr(ValueError, "pow(x, y, z) with z==0");
490 return NULL;
491 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000492 }
493 /*
494 * XXX: The original exponentiation code stopped looping
495 * when temp hit zero; this code will continue onwards
496 * unnecessarily, but at least it won't cause any errors.
497 * Hopefully the speed improvement from the fast exponentiation
498 * will compensate for the slight inefficiency.
499 * XXX: Better handling of overflows is desperately needed.
500 */
501 temp = iv;
502 ix = 1;
503 while (iw > 0) {
504 prev = ix; /* Save value for overflow check */
505 if (iw & 1) {
506 ix = ix*temp;
507 if (temp == 0)
508 break; /* Avoid ix / 0 */
509 if (ix / temp != prev)
510 return err_ovf("integer pow()");
511 }
512 iw >>= 1; /* Shift exponent down by 1 bit */
513 if (iw==0) break;
514 prev = temp;
515 temp *= temp; /* Square the value of temp */
516 if (prev!=0 && temp/prev!=prev)
517 return err_ovf("integer pow()");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000518 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000519 /* If we did a multiplication, perform a modulo */
520 ix = ix % iz;
521 temp = temp % iz;
522 }
523 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000524 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525 object *t1, *t2;
526 long int div, mod;
527 t1=newintobject(ix);
528 t2=newintobject(iz);
529 if (t1==NULL || t2==NULL ||
530 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
531 XDECREF(t1);
532 XDECREF(t2);
533 return(NULL);
534 }
Guido van Rossumc206c761995-01-10 15:23:19 +0000535 DECREF(t1);
536 DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000537 ix=mod;
538 }
539 return newintobject(ix);
540#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000543 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000544 if (iw < 0) {
Guido van Rossum3a628451991-12-10 13:57:36 +0000545 err_setstr(ValueError, "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000546 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000548 if ((object *)z != None) {
549 err_setstr(TypeError, "pow(int, int, int) not yet supported");
550 return NULL;
551 }
Guido van Rossum00466951991-05-05 20:08:27 +0000552 ix = 1;
553 while (--iw >= 0) {
554 long prev = ix;
555 ix = ix * iv;
556 if (iv == 0)
557 break; /* 0 to some power -- avoid ix / 0 */
558 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000559 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000560 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561 return newintobject(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000562#endif
563}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564
565static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000566int_neg(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567 intobject *v;
568{
569 register long a, x;
570 a = v->ob_ival;
571 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000572 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000573 return err_ovf("integer negation");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574 return newintobject(x);
575}
576
577static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000578int_pos(v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579 intobject *v;
580{
581 INCREF(v);
582 return (object *)v;
583}
584
Guido van Rossum00466951991-05-05 20:08:27 +0000585static object *
586int_abs(v)
587 intobject *v;
588{
589 if (v->ob_ival >= 0)
590 return int_pos(v);
591 else
592 return int_neg(v);
593}
594
Guido van Rossum0bff0151991-05-14 12:05:32 +0000595static int
596int_nonzero(v)
597 intobject *v;
598{
599 return v->ob_ival != 0;
600}
601
Guido van Rossum7928cd71991-10-24 14:59:31 +0000602static object *
603int_invert(v)
604 intobject *v;
605{
606 return newintobject(~v->ob_ival);
607}
608
609static object *
610int_lshift(v, w)
611 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000612 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000613{
614 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000615 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000616 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000617 if (b < 0) {
618 err_setstr(ValueError, "negative shift count");
619 return NULL;
620 }
621 if (a == 0 || b == 0) {
622 INCREF(v);
623 return (object *) v;
624 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000625 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000626 return newintobject(0L);
627 }
628 a = (unsigned long)a << b;
629 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000630}
631
632static object *
633int_rshift(v, w)
634 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000635 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000636{
637 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000638 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000639 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000640 if (b < 0) {
641 err_setstr(ValueError, "negative shift count");
642 return NULL;
643 }
644 if (a == 0 || b == 0) {
645 INCREF(v);
646 return (object *) v;
647 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000648 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 if (a < 0)
650 a = -1;
651 else
652 a = 0;
653 }
654 else {
655 if (a < 0)
656 a = ~( ~(unsigned long)a >> b );
657 else
658 a = (unsigned long)a >> b;
659 }
660 return newintobject(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661}
662
663static object *
664int_and(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_xor(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
685static object *
686int_or(v, w)
687 intobject *v;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000688 intobject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000689{
690 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000692 b = w->ob_ival;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693 return newintobject(a | b);
694}
695
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000696static object *
697int_int(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000698 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000699{
700 INCREF(v);
Guido van Rossum9575a441993-04-07 14:06:14 +0000701 return (object *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000702}
703
704static object *
705int_long(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000706 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000707{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000708 return newlongobject((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000709}
710
711static object *
712int_float(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000713 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000714{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000715 return newfloatobject((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716}
717
718static object *
719int_oct(v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000720 intobject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000721{
722 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000723 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000724 if (x == 0)
725 strcpy(buf, "0");
726 else if (x > 0)
727 sprintf(buf, "0%lo", x);
728 else
729 sprintf(buf, "-0%lo", -x);
730 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{
737 char buf[20];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000738 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000739 if (x >= 0)
740 sprintf(buf, "0x%lx", x);
741 else
742 sprintf(buf, "-0x%lx", -x);
743 return newstringobject(buf);
744}
745
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000746static number_methods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000747 (binaryfunc)int_add, /*nb_add*/
748 (binaryfunc)int_sub, /*nb_subtract*/
749 (binaryfunc)int_mul, /*nb_multiply*/
750 (binaryfunc)int_div, /*nb_divide*/
751 (binaryfunc)int_mod, /*nb_remainder*/
752 (binaryfunc)int_divmod, /*nb_divmod*/
753 (ternaryfunc)int_pow, /*nb_power*/
754 (unaryfunc)int_neg, /*nb_negative*/
755 (unaryfunc)int_pos, /*nb_positive*/
756 (unaryfunc)int_abs, /*nb_absolute*/
757 (inquiry)int_nonzero, /*nb_nonzero*/
758 (unaryfunc)int_invert, /*nb_invert*/
759 (binaryfunc)int_lshift, /*nb_lshift*/
760 (binaryfunc)int_rshift, /*nb_rshift*/
761 (binaryfunc)int_and, /*nb_and*/
762 (binaryfunc)int_xor, /*nb_xor*/
763 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000764 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000765 (unaryfunc)int_int, /*nb_int*/
766 (unaryfunc)int_long, /*nb_long*/
767 (unaryfunc)int_float, /*nb_float*/
768 (unaryfunc)int_oct, /*nb_oct*/
769 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770};
771
772typeobject Inttype = {
773 OB_HEAD_INIT(&Typetype)
774 0,
775 "int",
776 sizeof(intobject),
777 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000778 (destructor)int_dealloc, /*tp_dealloc*/
779 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 0, /*tp_getattr*/
781 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000782 (cmpfunc)int_compare, /*tp_compare*/
783 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784 &int_as_number, /*tp_as_number*/
785 0, /*tp_as_sequence*/
786 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000787 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788};