blob: 022c2983a6766d80641035c47c622bed74b1263f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Integer object implementation */
12
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +000014#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Guido van Rossumbf8c0e31994-08-29 12:48:32 +000016#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +000017#include <limits.h>
18#endif
19
20#ifndef LONG_MAX
21#define LONG_MAX 0X7FFFFFFFL
22#endif
23
24#ifndef LONG_MIN
25#define LONG_MIN (-LONG_MAX-1)
26#endif
27
28#ifndef CHAR_BIT
29#define CHAR_BIT 8
30#endif
31
Guido van Rossumb376a4a1993-11-23 17:53:17 +000032#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000033#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000034#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000035
Guido van Rossum2e1d4331993-12-24 10:22:45 +000036long
Fred Drakea2f55112000-07-09 15:16:51 +000037PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +000038{
39 return LONG_MAX; /* To initialize sys.maxint */
40}
41
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044PyIntObject _Py_ZeroStruct = {
45 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 0
47};
Guido van Rossum3f5da241990-12-20 15:06:42 +000048
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyIntObject _Py_TrueStruct = {
50 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 1
52};
53
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000055err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000056{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000058 return NULL;
59}
60
Guido van Rossum3f5da241990-12-20 15:06:42 +000061/* Integers are quite normal objects, to make object handling uniform.
62 (Using odd pointers to represent integers would save much space
63 but require extra checks for this special case throughout the code.)
64 Since, a typical Python program spends much of its time allocating
65 and deallocating integers, these operations should be very fast.
66 Therefore we use a dedicated allocation scheme with a much lower
67 overhead (in space and time) than straight malloc(): a simple
68 dedicated free list, filled when necessary with memory from malloc().
69*/
70
71#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000072#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
73#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000074
Guido van Rossum3fce8831999-03-12 19:43:17 +000075struct _intblock {
76 struct _intblock *next;
77 PyIntObject objects[N_INTOBJECTS];
78};
79
80typedef struct _intblock PyIntBlock;
81
82static PyIntBlock *block_list = NULL;
83static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000084
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000086fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000087{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000089 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
90 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000092 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000093 ((PyIntBlock *)p)->next = block_list;
94 block_list = (PyIntBlock *)p;
95 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000096 q = p + N_INTOBJECTS;
97 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000098 q->ob_type = (struct _typeobject *)(q-1);
99 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 return p + N_INTOBJECTS - 1;
101}
102
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifndef NSMALLPOSINTS
104#define NSMALLPOSINTS 100
105#endif
106#ifndef NSMALLNEGINTS
107#define NSMALLNEGINTS 1
108#endif
109#if NSMALLNEGINTS + NSMALLPOSINTS > 0
110/* References to small integers are saved in this array so that they
111 can be shared.
112 The integers that are saved are those in the range
113 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
114*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000116#endif
117#ifdef COUNT_ALLOCS
118int quick_int_allocs, quick_neg_int_allocs;
119#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000122PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000125#if NSMALLNEGINTS + NSMALLPOSINTS > 0
126 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
127 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000129#ifdef COUNT_ALLOCS
130 if (ival >= 0)
131 quick_int_allocs++;
132 else
133 quick_neg_int_allocs++;
134#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000136 }
137#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 if (free_list == NULL) {
139 if ((free_list = fill_free_list()) == NULL)
140 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000144 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000147#if NSMALLNEGINTS + NSMALLPOSINTS > 0
148 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
149 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000151 small_ints[ival + NSMALLNEGINTS] = v;
152 }
153#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000155}
156
157static void
Fred Drakea2f55112000-07-09 15:16:51 +0000158int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000160 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
164long
Fred Drakea2f55112000-07-09 15:16:51 +0000165PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 PyNumberMethods *nb;
168 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000169 long val;
170
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 if (op && PyInt_Check(op))
172 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000173
174 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
175 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000176 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 return -1;
178 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000179
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000181 if (io == NULL)
182 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 if (!PyInt_Check(io)) {
184 PyErr_SetString(PyExc_TypeError,
185 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000186 return -1;
187 }
188
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 val = PyInt_AS_LONG(io);
190 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000191
192 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193}
194
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000195PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000196PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197{
198 char *end;
199 long x;
200 char buffer[256]; /* For errors */
201
202 if ((base != 0 && base < 2) || base > 36) {
203 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
204 return NULL;
205 }
206
207 while (*s && isspace(Py_CHARMASK(*s)))
208 s++;
209 errno = 0;
210 if (base == 0 && s[0] == '0')
211 x = (long) PyOS_strtoul(s, &end, base);
212 else
213 x = PyOS_strtol(s, &end, base);
214 if (end == s || !isalnum(end[-1]))
215 goto bad;
216 while (*end && isspace(Py_CHARMASK(*end)))
217 end++;
218 if (*end != '\0') {
219 bad:
220 sprintf(buffer, "invalid literal for int(): %.200s", s);
221 PyErr_SetString(PyExc_ValueError, buffer);
222 return NULL;
223 }
224 else if (errno != 0) {
225 sprintf(buffer, "int() literal too large: %.200s", s);
226 PyErr_SetString(PyExc_ValueError, buffer);
227 return NULL;
228 }
229 if (pend)
230 *pend = end;
231 return PyInt_FromLong(x);
232}
233
Guido van Rossum9e896b32000-04-05 20:11:21 +0000234PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000235PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000236{
237 char buffer[256];
238
239 if (length >= sizeof(buffer)) {
240 PyErr_SetString(PyExc_ValueError,
241 "int() literal too large to convert");
242 return NULL;
243 }
244 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
245 return NULL;
246 return PyInt_FromString(buffer, NULL, base);
247}
248
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249/* Methods */
250
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000251/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000252static int
Fred Drakea2f55112000-07-09 15:16:51 +0000253int_print(PyIntObject *v, FILE *fp, int flags)
254 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
256 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000257 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}
259
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000261int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
263 char buf[20];
264 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}
267
268static int
Fred Drakea2f55112000-07-09 15:16:51 +0000269int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
271 register long i = v->ob_ival;
272 register long j = w->ob_ival;
273 return (i < j) ? -1 : (i > j) ? 1 : 0;
274}
275
Guido van Rossum9bfef441993-03-29 10:43:31 +0000276static long
Fred Drakea2f55112000-07-09 15:16:51 +0000277int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000279 /* XXX If this is changed, you also need to change the way
280 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000281 long x = v -> ob_ival;
282 if (x == -1)
283 x = -2;
284 return x;
285}
286
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000288int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289{
290 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000292 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000294 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000295 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297}
298
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000300int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301{
302 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000304 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000306 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000307 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309}
310
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000311/*
312Integer overflow checking used to be done using a double, but on 64
313bit machines (where both long and double are 64 bit) this fails
314because the double doesn't have enouvg precision. John Tromp suggests
315the following algorithm:
316
317Suppose again we normalize a and b to be nonnegative.
318Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
319Now we test ah and bh against zero and get essentially 3 possible outcomes.
320
3211) both ah and bh > 0 : then report overflow
322
3232) both ah and bh = 0 : then compute a*b and report overflow if it comes out
324 negative
325
3263) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
327 compute al*bl and report overflow if it's negative
328 add (ah*bl)<<32 to al*bl and report overflow if
329 it's negative
330
331In case of no overflow the result is then negated if necessary.
332
333The majority of cases will be 2), in which case this method is the same as
334what I suggested before. If multiplication is expensive enough, then the
335other method is faster on case 3), but also more work to program, so I
336guess the above is the preferred solution.
337
338*/
339
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000341int_mul(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000343 long a, b, ah, bh, x, y;
344 int s = 1;
345
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000347 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000348 ah = a >> (LONG_BIT/2);
349 bh = b >> (LONG_BIT/2);
350
351 /* Quick test for common case: two small positive ints */
352
353 if (ah == 0 && bh == 0) {
354 x = a*b;
355 if (x < 0)
356 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000358 }
359
360 /* Arrange that a >= b >= 0 */
361
362 if (a < 0) {
363 a = -a;
364 if (a < 0) {
365 /* Largest negative */
366 if (b == 0 || b == 1) {
367 x = a*b;
368 goto ok;
369 }
370 else
371 goto bad;
372 }
373 s = -s;
374 ah = a >> (LONG_BIT/2);
375 }
376 if (b < 0) {
377 b = -b;
378 if (b < 0) {
379 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000380 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000381 x = a*b;
382 goto ok;
383 }
384 else
385 goto bad;
386 }
387 s = -s;
388 bh = b >> (LONG_BIT/2);
389 }
390
391 /* 1) both ah and bh > 0 : then report overflow */
392
393 if (ah != 0 && bh != 0)
394 goto bad;
395
396 /* 2) both ah and bh = 0 : then compute a*b and report
397 overflow if it comes out negative */
398
399 if (ah == 0 && bh == 0) {
400 x = a*b;
401 if (x < 0)
402 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000404 }
405
406 if (a < b) {
407 /* Swap */
408 x = a;
409 a = b;
410 b = x;
411 ah = bh;
412 /* bh not used beyond this point */
413 }
414
415 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
416 it's >= 2^31
417 compute al*bl and report overflow if it's negative
418 add (ah*bl)<<32 to al*bl and report overflow if
419 it's negative
420 (NB b == bl in this case, and we make a = al) */
421
422 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000423 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000424 goto bad;
425 a &= (1L << (LONG_BIT/2)) - 1;
426 x = a*b;
427 if (x < 0)
428 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000429 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000430 if (x < 0)
431 goto bad;
432 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000434
435 bad:
436 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000439static int
Fred Drakea2f55112000-07-09 15:16:51 +0000440i_divmod(register PyIntObject *x, register PyIntObject *y,
441 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000443 long xi = x->ob_ival;
444 long yi = y->ob_ival;
445 long xdivy, xmody;
446
447 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 PyErr_SetString(PyExc_ZeroDivisionError,
449 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000450 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 }
Guido van Rossum00466951991-05-05 20:08:27 +0000452 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000453 if (xi < 0) {
454 if (yi == -1 && -xi < 0) {
455 /* most negative / -1 */
456 err_ovf("integer division");
457 return -1;
458 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000459 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000460 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000461 else
462 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000463 }
464 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000465 if (xi < 0)
466 xdivy = - (-xi / yi);
467 else
468 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000469 }
470 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000471 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000472 xmody += yi;
473 xdivy -= 1;
474 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475 *p_xdivy = xdivy;
476 *p_xmody = xmody;
477 return 0;
478}
479
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000481int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000482{
483 long d, m;
484 if (i_divmod(x, y, &d, &m) < 0)
485 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000490int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000491{
492 long d, m;
493 if (i_divmod(x, y, &d, &m) < 0)
494 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000496}
497
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000499int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000500{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000501 long d, m;
502 if (i_divmod(x, y, &d, &m) < 0)
503 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000505}
506
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000508int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000510#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000511 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000512 iv = v->ob_ival;
513 iw = w->ob_ival;
514 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 PyErr_SetString(PyExc_ValueError,
516 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000517 return NULL;
518 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000520 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000521 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 PyErr_SetString(PyExc_ValueError,
523 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000524 return NULL;
525 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000526 }
527 /*
528 * XXX: The original exponentiation code stopped looping
529 * when temp hit zero; this code will continue onwards
530 * unnecessarily, but at least it won't cause any errors.
531 * Hopefully the speed improvement from the fast exponentiation
532 * will compensate for the slight inefficiency.
533 * XXX: Better handling of overflows is desperately needed.
534 */
535 temp = iv;
536 ix = 1;
537 while (iw > 0) {
538 prev = ix; /* Save value for overflow check */
539 if (iw & 1) {
540 ix = ix*temp;
541 if (temp == 0)
542 break; /* Avoid ix / 0 */
543 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000544 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000545 }
546 iw >>= 1; /* Shift exponent down by 1 bit */
547 if (iw==0) break;
548 prev = temp;
549 temp *= temp; /* Square the value of temp */
550 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000551 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000552 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000553 /* If we did a multiplication, perform a modulo */
554 ix = ix % iz;
555 temp = temp % iz;
556 }
557 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000558 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000560 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 t1=PyInt_FromLong(ix);
562 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000563 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 i_divmod((PyIntObject *)t1,
565 (PyIntObject *)t2, &div, &mod)<0)
566 {
567 Py_XDECREF(t1);
568 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000569 return(NULL);
570 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 Py_DECREF(t1);
572 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000573 ix=mod;
574 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000576#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000579 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000580 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyErr_SetString(PyExc_ValueError,
582 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000583 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 if ((PyObject *)z != Py_None) {
586 PyErr_SetString(PyExc_TypeError,
587 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000588 return NULL;
589 }
Guido van Rossum00466951991-05-05 20:08:27 +0000590 ix = 1;
591 while (--iw >= 0) {
592 long prev = ix;
593 ix = ix * iv;
594 if (iv == 0)
595 break; /* 0 to some power -- avoid ix / 0 */
596 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000597 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000600#endif
601}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000604int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
606 register long a, x;
607 a = v->ob_ival;
608 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000609 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000610 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000615int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 Py_INCREF(v);
618 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000623{
624 if (v->ob_ival >= 0)
625 return int_pos(v);
626 else
627 return int_neg(v);
628}
629
Guido van Rossum0bff0151991-05-14 12:05:32 +0000630static int
Fred Drakea2f55112000-07-09 15:16:51 +0000631int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000632{
633 return v->ob_ival != 0;
634}
635
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000637int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000638{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000643int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000644{
645 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000646 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000647 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000648 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000650 return NULL;
651 }
652 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 Py_INCREF(v);
654 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000655 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000656 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000658 }
659 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661}
662
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000664int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000665{
666 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000669 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000671 return NULL;
672 }
673 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 Py_INCREF(v);
675 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000676 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000677 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000678 if (a < 0)
679 a = -1;
680 else
681 a = 0;
682 }
683 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000684 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000685 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000687}
688
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000690int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691{
692 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000694 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000696}
697
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000699int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000700{
701 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000702 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000703 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000705}
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000708int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000709{
710 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000711 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000712 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000714}
715
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000717int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000718{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 Py_INCREF(v);
720 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000721}
722
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000724int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000725{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000730int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000731{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000733}
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000736int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000737{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000738 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000739 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000740 if (x == 0)
741 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000742 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000743 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000745}
746
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000748int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000749{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000750 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000751 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000752 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000754}
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000757 (binaryfunc)int_add, /*nb_add*/
758 (binaryfunc)int_sub, /*nb_subtract*/
759 (binaryfunc)int_mul, /*nb_multiply*/
760 (binaryfunc)int_div, /*nb_divide*/
761 (binaryfunc)int_mod, /*nb_remainder*/
762 (binaryfunc)int_divmod, /*nb_divmod*/
763 (ternaryfunc)int_pow, /*nb_power*/
764 (unaryfunc)int_neg, /*nb_negative*/
765 (unaryfunc)int_pos, /*nb_positive*/
766 (unaryfunc)int_abs, /*nb_absolute*/
767 (inquiry)int_nonzero, /*nb_nonzero*/
768 (unaryfunc)int_invert, /*nb_invert*/
769 (binaryfunc)int_lshift, /*nb_lshift*/
770 (binaryfunc)int_rshift, /*nb_rshift*/
771 (binaryfunc)int_and, /*nb_and*/
772 (binaryfunc)int_xor, /*nb_xor*/
773 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000774 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000775 (unaryfunc)int_int, /*nb_int*/
776 (unaryfunc)int_long, /*nb_long*/
777 (unaryfunc)int_float, /*nb_float*/
778 (unaryfunc)int_oct, /*nb_oct*/
779 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780};
781
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782PyTypeObject PyInt_Type = {
783 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784 0,
785 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000788 (destructor)int_dealloc, /*tp_dealloc*/
789 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790 0, /*tp_getattr*/
791 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000792 (cmpfunc)int_compare, /*tp_compare*/
793 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794 &int_as_number, /*tp_as_number*/
795 0, /*tp_as_sequence*/
796 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000797 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000799
800void
Fred Drakea2f55112000-07-09 15:16:51 +0000801PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000802{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000803 PyIntObject *p;
804 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000805 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000806 int bc, bf; /* block count, number of freed blocks */
807 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000808
Guido van Rossumda084ed1999-03-10 22:55:24 +0000809#if NSMALLNEGINTS + NSMALLPOSINTS > 0
810 PyIntObject **q;
811
812 i = NSMALLNEGINTS + NSMALLPOSINTS;
813 q = small_ints;
814 while (--i >= 0) {
815 Py_XDECREF(*q);
816 *q++ = NULL;
817 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000818#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000819 bc = 0;
820 bf = 0;
821 isum = 0;
822 list = block_list;
823 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000824 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000825 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000826 bc++;
827 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000828 for (i = 0, p = &list->objects[0];
829 i < N_INTOBJECTS;
830 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000831 if (PyInt_Check(p) && p->ob_refcnt != 0)
832 irem++;
833 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000834 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000835 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000836 list->next = block_list;
837 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000838 for (i = 0, p = &list->objects[0];
839 i < N_INTOBJECTS;
840 i++, p++) {
841 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
842 p->ob_type = (struct _typeobject *)
843 free_list;
844 free_list = p;
845 }
846#if NSMALLNEGINTS + NSMALLPOSINTS > 0
847 else if (-NSMALLNEGINTS <= p->ob_ival &&
848 p->ob_ival < NSMALLPOSINTS &&
849 small_ints[p->ob_ival +
850 NSMALLNEGINTS] == NULL) {
851 Py_INCREF(p);
852 small_ints[p->ob_ival +
853 NSMALLNEGINTS] = p;
854 }
855#endif
856 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000857 }
858 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000859 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000860 bf++;
861 }
862 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000863 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000864 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000865 if (!Py_VerboseFlag)
866 return;
867 fprintf(stderr, "# cleanup ints");
868 if (!isum) {
869 fprintf(stderr, "\n");
870 }
871 else {
872 fprintf(stderr,
873 ": %d unfreed int%s in %d out of %d block%s\n",
874 isum, isum == 1 ? "" : "s",
875 bc - bf, bc, bc == 1 ? "" : "s");
876 }
877 if (Py_VerboseFlag > 1) {
878 list = block_list;
879 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000880 for (i = 0, p = &list->objects[0];
881 i < N_INTOBJECTS;
882 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000883 if (PyInt_Check(p) && p->ob_refcnt != 0)
884 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000885 "# <int at %p, refcnt=%d, val=%ld>\n",
886 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000887 }
888 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000889 }
890 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000891}