blob: 79435a990a55c0c1a16e6d8a02e509b389af9991 [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 Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +000035#include <ctype.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
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058PyInt_GetMax()
Guido van Rossum2e1d4331993-12-24 10:22:45 +000059{
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 Rossumc0b618a1997-05-02 03:12:38 +000065PyIntObject _Py_ZeroStruct = {
66 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 0
68};
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070PyIntObject _Py_TrueStruct = {
71 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 1
73};
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075static PyObject *
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 Rossumc0b618a1997-05-02 03:12:38 +000079 PyErr_SetString(PyExc_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 */
Guido van Rossum3fce8831999-03-12 19:43:17 +000094#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
95#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000096
Guido van Rossum3fce8831999-03-12 19:43:17 +000097struct _intblock {
98 struct _intblock *next;
99 PyIntObject objects[N_INTOBJECTS];
100};
101
102typedef struct _intblock PyIntBlock;
103
104static PyIntBlock *block_list = NULL;
105static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000106
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107static PyIntObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108fill_free_list()
109{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
112 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000114 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +0000115 ((PyIntBlock *)p)->next = block_list;
116 block_list = (PyIntBlock *)p;
117 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000118 q = p + N_INTOBJECTS;
119 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000120 q->ob_type = (struct _typeobject *)(q-1);
121 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122 return p + N_INTOBJECTS - 1;
123}
124
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000125#ifndef NSMALLPOSINTS
126#define NSMALLPOSINTS 100
127#endif
128#ifndef NSMALLNEGINTS
129#define NSMALLNEGINTS 1
130#endif
131#if NSMALLNEGINTS + NSMALLPOSINTS > 0
132/* References to small integers are saved in this array so that they
133 can be shared.
134 The integers that are saved are those in the range
135 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
136*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000138#endif
139#ifdef COUNT_ALLOCS
140int quick_int_allocs, quick_neg_int_allocs;
141#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143PyObject *
144PyInt_FromLong(ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 long ival;
146{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000148#if NSMALLNEGINTS + NSMALLPOSINTS > 0
149 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
150 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000151 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000152#ifdef COUNT_ALLOCS
153 if (ival >= 0)
154 quick_int_allocs++;
155 else
156 quick_neg_int_allocs++;
157#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000159 }
160#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161 if (free_list == NULL) {
162 if ((free_list = fill_free_list()) == NULL)
163 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000165 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000166 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000167 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000168 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000170#if NSMALLNEGINTS + NSMALLPOSINTS > 0
171 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
172 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000174 small_ints[ival + NSMALLNEGINTS] = v;
175 }
176#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000178}
179
180static void
181int_dealloc(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 PyIntObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000184 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000185 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186}
187
188long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189PyInt_AsLong(op)
190 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 PyNumberMethods *nb;
193 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000194 long val;
195
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 if (op && PyInt_Check(op))
197 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000198
199 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
200 nb->nb_int == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 return -1;
203 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000204
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000206 if (io == NULL)
207 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 if (!PyInt_Check(io)) {
209 PyErr_SetString(PyExc_TypeError,
210 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000211 return -1;
212 }
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 val = PyInt_AS_LONG(io);
215 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000216
217 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000220PyObject *
221PyInt_FromString(s, pend, base)
222 char *s;
223 char **pend;
224 int base;
225{
226 char *end;
227 long x;
228 char buffer[256]; /* For errors */
229
230 if ((base != 0 && base < 2) || base > 36) {
231 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
232 return NULL;
233 }
234
235 while (*s && isspace(Py_CHARMASK(*s)))
236 s++;
237 errno = 0;
238 if (base == 0 && s[0] == '0')
239 x = (long) PyOS_strtoul(s, &end, base);
240 else
241 x = PyOS_strtol(s, &end, base);
242 if (end == s || !isalnum(end[-1]))
243 goto bad;
244 while (*end && isspace(Py_CHARMASK(*end)))
245 end++;
246 if (*end != '\0') {
247 bad:
248 sprintf(buffer, "invalid literal for int(): %.200s", s);
249 PyErr_SetString(PyExc_ValueError, buffer);
250 return NULL;
251 }
252 else if (errno != 0) {
253 sprintf(buffer, "int() literal too large: %.200s", s);
254 PyErr_SetString(PyExc_ValueError, buffer);
255 return NULL;
256 }
257 if (pend)
258 *pend = end;
259 return PyInt_FromLong(x);
260}
261
Guido van Rossum9e896b32000-04-05 20:11:21 +0000262PyObject *
263PyInt_FromUnicode(s, length, base)
264 Py_UNICODE *s;
265 int length;
266 int base;
267{
268 char buffer[256];
269
270 if (length >= sizeof(buffer)) {
271 PyErr_SetString(PyExc_ValueError,
272 "int() literal too large to convert");
273 return NULL;
274 }
275 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
276 return NULL;
277 return PyInt_FromString(buffer, NULL, base);
278}
279
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280/* Methods */
281
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000282/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000283static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000284int_print(v, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000287 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288{
289 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000290 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291}
292
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000294int_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296{
297 char buf[20];
298 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300}
301
302static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000303int_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyIntObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
306 register long i = v->ob_ival;
307 register long j = w->ob_ival;
308 return (i < j) ? -1 : (i > j) ? 1 : 0;
309}
310
Guido van Rossum9bfef441993-03-29 10:43:31 +0000311static long
312int_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 PyIntObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000314{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000315 /* XXX If this is changed, you also need to change the way
316 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000317 long x = v -> ob_ival;
318 if (x == -1)
319 x = -2;
320 return x;
321}
322
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324int_add(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325 PyIntObject *v;
326 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327{
328 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000330 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000332 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000333 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
336
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000338int_sub(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 PyIntObject *v;
340 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
342 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000344 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000346 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000347 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349}
350
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000351/*
352Integer overflow checking used to be done using a double, but on 64
353bit machines (where both long and double are 64 bit) this fails
354because the double doesn't have enouvg precision. John Tromp suggests
355the following algorithm:
356
357Suppose again we normalize a and b to be nonnegative.
358Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
359Now we test ah and bh against zero and get essentially 3 possible outcomes.
360
3611) both ah and bh > 0 : then report overflow
362
3632) both ah and bh = 0 : then compute a*b and report overflow if it comes out
364 negative
365
3663) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
367 compute al*bl and report overflow if it's negative
368 add (ah*bl)<<32 to al*bl and report overflow if
369 it's negative
370
371In case of no overflow the result is then negated if necessary.
372
373The majority of cases will be 2), in which case this method is the same as
374what I suggested before. If multiplication is expensive enough, then the
375other method is faster on case 3), but also more work to program, so I
376guess the above is the preferred solution.
377
378*/
379
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381int_mul(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 PyIntObject *v;
383 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000385 long a, b, ah, bh, x, y;
386 int s = 1;
387
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000389 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000390 ah = a >> (LONG_BIT/2);
391 bh = b >> (LONG_BIT/2);
392
393 /* Quick test for common case: two small positive ints */
394
395 if (ah == 0 && bh == 0) {
396 x = a*b;
397 if (x < 0)
398 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000400 }
401
402 /* Arrange that a >= b >= 0 */
403
404 if (a < 0) {
405 a = -a;
406 if (a < 0) {
407 /* Largest negative */
408 if (b == 0 || b == 1) {
409 x = a*b;
410 goto ok;
411 }
412 else
413 goto bad;
414 }
415 s = -s;
416 ah = a >> (LONG_BIT/2);
417 }
418 if (b < 0) {
419 b = -b;
420 if (b < 0) {
421 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000422 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000423 x = a*b;
424 goto ok;
425 }
426 else
427 goto bad;
428 }
429 s = -s;
430 bh = b >> (LONG_BIT/2);
431 }
432
433 /* 1) both ah and bh > 0 : then report overflow */
434
435 if (ah != 0 && bh != 0)
436 goto bad;
437
438 /* 2) both ah and bh = 0 : then compute a*b and report
439 overflow if it comes out negative */
440
441 if (ah == 0 && bh == 0) {
442 x = a*b;
443 if (x < 0)
444 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000446 }
447
448 if (a < b) {
449 /* Swap */
450 x = a;
451 a = b;
452 b = x;
453 ah = bh;
454 /* bh not used beyond this point */
455 }
456
457 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
458 it's >= 2^31
459 compute al*bl and report overflow if it's negative
460 add (ah*bl)<<32 to al*bl and report overflow if
461 it's negative
462 (NB b == bl in this case, and we make a = al) */
463
464 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000465 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000466 goto bad;
467 a &= (1L << (LONG_BIT/2)) - 1;
468 x = a*b;
469 if (x < 0)
470 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000471 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000472 if (x < 0)
473 goto bad;
474 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000476
477 bad:
478 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479}
480
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000481static int
482i_divmod(x, y, p_xdivy, p_xmody)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 register PyIntObject *x, *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000484 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000486 long xi = x->ob_ival;
487 long yi = y->ob_ival;
488 long xdivy, xmody;
489
490 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 PyErr_SetString(PyExc_ZeroDivisionError,
492 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000493 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494 }
Guido van Rossum00466951991-05-05 20:08:27 +0000495 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000496 if (xi < 0) {
497 if (yi == -1 && -xi < 0) {
498 /* most negative / -1 */
499 err_ovf("integer division");
500 return -1;
501 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000502 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000503 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000504 else
505 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000506 }
507 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000508 if (xi < 0)
509 xdivy = - (-xi / yi);
510 else
511 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000512 }
513 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000514 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000515 xmody += yi;
516 xdivy -= 1;
517 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000518 *p_xdivy = xdivy;
519 *p_xmody = xmody;
520 return 0;
521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000524int_div(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 PyIntObject *x;
526 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000527{
528 long d, m;
529 if (i_divmod(x, y, &d, &m) < 0)
530 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000532}
533
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000535int_mod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 PyIntObject *x;
537 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000538{
539 long d, m;
540 if (i_divmod(x, y, &d, &m) < 0)
541 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000543}
544
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000546int_divmod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 PyIntObject *x;
548 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000549{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000550 long d, m;
551 if (i_divmod(x, y, &d, &m) < 0)
552 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000554}
555
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556static PyObject *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000557int_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyIntObject *v;
559 PyIntObject *w;
560 PyIntObject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000562#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000563 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000564 iv = v->ob_ival;
565 iw = w->ob_ival;
566 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyErr_SetString(PyExc_ValueError,
568 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000569 return NULL;
570 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000572 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000573 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 PyErr_SetString(PyExc_ValueError,
575 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000576 return NULL;
577 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000578 }
579 /*
580 * XXX: The original exponentiation code stopped looping
581 * when temp hit zero; this code will continue onwards
582 * unnecessarily, but at least it won't cause any errors.
583 * Hopefully the speed improvement from the fast exponentiation
584 * will compensate for the slight inefficiency.
585 * XXX: Better handling of overflows is desperately needed.
586 */
587 temp = iv;
588 ix = 1;
589 while (iw > 0) {
590 prev = ix; /* Save value for overflow check */
591 if (iw & 1) {
592 ix = ix*temp;
593 if (temp == 0)
594 break; /* Avoid ix / 0 */
595 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000596 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000597 }
598 iw >>= 1; /* Shift exponent down by 1 bit */
599 if (iw==0) break;
600 prev = temp;
601 temp *= temp; /* Square the value of temp */
602 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000603 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000604 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000605 /* If we did a multiplication, perform a modulo */
606 ix = ix % iz;
607 temp = temp % iz;
608 }
609 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000610 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000612 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 t1=PyInt_FromLong(ix);
614 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000615 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 i_divmod((PyIntObject *)t1,
617 (PyIntObject *)t2, &div, &mod)<0)
618 {
619 Py_XDECREF(t1);
620 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000621 return(NULL);
622 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623 Py_DECREF(t1);
624 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000625 ix=mod;
626 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000628#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000631 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000632 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 PyErr_SetString(PyExc_ValueError,
634 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000635 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 if ((PyObject *)z != Py_None) {
638 PyErr_SetString(PyExc_TypeError,
639 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000640 return NULL;
641 }
Guido van Rossum00466951991-05-05 20:08:27 +0000642 ix = 1;
643 while (--iw >= 0) {
644 long prev = ix;
645 ix = ix * iv;
646 if (iv == 0)
647 break; /* 0 to some power -- avoid ix / 0 */
648 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000649 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000650 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000652#endif
653}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000656int_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658{
659 register long a, x;
660 a = v->ob_ival;
661 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000662 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000663 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665}
666
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000668int_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_INCREF(v);
672 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673}
674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675static PyObject *
Guido van Rossum00466951991-05-05 20:08:27 +0000676int_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 PyIntObject *v;
Guido van Rossum00466951991-05-05 20:08:27 +0000678{
679 if (v->ob_ival >= 0)
680 return int_pos(v);
681 else
682 return int_neg(v);
683}
684
Guido van Rossum0bff0151991-05-14 12:05:32 +0000685static int
686int_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 PyIntObject *v;
Guido van Rossum0bff0151991-05-14 12:05:32 +0000688{
689 return v->ob_ival != 0;
690}
691
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693int_invert(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyIntObject *v;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000695{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000697}
698
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000700int_lshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701 PyIntObject *v;
702 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000703{
704 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000705 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000706 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000707 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000709 return NULL;
710 }
711 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 Py_INCREF(v);
713 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000714 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000715 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000717 }
718 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000720}
721
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000723int_rshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 PyIntObject *v;
725 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000726{
727 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000728 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000729 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000730 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000732 return NULL;
733 }
734 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 Py_INCREF(v);
736 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000737 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000738 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000739 if (a < 0)
740 a = -1;
741 else
742 a = 0;
743 }
744 else {
745 if (a < 0)
746 a = ~( ~(unsigned long)a >> b );
747 else
748 a = (unsigned long)a >> b;
749 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754int_and(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 PyIntObject *v;
756 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000757{
758 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000759 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000760 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000762}
763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000765int_xor(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 PyIntObject *v;
767 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000768{
769 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000770 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000771 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000773}
774
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000776int_or(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777 PyIntObject *v;
778 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000779{
780 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000781 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000782 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000784}
785
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000787int_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000789{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 Py_INCREF(v);
791 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000792}
793
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000795int_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000797{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000799}
800
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000802int_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000804{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000806}
807
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000809int_oct(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000811{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000812 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000813 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000814 if (x == 0)
815 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000816 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000817 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000819}
820
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000822int_hex(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000824{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000825 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000826 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000827 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000829}
830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000832 (binaryfunc)int_add, /*nb_add*/
833 (binaryfunc)int_sub, /*nb_subtract*/
834 (binaryfunc)int_mul, /*nb_multiply*/
835 (binaryfunc)int_div, /*nb_divide*/
836 (binaryfunc)int_mod, /*nb_remainder*/
837 (binaryfunc)int_divmod, /*nb_divmod*/
838 (ternaryfunc)int_pow, /*nb_power*/
839 (unaryfunc)int_neg, /*nb_negative*/
840 (unaryfunc)int_pos, /*nb_positive*/
841 (unaryfunc)int_abs, /*nb_absolute*/
842 (inquiry)int_nonzero, /*nb_nonzero*/
843 (unaryfunc)int_invert, /*nb_invert*/
844 (binaryfunc)int_lshift, /*nb_lshift*/
845 (binaryfunc)int_rshift, /*nb_rshift*/
846 (binaryfunc)int_and, /*nb_and*/
847 (binaryfunc)int_xor, /*nb_xor*/
848 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000849 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000850 (unaryfunc)int_int, /*nb_int*/
851 (unaryfunc)int_long, /*nb_long*/
852 (unaryfunc)int_float, /*nb_float*/
853 (unaryfunc)int_oct, /*nb_oct*/
854 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000855};
856
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857PyTypeObject PyInt_Type = {
858 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 0,
860 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000863 (destructor)int_dealloc, /*tp_dealloc*/
864 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865 0, /*tp_getattr*/
866 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000867 (cmpfunc)int_compare, /*tp_compare*/
868 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 &int_as_number, /*tp_as_number*/
870 0, /*tp_as_sequence*/
871 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000872 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000874
875void
876PyInt_Fini()
877{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000878 PyIntObject *p;
879 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000880 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000881 int bc, bf; /* block count, number of freed blocks */
882 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000883
Guido van Rossumda084ed1999-03-10 22:55:24 +0000884#if NSMALLNEGINTS + NSMALLPOSINTS > 0
885 PyIntObject **q;
886
887 i = NSMALLNEGINTS + NSMALLPOSINTS;
888 q = small_ints;
889 while (--i >= 0) {
890 Py_XDECREF(*q);
891 *q++ = NULL;
892 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000893#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000894 bc = 0;
895 bf = 0;
896 isum = 0;
897 list = block_list;
898 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000899 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000900 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000901 bc++;
902 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000903 for (i = 0, p = &list->objects[0];
904 i < N_INTOBJECTS;
905 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000906 if (PyInt_Check(p) && p->ob_refcnt != 0)
907 irem++;
908 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000909 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000910 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000911 list->next = block_list;
912 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000913 for (i = 0, p = &list->objects[0];
914 i < N_INTOBJECTS;
915 i++, p++) {
916 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
917 p->ob_type = (struct _typeobject *)
918 free_list;
919 free_list = p;
920 }
921#if NSMALLNEGINTS + NSMALLPOSINTS > 0
922 else if (-NSMALLNEGINTS <= p->ob_ival &&
923 p->ob_ival < NSMALLPOSINTS &&
924 small_ints[p->ob_ival +
925 NSMALLNEGINTS] == NULL) {
926 Py_INCREF(p);
927 small_ints[p->ob_ival +
928 NSMALLNEGINTS] = p;
929 }
930#endif
931 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000932 }
933 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000934 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000935 bf++;
936 }
937 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000938 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000939 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000940 if (!Py_VerboseFlag)
941 return;
942 fprintf(stderr, "# cleanup ints");
943 if (!isum) {
944 fprintf(stderr, "\n");
945 }
946 else {
947 fprintf(stderr,
948 ": %d unfreed int%s in %d out of %d block%s\n",
949 isum, isum == 1 ? "" : "s",
950 bc - bf, bc, bc == 1 ? "" : "s");
951 }
952 if (Py_VerboseFlag > 1) {
953 list = block_list;
954 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000955 for (i = 0, p = &list->objects[0];
956 i < N_INTOBJECTS;
957 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000958 if (PyInt_Check(p) && p->ob_refcnt != 0)
959 fprintf(stderr,
Guido van Rossum51288bc1999-03-19 20:30:39 +0000960 "# <int at %lx, refcnt=%d, val=%ld>\n",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000961 (long)p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000962 }
963 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000964 }
965 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000966}