blob: 7293515e6c30981b0ee22ded5a54e04e59117bcf [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"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossumbf8c0e31994-08-29 12:48:32 +000036#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +000037#include <limits.h>
38#endif
39
40#ifndef LONG_MAX
41#define LONG_MAX 0X7FFFFFFFL
42#endif
43
44#ifndef LONG_MIN
45#define LONG_MIN (-LONG_MAX-1)
46#endif
47
48#ifndef CHAR_BIT
49#define CHAR_BIT 8
50#endif
51
Guido van Rossumb376a4a1993-11-23 17:53:17 +000052#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000053#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000054#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000055
Guido van Rossum2e1d4331993-12-24 10:22:45 +000056long
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057PyInt_GetMax()
Guido van Rossum2e1d4331993-12-24 10:22:45 +000058{
59 return LONG_MAX; /* To initialize sys.maxint */
60}
61
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyIntObject _Py_ZeroStruct = {
65 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 0
67};
Guido van Rossum3f5da241990-12-20 15:06:42 +000068
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069PyIntObject _Py_TrueStruct = {
70 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 1
72};
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074static PyObject *
Guido van Rossum3a628451991-12-10 13:57:36 +000075err_ovf(msg)
76 char *msg;
Guido van Rossum165e67e1990-10-14 20:02:26 +000077{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000079 return NULL;
80}
81
Guido van Rossum3f5da241990-12-20 15:06:42 +000082/* Integers are quite normal objects, to make object handling uniform.
83 (Using odd pointers to represent integers would save much space
84 but require extra checks for this special case throughout the code.)
85 Since, a typical Python program spends much of its time allocating
86 and deallocating integers, these operations should be very fast.
87 Therefore we use a dedicated allocation scheme with a much lower
88 overhead (in space and time) than straight malloc(): a simple
89 dedicated free list, filled when necessary with memory from malloc().
90*/
91
92#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000093#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
94#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000095
96#define PyMem_MALLOC malloc
97#define PyMem_FREE free
Guido van Rossum3fce8831999-03-12 19:43:17 +000098
99struct _intblock {
100 struct _intblock *next;
101 PyIntObject objects[N_INTOBJECTS];
102};
103
104typedef struct _intblock PyIntBlock;
105
106static PyIntBlock *block_list = NULL;
107static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109static PyIntObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110fill_free_list()
111{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 PyIntObject *p, *q;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000113 p = (PyIntObject *)PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000114 if (p == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 return (PyIntObject *)PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +0000116 ((PyIntBlock *)p)->next = block_list;
117 block_list = (PyIntBlock *)p;
118 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000119 q = p + N_INTOBJECTS;
120 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000121 q->ob_type = (struct _typeobject *)(q-1);
122 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123 return p + N_INTOBJECTS - 1;
124}
125
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000126#ifndef NSMALLPOSINTS
127#define NSMALLPOSINTS 100
128#endif
129#ifndef NSMALLNEGINTS
130#define NSMALLNEGINTS 1
131#endif
132#if NSMALLNEGINTS + NSMALLPOSINTS > 0
133/* References to small integers are saved in this array so that they
134 can be shared.
135 The integers that are saved are those in the range
136 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
137*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000139#endif
140#ifdef COUNT_ALLOCS
141int quick_int_allocs, quick_neg_int_allocs;
142#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144PyObject *
145PyInt_FromLong(ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 long ival;
147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000149#if NSMALLNEGINTS + NSMALLPOSINTS > 0
150 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
151 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000153#ifdef COUNT_ALLOCS
154 if (ival >= 0)
155 quick_int_allocs++;
156 else
157 quick_neg_int_allocs++;
158#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000160 }
161#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000162 if (free_list == NULL) {
163 if ((free_list = fill_free_list()) == NULL)
164 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 }
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 Rossumc0b618a1997-05-02 03:12:38 +0000168 v->ob_type = &PyInt_Type;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 _Py_NewReference(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000171#if NSMALLNEGINTS + NSMALLPOSINTS > 0
172 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
173 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000175 small_ints[ival + NSMALLNEGINTS] = v;
176 }
177#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000179}
180
181static void
182int_dealloc(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 PyIntObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000184{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000185 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000186 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
189long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190PyInt_AsLong(op)
191 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 PyNumberMethods *nb;
194 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000195 long val;
196
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 if (op && PyInt_Check(op))
198 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000199
200 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
201 nb->nb_int == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203 return -1;
204 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000205
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000207 if (io == NULL)
208 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 if (!PyInt_Check(io)) {
210 PyErr_SetString(PyExc_TypeError,
211 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000212 return -1;
213 }
214
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 val = PyInt_AS_LONG(io);
216 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000217
218 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219}
220
221/* Methods */
222
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000223/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000224static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225int_print(v, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227 FILE *fp;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000228 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229{
230 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000231 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232}
233
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235int_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
238 char buf[20];
239 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241}
242
243static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244int_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245 PyIntObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
247 register long i = v->ob_ival;
248 register long j = w->ob_ival;
249 return (i < j) ? -1 : (i > j) ? 1 : 0;
250}
251
Guido van Rossum9bfef441993-03-29 10:43:31 +0000252static long
253int_hash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254 PyIntObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000255{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000256 /* XXX If this is changed, you also need to change the way
257 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000258 long x = v -> ob_ival;
259 if (x == -1)
260 x = -2;
261 return x;
262}
263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000265int_add(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 PyIntObject *v;
267 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268{
269 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000271 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000273 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000274 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000279int_sub(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 PyIntObject *v;
281 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282{
283 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000285 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000287 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000288 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290}
291
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000292/*
293Integer overflow checking used to be done using a double, but on 64
294bit machines (where both long and double are 64 bit) this fails
295because the double doesn't have enouvg precision. John Tromp suggests
296the following algorithm:
297
298Suppose again we normalize a and b to be nonnegative.
299Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
300Now we test ah and bh against zero and get essentially 3 possible outcomes.
301
3021) both ah and bh > 0 : then report overflow
303
3042) both ah and bh = 0 : then compute a*b and report overflow if it comes out
305 negative
306
3073) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
308 compute al*bl and report overflow if it's negative
309 add (ah*bl)<<32 to al*bl and report overflow if
310 it's negative
311
312In case of no overflow the result is then negated if necessary.
313
314The majority of cases will be 2), in which case this method is the same as
315what I suggested before. If multiplication is expensive enough, then the
316other method is faster on case 3), but also more work to program, so I
317guess the above is the preferred solution.
318
319*/
320
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322int_mul(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 PyIntObject *v;
324 PyIntObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000326 long a, b, ah, bh, x, y;
327 int s = 1;
328
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 Rossumbf8c0e31994-08-29 12:48:32 +0000331 ah = a >> (LONG_BIT/2);
332 bh = b >> (LONG_BIT/2);
333
334 /* Quick test for common case: two small positive ints */
335
336 if (ah == 0 && bh == 0) {
337 x = a*b;
338 if (x < 0)
339 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000341 }
342
343 /* Arrange that a >= b >= 0 */
344
345 if (a < 0) {
346 a = -a;
347 if (a < 0) {
348 /* Largest negative */
349 if (b == 0 || b == 1) {
350 x = a*b;
351 goto ok;
352 }
353 else
354 goto bad;
355 }
356 s = -s;
357 ah = a >> (LONG_BIT/2);
358 }
359 if (b < 0) {
360 b = -b;
361 if (b < 0) {
362 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000363 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000364 x = a*b;
365 goto ok;
366 }
367 else
368 goto bad;
369 }
370 s = -s;
371 bh = b >> (LONG_BIT/2);
372 }
373
374 /* 1) both ah and bh > 0 : then report overflow */
375
376 if (ah != 0 && bh != 0)
377 goto bad;
378
379 /* 2) both ah and bh = 0 : then compute a*b and report
380 overflow if it comes out negative */
381
382 if (ah == 0 && bh == 0) {
383 x = a*b;
384 if (x < 0)
385 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000387 }
388
389 if (a < b) {
390 /* Swap */
391 x = a;
392 a = b;
393 b = x;
394 ah = bh;
395 /* bh not used beyond this point */
396 }
397
398 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
399 it's >= 2^31
400 compute al*bl and report overflow if it's negative
401 add (ah*bl)<<32 to al*bl and report overflow if
402 it's negative
403 (NB b == bl in this case, and we make a = al) */
404
405 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000406 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000407 goto bad;
408 a &= (1L << (LONG_BIT/2)) - 1;
409 x = a*b;
410 if (x < 0)
411 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000412 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000413 if (x < 0)
414 goto bad;
415 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000417
418 bad:
419 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
421
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000422static int
423i_divmod(x, y, p_xdivy, p_xmody)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 register PyIntObject *x, *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000425 long *p_xdivy, *p_xmody;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000427 long xi = x->ob_ival;
428 long yi = y->ob_ival;
429 long xdivy, xmody;
430
431 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 PyErr_SetString(PyExc_ZeroDivisionError,
433 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000434 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435 }
Guido van Rossum00466951991-05-05 20:08:27 +0000436 if (yi < 0) {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000437 if (xi < 0)
438 xdivy = -xi / -yi;
439 else
440 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000441 }
442 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000443 if (xi < 0)
444 xdivy = - (-xi / yi);
445 else
446 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000447 }
448 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000449 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000450 xmody += yi;
451 xdivy -= 1;
452 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000453 *p_xdivy = xdivy;
454 *p_xmody = xmody;
455 return 0;
456}
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000459int_div(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460 PyIntObject *x;
461 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000462{
463 long d, m;
464 if (i_divmod(x, y, &d, &m) < 0)
465 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000467}
468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000470int_mod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 PyIntObject *x;
472 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000473{
474 long d, m;
475 if (i_divmod(x, y, &d, &m) < 0)
476 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000478}
479
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480static PyObject *
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000481int_divmod(x, y)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482 PyIntObject *x;
483 PyIntObject *y;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000484{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000485 long d, m;
486 if (i_divmod(x, y, &d, &m) < 0)
487 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000489}
490
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491static PyObject *
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000492int_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 PyIntObject *v;
494 PyIntObject *w;
495 PyIntObject *z;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000497#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000498 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000499 iv = v->ob_ival;
500 iw = w->ob_ival;
501 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502 PyErr_SetString(PyExc_ValueError,
503 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000504 return NULL;
505 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000507 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000508 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 PyErr_SetString(PyExc_ValueError,
510 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000511 return NULL;
512 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000513 }
514 /*
515 * XXX: The original exponentiation code stopped looping
516 * when temp hit zero; this code will continue onwards
517 * unnecessarily, but at least it won't cause any errors.
518 * Hopefully the speed improvement from the fast exponentiation
519 * will compensate for the slight inefficiency.
520 * XXX: Better handling of overflows is desperately needed.
521 */
522 temp = iv;
523 ix = 1;
524 while (iw > 0) {
525 prev = ix; /* Save value for overflow check */
526 if (iw & 1) {
527 ix = ix*temp;
528 if (temp == 0)
529 break; /* Avoid ix / 0 */
530 if (ix / temp != prev)
531 return err_ovf("integer pow()");
532 }
533 iw >>= 1; /* Shift exponent down by 1 bit */
534 if (iw==0) break;
535 prev = temp;
536 temp *= temp; /* Square the value of temp */
537 if (prev!=0 && temp/prev!=prev)
538 return err_ovf("integer pow()");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000539 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000540 /* If we did a multiplication, perform a modulo */
541 ix = ix % iz;
542 temp = temp % iz;
543 }
544 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000545 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000547 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 t1=PyInt_FromLong(ix);
549 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000550 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 i_divmod((PyIntObject *)t1,
552 (PyIntObject *)t2, &div, &mod)<0)
553 {
554 Py_XDECREF(t1);
555 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000556 return(NULL);
557 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 Py_DECREF(t1);
559 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000560 ix=mod;
561 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000563#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000566 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000567 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyErr_SetString(PyExc_ValueError,
569 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000570 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 if ((PyObject *)z != Py_None) {
573 PyErr_SetString(PyExc_TypeError,
574 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000575 return NULL;
576 }
Guido van Rossum00466951991-05-05 20:08:27 +0000577 ix = 1;
578 while (--iw >= 0) {
579 long prev = ix;
580 ix = ix * iv;
581 if (iv == 0)
582 break; /* 0 to some power -- avoid ix / 0 */
583 if (ix / iv != prev)
Guido van Rossum3a628451991-12-10 13:57:36 +0000584 return err_ovf("integer pow()");
Guido van Rossum00466951991-05-05 20:08:27 +0000585 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000587#endif
588}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000591int_neg(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593{
594 register long a, x;
595 a = v->ob_ival;
596 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000597 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000598 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603int_pos(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 PyIntObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 Py_INCREF(v);
607 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608}
609
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610static PyObject *
Guido van Rossum00466951991-05-05 20:08:27 +0000611int_abs(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 PyIntObject *v;
Guido van Rossum00466951991-05-05 20:08:27 +0000613{
614 if (v->ob_ival >= 0)
615 return int_pos(v);
616 else
617 return int_neg(v);
618}
619
Guido van Rossum0bff0151991-05-14 12:05:32 +0000620static int
621int_nonzero(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000622 PyIntObject *v;
Guido van Rossum0bff0151991-05-14 12:05:32 +0000623{
624 return v->ob_ival != 0;
625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000628int_invert(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 PyIntObject *v;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000630{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000632}
633
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000635int_lshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 PyIntObject *v;
637 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000638{
639 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000640 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000641 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000642 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000644 return NULL;
645 }
646 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_INCREF(v);
648 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000650 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000652 }
653 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658int_rshift(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 PyIntObject *v;
660 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661{
662 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000663 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000664 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000665 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000667 return NULL;
668 }
669 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 Py_INCREF(v);
671 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000672 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000673 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000674 if (a < 0)
675 a = -1;
676 else
677 a = 0;
678 }
679 else {
680 if (a < 0)
681 a = ~( ~(unsigned long)a >> b );
682 else
683 a = (unsigned long)a >> b;
684 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000686}
687
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000689int_and(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 PyIntObject *v;
691 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000692{
693 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000694 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000695 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 return PyInt_FromLong(a & b);
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_xor(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 Rossumc0b618a1997-05-02 03:12:38 +0000707 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000708}
709
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710static PyObject *
Guido van Rossum7928cd71991-10-24 14:59:31 +0000711int_or(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 PyIntObject *v;
713 PyIntObject *w;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000714{
715 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000717 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000719}
720
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000722int_int(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000724{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 Py_INCREF(v);
726 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000730int_long(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000732{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000734}
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000737int_float(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000739{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000741}
742
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743static PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000744int_oct(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000746{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000747 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000748 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000749 if (x == 0)
750 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000751 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000752 sprintf(buf, "0%lo", 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 PyObject *
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000757int_hex(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758 PyIntObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000759{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000760 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000762 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000764}
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000767 (binaryfunc)int_add, /*nb_add*/
768 (binaryfunc)int_sub, /*nb_subtract*/
769 (binaryfunc)int_mul, /*nb_multiply*/
770 (binaryfunc)int_div, /*nb_divide*/
771 (binaryfunc)int_mod, /*nb_remainder*/
772 (binaryfunc)int_divmod, /*nb_divmod*/
773 (ternaryfunc)int_pow, /*nb_power*/
774 (unaryfunc)int_neg, /*nb_negative*/
775 (unaryfunc)int_pos, /*nb_positive*/
776 (unaryfunc)int_abs, /*nb_absolute*/
777 (inquiry)int_nonzero, /*nb_nonzero*/
778 (unaryfunc)int_invert, /*nb_invert*/
779 (binaryfunc)int_lshift, /*nb_lshift*/
780 (binaryfunc)int_rshift, /*nb_rshift*/
781 (binaryfunc)int_and, /*nb_and*/
782 (binaryfunc)int_xor, /*nb_xor*/
783 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000784 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000785 (unaryfunc)int_int, /*nb_int*/
786 (unaryfunc)int_long, /*nb_long*/
787 (unaryfunc)int_float, /*nb_float*/
788 (unaryfunc)int_oct, /*nb_oct*/
789 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790};
791
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792PyTypeObject PyInt_Type = {
793 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794 0,
795 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000798 (destructor)int_dealloc, /*tp_dealloc*/
799 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000800 0, /*tp_getattr*/
801 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000802 (cmpfunc)int_compare, /*tp_compare*/
803 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 &int_as_number, /*tp_as_number*/
805 0, /*tp_as_sequence*/
806 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000807 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000809
810void
811PyInt_Fini()
812{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000813 PyIntObject *p;
814 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000815 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000816 int bc, bf; /* block count, number of freed blocks */
817 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000818
Guido van Rossumda084ed1999-03-10 22:55:24 +0000819#if NSMALLNEGINTS + NSMALLPOSINTS > 0
820 PyIntObject **q;
821
822 i = NSMALLNEGINTS + NSMALLPOSINTS;
823 q = small_ints;
824 while (--i >= 0) {
825 Py_XDECREF(*q);
826 *q++ = NULL;
827 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000828#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000829 bc = 0;
830 bf = 0;
831 isum = 0;
832 list = block_list;
833 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000834 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000835 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000836 bc++;
837 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000838 for (i = 0, p = &list->objects[0];
839 i < N_INTOBJECTS;
840 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000841 if (PyInt_Check(p) && p->ob_refcnt != 0)
842 irem++;
843 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000844 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000845 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000846 list->next = block_list;
847 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000848 for (i = 0, p = &list->objects[0];
849 i < N_INTOBJECTS;
850 i++, p++) {
851 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
852 p->ob_type = (struct _typeobject *)
853 free_list;
854 free_list = p;
855 }
856#if NSMALLNEGINTS + NSMALLPOSINTS > 0
857 else if (-NSMALLNEGINTS <= p->ob_ival &&
858 p->ob_ival < NSMALLPOSINTS &&
859 small_ints[p->ob_ival +
860 NSMALLNEGINTS] == NULL) {
861 Py_INCREF(p);
862 small_ints[p->ob_ival +
863 NSMALLNEGINTS] = p;
864 }
865#endif
866 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000867 }
868 else {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000869 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +0000870 bf++;
871 }
872 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000873 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000874 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000875 if (!Py_VerboseFlag)
876 return;
877 fprintf(stderr, "# cleanup ints");
878 if (!isum) {
879 fprintf(stderr, "\n");
880 }
881 else {
882 fprintf(stderr,
883 ": %d unfreed int%s in %d out of %d block%s\n",
884 isum, isum == 1 ? "" : "s",
885 bc - bf, bc, bc == 1 ? "" : "s");
886 }
887 if (Py_VerboseFlag > 1) {
888 list = block_list;
889 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000890 for (i = 0, p = &list->objects[0];
891 i < N_INTOBJECTS;
892 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000893 if (PyInt_Check(p) && p->ob_refcnt != 0)
894 fprintf(stderr,
Guido van Rossum51288bc1999-03-19 20:30:39 +0000895 "# <int at %lx, refcnt=%d, val=%ld>\n",
Guido van Rossum3fce8831999-03-12 19:43:17 +0000896 p, p->ob_refcnt, p->ob_ival);
897 }
898 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000899 }
900 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000901}