blob: 763ed53d4b9cd308df5e5389af530da9c0d05ebc [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 Rossum3f5da241990-12-20 15:06:42 +000013/* Integers are quite normal objects, to make object handling uniform.
14 (Using odd pointers to represent integers would save much space
15 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000016 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000017 and deallocating integers, these operations should be very fast.
18 Therefore we use a dedicated allocation scheme with a much lower
19 overhead (in space and time) than straight malloc(): a simple
20 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000021
22 block_list is a singly-linked list of all PyIntBlocks ever allocated,
23 linked via their next members. PyIntBlocks are never returned to the
24 system before shutdown (PyInt_Fini).
25
26 free_list is a singly-linked list of available PyIntObjects, linked
27 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000028*/
29
30#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000031#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
32#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000033
Guido van Rossum3fce8831999-03-12 19:43:17 +000034struct _intblock {
35 struct _intblock *next;
36 PyIntObject objects[N_INTOBJECTS];
37};
38
39typedef struct _intblock PyIntBlock;
40
41static PyIntBlock *block_list = NULL;
42static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000045fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000048 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000049 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000050 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000052 ((PyIntBlock *)p)->next = block_list;
53 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000054 /* Link the int objects together, from rear to front, then return
55 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000056 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 q = p + N_INTOBJECTS;
58 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000059 q->ob_type = (struct _typeobject *)(q-1);
60 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061 return p + N_INTOBJECTS - 1;
62}
63
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000064#ifndef NSMALLPOSINTS
65#define NSMALLPOSINTS 100
66#endif
67#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000068#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#endif
70#if NSMALLNEGINTS + NSMALLPOSINTS > 0
71/* References to small integers are saved in this array so that they
72 can be shared.
73 The integers that are saved are those in the range
74 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
75*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#endif
78#ifdef COUNT_ALLOCS
79int quick_int_allocs, quick_neg_int_allocs;
80#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000081
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000083PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000086#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000087 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
88 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#ifdef COUNT_ALLOCS
91 if (ival >= 0)
92 quick_int_allocs++;
93 else
94 quick_neg_int_allocs++;
95#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097 }
98#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 if (free_list == NULL) {
100 if ((free_list = fill_free_list()) == NULL)
101 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000103 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000105 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109}
110
111static void
Fred Drakea2f55112000-07-09 15:16:51 +0000112int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000114 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000115 v->ob_type = (struct _typeobject *)free_list;
116 free_list = v;
117 }
118 else
119 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120}
121
Guido van Rossum93646982002-04-26 00:53:34 +0000122static void
123int_free(PyIntObject *v)
124{
125 v->ob_type = (struct _typeobject *)free_list;
126 free_list = v;
127}
128
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129long
Fred Drakea2f55112000-07-09 15:16:51 +0000130PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132 PyNumberMethods *nb;
133 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000134 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000135
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136 if (op && PyInt_Check(op))
137 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000138
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000139 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
140 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000141 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142 return -1;
143 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000144
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000146 if (io == NULL)
147 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000149 if (PyLong_Check(io)) {
150 /* got a long? => retry int conversion */
151 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000152 Py_DECREF(io);
153 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000154 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000155 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000156 }
157 else
158 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000159 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000160 PyErr_SetString(PyExc_TypeError,
161 "nb_int should return int object");
162 return -1;
163 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000164 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000165
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166 val = PyInt_AS_LONG(io);
167 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000168
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000169 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Thomas Hellera4ea6032003-04-17 18:55:45 +0000172unsigned long
173PyInt_AsUnsignedLongMask(register PyObject *op)
174{
175 PyNumberMethods *nb;
176 PyIntObject *io;
177 unsigned long val;
178
179 if (op && PyInt_Check(op))
180 return PyInt_AS_LONG((PyIntObject*) op);
181 if (op && PyLong_Check(op))
182 return PyLong_AsUnsignedLongMask(op);
183
184 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
185 nb->nb_int == NULL) {
186 PyErr_SetString(PyExc_TypeError, "an integer is required");
187 return -1;
188 }
189
190 io = (PyIntObject*) (*nb->nb_int) (op);
191 if (io == NULL)
192 return -1;
193 if (!PyInt_Check(io)) {
194 if (PyLong_Check(io)) {
195 val = PyLong_AsUnsignedLongMask((PyObject *)io);
196 Py_DECREF(io);
197 if (PyErr_Occurred())
198 return -1;
199 return val;
200 }
201 else
202 {
203 Py_DECREF(io);
204 PyErr_SetString(PyExc_TypeError,
205 "nb_int should return int object");
206 return -1;
207 }
208 }
209
210 val = PyInt_AS_LONG(io);
211 Py_DECREF(io);
212
213 return val;
214}
215
216#ifdef HAVE_LONG_LONG
217unsigned PY_LONG_LONG
218PyInt_AsUnsignedLongLongMask(register PyObject *op)
219{
220 PyNumberMethods *nb;
221 PyIntObject *io;
222 unsigned PY_LONG_LONG val;
223
224 if (op && PyInt_Check(op))
225 return PyInt_AS_LONG((PyIntObject*) op);
226 if (op && PyLong_Check(op))
227 return PyLong_AsUnsignedLongLongMask(op);
228
229 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
230 nb->nb_int == NULL) {
231 PyErr_SetString(PyExc_TypeError, "an integer is required");
232 return -1;
233 }
234
235 io = (PyIntObject*) (*nb->nb_int) (op);
236 if (io == NULL)
237 return -1;
238 if (!PyInt_Check(io)) {
239 if (PyLong_Check(io)) {
240 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
241 Py_DECREF(io);
242 if (PyErr_Occurred())
243 return -1;
244 return val;
245 }
246 else
247 {
248 Py_DECREF(io);
249 PyErr_SetString(PyExc_TypeError,
250 "nb_int should return int object");
251 return -1;
252 }
253 }
254
255 val = PyInt_AS_LONG(io);
256 Py_DECREF(io);
257
258 return val;
259}
260#endif
261
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000263PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000264{
265 char *end;
266 long x;
267 char buffer[256]; /* For errors */
268
269 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000270 PyErr_SetString(PyExc_ValueError,
271 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000272 return NULL;
273 }
274
275 while (*s && isspace(Py_CHARMASK(*s)))
276 s++;
277 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000278 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000279 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000280 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000281 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000282 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000283 else
284 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000285 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000286 goto bad;
287 while (*end && isspace(Py_CHARMASK(*end)))
288 end++;
289 if (*end != '\0') {
290 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000291 PyOS_snprintf(buffer, sizeof(buffer),
292 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 PyErr_SetString(PyExc_ValueError, buffer);
294 return NULL;
295 }
Tim Petersc8854432004-08-25 02:14:08 +0000296 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000297 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000298 if (pend)
299 *pend = end;
300 return PyInt_FromLong(x);
301}
302
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000303#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000304PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000305PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000306{
Walter Dörwald07e14762002-11-06 16:15:14 +0000307 PyObject *result;
308 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000309
Walter Dörwald07e14762002-11-06 16:15:14 +0000310 if (buffer == NULL)
311 return NULL;
312
313 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
314 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000315 return NULL;
316 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000317 result = PyInt_FromString(buffer, NULL, base);
318 PyMem_FREE(buffer);
319 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000320}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000321#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000322
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323/* Methods */
324
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000325/* Integers are seen as the "smallest" of all numeric types and thus
326 don't have any knowledge about conversion of other types to
327 integers. */
328
329#define CONVERT_TO_LONG(obj, lng) \
330 if (PyInt_Check(obj)) { \
331 lng = PyInt_AS_LONG(obj); \
332 } \
333 else { \
334 Py_INCREF(Py_NotImplemented); \
335 return Py_NotImplemented; \
336 }
337
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000338/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000339static int
Fred Drakea2f55112000-07-09 15:16:51 +0000340int_print(PyIntObject *v, FILE *fp, int flags)
341 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342{
343 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000344 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345}
346
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000348int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349{
Tim Peters42221042001-12-01 02:52:56 +0000350 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000351 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000352 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353}
354
355static int
Fred Drakea2f55112000-07-09 15:16:51 +0000356int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
358 register long i = v->ob_ival;
359 register long j = w->ob_ival;
360 return (i < j) ? -1 : (i > j) ? 1 : 0;
361}
362
Guido van Rossum9bfef441993-03-29 10:43:31 +0000363static long
Fred Drakea2f55112000-07-09 15:16:51 +0000364int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000365{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000366 /* XXX If this is changed, you also need to change the way
367 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000368 long x = v -> ob_ival;
369 if (x == -1)
370 x = -2;
371 return x;
372}
373
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000375int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
377 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000378 CONVERT_TO_LONG(v, a);
379 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000381 if ((x^a) >= 0 || (x^b) >= 0)
382 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000383 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000387int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
389 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000390 CONVERT_TO_LONG(v, a);
391 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000393 if ((x^a) >= 0 || (x^~b) >= 0)
394 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000395 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
396 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397}
398
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000399/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000400Integer overflow checking for * is painful: Python tried a couple ways, but
401they didn't work on all platforms, or failed in endcases (a product of
402-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000403
Tim Petersa3c01ce2001-12-04 23:05:10 +0000404Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000405
Tim Petersa3c01ce2001-12-04 23:05:10 +0000406The native long product x*y is either exactly right or *way* off, being
407just the last n bits of the true product, where n is the number of bits
408in a long (the delivered product is the true product plus i*2**n for
409some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000410
Tim Petersa3c01ce2001-12-04 23:05:10 +0000411The native double product (double)x * (double)y is subject to three
412rounding errors: on a sizeof(long)==8 box, each cast to double can lose
413info, and even on a sizeof(long)==4 box, the multiplication can lose info.
414But, unlike the native long product, it's not in *range* trouble: even
415if sizeof(long)==32 (256-bit longs), the product easily fits in the
416dynamic range of a double. So the leading 50 (or so) bits of the double
417product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000418
Tim Petersa3c01ce2001-12-04 23:05:10 +0000419We check these two ways against each other, and declare victory if they're
420approximately the same. Else, because the native long product is the only
421one that can lose catastrophic amounts of information, it's the native long
422product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000423*/
424
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000426int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000428 long a, b;
429 long longprod; /* a*b in native long arithmetic */
430 double doubled_longprod; /* (double)longprod */
431 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000432
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000433 CONVERT_TO_LONG(v, a);
434 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000435 longprod = a * b;
436 doubleprod = (double)a * (double)b;
437 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000438
Tim Petersa3c01ce2001-12-04 23:05:10 +0000439 /* Fast path for normal case: small multiplicands, and no info
440 is lost in either method. */
441 if (doubled_longprod == doubleprod)
442 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000443
Tim Petersa3c01ce2001-12-04 23:05:10 +0000444 /* Somebody somewhere lost info. Close enough, or way off? Note
445 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
446 The difference either is or isn't significant compared to the
447 true value (of which doubleprod is a good approximation).
448 */
449 {
450 const double diff = doubled_longprod - doubleprod;
451 const double absdiff = diff >= 0.0 ? diff : -diff;
452 const double absprod = doubleprod >= 0.0 ? doubleprod :
453 -doubleprod;
454 /* absdiff/absprod <= 1/32 iff
455 32 * absdiff <= absprod -- 5 good bits is "close enough" */
456 if (32.0 * absdiff <= absprod)
457 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000458 else
459 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000460 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461}
462
Guido van Rossume27f7952001-08-23 02:59:04 +0000463/* Return type of i_divmod */
464enum divmod_result {
465 DIVMOD_OK, /* Correct result */
466 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
467 DIVMOD_ERROR /* Exception raised */
468};
469
470static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000471i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000472 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000474 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000475
Tim Peters1dad6a82001-06-18 19:21:11 +0000476 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000478 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000479 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000481 /* (-sys.maxint-1)/-1 is the only overflow case. */
Tim Petersc8854432004-08-25 02:14:08 +0000482 if (y == -1 && x < 0 && x == -x)
Guido van Rossume27f7952001-08-23 02:59:04 +0000483 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000484 xdivy = x / y;
485 xmody = x - xdivy * y;
486 /* If the signs of x and y differ, and the remainder is non-0,
487 * C89 doesn't define whether xdivy is now the floor or the
488 * ceiling of the infinitely precise quotient. We want the floor,
489 * and we have it iff the remainder's sign matches y's.
490 */
491 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
492 xmody += y;
493 --xdivy;
494 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000495 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000496 *p_xdivy = xdivy;
497 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000498 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000499}
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000502int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000503{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000504 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000505 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000506 CONVERT_TO_LONG(x, xi);
507 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000508 switch (i_divmod(xi, yi, &d, &m)) {
509 case DIVMOD_OK:
510 return PyInt_FromLong(d);
511 case DIVMOD_OVERFLOW:
512 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
513 (PyObject *)y);
514 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000515 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000516 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000520int_classic_div(PyIntObject *x, PyIntObject *y)
521{
522 long xi, yi;
523 long d, m;
524 CONVERT_TO_LONG(x, xi);
525 CONVERT_TO_LONG(y, yi);
526 if (Py_DivisionWarningFlag &&
527 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
528 return NULL;
529 switch (i_divmod(xi, yi, &d, &m)) {
530 case DIVMOD_OK:
531 return PyInt_FromLong(d);
532 case DIVMOD_OVERFLOW:
533 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
534 (PyObject *)y);
535 default:
536 return NULL;
537 }
538}
539
540static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000541int_true_divide(PyObject *v, PyObject *w)
542{
Tim Peterse2a60002001-09-04 06:17:36 +0000543 /* If they aren't both ints, give someone else a chance. In
544 particular, this lets int/long get handled by longs, which
545 underflows to 0 gracefully if the long is too big to convert
546 to float. */
547 if (PyInt_Check(v) && PyInt_Check(w))
548 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
549 Py_INCREF(Py_NotImplemented);
550 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000551}
552
553static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000554int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000555{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000556 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000557 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000558 CONVERT_TO_LONG(x, xi);
559 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000560 switch (i_divmod(xi, yi, &d, &m)) {
561 case DIVMOD_OK:
562 return PyInt_FromLong(m);
563 case DIVMOD_OVERFLOW:
564 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
565 (PyObject *)y);
566 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000567 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000568 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000569}
570
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000572int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000573{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000574 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000575 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000576 CONVERT_TO_LONG(x, xi);
577 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000578 switch (i_divmod(xi, yi, &d, &m)) {
579 case DIVMOD_OK:
580 return Py_BuildValue("(ll)", d, m);
581 case DIVMOD_OVERFLOW:
582 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
583 (PyObject *)y);
584 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000585 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000586 }
Guido van Rossum00466951991-05-05 20:08:27 +0000587}
588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000590int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000592 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000593 CONVERT_TO_LONG(v, iv);
594 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000595 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000596 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000597 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
598 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000599 return NULL;
600 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000601 /* Return a float. This works because we know that
602 this calls float_pow() which converts its
603 arguments to double. */
604 return PyFloat_Type.tp_as_number->nb_power(
605 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000606 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000608 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000609 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000611 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000612 return NULL;
613 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000614 }
615 /*
616 * XXX: The original exponentiation code stopped looping
617 * when temp hit zero; this code will continue onwards
618 * unnecessarily, but at least it won't cause any errors.
619 * Hopefully the speed improvement from the fast exponentiation
620 * will compensate for the slight inefficiency.
621 * XXX: Better handling of overflows is desperately needed.
622 */
623 temp = iv;
624 ix = 1;
625 while (iw > 0) {
626 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000627 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000628 ix = ix*temp;
629 if (temp == 0)
630 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000631 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000632 return PyLong_Type.tp_as_number->nb_power(
633 (PyObject *)v,
634 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000635 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000636 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000637 }
638 iw >>= 1; /* Shift exponent down by 1 bit */
639 if (iw==0) break;
640 prev = temp;
641 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000642 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000643 return PyLong_Type.tp_as_number->nb_power(
644 (PyObject *)v, (PyObject *)w, (PyObject *)z);
645 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000646 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000647 /* If we did a multiplication, perform a modulo */
648 ix = ix % iz;
649 temp = temp % iz;
650 }
651 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000652 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000653 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000654 switch (i_divmod(ix, iz, &div, &mod)) {
655 case DIVMOD_OK:
656 ix = mod;
657 break;
658 case DIVMOD_OVERFLOW:
659 return PyLong_Type.tp_as_number->nb_power(
660 (PyObject *)v, (PyObject *)w, (PyObject *)z);
661 default:
662 return NULL;
663 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000664 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000666}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000669int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670{
671 register long a, x;
672 a = v->ob_ival;
673 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000674 if (a < 0 && x < 0) {
Tim Petersc8854432004-08-25 02:14:08 +0000675 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000676 if (o != NULL) {
677 PyObject *result = PyNumber_Negative(o);
678 Py_DECREF(o);
679 return result;
680 }
681 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000682 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684}
685
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000687int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000689 if (PyInt_CheckExact(v)) {
690 Py_INCREF(v);
691 return (PyObject *)v;
692 }
693 else
694 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695}
696
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000698int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000699{
700 if (v->ob_ival >= 0)
701 return int_pos(v);
702 else
703 return int_neg(v);
704}
705
Guido van Rossum0bff0151991-05-14 12:05:32 +0000706static int
Fred Drakea2f55112000-07-09 15:16:51 +0000707int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000708{
709 return v->ob_ival != 0;
710}
711
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000713int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000714{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716}
717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000719int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000720{
Guido van Rossum078151d2002-08-11 04:24:12 +0000721 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000722 PyObject *vv, *ww, *result;
723
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000724 CONVERT_TO_LONG(v, a);
725 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000726 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000728 return NULL;
729 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000730 if (a == 0 || b == 0)
731 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000732 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000733 vv = PyLong_FromLong(PyInt_AS_LONG(v));
734 if (vv == NULL)
735 return NULL;
736 ww = PyLong_FromLong(PyInt_AS_LONG(w));
737 if (ww == NULL) {
738 Py_DECREF(vv);
739 return NULL;
740 }
741 result = PyNumber_Lshift(vv, ww);
742 Py_DECREF(vv);
743 Py_DECREF(ww);
744 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000745 }
Tim Petersda1a2212002-08-11 17:54:42 +0000746 c = a << b;
747 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000748 vv = PyLong_FromLong(PyInt_AS_LONG(v));
749 if (vv == NULL)
750 return NULL;
751 ww = PyLong_FromLong(PyInt_AS_LONG(w));
752 if (ww == NULL) {
753 Py_DECREF(vv);
754 return NULL;
755 }
756 result = PyNumber_Lshift(vv, ww);
757 Py_DECREF(vv);
758 Py_DECREF(ww);
759 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000760 }
761 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000762}
763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000765int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000766{
767 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000768 CONVERT_TO_LONG(v, a);
769 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000770 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000772 return NULL;
773 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000774 if (a == 0 || b == 0)
775 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000776 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000777 if (a < 0)
778 a = -1;
779 else
780 a = 0;
781 }
782 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000783 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000784 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000786}
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000789int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000790{
791 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000792 CONVERT_TO_LONG(v, a);
793 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000795}
796
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000798int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000799{
800 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000801 CONVERT_TO_LONG(v, a);
802 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000804}
805
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000807int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000808{
809 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000810 CONVERT_TO_LONG(v, a);
811 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000813}
814
Guido van Rossum1952e382001-09-19 01:25:16 +0000815static int
816int_coerce(PyObject **pv, PyObject **pw)
817{
818 if (PyInt_Check(*pw)) {
819 Py_INCREF(*pv);
820 Py_INCREF(*pw);
821 return 0;
822 }
823 return 1; /* Can't do it */
824}
825
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000827int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000828{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829 Py_INCREF(v);
830 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000831}
832
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000834int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000835{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000837}
838
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000840int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000841{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000843}
844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000846int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000847{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000848 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000849 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000850 if (x < 0)
851 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
852 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000853 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000854 else
Barry Warsaw61975092001-11-28 20:55:34 +0000855 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000857}
858
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000860int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000861{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000862 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000863 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000864 if (x < 0)
865 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
866 else
867 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000869}
870
Jeremy Hylton938ace62002-07-17 16:30:39 +0000871static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000872int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
873
Tim Peters6d6c1a32001-08-02 04:15:00 +0000874static PyObject *
875int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
876{
877 PyObject *x = NULL;
878 int base = -909;
879 static char *kwlist[] = {"x", "base", 0};
880
Guido van Rossumbef14172001-08-29 15:47:46 +0000881 if (type != &PyInt_Type)
882 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
884 &x, &base))
885 return NULL;
886 if (x == NULL)
887 return PyInt_FromLong(0L);
888 if (base == -909)
889 return PyNumber_Int(x);
890 if (PyString_Check(x))
891 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000892#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893 if (PyUnicode_Check(x))
894 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
895 PyUnicode_GET_SIZE(x),
896 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000897#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898 PyErr_SetString(PyExc_TypeError,
899 "int() can't convert non-string with explicit base");
900 return NULL;
901}
902
Guido van Rossumbef14172001-08-29 15:47:46 +0000903/* Wimpy, slow approach to tp_new calls for subtypes of int:
904 first create a regular int from whatever arguments we got,
905 then allocate a subtype instance and initialize its ob_ival
906 from the regular int. The regular int is then thrown away.
907*/
908static PyObject *
909int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
910{
911 PyObject *tmp, *new;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000912 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000913
914 assert(PyType_IsSubtype(type, &PyInt_Type));
915 tmp = int_new(&PyInt_Type, args, kwds);
916 if (tmp == NULL)
917 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000918 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000919 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000920 if (ival == -1 && PyErr_Occurred()) {
921 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000922 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000923 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000924 } else {
925 ival = ((PyIntObject *)tmp)->ob_ival;
926 }
927
Guido van Rossumd93dce12001-08-30 03:09:31 +0000928 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000929 if (new == NULL) {
930 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000931 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000932 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000933 ((PyIntObject *)new)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000934 Py_DECREF(tmp);
935 return new;
936}
937
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000938static PyObject *
939int_getnewargs(PyIntObject *v)
940{
941 return Py_BuildValue("(l)", v->ob_ival);
942}
943
944static PyMethodDef int_methods[] = {
945 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
946 {NULL, NULL} /* sentinel */
947};
948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000949PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950"int(x[, base]) -> integer\n\
951\n\
952Convert a string or number to an integer, if possible. A floating point\n\
953argument will be truncated towards zero (this does not include a string\n\
954representation of a floating point number!) When converting a string, use\n\
955the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000956non-string. If the argument is outside the integer range a long object\n\
957will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000960 (binaryfunc)int_add, /*nb_add*/
961 (binaryfunc)int_sub, /*nb_subtract*/
962 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000963 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000964 (binaryfunc)int_mod, /*nb_remainder*/
965 (binaryfunc)int_divmod, /*nb_divmod*/
966 (ternaryfunc)int_pow, /*nb_power*/
967 (unaryfunc)int_neg, /*nb_negative*/
968 (unaryfunc)int_pos, /*nb_positive*/
969 (unaryfunc)int_abs, /*nb_absolute*/
970 (inquiry)int_nonzero, /*nb_nonzero*/
971 (unaryfunc)int_invert, /*nb_invert*/
972 (binaryfunc)int_lshift, /*nb_lshift*/
973 (binaryfunc)int_rshift, /*nb_rshift*/
974 (binaryfunc)int_and, /*nb_and*/
975 (binaryfunc)int_xor, /*nb_xor*/
976 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000977 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000978 (unaryfunc)int_int, /*nb_int*/
979 (unaryfunc)int_long, /*nb_long*/
980 (unaryfunc)int_float, /*nb_float*/
981 (unaryfunc)int_oct, /*nb_oct*/
982 (unaryfunc)int_hex, /*nb_hex*/
983 0, /*nb_inplace_add*/
984 0, /*nb_inplace_subtract*/
985 0, /*nb_inplace_multiply*/
986 0, /*nb_inplace_divide*/
987 0, /*nb_inplace_remainder*/
988 0, /*nb_inplace_power*/
989 0, /*nb_inplace_lshift*/
990 0, /*nb_inplace_rshift*/
991 0, /*nb_inplace_and*/
992 0, /*nb_inplace_xor*/
993 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000994 (binaryfunc)int_div, /* nb_floor_divide */
995 int_true_divide, /* nb_true_divide */
996 0, /* nb_inplace_floor_divide */
997 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000998};
999
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000PyTypeObject PyInt_Type = {
1001 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001002 0,
1003 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001004 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001005 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001006 (destructor)int_dealloc, /* tp_dealloc */
1007 (printfunc)int_print, /* tp_print */
1008 0, /* tp_getattr */
1009 0, /* tp_setattr */
1010 (cmpfunc)int_compare, /* tp_compare */
1011 (reprfunc)int_repr, /* tp_repr */
1012 &int_as_number, /* tp_as_number */
1013 0, /* tp_as_sequence */
1014 0, /* tp_as_mapping */
1015 (hashfunc)int_hash, /* tp_hash */
1016 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001017 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001018 PyObject_GenericGetAttr, /* tp_getattro */
1019 0, /* tp_setattro */
1020 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001021 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1022 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023 int_doc, /* tp_doc */
1024 0, /* tp_traverse */
1025 0, /* tp_clear */
1026 0, /* tp_richcompare */
1027 0, /* tp_weaklistoffset */
1028 0, /* tp_iter */
1029 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001030 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031 0, /* tp_members */
1032 0, /* tp_getset */
1033 0, /* tp_base */
1034 0, /* tp_dict */
1035 0, /* tp_descr_get */
1036 0, /* tp_descr_set */
1037 0, /* tp_dictoffset */
1038 0, /* tp_init */
1039 0, /* tp_alloc */
1040 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001041 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001042};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001043
Neal Norwitzc91ed402002-12-30 22:29:22 +00001044int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001045_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001046{
1047 PyIntObject *v;
1048 int ival;
1049#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1050 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001051 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001052 return 0;
1053 /* PyObject_New is inlined */
1054 v = free_list;
1055 free_list = (PyIntObject *)v->ob_type;
1056 PyObject_INIT(v, &PyInt_Type);
1057 v->ob_ival = ival;
1058 small_ints[ival + NSMALLNEGINTS] = v;
1059 }
1060#endif
1061 return 1;
1062}
1063
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001064void
Fred Drakea2f55112000-07-09 15:16:51 +00001065PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001066{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001067 PyIntObject *p;
1068 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001069 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001070 int bc, bf; /* block count, number of freed blocks */
1071 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001072
Guido van Rossumda084ed1999-03-10 22:55:24 +00001073#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1074 PyIntObject **q;
1075
1076 i = NSMALLNEGINTS + NSMALLPOSINTS;
1077 q = small_ints;
1078 while (--i >= 0) {
1079 Py_XDECREF(*q);
1080 *q++ = NULL;
1081 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001082#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001083 bc = 0;
1084 bf = 0;
1085 isum = 0;
1086 list = block_list;
1087 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001088 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001089 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001090 bc++;
1091 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001092 for (i = 0, p = &list->objects[0];
1093 i < N_INTOBJECTS;
1094 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001095 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001096 irem++;
1097 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001098 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001099 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001100 list->next = block_list;
1101 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001102 for (i = 0, p = &list->objects[0];
1103 i < N_INTOBJECTS;
1104 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001105 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001106 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001107 p->ob_type = (struct _typeobject *)
1108 free_list;
1109 free_list = p;
1110 }
1111#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1112 else if (-NSMALLNEGINTS <= p->ob_ival &&
1113 p->ob_ival < NSMALLPOSINTS &&
1114 small_ints[p->ob_ival +
1115 NSMALLNEGINTS] == NULL) {
1116 Py_INCREF(p);
1117 small_ints[p->ob_ival +
1118 NSMALLNEGINTS] = p;
1119 }
1120#endif
1121 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001122 }
1123 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001124 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001125 bf++;
1126 }
1127 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001128 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001129 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001130 if (!Py_VerboseFlag)
1131 return;
1132 fprintf(stderr, "# cleanup ints");
1133 if (!isum) {
1134 fprintf(stderr, "\n");
1135 }
1136 else {
1137 fprintf(stderr,
1138 ": %d unfreed int%s in %d out of %d block%s\n",
1139 isum, isum == 1 ? "" : "s",
1140 bc - bf, bc, bc == 1 ? "" : "s");
1141 }
1142 if (Py_VerboseFlag > 1) {
1143 list = block_list;
1144 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001145 for (i = 0, p = &list->objects[0];
1146 i < N_INTOBJECTS;
1147 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001148 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001149 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001150 "# <int at %p, refcnt=%d, val=%ld>\n",
1151 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001152 }
1153 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001154 }
1155 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001156}