blob: c73484004f09b83e07d6a2248bc714087ac3026c [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
Georg Brandl418a1ef2006-02-22 11:30:06 +000065#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000066#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
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111PyObject *
112PyInt_FromSize_t(size_t ival)
113{
114 if (ival <= LONG_MAX)
115 return PyInt_FromLong((long)ival);
116 return _PyLong_FromSize_t(ival);
117}
118
119PyObject *
120PyInt_FromSsize_t(Py_ssize_t ival)
121{
122 if (ival >= LONG_MIN && ival <= LONG_MAX)
123 return PyInt_FromLong((long)ival);
124 return _PyLong_FromSsize_t(ival);
125}
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127static void
Fred Drakea2f55112000-07-09 15:16:51 +0000128int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000130 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000131 v->ob_type = (struct _typeobject *)free_list;
132 free_list = v;
133 }
134 else
135 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossum93646982002-04-26 00:53:34 +0000138static void
139int_free(PyIntObject *v)
140{
141 v->ob_type = (struct _typeobject *)free_list;
142 free_list = v;
143}
144
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145long
Fred Drakea2f55112000-07-09 15:16:51 +0000146PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyNumberMethods *nb;
149 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (op && PyInt_Check(op))
153 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000154
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
156 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000157 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 return -1;
159 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 if (io == NULL)
163 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000165 if (PyLong_Check(io)) {
166 /* got a long? => retry int conversion */
167 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000168 Py_DECREF(io);
169 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000170 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000171 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000172 }
173 else
174 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000175 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000176 PyErr_SetString(PyExc_TypeError,
177 "nb_int should return int object");
178 return -1;
179 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000180 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000181
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 val = PyInt_AS_LONG(io);
183 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000184
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000185 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186}
187
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188Py_ssize_t
189PyInt_AsSsize_t(register PyObject *op)
190{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000191#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192 PyNumberMethods *nb;
193 PyIntObject *io;
194 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000195#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196 if (op && !PyInt_CheckExact(op) && PyLong_Check(op))
197 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000198#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199 return PyInt_AsLong(op);
200#else
201
202 if (op && PyInt_Check(op))
203 return PyInt_AS_LONG((PyIntObject*) op);
204
205 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
206 (nb->nb_int == NULL && nb->nb_long == 0)) {
207 PyErr_SetString(PyExc_TypeError, "an integer is required");
208 return -1;
209 }
210
211 if (nb->nb_long != 0) {
212 io = (PyIntObject*) (*nb->nb_long) (op);
213 } else {
214 io = (PyIntObject*) (*nb->nb_int) (op);
215 }
216 if (io == NULL)
217 return -1;
218 if (!PyInt_Check(io)) {
219 if (PyLong_Check(io)) {
220 /* got a long? => retry int conversion */
221 val = _PyLong_AsSsize_t((PyObject *)io);
222 Py_DECREF(io);
223 if ((val == -1) && PyErr_Occurred())
224 return -1;
225 return val;
226 }
227 else
228 {
229 Py_DECREF(io);
230 PyErr_SetString(PyExc_TypeError,
231 "nb_int should return int object");
232 return -1;
233 }
234 }
235
236 val = PyInt_AS_LONG(io);
237 Py_DECREF(io);
238
239 return val;
240#endif
241}
242
Thomas Hellera4ea6032003-04-17 18:55:45 +0000243unsigned long
244PyInt_AsUnsignedLongMask(register PyObject *op)
245{
246 PyNumberMethods *nb;
247 PyIntObject *io;
248 unsigned long val;
249
250 if (op && PyInt_Check(op))
251 return PyInt_AS_LONG((PyIntObject*) op);
252 if (op && PyLong_Check(op))
253 return PyLong_AsUnsignedLongMask(op);
254
255 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
256 nb->nb_int == NULL) {
257 PyErr_SetString(PyExc_TypeError, "an integer is required");
258 return -1;
259 }
260
261 io = (PyIntObject*) (*nb->nb_int) (op);
262 if (io == NULL)
263 return -1;
264 if (!PyInt_Check(io)) {
265 if (PyLong_Check(io)) {
266 val = PyLong_AsUnsignedLongMask((PyObject *)io);
267 Py_DECREF(io);
268 if (PyErr_Occurred())
269 return -1;
270 return val;
271 }
272 else
273 {
274 Py_DECREF(io);
275 PyErr_SetString(PyExc_TypeError,
276 "nb_int should return int object");
277 return -1;
278 }
279 }
280
281 val = PyInt_AS_LONG(io);
282 Py_DECREF(io);
283
284 return val;
285}
286
287#ifdef HAVE_LONG_LONG
288unsigned PY_LONG_LONG
289PyInt_AsUnsignedLongLongMask(register PyObject *op)
290{
291 PyNumberMethods *nb;
292 PyIntObject *io;
293 unsigned PY_LONG_LONG val;
294
295 if (op && PyInt_Check(op))
296 return PyInt_AS_LONG((PyIntObject*) op);
297 if (op && PyLong_Check(op))
298 return PyLong_AsUnsignedLongLongMask(op);
299
300 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
301 nb->nb_int == NULL) {
302 PyErr_SetString(PyExc_TypeError, "an integer is required");
303 return -1;
304 }
305
306 io = (PyIntObject*) (*nb->nb_int) (op);
307 if (io == NULL)
308 return -1;
309 if (!PyInt_Check(io)) {
310 if (PyLong_Check(io)) {
311 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
312 Py_DECREF(io);
313 if (PyErr_Occurred())
314 return -1;
315 return val;
316 }
317 else
318 {
319 Py_DECREF(io);
320 PyErr_SetString(PyExc_TypeError,
321 "nb_int should return int object");
322 return -1;
323 }
324 }
325
326 val = PyInt_AS_LONG(io);
327 Py_DECREF(io);
328
329 return val;
330}
331#endif
332
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000333PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000334PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000335{
336 char *end;
337 long x;
338 char buffer[256]; /* For errors */
339
340 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000341 PyErr_SetString(PyExc_ValueError,
342 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000343 return NULL;
344 }
345
346 while (*s && isspace(Py_CHARMASK(*s)))
347 s++;
348 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000349 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000350 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000351 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000352 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000353 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 else
355 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000356 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 goto bad;
358 while (*end && isspace(Py_CHARMASK(*end)))
359 end++;
360 if (*end != '\0') {
361 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000362 PyOS_snprintf(buffer, sizeof(buffer),
363 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 PyErr_SetString(PyExc_ValueError, buffer);
365 return NULL;
366 }
Tim Petersc8854432004-08-25 02:14:08 +0000367 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000368 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000369 if (pend)
370 *pend = end;
371 return PyInt_FromLong(x);
372}
373
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000374#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000375PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000377{
Walter Dörwald07e14762002-11-06 16:15:14 +0000378 PyObject *result;
379 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000380
Walter Dörwald07e14762002-11-06 16:15:14 +0000381 if (buffer == NULL)
382 return NULL;
383
384 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
385 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000386 return NULL;
387 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000388 result = PyInt_FromString(buffer, NULL, base);
389 PyMem_FREE(buffer);
390 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000391}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000392#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000393
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394/* Methods */
395
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000396/* Integers are seen as the "smallest" of all numeric types and thus
397 don't have any knowledge about conversion of other types to
398 integers. */
399
400#define CONVERT_TO_LONG(obj, lng) \
401 if (PyInt_Check(obj)) { \
402 lng = PyInt_AS_LONG(obj); \
403 } \
404 else { \
405 Py_INCREF(Py_NotImplemented); \
406 return Py_NotImplemented; \
407 }
408
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000409/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000410static int
Fred Drakea2f55112000-07-09 15:16:51 +0000411int_print(PyIntObject *v, FILE *fp, int flags)
412 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413{
414 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000415 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416}
417
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000419int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420{
Tim Peters42221042001-12-01 02:52:56 +0000421 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000422 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424}
425
426static int
Fred Drakea2f55112000-07-09 15:16:51 +0000427int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
429 register long i = v->ob_ival;
430 register long j = w->ob_ival;
431 return (i < j) ? -1 : (i > j) ? 1 : 0;
432}
433
Guido van Rossum9bfef441993-03-29 10:43:31 +0000434static long
Fred Drakea2f55112000-07-09 15:16:51 +0000435int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000436{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000437 /* XXX If this is changed, you also need to change the way
438 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000439 long x = v -> ob_ival;
440 if (x == -1)
441 x = -2;
442 return x;
443}
444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000446int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
448 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000449 CONVERT_TO_LONG(v, a);
450 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000452 if ((x^a) >= 0 || (x^b) >= 0)
453 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000454 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455}
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000458int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
460 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000461 CONVERT_TO_LONG(v, a);
462 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000464 if ((x^a) >= 0 || (x^~b) >= 0)
465 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000466 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
467 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468}
469
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000470/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000471Integer overflow checking for * is painful: Python tried a couple ways, but
472they didn't work on all platforms, or failed in endcases (a product of
473-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000474
Tim Petersa3c01ce2001-12-04 23:05:10 +0000475Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000476
Tim Petersa3c01ce2001-12-04 23:05:10 +0000477The native long product x*y is either exactly right or *way* off, being
478just the last n bits of the true product, where n is the number of bits
479in a long (the delivered product is the true product plus i*2**n for
480some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000481
Tim Petersa3c01ce2001-12-04 23:05:10 +0000482The native double product (double)x * (double)y is subject to three
483rounding errors: on a sizeof(long)==8 box, each cast to double can lose
484info, and even on a sizeof(long)==4 box, the multiplication can lose info.
485But, unlike the native long product, it's not in *range* trouble: even
486if sizeof(long)==32 (256-bit longs), the product easily fits in the
487dynamic range of a double. So the leading 50 (or so) bits of the double
488product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000489
Tim Petersa3c01ce2001-12-04 23:05:10 +0000490We check these two ways against each other, and declare victory if they're
491approximately the same. Else, because the native long product is the only
492one that can lose catastrophic amounts of information, it's the native long
493product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000494*/
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000497int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000499 long a, b;
500 long longprod; /* a*b in native long arithmetic */
501 double doubled_longprod; /* (double)longprod */
502 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000503
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000504 CONVERT_TO_LONG(v, a);
505 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000506 longprod = a * b;
507 doubleprod = (double)a * (double)b;
508 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000509
Tim Petersa3c01ce2001-12-04 23:05:10 +0000510 /* Fast path for normal case: small multiplicands, and no info
511 is lost in either method. */
512 if (doubled_longprod == doubleprod)
513 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000514
Tim Petersa3c01ce2001-12-04 23:05:10 +0000515 /* Somebody somewhere lost info. Close enough, or way off? Note
516 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
517 The difference either is or isn't significant compared to the
518 true value (of which doubleprod is a good approximation).
519 */
520 {
521 const double diff = doubled_longprod - doubleprod;
522 const double absdiff = diff >= 0.0 ? diff : -diff;
523 const double absprod = doubleprod >= 0.0 ? doubleprod :
524 -doubleprod;
525 /* absdiff/absprod <= 1/32 iff
526 32 * absdiff <= absprod -- 5 good bits is "close enough" */
527 if (32.0 * absdiff <= absprod)
528 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000529 else
530 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000531 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532}
533
Guido van Rossume27f7952001-08-23 02:59:04 +0000534/* Return type of i_divmod */
535enum divmod_result {
536 DIVMOD_OK, /* Correct result */
537 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
538 DIVMOD_ERROR /* Exception raised */
539};
540
541static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000542i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000543 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000545 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000546
Tim Peters1dad6a82001-06-18 19:21:11 +0000547 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000549 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000550 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000552 /* (-sys.maxint-1)/-1 is the only overflow case. */
Tim Petersc8854432004-08-25 02:14:08 +0000553 if (y == -1 && x < 0 && x == -x)
Guido van Rossume27f7952001-08-23 02:59:04 +0000554 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000555 xdivy = x / y;
556 xmody = x - xdivy * y;
557 /* If the signs of x and y differ, and the remainder is non-0,
558 * C89 doesn't define whether xdivy is now the floor or the
559 * ceiling of the infinitely precise quotient. We want the floor,
560 * and we have it iff the remainder's sign matches y's.
561 */
562 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
563 xmody += y;
564 --xdivy;
565 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000566 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000567 *p_xdivy = xdivy;
568 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000569 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000570}
571
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000573int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000574{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000575 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000576 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000577 CONVERT_TO_LONG(x, xi);
578 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000579 switch (i_divmod(xi, yi, &d, &m)) {
580 case DIVMOD_OK:
581 return PyInt_FromLong(d);
582 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000583 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
584 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000585 default:
586 return NULL;
587 }
588}
589
590static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000591int_true_divide(PyObject *v, PyObject *w)
592{
Tim Peterse2a60002001-09-04 06:17:36 +0000593 /* If they aren't both ints, give someone else a chance. In
594 particular, this lets int/long get handled by longs, which
595 underflows to 0 gracefully if the long is too big to convert
596 to float. */
597 if (PyInt_Check(v) && PyInt_Check(w))
598 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
599 Py_INCREF(Py_NotImplemented);
600 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000601}
602
603static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000604int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000605{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000606 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000607 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000608 CONVERT_TO_LONG(x, xi);
609 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000610 switch (i_divmod(xi, yi, &d, &m)) {
611 case DIVMOD_OK:
612 return PyInt_FromLong(m);
613 case DIVMOD_OVERFLOW:
614 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
615 (PyObject *)y);
616 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000617 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000618 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000623{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000624 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000625 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000626 CONVERT_TO_LONG(x, xi);
627 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000628 switch (i_divmod(xi, yi, &d, &m)) {
629 case DIVMOD_OK:
630 return Py_BuildValue("(ll)", d, m);
631 case DIVMOD_OVERFLOW:
632 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
633 (PyObject *)y);
634 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000635 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000636 }
Guido van Rossum00466951991-05-05 20:08:27 +0000637}
638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000640int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000642 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000643 CONVERT_TO_LONG(v, iv);
644 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000645 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000646 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000647 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
648 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000649 return NULL;
650 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000651 /* Return a float. This works because we know that
652 this calls float_pow() which converts its
653 arguments to double. */
654 return PyFloat_Type.tp_as_number->nb_power(
655 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000656 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000658 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000659 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000661 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000662 return NULL;
663 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000664 }
665 /*
666 * XXX: The original exponentiation code stopped looping
667 * when temp hit zero; this code will continue onwards
668 * unnecessarily, but at least it won't cause any errors.
669 * Hopefully the speed improvement from the fast exponentiation
670 * will compensate for the slight inefficiency.
671 * XXX: Better handling of overflows is desperately needed.
672 */
673 temp = iv;
674 ix = 1;
675 while (iw > 0) {
676 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000677 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000678 ix = ix*temp;
679 if (temp == 0)
680 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000681 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000682 return PyLong_Type.tp_as_number->nb_power(
683 (PyObject *)v,
684 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000685 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000686 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000687 }
688 iw >>= 1; /* Shift exponent down by 1 bit */
689 if (iw==0) break;
690 prev = temp;
691 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000692 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000693 return PyLong_Type.tp_as_number->nb_power(
694 (PyObject *)v, (PyObject *)w, (PyObject *)z);
695 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000696 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000697 /* If we did a multiplication, perform a modulo */
698 ix = ix % iz;
699 temp = temp % iz;
700 }
701 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000702 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000703 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000704 switch (i_divmod(ix, iz, &div, &mod)) {
705 case DIVMOD_OK:
706 ix = mod;
707 break;
708 case DIVMOD_OVERFLOW:
709 return PyLong_Type.tp_as_number->nb_power(
710 (PyObject *)v, (PyObject *)w, (PyObject *)z);
711 default:
712 return NULL;
713 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000714 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000716}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000719int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000720{
721 register long a, x;
722 a = v->ob_ival;
723 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000724 if (a < 0 && x < 0) {
Tim Petersc8854432004-08-25 02:14:08 +0000725 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000726 if (o != NULL) {
727 PyObject *result = PyNumber_Negative(o);
728 Py_DECREF(o);
729 return result;
730 }
731 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000732 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734}
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000737int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000738{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000739 if (PyInt_CheckExact(v)) {
740 Py_INCREF(v);
741 return (PyObject *)v;
742 }
743 else
744 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745}
746
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000748int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000749{
750 if (v->ob_ival >= 0)
751 return int_pos(v);
752 else
753 return int_neg(v);
754}
755
Guido van Rossum0bff0151991-05-14 12:05:32 +0000756static int
Fred Drakea2f55112000-07-09 15:16:51 +0000757int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000758{
759 return v->ob_ival != 0;
760}
761
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000763int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000764{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000766}
767
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000769int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000770{
Guido van Rossum078151d2002-08-11 04:24:12 +0000771 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000772 PyObject *vv, *ww, *result;
773
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000774 CONVERT_TO_LONG(v, a);
775 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000776 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000778 return NULL;
779 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000780 if (a == 0 || b == 0)
781 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000782 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000783 vv = PyLong_FromLong(PyInt_AS_LONG(v));
784 if (vv == NULL)
785 return NULL;
786 ww = PyLong_FromLong(PyInt_AS_LONG(w));
787 if (ww == NULL) {
788 Py_DECREF(vv);
789 return NULL;
790 }
791 result = PyNumber_Lshift(vv, ww);
792 Py_DECREF(vv);
793 Py_DECREF(ww);
794 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000795 }
Tim Petersda1a2212002-08-11 17:54:42 +0000796 c = a << b;
797 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000798 vv = PyLong_FromLong(PyInt_AS_LONG(v));
799 if (vv == NULL)
800 return NULL;
801 ww = PyLong_FromLong(PyInt_AS_LONG(w));
802 if (ww == NULL) {
803 Py_DECREF(vv);
804 return NULL;
805 }
806 result = PyNumber_Lshift(vv, ww);
807 Py_DECREF(vv);
808 Py_DECREF(ww);
809 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000810 }
811 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000812}
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000815int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000816{
817 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000818 CONVERT_TO_LONG(v, a);
819 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000820 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000822 return NULL;
823 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000824 if (a == 0 || b == 0)
825 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000826 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000827 if (a < 0)
828 a = -1;
829 else
830 a = 0;
831 }
832 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000833 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000834 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000836}
837
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000839int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000840{
841 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000842 CONVERT_TO_LONG(v, a);
843 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000845}
846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000848int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000849{
850 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000851 CONVERT_TO_LONG(v, a);
852 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000853 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000854}
855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000857int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000858{
859 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000860 CONVERT_TO_LONG(v, a);
861 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000863}
864
Guido van Rossum1952e382001-09-19 01:25:16 +0000865static int
866int_coerce(PyObject **pv, PyObject **pw)
867{
868 if (PyInt_Check(*pw)) {
869 Py_INCREF(*pv);
870 Py_INCREF(*pw);
871 return 0;
872 }
873 return 1; /* Can't do it */
874}
875
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000877int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000878{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000879 if (PyInt_CheckExact(v))
880 Py_INCREF(v);
881 else
882 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000884}
885
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000887int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000888{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000890}
891
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000893int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000894{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000896}
897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000899int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000900{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000901 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000902 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000903 if (x < 0)
904 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
905 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000906 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000907 else
Barry Warsaw61975092001-11-28 20:55:34 +0000908 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000910}
911
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000913int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000914{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000915 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000916 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000917 if (x < 0)
918 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
919 else
920 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000922}
923
Jeremy Hylton938ace62002-07-17 16:30:39 +0000924static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000925int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
926
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927static PyObject *
928int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
929{
930 PyObject *x = NULL;
931 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000932 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000933
Guido van Rossumbef14172001-08-29 15:47:46 +0000934 if (type != &PyInt_Type)
935 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000936 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
937 &x, &base))
938 return NULL;
939 if (x == NULL)
940 return PyInt_FromLong(0L);
941 if (base == -909)
942 return PyNumber_Int(x);
943 if (PyString_Check(x))
944 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000945#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946 if (PyUnicode_Check(x))
947 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
948 PyUnicode_GET_SIZE(x),
949 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000950#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951 PyErr_SetString(PyExc_TypeError,
952 "int() can't convert non-string with explicit base");
953 return NULL;
954}
955
Guido van Rossumbef14172001-08-29 15:47:46 +0000956/* Wimpy, slow approach to tp_new calls for subtypes of int:
957 first create a regular int from whatever arguments we got,
958 then allocate a subtype instance and initialize its ob_ival
959 from the regular int. The regular int is then thrown away.
960*/
961static PyObject *
962int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
963{
964 PyObject *tmp, *new;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000965 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000966
967 assert(PyType_IsSubtype(type, &PyInt_Type));
968 tmp = int_new(&PyInt_Type, args, kwds);
969 if (tmp == NULL)
970 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000971 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000972 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000973 if (ival == -1 && PyErr_Occurred()) {
974 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000975 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000976 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000977 } else {
978 ival = ((PyIntObject *)tmp)->ob_ival;
979 }
980
Guido van Rossumd93dce12001-08-30 03:09:31 +0000981 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000982 if (new == NULL) {
983 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000984 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000985 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000986 ((PyIntObject *)new)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000987 Py_DECREF(tmp);
988 return new;
989}
990
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000991static PyObject *
992int_getnewargs(PyIntObject *v)
993{
994 return Py_BuildValue("(l)", v->ob_ival);
995}
996
997static PyMethodDef int_methods[] = {
998 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
999 {NULL, NULL} /* sentinel */
1000};
1001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003"int(x[, base]) -> integer\n\
1004\n\
1005Convert a string or number to an integer, if possible. A floating point\n\
1006argument will be truncated towards zero (this does not include a string\n\
1007representation of a floating point number!) When converting a string, use\n\
1008the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +00001009non-string. If the argument is outside the integer range a long object\n\
1010will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001013 (binaryfunc)int_add, /*nb_add*/
1014 (binaryfunc)int_sub, /*nb_subtract*/
1015 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001016 (binaryfunc)int_mod, /*nb_remainder*/
1017 (binaryfunc)int_divmod, /*nb_divmod*/
1018 (ternaryfunc)int_pow, /*nb_power*/
1019 (unaryfunc)int_neg, /*nb_negative*/
1020 (unaryfunc)int_pos, /*nb_positive*/
1021 (unaryfunc)int_abs, /*nb_absolute*/
1022 (inquiry)int_nonzero, /*nb_nonzero*/
1023 (unaryfunc)int_invert, /*nb_invert*/
1024 (binaryfunc)int_lshift, /*nb_lshift*/
1025 (binaryfunc)int_rshift, /*nb_rshift*/
1026 (binaryfunc)int_and, /*nb_and*/
1027 (binaryfunc)int_xor, /*nb_xor*/
1028 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001029 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001030 (unaryfunc)int_int, /*nb_int*/
1031 (unaryfunc)int_long, /*nb_long*/
1032 (unaryfunc)int_float, /*nb_float*/
1033 (unaryfunc)int_oct, /*nb_oct*/
1034 (unaryfunc)int_hex, /*nb_hex*/
1035 0, /*nb_inplace_add*/
1036 0, /*nb_inplace_subtract*/
1037 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001038 0, /*nb_inplace_remainder*/
1039 0, /*nb_inplace_power*/
1040 0, /*nb_inplace_lshift*/
1041 0, /*nb_inplace_rshift*/
1042 0, /*nb_inplace_and*/
1043 0, /*nb_inplace_xor*/
1044 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001045 (binaryfunc)int_div, /* nb_floor_divide */
1046 int_true_divide, /* nb_true_divide */
1047 0, /* nb_inplace_floor_divide */
1048 0, /* nb_inplace_true_divide */
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001049 (lenfunc)PyInt_AsSsize_t, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001050};
1051
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001052PyTypeObject PyInt_Type = {
1053 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001054 0,
1055 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001057 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001058 (destructor)int_dealloc, /* tp_dealloc */
1059 (printfunc)int_print, /* tp_print */
1060 0, /* tp_getattr */
1061 0, /* tp_setattr */
1062 (cmpfunc)int_compare, /* tp_compare */
1063 (reprfunc)int_repr, /* tp_repr */
1064 &int_as_number, /* tp_as_number */
1065 0, /* tp_as_sequence */
1066 0, /* tp_as_mapping */
1067 (hashfunc)int_hash, /* tp_hash */
1068 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001069 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001070 PyObject_GenericGetAttr, /* tp_getattro */
1071 0, /* tp_setattro */
1072 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001073 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1074 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075 int_doc, /* tp_doc */
1076 0, /* tp_traverse */
1077 0, /* tp_clear */
1078 0, /* tp_richcompare */
1079 0, /* tp_weaklistoffset */
1080 0, /* tp_iter */
1081 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001082 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001083 0, /* tp_members */
1084 0, /* tp_getset */
1085 0, /* tp_base */
1086 0, /* tp_dict */
1087 0, /* tp_descr_get */
1088 0, /* tp_descr_set */
1089 0, /* tp_dictoffset */
1090 0, /* tp_init */
1091 0, /* tp_alloc */
1092 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001093 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001095
Neal Norwitzc91ed402002-12-30 22:29:22 +00001096int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001097_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001098{
1099 PyIntObject *v;
1100 int ival;
1101#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1102 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001103 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001104 return 0;
1105 /* PyObject_New is inlined */
1106 v = free_list;
1107 free_list = (PyIntObject *)v->ob_type;
1108 PyObject_INIT(v, &PyInt_Type);
1109 v->ob_ival = ival;
1110 small_ints[ival + NSMALLNEGINTS] = v;
1111 }
1112#endif
1113 return 1;
1114}
1115
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001116void
Fred Drakea2f55112000-07-09 15:16:51 +00001117PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001118{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001119 PyIntObject *p;
1120 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001121 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001122 int bc, bf; /* block count, number of freed blocks */
1123 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001124
Guido van Rossumda084ed1999-03-10 22:55:24 +00001125#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1126 PyIntObject **q;
1127
1128 i = NSMALLNEGINTS + NSMALLPOSINTS;
1129 q = small_ints;
1130 while (--i >= 0) {
1131 Py_XDECREF(*q);
1132 *q++ = NULL;
1133 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001134#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001135 bc = 0;
1136 bf = 0;
1137 isum = 0;
1138 list = block_list;
1139 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001140 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001141 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001142 bc++;
1143 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001144 for (i = 0, p = &list->objects[0];
1145 i < N_INTOBJECTS;
1146 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001147 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001148 irem++;
1149 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001150 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001151 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001152 list->next = block_list;
1153 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001154 for (i = 0, p = &list->objects[0];
1155 i < N_INTOBJECTS;
1156 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001157 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001158 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001159 p->ob_type = (struct _typeobject *)
1160 free_list;
1161 free_list = p;
1162 }
1163#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1164 else if (-NSMALLNEGINTS <= p->ob_ival &&
1165 p->ob_ival < NSMALLPOSINTS &&
1166 small_ints[p->ob_ival +
1167 NSMALLNEGINTS] == NULL) {
1168 Py_INCREF(p);
1169 small_ints[p->ob_ival +
1170 NSMALLNEGINTS] = p;
1171 }
1172#endif
1173 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001174 }
1175 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001176 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001177 bf++;
1178 }
1179 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001180 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001181 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001182 if (!Py_VerboseFlag)
1183 return;
1184 fprintf(stderr, "# cleanup ints");
1185 if (!isum) {
1186 fprintf(stderr, "\n");
1187 }
1188 else {
1189 fprintf(stderr,
1190 ": %d unfreed int%s in %d out of %d block%s\n",
1191 isum, isum == 1 ? "" : "s",
1192 bc - bf, bc, bc == 1 ? "" : "s");
1193 }
1194 if (Py_VerboseFlag > 1) {
1195 list = block_list;
1196 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001197 for (i = 0, p = &list->objects[0];
1198 i < N_INTOBJECTS;
1199 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001200 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001201 /* XXX(twouters) cast refcount to
1202 long until %zd is universally
1203 available
1204 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001205 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001206 "# <int at %p, refcnt=%ld, val=%ld>\n",
1207 p, (long)p->ob_refcnt,
1208 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001209 }
1210 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001211 }
1212 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001213}