blob: 0c8eefc4fa77f5ab27910ab1c6fb3f3054467d18 [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
97#define PyMem_MALLOC malloc
98#define PyMem_FREE free
Guido van Rossum3fce8831999-03-12 19:43:17 +000099
100struct _intblock {
101 struct _intblock *next;
102 PyIntObject objects[N_INTOBJECTS];
103};
104
105typedef struct _intblock PyIntBlock;
106
107static PyIntBlock *block_list = NULL;
108static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110static PyIntObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111fill_free_list()
112{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 PyIntObject *p, *q;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000114 p = (PyIntObject *)PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115 if (p == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 return (PyIntObject *)PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +0000117 ((PyIntBlock *)p)->next = block_list;
118 block_list = (PyIntBlock *)p;
119 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 q = p + N_INTOBJECTS;
121 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000122 q->ob_type = (struct _typeobject *)(q-1);
123 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124 return p + N_INTOBJECTS - 1;
125}
126
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000127#ifndef NSMALLPOSINTS
128#define NSMALLPOSINTS 100
129#endif
130#ifndef NSMALLNEGINTS
131#define NSMALLNEGINTS 1
132#endif
133#if NSMALLNEGINTS + NSMALLPOSINTS > 0
134/* References to small integers are saved in this array so that they
135 can be shared.
136 The integers that are saved are those in the range
137 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
138*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000140#endif
141#ifdef COUNT_ALLOCS
142int quick_int_allocs, quick_neg_int_allocs;
143#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145PyObject *
146PyInt_FromLong(ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 long ival;
148{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000150#if NSMALLNEGINTS + NSMALLPOSINTS > 0
151 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
152 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000154#ifdef COUNT_ALLOCS
155 if (ival >= 0)
156 quick_int_allocs++;
157 else
158 quick_neg_int_allocs++;
159#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000161 }
162#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163 if (free_list == NULL) {
164 if ((free_list = fill_free_list()) == NULL)
165 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000167 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000168 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 v->ob_type = &PyInt_Type;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000170 v->ob_ival = ival;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000171 _Py_NewReference((PyObject *)v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000172#if NSMALLNEGINTS + NSMALLPOSINTS > 0
173 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
174 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000176 small_ints[ival + NSMALLNEGINTS] = v;
177 }
178#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000180}
181
182static void
183int_dealloc(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 PyIntObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000185{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000186 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188}
189
190long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191PyInt_AsLong(op)
192 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 PyNumberMethods *nb;
195 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000196 long val;
197
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 if (op && PyInt_Check(op))
199 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000200
201 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
202 nb->nb_int == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 return -1;
205 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000206
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000208 if (io == NULL)
209 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210 if (!PyInt_Check(io)) {
211 PyErr_SetString(PyExc_TypeError,
212 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000213 return -1;
214 }
215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 val = PyInt_AS_LONG(io);
217 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000218
219 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
221
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000222PyObject *
223PyInt_FromString(s, pend, base)
224 char *s;
225 char **pend;
226 int base;
227{
228 char *end;
229 long x;
230 char buffer[256]; /* For errors */
231
232 if ((base != 0 && base < 2) || base > 36) {
233 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
234 return NULL;
235 }
236
237 while (*s && isspace(Py_CHARMASK(*s)))
238 s++;
239 errno = 0;
240 if (base == 0 && s[0] == '0')
241 x = (long) PyOS_strtoul(s, &end, base);
242 else
243 x = PyOS_strtol(s, &end, base);
244 if (end == s || !isalnum(end[-1]))
245 goto bad;
246 while (*end && isspace(Py_CHARMASK(*end)))
247 end++;
248 if (*end != '\0') {
249 bad:
250 sprintf(buffer, "invalid literal for int(): %.200s", s);
251 PyErr_SetString(PyExc_ValueError, buffer);
252 return NULL;
253 }
254 else if (errno != 0) {
255 sprintf(buffer, "int() literal too large: %.200s", s);
256 PyErr_SetString(PyExc_ValueError, buffer);
257 return NULL;
258 }
259 if (pend)
260 *pend = end;
261 return PyInt_FromLong(x);
262}
263
Guido van Rossum9e896b32000-04-05 20:11:21 +0000264PyObject *
265PyInt_FromUnicode(s, length, base)
266 Py_UNICODE *s;
267 int length;
268 int base;
269{
270 char buffer[256];
271
272 if (length >= sizeof(buffer)) {
273 PyErr_SetString(PyExc_ValueError,
274 "int() literal too large to convert");
275 return NULL;
276 }
277 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
278 return NULL;
279 return PyInt_FromString(buffer, NULL, base);
280}
281
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282/* Methods */
283
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000284/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000285static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000286int_print(v, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000289 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290{
291 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000292 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293}
294
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000296int_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298{
299 char buf[20];
300 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000301 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302}
303
304static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000305int_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306 PyIntObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
308 register long i = v->ob_ival;
309 register long j = w->ob_ival;
310 return (i < j) ? -1 : (i > j) ? 1 : 0;
311}
312
Guido van Rossum9bfef441993-03-29 10:43:31 +0000313static long
314int_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000315 PyIntObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000316{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000317 /* XXX If this is changed, you also need to change the way
318 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000319 long x = v -> ob_ival;
320 if (x == -1)
321 x = -2;
322 return x;
323}
324
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326int_add(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 PyIntObject *v;
328 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329{
330 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000332 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000334 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000335 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
338
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000340int_sub(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyIntObject *v;
342 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
344 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000346 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000348 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000349 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351}
352
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000353/*
354Integer overflow checking used to be done using a double, but on 64
355bit machines (where both long and double are 64 bit) this fails
356because the double doesn't have enouvg precision. John Tromp suggests
357the following algorithm:
358
359Suppose again we normalize a and b to be nonnegative.
360Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
361Now we test ah and bh against zero and get essentially 3 possible outcomes.
362
3631) both ah and bh > 0 : then report overflow
364
3652) both ah and bh = 0 : then compute a*b and report overflow if it comes out
366 negative
367
3683) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
369 compute al*bl and report overflow if it's negative
370 add (ah*bl)<<32 to al*bl and report overflow if
371 it's negative
372
373In case of no overflow the result is then negated if necessary.
374
375The majority of cases will be 2), in which case this method is the same as
376what I suggested before. If multiplication is expensive enough, then the
377other method is faster on case 3), but also more work to program, so I
378guess the above is the preferred solution.
379
380*/
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383int_mul(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384 PyIntObject *v;
385 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000387 long a, b, ah, bh, x, y;
388 int s = 1;
389
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000391 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000392 ah = a >> (LONG_BIT/2);
393 bh = b >> (LONG_BIT/2);
394
395 /* Quick test for common case: two small positive ints */
396
397 if (ah == 0 && bh == 0) {
398 x = a*b;
399 if (x < 0)
400 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000402 }
403
404 /* Arrange that a >= b >= 0 */
405
406 if (a < 0) {
407 a = -a;
408 if (a < 0) {
409 /* Largest negative */
410 if (b == 0 || b == 1) {
411 x = a*b;
412 goto ok;
413 }
414 else
415 goto bad;
416 }
417 s = -s;
418 ah = a >> (LONG_BIT/2);
419 }
420 if (b < 0) {
421 b = -b;
422 if (b < 0) {
423 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000424 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000425 x = a*b;
426 goto ok;
427 }
428 else
429 goto bad;
430 }
431 s = -s;
432 bh = b >> (LONG_BIT/2);
433 }
434
435 /* 1) both ah and bh > 0 : then report overflow */
436
437 if (ah != 0 && bh != 0)
438 goto bad;
439
440 /* 2) both ah and bh = 0 : then compute a*b and report
441 overflow if it comes out negative */
442
443 if (ah == 0 && bh == 0) {
444 x = a*b;
445 if (x < 0)
446 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000448 }
449
450 if (a < b) {
451 /* Swap */
452 x = a;
453 a = b;
454 b = x;
455 ah = bh;
456 /* bh not used beyond this point */
457 }
458
459 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
460 it's >= 2^31
461 compute al*bl and report overflow if it's negative
462 add (ah*bl)<<32 to al*bl and report overflow if
463 it's negative
464 (NB b == bl in this case, and we make a = al) */
465
466 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000467 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000468 goto bad;
469 a &= (1L << (LONG_BIT/2)) - 1;
470 x = a*b;
471 if (x < 0)
472 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000473 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000474 if (x < 0)
475 goto bad;
476 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000478
479 bad:
480 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000483static int
484i_divmod(x, y, p_xdivy, p_xmody)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 register PyIntObject *x, *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000486 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000488 long xi = x->ob_ival;
489 long yi = y->ob_ival;
490 long xdivy, xmody;
491
492 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 PyErr_SetString(PyExc_ZeroDivisionError,
494 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000495 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496 }
Guido van Rossum00466951991-05-05 20:08:27 +0000497 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000498 if (xi < 0) {
499 if (yi == -1 && -xi < 0) {
500 /* most negative / -1 */
501 err_ovf("integer division");
502 return -1;
503 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000504 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000505 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000506 else
507 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000508 }
509 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000510 if (xi < 0)
511 xdivy = - (-xi / yi);
512 else
513 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000514 }
515 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000516 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000517 xmody += yi;
518 xdivy -= 1;
519 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000520 *p_xdivy = xdivy;
521 *p_xmody = xmody;
522 return 0;
523}
524
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000526int_div(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 PyIntObject *x;
528 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000529{
530 long d, m;
531 if (i_divmod(x, y, &d, &m) < 0)
532 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000534}
535
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000537int_mod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 PyIntObject *x;
539 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000540{
541 long d, m;
542 if (i_divmod(x, y, &d, &m) < 0)
543 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000545}
546
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000548int_divmod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyIntObject *x;
550 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000551{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000552 long d, m;
553 if (i_divmod(x, y, &d, &m) < 0)
554 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000556}
557
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558static PyObject *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000559int_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 PyIntObject *v;
561 PyIntObject *w;
562 PyIntObject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000564#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000565 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000566 iv = v->ob_ival;
567 iw = w->ob_ival;
568 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyErr_SetString(PyExc_ValueError,
570 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000571 return NULL;
572 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000574 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000575 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 PyErr_SetString(PyExc_ValueError,
577 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000578 return NULL;
579 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000580 }
581 /*
582 * XXX: The original exponentiation code stopped looping
583 * when temp hit zero; this code will continue onwards
584 * unnecessarily, but at least it won't cause any errors.
585 * Hopefully the speed improvement from the fast exponentiation
586 * will compensate for the slight inefficiency.
587 * XXX: Better handling of overflows is desperately needed.
588 */
589 temp = iv;
590 ix = 1;
591 while (iw > 0) {
592 prev = ix; /* Save value for overflow check */
593 if (iw & 1) {
594 ix = ix*temp;
595 if (temp == 0)
596 break; /* Avoid ix / 0 */
597 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000598 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000599 }
600 iw >>= 1; /* Shift exponent down by 1 bit */
601 if (iw==0) break;
602 prev = temp;
603 temp *= temp; /* Square the value of temp */
604 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000605 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000606 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000607 /* If we did a multiplication, perform a modulo */
608 ix = ix % iz;
609 temp = temp % iz;
610 }
611 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000612 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000614 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 t1=PyInt_FromLong(ix);
616 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000617 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 i_divmod((PyIntObject *)t1,
619 (PyIntObject *)t2, &div, &mod)<0)
620 {
621 Py_XDECREF(t1);
622 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000623 return(NULL);
624 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625 Py_DECREF(t1);
626 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000627 ix=mod;
628 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000630#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000633 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000634 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 PyErr_SetString(PyExc_ValueError,
636 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000637 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 if ((PyObject *)z != Py_None) {
640 PyErr_SetString(PyExc_TypeError,
641 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000642 return NULL;
643 }
Guido van Rossum00466951991-05-05 20:08:27 +0000644 ix = 1;
645 while (--iw >= 0) {
646 long prev = ix;
647 ix = ix * iv;
648 if (iv == 0)
649 break; /* 0 to some power -- avoid ix / 0 */
650 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000651 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000652 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000654#endif
655}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000658int_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660{
661 register long a, x;
662 a = v->ob_ival;
663 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000664 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000665 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000670int_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 Py_INCREF(v);
674 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675}
676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677static PyObject *
Guido van Rossum00466951991-05-05 20:08:27 +0000678int_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 PyIntObject *v;
Guido van Rossum00466951991-05-05 20:08:27 +0000680{
681 if (v->ob_ival >= 0)
682 return int_pos(v);
683 else
684 return int_neg(v);
685}
686
Guido van Rossum0bff0151991-05-14 12:05:32 +0000687static int
688int_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689 PyIntObject *v;
Guido van Rossum0bff0151991-05-14 12:05:32 +0000690{
691 return v->ob_ival != 0;
692}
693
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000695int_invert(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 PyIntObject *v;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000697{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000699}
700
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000702int_lshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 PyIntObject *v;
704 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000705{
706 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000707 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000708 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000709 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000711 return NULL;
712 }
713 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 Py_INCREF(v);
715 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000716 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000717 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000719 }
720 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000722}
723
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000725int_rshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 PyIntObject *v;
727 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000728{
729 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000730 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000731 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000732 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000734 return NULL;
735 }
736 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 Py_INCREF(v);
738 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000739 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000740 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000741 if (a < 0)
742 a = -1;
743 else
744 a = 0;
745 }
746 else {
747 if (a < 0)
748 a = ~( ~(unsigned long)a >> b );
749 else
750 a = (unsigned long)a >> b;
751 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000753}
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000756int_and(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 PyIntObject *v;
758 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000759{
760 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000761 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000762 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000764}
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000767int_xor(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768 PyIntObject *v;
769 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000770{
771 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000772 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000773 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000775}
776
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000778int_or(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779 PyIntObject *v;
780 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000781{
782 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000783 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000784 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000786}
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000789int_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000791{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792 Py_INCREF(v);
793 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000794}
795
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000797int_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000799{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000801}
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000804int_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000806{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000808}
809
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000811int_oct(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000813{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000814 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000815 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000816 if (x == 0)
817 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000818 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000819 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000820 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000821}
822
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000824int_hex(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000826{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000827 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000828 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000829 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000831}
832
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000834 (binaryfunc)int_add, /*nb_add*/
835 (binaryfunc)int_sub, /*nb_subtract*/
836 (binaryfunc)int_mul, /*nb_multiply*/
837 (binaryfunc)int_div, /*nb_divide*/
838 (binaryfunc)int_mod, /*nb_remainder*/
839 (binaryfunc)int_divmod, /*nb_divmod*/
840 (ternaryfunc)int_pow, /*nb_power*/
841 (unaryfunc)int_neg, /*nb_negative*/
842 (unaryfunc)int_pos, /*nb_positive*/
843 (unaryfunc)int_abs, /*nb_absolute*/
844 (inquiry)int_nonzero, /*nb_nonzero*/
845 (unaryfunc)int_invert, /*nb_invert*/
846 (binaryfunc)int_lshift, /*nb_lshift*/
847 (binaryfunc)int_rshift, /*nb_rshift*/
848 (binaryfunc)int_and, /*nb_and*/
849 (binaryfunc)int_xor, /*nb_xor*/
850 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000851 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000852 (unaryfunc)int_int, /*nb_int*/
853 (unaryfunc)int_long, /*nb_long*/
854 (unaryfunc)int_float, /*nb_float*/
855 (unaryfunc)int_oct, /*nb_oct*/
856 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857};
858
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859PyTypeObject PyInt_Type = {
860 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861 0,
862 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000865 (destructor)int_dealloc, /*tp_dealloc*/
866 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867 0, /*tp_getattr*/
868 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000869 (cmpfunc)int_compare, /*tp_compare*/
870 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871 &int_as_number, /*tp_as_number*/
872 0, /*tp_as_sequence*/
873 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000874 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000875};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000876
877void
878PyInt_Fini()
879{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000880 PyIntObject *p;
881 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000882 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000883 int bc, bf; /* block count, number of freed blocks */
884 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000885
Guido van Rossumda084ed1999-03-10 22:55:24 +0000886#if NSMALLNEGINTS + NSMALLPOSINTS > 0
887 PyIntObject **q;
888
889 i = NSMALLNEGINTS + NSMALLPOSINTS;
890 q = small_ints;
891 while (--i >= 0) {
892 Py_XDECREF(*q);
893 *q++ = NULL;
894 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000895#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000896 bc = 0;
897 bf = 0;
898 isum = 0;
899 list = block_list;
900 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000901 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000902 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000903 bc++;
904 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000905 for (i = 0, p = &list->objects[0];
906 i < N_INTOBJECTS;
907 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000908 if (PyInt_Check(p) && p->ob_refcnt != 0)
909 irem++;
910 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000911 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000912 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000913 list->next = block_list;
914 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000915 for (i = 0, p = &list->objects[0];
916 i < N_INTOBJECTS;
917 i++, p++) {
918 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
919 p->ob_type = (struct _typeobject *)
920 free_list;
921 free_list = p;
922 }
923#if NSMALLNEGINTS + NSMALLPOSINTS > 0
924 else if (-NSMALLNEGINTS <= p->ob_ival &&
925 p->ob_ival < NSMALLPOSINTS &&
926 small_ints[p->ob_ival +
927 NSMALLNEGINTS] == NULL) {
928 Py_INCREF(p);
929 small_ints[p->ob_ival +
930 NSMALLNEGINTS] = p;
931 }
932#endif
933 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000934 }
935 else {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000936 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +0000937 bf++;
938 }
939 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000940 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000941 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000942 if (!Py_VerboseFlag)
943 return;
944 fprintf(stderr, "# cleanup ints");
945 if (!isum) {
946 fprintf(stderr, "\n");
947 }
948 else {
949 fprintf(stderr,
950 ": %d unfreed int%s in %d out of %d block%s\n",
951 isum, isum == 1 ? "" : "s",
952 bc - bf, bc, bc == 1 ? "" : "s");
953 }
954 if (Py_VerboseFlag > 1) {
955 list = block_list;
956 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000957 for (i = 0, p = &list->objects[0];
958 i < N_INTOBJECTS;
959 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000960 if (PyInt_Check(p) && p->ob_refcnt != 0)
961 fprintf(stderr,
Guido van Rossum51288bc1999-03-19 20:30:39 +0000962 "# <int at %lx, refcnt=%d, val=%ld>\n",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000963 (long)p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000964 }
965 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000966 }
967 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000968}