blob: 18624b31ef1d7044a4ad2dcbec6a0054d7e1b3fd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossume27f7952001-08-23 02:59:04 +000013/* Return 1 if exception raised, 0 if caller should retry using longs */
14static int
Fred Drakea2f55112000-07-09 15:16:51 +000015err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000016{
Guido van Rossume27f7952001-08-23 02:59:04 +000017 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
Guido van Rossum0b131162001-08-23 21:32:40 +000018 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
19 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000020 return 1;
21 }
22 else
23 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000024}
25
Guido van Rossum3f5da241990-12-20 15:06:42 +000026/* Integers are quite normal objects, to make object handling uniform.
27 (Using odd pointers to represent integers would save much space
28 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000029 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000030 and deallocating integers, these operations should be very fast.
31 Therefore we use a dedicated allocation scheme with a much lower
32 overhead (in space and time) than straight malloc(): a simple
33 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000034
35 block_list is a singly-linked list of all PyIntBlocks ever allocated,
36 linked via their next members. PyIntBlocks are never returned to the
37 system before shutdown (PyInt_Fini).
38
39 free_list is a singly-linked list of available PyIntObjects, linked
40 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000041*/
42
43#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000044#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
45#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000046
Guido van Rossum3fce8831999-03-12 19:43:17 +000047struct _intblock {
48 struct _intblock *next;
49 PyIntObject objects[N_INTOBJECTS];
50};
51
52typedef struct _intblock PyIntBlock;
53
54static PyIntBlock *block_list = NULL;
55static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000058fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000061 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000062 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000065 ((PyIntBlock *)p)->next = block_list;
66 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000067 /* Link the int objects together, from rear to front, then return
68 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000069 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 q = p + N_INTOBJECTS;
71 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000072 q->ob_type = (struct _typeobject *)(q-1);
73 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 return p + N_INTOBJECTS - 1;
75}
76
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#ifndef NSMALLPOSINTS
78#define NSMALLPOSINTS 100
79#endif
80#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000081#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000082#endif
83#if NSMALLNEGINTS + NSMALLPOSINTS > 0
84/* References to small integers are saved in this array so that they
85 can be shared.
86 The integers that are saved are those in the range
87 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
88*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
91#ifdef COUNT_ALLOCS
92int quick_int_allocs, quick_neg_int_allocs;
93#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000096PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +0000100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
101 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifdef COUNT_ALLOCS
104 if (ival >= 0)
105 quick_int_allocs++;
106 else
107 quick_neg_int_allocs++;
108#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110 }
111#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 if (free_list == NULL) {
113 if ((free_list = fill_free_list()) == NULL)
114 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000116 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000118 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122}
123
124static void
Fred Drakea2f55112000-07-09 15:16:51 +0000125int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000127 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000128 v->ob_type = (struct _typeobject *)free_list;
129 free_list = v;
130 }
131 else
132 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossum93646982002-04-26 00:53:34 +0000135static void
136int_free(PyIntObject *v)
137{
138 v->ob_type = (struct _typeobject *)free_list;
139 free_list = v;
140}
141
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142long
Fred Drakea2f55112000-07-09 15:16:51 +0000143PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 PyNumberMethods *nb;
146 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000147 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 if (op && PyInt_Check(op))
150 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
153 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000154 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return -1;
156 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000157
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 if (io == NULL)
160 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000162 if (PyLong_Check(io)) {
163 /* got a long? => retry int conversion */
164 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000165 Py_DECREF(io);
166 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000167 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000168 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000169 }
170 else
171 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000172 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000173 PyErr_SetString(PyExc_TypeError,
174 "nb_int should return int object");
175 return -1;
176 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000177 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000178
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 val = PyInt_AS_LONG(io);
180 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000181
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000182 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
Thomas Hellera4ea6032003-04-17 18:55:45 +0000185unsigned long
186PyInt_AsUnsignedLongMask(register PyObject *op)
187{
188 PyNumberMethods *nb;
189 PyIntObject *io;
190 unsigned long val;
191
192 if (op && PyInt_Check(op))
193 return PyInt_AS_LONG((PyIntObject*) op);
194 if (op && PyLong_Check(op))
195 return PyLong_AsUnsignedLongMask(op);
196
197 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
198 nb->nb_int == NULL) {
199 PyErr_SetString(PyExc_TypeError, "an integer is required");
200 return -1;
201 }
202
203 io = (PyIntObject*) (*nb->nb_int) (op);
204 if (io == NULL)
205 return -1;
206 if (!PyInt_Check(io)) {
207 if (PyLong_Check(io)) {
208 val = PyLong_AsUnsignedLongMask((PyObject *)io);
209 Py_DECREF(io);
210 if (PyErr_Occurred())
211 return -1;
212 return val;
213 }
214 else
215 {
216 Py_DECREF(io);
217 PyErr_SetString(PyExc_TypeError,
218 "nb_int should return int object");
219 return -1;
220 }
221 }
222
223 val = PyInt_AS_LONG(io);
224 Py_DECREF(io);
225
226 return val;
227}
228
229#ifdef HAVE_LONG_LONG
230unsigned PY_LONG_LONG
231PyInt_AsUnsignedLongLongMask(register PyObject *op)
232{
233 PyNumberMethods *nb;
234 PyIntObject *io;
235 unsigned PY_LONG_LONG val;
236
237 if (op && PyInt_Check(op))
238 return PyInt_AS_LONG((PyIntObject*) op);
239 if (op && PyLong_Check(op))
240 return PyLong_AsUnsignedLongLongMask(op);
241
242 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
243 nb->nb_int == NULL) {
244 PyErr_SetString(PyExc_TypeError, "an integer is required");
245 return -1;
246 }
247
248 io = (PyIntObject*) (*nb->nb_int) (op);
249 if (io == NULL)
250 return -1;
251 if (!PyInt_Check(io)) {
252 if (PyLong_Check(io)) {
253 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
254 Py_DECREF(io);
255 if (PyErr_Occurred())
256 return -1;
257 return val;
258 }
259 else
260 {
261 Py_DECREF(io);
262 PyErr_SetString(PyExc_TypeError,
263 "nb_int should return int object");
264 return -1;
265 }
266 }
267
268 val = PyInt_AS_LONG(io);
269 Py_DECREF(io);
270
271 return val;
272}
273#endif
274
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000276PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000277{
278 char *end;
279 long x;
280 char buffer[256]; /* For errors */
281
282 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000283 PyErr_SetString(PyExc_ValueError,
284 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000285 return NULL;
286 }
287
288 while (*s && isspace(Py_CHARMASK(*s)))
289 s++;
290 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000291 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000292 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000293 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000294 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000295 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000296 else
297 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000298 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000299 goto bad;
300 while (*end && isspace(Py_CHARMASK(*end)))
301 end++;
302 if (*end != '\0') {
303 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000304 PyOS_snprintf(buffer, sizeof(buffer),
305 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000306 PyErr_SetString(PyExc_ValueError, buffer);
307 return NULL;
308 }
309 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000310 if (err_ovf("string/unicode conversion"))
311 return NULL;
312 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 }
314 if (pend)
315 *pend = end;
316 return PyInt_FromLong(x);
317}
318
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000319#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000320PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000321PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000322{
Walter Dörwald07e14762002-11-06 16:15:14 +0000323 PyObject *result;
324 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000325
Walter Dörwald07e14762002-11-06 16:15:14 +0000326 if (buffer == NULL)
327 return NULL;
328
329 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
330 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000331 return NULL;
332 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000333 result = PyInt_FromString(buffer, NULL, base);
334 PyMem_FREE(buffer);
335 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000336}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000337#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000338
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339/* Methods */
340
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000341/* Integers are seen as the "smallest" of all numeric types and thus
342 don't have any knowledge about conversion of other types to
343 integers. */
344
345#define CONVERT_TO_LONG(obj, lng) \
346 if (PyInt_Check(obj)) { \
347 lng = PyInt_AS_LONG(obj); \
348 } \
349 else { \
350 Py_INCREF(Py_NotImplemented); \
351 return Py_NotImplemented; \
352 }
353
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000354/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000355static int
Fred Drakea2f55112000-07-09 15:16:51 +0000356int_print(PyIntObject *v, FILE *fp, int flags)
357 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358{
359 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000360 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361}
362
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000364int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365{
Tim Peters42221042001-12-01 02:52:56 +0000366 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000367 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369}
370
371static int
Fred Drakea2f55112000-07-09 15:16:51 +0000372int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373{
374 register long i = v->ob_ival;
375 register long j = w->ob_ival;
376 return (i < j) ? -1 : (i > j) ? 1 : 0;
377}
378
Guido van Rossum9bfef441993-03-29 10:43:31 +0000379static long
Fred Drakea2f55112000-07-09 15:16:51 +0000380int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000381{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000382 /* XXX If this is changed, you also need to change the way
383 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000384 long x = v -> ob_ival;
385 if (x == -1)
386 x = -2;
387 return x;
388}
389
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000391int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392{
393 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000394 CONVERT_TO_LONG(v, a);
395 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000397 if ((x^a) >= 0 || (x^b) >= 0)
398 return PyInt_FromLong(x);
399 if (err_ovf("integer addition"))
400 return NULL;
401 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402}
403
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000405int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406{
407 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000408 CONVERT_TO_LONG(v, a);
409 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000411 if ((x^a) >= 0 || (x^~b) >= 0)
412 return PyInt_FromLong(x);
413 if (err_ovf("integer subtraction"))
414 return NULL;
415 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
416 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417}
418
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000419/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000420Integer overflow checking for * is painful: Python tried a couple ways, but
421they didn't work on all platforms, or failed in endcases (a product of
422-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000423
Tim Petersa3c01ce2001-12-04 23:05:10 +0000424Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000425
Tim Petersa3c01ce2001-12-04 23:05:10 +0000426The native long product x*y is either exactly right or *way* off, being
427just the last n bits of the true product, where n is the number of bits
428in a long (the delivered product is the true product plus i*2**n for
429some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000430
Tim Petersa3c01ce2001-12-04 23:05:10 +0000431The native double product (double)x * (double)y is subject to three
432rounding errors: on a sizeof(long)==8 box, each cast to double can lose
433info, and even on a sizeof(long)==4 box, the multiplication can lose info.
434But, unlike the native long product, it's not in *range* trouble: even
435if sizeof(long)==32 (256-bit longs), the product easily fits in the
436dynamic range of a double. So the leading 50 (or so) bits of the double
437product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000438
Tim Petersa3c01ce2001-12-04 23:05:10 +0000439We check these two ways against each other, and declare victory if they're
440approximately the same. Else, because the native long product is the only
441one that can lose catastrophic amounts of information, it's the native long
442product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000443*/
444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000446int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000448 long a, b;
449 long longprod; /* a*b in native long arithmetic */
450 double doubled_longprod; /* (double)longprod */
451 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000452
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000453 CONVERT_TO_LONG(v, a);
454 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000455 longprod = a * b;
456 doubleprod = (double)a * (double)b;
457 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000458
Tim Petersa3c01ce2001-12-04 23:05:10 +0000459 /* Fast path for normal case: small multiplicands, and no info
460 is lost in either method. */
461 if (doubled_longprod == doubleprod)
462 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000463
Tim Petersa3c01ce2001-12-04 23:05:10 +0000464 /* Somebody somewhere lost info. Close enough, or way off? Note
465 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
466 The difference either is or isn't significant compared to the
467 true value (of which doubleprod is a good approximation).
468 */
469 {
470 const double diff = doubled_longprod - doubleprod;
471 const double absdiff = diff >= 0.0 ? diff : -diff;
472 const double absprod = doubleprod >= 0.0 ? doubleprod :
473 -doubleprod;
474 /* absdiff/absprod <= 1/32 iff
475 32 * absdiff <= absprod -- 5 good bits is "close enough" */
476 if (32.0 * absdiff <= absprod)
477 return PyInt_FromLong(longprod);
478 else if (err_ovf("integer multiplication"))
479 return NULL;
480 else
481 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000482 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483}
484
Guido van Rossume27f7952001-08-23 02:59:04 +0000485/* Return type of i_divmod */
486enum divmod_result {
487 DIVMOD_OK, /* Correct result */
488 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
489 DIVMOD_ERROR /* Exception raised */
490};
491
492static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000493i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000494 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000496 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000497
Tim Peters1dad6a82001-06-18 19:21:11 +0000498 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000500 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000501 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000503 /* (-sys.maxint-1)/-1 is the only overflow case. */
504 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000505 if (err_ovf("integer division"))
506 return DIVMOD_ERROR;
507 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000508 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000509 xdivy = x / y;
510 xmody = x - xdivy * y;
511 /* If the signs of x and y differ, and the remainder is non-0,
512 * C89 doesn't define whether xdivy is now the floor or the
513 * ceiling of the infinitely precise quotient. We want the floor,
514 * and we have it iff the remainder's sign matches y's.
515 */
516 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
517 xmody += y;
518 --xdivy;
519 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000520 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000521 *p_xdivy = xdivy;
522 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000523 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000524}
525
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000527int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000528{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000529 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000530 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000531 CONVERT_TO_LONG(x, xi);
532 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000533 switch (i_divmod(xi, yi, &d, &m)) {
534 case DIVMOD_OK:
535 return PyInt_FromLong(d);
536 case DIVMOD_OVERFLOW:
537 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
538 (PyObject *)y);
539 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000540 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000541 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000542}
543
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000545int_classic_div(PyIntObject *x, PyIntObject *y)
546{
547 long xi, yi;
548 long d, m;
549 CONVERT_TO_LONG(x, xi);
550 CONVERT_TO_LONG(y, yi);
551 if (Py_DivisionWarningFlag &&
552 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
553 return NULL;
554 switch (i_divmod(xi, yi, &d, &m)) {
555 case DIVMOD_OK:
556 return PyInt_FromLong(d);
557 case DIVMOD_OVERFLOW:
558 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
559 (PyObject *)y);
560 default:
561 return NULL;
562 }
563}
564
565static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000566int_true_divide(PyObject *v, PyObject *w)
567{
Tim Peterse2a60002001-09-04 06:17:36 +0000568 /* If they aren't both ints, give someone else a chance. In
569 particular, this lets int/long get handled by longs, which
570 underflows to 0 gracefully if the long is too big to convert
571 to float. */
572 if (PyInt_Check(v) && PyInt_Check(w))
573 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
574 Py_INCREF(Py_NotImplemented);
575 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000576}
577
578static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000579int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000580{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000581 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000582 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000583 CONVERT_TO_LONG(x, xi);
584 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000585 switch (i_divmod(xi, yi, &d, &m)) {
586 case DIVMOD_OK:
587 return PyInt_FromLong(m);
588 case DIVMOD_OVERFLOW:
589 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
590 (PyObject *)y);
591 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000592 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000593 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000594}
595
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000597int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000598{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000599 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000600 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000601 CONVERT_TO_LONG(x, xi);
602 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000603 switch (i_divmod(xi, yi, &d, &m)) {
604 case DIVMOD_OK:
605 return Py_BuildValue("(ll)", d, m);
606 case DIVMOD_OVERFLOW:
607 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
608 (PyObject *)y);
609 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000610 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000611 }
Guido van Rossum00466951991-05-05 20:08:27 +0000612}
613
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000615int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000617 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000618 CONVERT_TO_LONG(v, iv);
619 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000620 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000621 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000622 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
623 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000624 return NULL;
625 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000626 /* Return a float. This works because we know that
627 this calls float_pow() which converts its
628 arguments to double. */
629 return PyFloat_Type.tp_as_number->nb_power(
630 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000631 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000633 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000634 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000636 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000637 return NULL;
638 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000639 }
640 /*
641 * XXX: The original exponentiation code stopped looping
642 * when temp hit zero; this code will continue onwards
643 * unnecessarily, but at least it won't cause any errors.
644 * Hopefully the speed improvement from the fast exponentiation
645 * will compensate for the slight inefficiency.
646 * XXX: Better handling of overflows is desperately needed.
647 */
648 temp = iv;
649 ix = 1;
650 while (iw > 0) {
651 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000652 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000653 ix = ix*temp;
654 if (temp == 0)
655 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000656 if (ix / temp != prev) {
657 if (err_ovf("integer exponentiation"))
658 return NULL;
659 return PyLong_Type.tp_as_number->nb_power(
660 (PyObject *)v,
661 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000662 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000663 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000664 }
665 iw >>= 1; /* Shift exponent down by 1 bit */
666 if (iw==0) break;
667 prev = temp;
668 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000669 if (prev!=0 && temp/prev!=prev) {
670 if (err_ovf("integer exponentiation"))
671 return NULL;
672 return PyLong_Type.tp_as_number->nb_power(
673 (PyObject *)v, (PyObject *)w, (PyObject *)z);
674 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000675 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000676 /* If we did a multiplication, perform a modulo */
677 ix = ix % iz;
678 temp = temp % iz;
679 }
680 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000681 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000682 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000683 switch (i_divmod(ix, iz, &div, &mod)) {
684 case DIVMOD_OK:
685 ix = mod;
686 break;
687 case DIVMOD_OVERFLOW:
688 return PyLong_Type.tp_as_number->nb_power(
689 (PyObject *)v, (PyObject *)w, (PyObject *)z);
690 default:
691 return NULL;
692 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000693 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000695}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000698int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699{
700 register long a, x;
701 a = v->ob_ival;
702 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000703 if (a < 0 && x < 0) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000704 PyObject *o;
Guido van Rossume27f7952001-08-23 02:59:04 +0000705 if (err_ovf("integer negation"))
706 return NULL;
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000707 o = PyLong_FromLong(a);
708 if (o != NULL) {
709 PyObject *result = PyNumber_Negative(o);
710 Py_DECREF(o);
711 return result;
712 }
713 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000714 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716}
717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000719int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000720{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000721 if (PyInt_CheckExact(v)) {
722 Py_INCREF(v);
723 return (PyObject *)v;
724 }
725 else
726 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000730int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000731{
732 if (v->ob_ival >= 0)
733 return int_pos(v);
734 else
735 return int_neg(v);
736}
737
Guido van Rossum0bff0151991-05-14 12:05:32 +0000738static int
Fred Drakea2f55112000-07-09 15:16:51 +0000739int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000740{
741 return v->ob_ival != 0;
742}
743
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000745int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000746{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000748}
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000751int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000752{
Guido van Rossum078151d2002-08-11 04:24:12 +0000753 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000754 CONVERT_TO_LONG(v, a);
755 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000756 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000758 return NULL;
759 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000760 if (a == 0 || b == 0)
761 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000762 if (b >= LONG_BIT) {
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000763 return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
764 PyLong_FromLong(PyInt_AS_LONG(w)));
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000765 }
Tim Petersda1a2212002-08-11 17:54:42 +0000766 c = a << b;
767 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000768 return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
769 PyLong_FromLong(PyInt_AS_LONG(w)));
Guido van Rossum078151d2002-08-11 04:24:12 +0000770 }
771 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000772}
773
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000775int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000776{
777 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000778 CONVERT_TO_LONG(v, a);
779 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000780 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000782 return NULL;
783 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000784 if (a == 0 || b == 0)
785 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000786 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000787 if (a < 0)
788 a = -1;
789 else
790 a = 0;
791 }
792 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000793 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000794 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000796}
797
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000799int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000800{
801 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000802 CONVERT_TO_LONG(v, a);
803 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000805}
806
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000808int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000809{
810 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000811 CONVERT_TO_LONG(v, a);
812 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000814}
815
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000817int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000818{
819 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000820 CONVERT_TO_LONG(v, a);
821 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000823}
824
Guido van Rossum1952e382001-09-19 01:25:16 +0000825static int
826int_coerce(PyObject **pv, PyObject **pw)
827{
828 if (PyInt_Check(*pw)) {
829 Py_INCREF(*pv);
830 Py_INCREF(*pw);
831 return 0;
832 }
833 return 1; /* Can't do it */
834}
835
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000837int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000838{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839 Py_INCREF(v);
840 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000841}
842
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000844int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000845{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000847}
848
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000850int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000851{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000853}
854
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000856int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000857{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000858 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000859 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000860 if (x < 0)
861 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
862 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000863 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000864 else
Barry Warsaw61975092001-11-28 20:55:34 +0000865 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000867}
868
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000870int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000871{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000872 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000873 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000874 if (x < 0)
875 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
876 else
877 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000879}
880
Jeremy Hylton938ace62002-07-17 16:30:39 +0000881static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000882int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
883
Tim Peters6d6c1a32001-08-02 04:15:00 +0000884static PyObject *
885int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
886{
887 PyObject *x = NULL;
888 int base = -909;
889 static char *kwlist[] = {"x", "base", 0};
890
Guido van Rossumbef14172001-08-29 15:47:46 +0000891 if (type != &PyInt_Type)
892 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
894 &x, &base))
895 return NULL;
896 if (x == NULL)
897 return PyInt_FromLong(0L);
898 if (base == -909)
899 return PyNumber_Int(x);
900 if (PyString_Check(x))
901 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000902#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903 if (PyUnicode_Check(x))
904 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
905 PyUnicode_GET_SIZE(x),
906 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000907#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908 PyErr_SetString(PyExc_TypeError,
909 "int() can't convert non-string with explicit base");
910 return NULL;
911}
912
Guido van Rossumbef14172001-08-29 15:47:46 +0000913/* Wimpy, slow approach to tp_new calls for subtypes of int:
914 first create a regular int from whatever arguments we got,
915 then allocate a subtype instance and initialize its ob_ival
916 from the regular int. The regular int is then thrown away.
917*/
918static PyObject *
919int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
920{
921 PyObject *tmp, *new;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000922 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000923
924 assert(PyType_IsSubtype(type, &PyInt_Type));
925 tmp = int_new(&PyInt_Type, args, kwds);
926 if (tmp == NULL)
927 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000928 if (!PyInt_Check(tmp)) {
929 if (!PyLong_Check(tmp)) {
930 PyErr_SetString(PyExc_ValueError,
931 "value must convertable to an int");
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000932 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000933 return NULL;
934 }
935 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000936 if (ival == -1 && PyErr_Occurred()) {
937 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000938 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000939 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000940 } else {
941 ival = ((PyIntObject *)tmp)->ob_ival;
942 }
943
Guido van Rossumd93dce12001-08-30 03:09:31 +0000944 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000945 if (new == NULL) {
946 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000947 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000948 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000949 ((PyIntObject *)new)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000950 Py_DECREF(tmp);
951 return new;
952}
953
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000954static PyObject *
955int_getnewargs(PyIntObject *v)
956{
957 return Py_BuildValue("(l)", v->ob_ival);
958}
959
960static PyMethodDef int_methods[] = {
961 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
962 {NULL, NULL} /* sentinel */
963};
964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000965PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000966"int(x[, base]) -> integer\n\
967\n\
968Convert a string or number to an integer, if possible. A floating point\n\
969argument will be truncated towards zero (this does not include a string\n\
970representation of a floating point number!) When converting a string, use\n\
971the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000972non-string. If the argument is outside the integer range a long object\n\
973will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000974
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000976 (binaryfunc)int_add, /*nb_add*/
977 (binaryfunc)int_sub, /*nb_subtract*/
978 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000979 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000980 (binaryfunc)int_mod, /*nb_remainder*/
981 (binaryfunc)int_divmod, /*nb_divmod*/
982 (ternaryfunc)int_pow, /*nb_power*/
983 (unaryfunc)int_neg, /*nb_negative*/
984 (unaryfunc)int_pos, /*nb_positive*/
985 (unaryfunc)int_abs, /*nb_absolute*/
986 (inquiry)int_nonzero, /*nb_nonzero*/
987 (unaryfunc)int_invert, /*nb_invert*/
988 (binaryfunc)int_lshift, /*nb_lshift*/
989 (binaryfunc)int_rshift, /*nb_rshift*/
990 (binaryfunc)int_and, /*nb_and*/
991 (binaryfunc)int_xor, /*nb_xor*/
992 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000993 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000994 (unaryfunc)int_int, /*nb_int*/
995 (unaryfunc)int_long, /*nb_long*/
996 (unaryfunc)int_float, /*nb_float*/
997 (unaryfunc)int_oct, /*nb_oct*/
998 (unaryfunc)int_hex, /*nb_hex*/
999 0, /*nb_inplace_add*/
1000 0, /*nb_inplace_subtract*/
1001 0, /*nb_inplace_multiply*/
1002 0, /*nb_inplace_divide*/
1003 0, /*nb_inplace_remainder*/
1004 0, /*nb_inplace_power*/
1005 0, /*nb_inplace_lshift*/
1006 0, /*nb_inplace_rshift*/
1007 0, /*nb_inplace_and*/
1008 0, /*nb_inplace_xor*/
1009 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001010 (binaryfunc)int_div, /* nb_floor_divide */
1011 int_true_divide, /* nb_true_divide */
1012 0, /* nb_inplace_floor_divide */
1013 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001014};
1015
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016PyTypeObject PyInt_Type = {
1017 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001018 0,
1019 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001020 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001021 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022 (destructor)int_dealloc, /* tp_dealloc */
1023 (printfunc)int_print, /* tp_print */
1024 0, /* tp_getattr */
1025 0, /* tp_setattr */
1026 (cmpfunc)int_compare, /* tp_compare */
1027 (reprfunc)int_repr, /* tp_repr */
1028 &int_as_number, /* tp_as_number */
1029 0, /* tp_as_sequence */
1030 0, /* tp_as_mapping */
1031 (hashfunc)int_hash, /* tp_hash */
1032 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001033 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 PyObject_GenericGetAttr, /* tp_getattro */
1035 0, /* tp_setattro */
1036 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001037 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1038 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001039 int_doc, /* tp_doc */
1040 0, /* tp_traverse */
1041 0, /* tp_clear */
1042 0, /* tp_richcompare */
1043 0, /* tp_weaklistoffset */
1044 0, /* tp_iter */
1045 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001046 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047 0, /* tp_members */
1048 0, /* tp_getset */
1049 0, /* tp_base */
1050 0, /* tp_dict */
1051 0, /* tp_descr_get */
1052 0, /* tp_descr_set */
1053 0, /* tp_dictoffset */
1054 0, /* tp_init */
1055 0, /* tp_alloc */
1056 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001057 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001058};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001059
Neal Norwitzc91ed402002-12-30 22:29:22 +00001060int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001061_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001062{
1063 PyIntObject *v;
1064 int ival;
1065#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1066 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001067 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001068 return 0;
1069 /* PyObject_New is inlined */
1070 v = free_list;
1071 free_list = (PyIntObject *)v->ob_type;
1072 PyObject_INIT(v, &PyInt_Type);
1073 v->ob_ival = ival;
1074 small_ints[ival + NSMALLNEGINTS] = v;
1075 }
1076#endif
1077 return 1;
1078}
1079
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001080void
Fred Drakea2f55112000-07-09 15:16:51 +00001081PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001082{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001083 PyIntObject *p;
1084 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001085 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001086 int bc, bf; /* block count, number of freed blocks */
1087 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001088
Guido van Rossumda084ed1999-03-10 22:55:24 +00001089#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1090 PyIntObject **q;
1091
1092 i = NSMALLNEGINTS + NSMALLPOSINTS;
1093 q = small_ints;
1094 while (--i >= 0) {
1095 Py_XDECREF(*q);
1096 *q++ = NULL;
1097 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001098#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001099 bc = 0;
1100 bf = 0;
1101 isum = 0;
1102 list = block_list;
1103 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001104 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001105 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001106 bc++;
1107 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001108 for (i = 0, p = &list->objects[0];
1109 i < N_INTOBJECTS;
1110 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001111 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001112 irem++;
1113 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001114 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001115 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001116 list->next = block_list;
1117 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001118 for (i = 0, p = &list->objects[0];
1119 i < N_INTOBJECTS;
1120 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001121 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001122 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001123 p->ob_type = (struct _typeobject *)
1124 free_list;
1125 free_list = p;
1126 }
1127#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1128 else if (-NSMALLNEGINTS <= p->ob_ival &&
1129 p->ob_ival < NSMALLPOSINTS &&
1130 small_ints[p->ob_ival +
1131 NSMALLNEGINTS] == NULL) {
1132 Py_INCREF(p);
1133 small_ints[p->ob_ival +
1134 NSMALLNEGINTS] = p;
1135 }
1136#endif
1137 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001138 }
1139 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001140 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001141 bf++;
1142 }
1143 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001144 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001145 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001146 if (!Py_VerboseFlag)
1147 return;
1148 fprintf(stderr, "# cleanup ints");
1149 if (!isum) {
1150 fprintf(stderr, "\n");
1151 }
1152 else {
1153 fprintf(stderr,
1154 ": %d unfreed int%s in %d out of %d block%s\n",
1155 isum, isum == 1 ? "" : "s",
1156 bc - bf, bc, bc == 1 ? "" : "s");
1157 }
1158 if (Py_VerboseFlag > 1) {
1159 list = block_list;
1160 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001161 for (i = 0, p = &list->objects[0];
1162 i < N_INTOBJECTS;
1163 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001164 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001165 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001166 "# <int at %p, refcnt=%d, val=%ld>\n",
1167 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001168 }
1169 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001170 }
1171 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001172}