blob: b97e2bc631eec236ca317257a246761ce22676e0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossume27f7952001-08-23 02:59:04 +000013/* Return 1 if exception raised, 0 if caller should retry using longs */
14static int
Fred Drakea2f55112000-07-09 15:16:51 +000015err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000016{
Guido van Rossume27f7952001-08-23 02:59:04 +000017 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
Guido van Rossum0b131162001-08-23 21:32:40 +000018 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
19 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000020 return 1;
21 }
22 else
23 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000024}
25
Guido van Rossum3f5da241990-12-20 15:06:42 +000026/* Integers are quite normal objects, to make object handling uniform.
27 (Using odd pointers to represent integers would save much space
28 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000029 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000030 and deallocating integers, these operations should be very fast.
31 Therefore we use a dedicated allocation scheme with a much lower
32 overhead (in space and time) than straight malloc(): a simple
33 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000034
35 block_list is a singly-linked list of all PyIntBlocks ever allocated,
36 linked via their next members. PyIntBlocks are never returned to the
37 system before shutdown (PyInt_Fini).
38
39 free_list is a singly-linked list of available PyIntObjects, linked
40 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000041*/
42
43#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000044#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
45#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000046
Guido van Rossum3fce8831999-03-12 19:43:17 +000047struct _intblock {
48 struct _intblock *next;
49 PyIntObject objects[N_INTOBJECTS];
50};
51
52typedef struct _intblock PyIntBlock;
53
54static PyIntBlock *block_list = NULL;
55static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000058fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000061 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000062 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000065 ((PyIntBlock *)p)->next = block_list;
66 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000067 /* Link the int objects together, from rear to front, then return
68 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000069 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 q = p + N_INTOBJECTS;
71 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000072 q->ob_type = (struct _typeobject *)(q-1);
73 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 return p + N_INTOBJECTS - 1;
75}
76
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#ifndef NSMALLPOSINTS
78#define NSMALLPOSINTS 100
79#endif
80#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000081#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000082#endif
83#if NSMALLNEGINTS + NSMALLPOSINTS > 0
84/* References to small integers are saved in this array so that they
85 can be shared.
86 The integers that are saved are those in the range
87 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
88*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
91#ifdef COUNT_ALLOCS
92int quick_int_allocs, quick_neg_int_allocs;
93#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000096PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +0000100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
101 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifdef COUNT_ALLOCS
104 if (ival >= 0)
105 quick_int_allocs++;
106 else
107 quick_neg_int_allocs++;
108#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110 }
111#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 if (free_list == NULL) {
113 if ((free_list = fill_free_list()) == NULL)
114 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000116 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000118 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122}
123
124static void
Fred Drakea2f55112000-07-09 15:16:51 +0000125int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000127 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000128 v->ob_type = (struct _typeobject *)free_list;
129 free_list = v;
130 }
131 else
132 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossum93646982002-04-26 00:53:34 +0000135static void
136int_free(PyIntObject *v)
137{
138 v->ob_type = (struct _typeobject *)free_list;
139 free_list = v;
140}
141
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142long
Fred Drakea2f55112000-07-09 15:16:51 +0000143PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 PyNumberMethods *nb;
146 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000147 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 if (op && PyInt_Check(op))
150 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
153 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000154 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return -1;
156 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000157
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 if (io == NULL)
160 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000162 if (PyLong_Check(io)) {
163 /* got a long? => retry int conversion */
164 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000165 Py_DECREF(io);
166 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000167 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000168 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000169 }
170 else
171 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000172 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000173 PyErr_SetString(PyExc_TypeError,
174 "nb_int should return int object");
175 return -1;
176 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000177 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000178
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 val = PyInt_AS_LONG(io);
180 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000181
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000182 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
Thomas Hellera4ea6032003-04-17 18:55:45 +0000185unsigned long
186PyInt_AsUnsignedLongMask(register PyObject *op)
187{
188 PyNumberMethods *nb;
189 PyIntObject *io;
190 unsigned long val;
191
192 if (op && PyInt_Check(op))
193 return PyInt_AS_LONG((PyIntObject*) op);
194 if (op && PyLong_Check(op))
195 return PyLong_AsUnsignedLongMask(op);
196
197 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
198 nb->nb_int == NULL) {
199 PyErr_SetString(PyExc_TypeError, "an integer is required");
200 return -1;
201 }
202
203 io = (PyIntObject*) (*nb->nb_int) (op);
204 if (io == NULL)
205 return -1;
206 if (!PyInt_Check(io)) {
207 if (PyLong_Check(io)) {
208 val = PyLong_AsUnsignedLongMask((PyObject *)io);
209 Py_DECREF(io);
210 if (PyErr_Occurred())
211 return -1;
212 return val;
213 }
214 else
215 {
216 Py_DECREF(io);
217 PyErr_SetString(PyExc_TypeError,
218 "nb_int should return int object");
219 return -1;
220 }
221 }
222
223 val = PyInt_AS_LONG(io);
224 Py_DECREF(io);
225
226 return val;
227}
228
229#ifdef HAVE_LONG_LONG
230unsigned PY_LONG_LONG
231PyInt_AsUnsignedLongLongMask(register PyObject *op)
232{
233 PyNumberMethods *nb;
234 PyIntObject *io;
235 unsigned PY_LONG_LONG val;
236
237 if (op && PyInt_Check(op))
238 return PyInt_AS_LONG((PyIntObject*) op);
239 if (op && PyLong_Check(op))
240 return PyLong_AsUnsignedLongLongMask(op);
241
242 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
243 nb->nb_int == NULL) {
244 PyErr_SetString(PyExc_TypeError, "an integer is required");
245 return -1;
246 }
247
248 io = (PyIntObject*) (*nb->nb_int) (op);
249 if (io == NULL)
250 return -1;
251 if (!PyInt_Check(io)) {
252 if (PyLong_Check(io)) {
253 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
254 Py_DECREF(io);
255 if (PyErr_Occurred())
256 return -1;
257 return val;
258 }
259 else
260 {
261 Py_DECREF(io);
262 PyErr_SetString(PyExc_TypeError,
263 "nb_int should return int object");
264 return -1;
265 }
266 }
267
268 val = PyInt_AS_LONG(io);
269 Py_DECREF(io);
270
271 return val;
272}
273#endif
274
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000276PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000277{
278 char *end;
279 long x;
280 char buffer[256]; /* For errors */
Guido van Rossum47710652003-02-12 20:48:22 +0000281 int warn = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000282
283 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000284 PyErr_SetString(PyExc_ValueError,
285 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000286 return NULL;
287 }
288
289 while (*s && isspace(Py_CHARMASK(*s)))
290 s++;
291 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000292 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000294 if (x < 0)
295 warn = 1;
296 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000297 else
298 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000299 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000300 goto bad;
301 while (*end && isspace(Py_CHARMASK(*end)))
302 end++;
303 if (*end != '\0') {
304 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000305 PyOS_snprintf(buffer, sizeof(buffer),
306 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000307 PyErr_SetString(PyExc_ValueError, buffer);
308 return NULL;
309 }
310 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000311 if (err_ovf("string/unicode conversion"))
312 return NULL;
313 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000314 }
Guido van Rossum47710652003-02-12 20:48:22 +0000315 if (warn) {
316 if (PyErr_Warn(PyExc_FutureWarning,
317 "int('0...', 0): sign will change in Python 2.4") < 0)
318 return NULL;
319 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000320 if (pend)
321 *pend = end;
322 return PyInt_FromLong(x);
323}
324
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000325#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000326PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000327PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000328{
Walter Dörwald07e14762002-11-06 16:15:14 +0000329 PyObject *result;
330 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000331
Walter Dörwald07e14762002-11-06 16:15:14 +0000332 if (buffer == NULL)
333 return NULL;
334
335 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
336 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000337 return NULL;
338 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000339 result = PyInt_FromString(buffer, NULL, base);
340 PyMem_FREE(buffer);
341 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000342}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000343#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345/* Methods */
346
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000347/* Integers are seen as the "smallest" of all numeric types and thus
348 don't have any knowledge about conversion of other types to
349 integers. */
350
351#define CONVERT_TO_LONG(obj, lng) \
352 if (PyInt_Check(obj)) { \
353 lng = PyInt_AS_LONG(obj); \
354 } \
355 else { \
356 Py_INCREF(Py_NotImplemented); \
357 return Py_NotImplemented; \
358 }
359
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000360/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000361static int
Fred Drakea2f55112000-07-09 15:16:51 +0000362int_print(PyIntObject *v, FILE *fp, int flags)
363 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364{
365 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000366 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367}
368
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000370int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Tim Peters42221042001-12-01 02:52:56 +0000372 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000373 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
377static int
Fred Drakea2f55112000-07-09 15:16:51 +0000378int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379{
380 register long i = v->ob_ival;
381 register long j = w->ob_ival;
382 return (i < j) ? -1 : (i > j) ? 1 : 0;
383}
384
Guido van Rossum9bfef441993-03-29 10:43:31 +0000385static long
Fred Drakea2f55112000-07-09 15:16:51 +0000386int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000387{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000388 /* XXX If this is changed, you also need to change the way
389 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000390 long x = v -> ob_ival;
391 if (x == -1)
392 x = -2;
393 return x;
394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000397int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398{
399 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000400 CONVERT_TO_LONG(v, a);
401 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000403 if ((x^a) >= 0 || (x^b) >= 0)
404 return PyInt_FromLong(x);
405 if (err_ovf("integer addition"))
406 return NULL;
407 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408}
409
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000411int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412{
413 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000414 CONVERT_TO_LONG(v, a);
415 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000417 if ((x^a) >= 0 || (x^~b) >= 0)
418 return PyInt_FromLong(x);
419 if (err_ovf("integer subtraction"))
420 return NULL;
421 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
422 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423}
424
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000425/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000426Integer overflow checking for * is painful: Python tried a couple ways, but
427they didn't work on all platforms, or failed in endcases (a product of
428-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000429
Tim Petersa3c01ce2001-12-04 23:05:10 +0000430Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000431
Tim Petersa3c01ce2001-12-04 23:05:10 +0000432The native long product x*y is either exactly right or *way* off, being
433just the last n bits of the true product, where n is the number of bits
434in a long (the delivered product is the true product plus i*2**n for
435some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000436
Tim Petersa3c01ce2001-12-04 23:05:10 +0000437The native double product (double)x * (double)y is subject to three
438rounding errors: on a sizeof(long)==8 box, each cast to double can lose
439info, and even on a sizeof(long)==4 box, the multiplication can lose info.
440But, unlike the native long product, it's not in *range* trouble: even
441if sizeof(long)==32 (256-bit longs), the product easily fits in the
442dynamic range of a double. So the leading 50 (or so) bits of the double
443product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000444
Tim Petersa3c01ce2001-12-04 23:05:10 +0000445We check these two ways against each other, and declare victory if they're
446approximately the same. Else, because the native long product is the only
447one that can lose catastrophic amounts of information, it's the native long
448product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000449*/
450
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000452int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000454 long a, b;
455 long longprod; /* a*b in native long arithmetic */
456 double doubled_longprod; /* (double)longprod */
457 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000458
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000459 CONVERT_TO_LONG(v, a);
460 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000461 longprod = a * b;
462 doubleprod = (double)a * (double)b;
463 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000464
Tim Petersa3c01ce2001-12-04 23:05:10 +0000465 /* Fast path for normal case: small multiplicands, and no info
466 is lost in either method. */
467 if (doubled_longprod == doubleprod)
468 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000469
Tim Petersa3c01ce2001-12-04 23:05:10 +0000470 /* Somebody somewhere lost info. Close enough, or way off? Note
471 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
472 The difference either is or isn't significant compared to the
473 true value (of which doubleprod is a good approximation).
474 */
475 {
476 const double diff = doubled_longprod - doubleprod;
477 const double absdiff = diff >= 0.0 ? diff : -diff;
478 const double absprod = doubleprod >= 0.0 ? doubleprod :
479 -doubleprod;
480 /* absdiff/absprod <= 1/32 iff
481 32 * absdiff <= absprod -- 5 good bits is "close enough" */
482 if (32.0 * absdiff <= absprod)
483 return PyInt_FromLong(longprod);
484 else if (err_ovf("integer multiplication"))
485 return NULL;
486 else
487 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000488 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489}
490
Guido van Rossume27f7952001-08-23 02:59:04 +0000491/* Return type of i_divmod */
492enum divmod_result {
493 DIVMOD_OK, /* Correct result */
494 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
495 DIVMOD_ERROR /* Exception raised */
496};
497
498static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000499i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000500 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000502 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000503
Tim Peters1dad6a82001-06-18 19:21:11 +0000504 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000506 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000507 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000509 /* (-sys.maxint-1)/-1 is the only overflow case. */
510 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000511 if (err_ovf("integer division"))
512 return DIVMOD_ERROR;
513 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000514 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000515 xdivy = x / y;
516 xmody = x - xdivy * y;
517 /* If the signs of x and y differ, and the remainder is non-0,
518 * C89 doesn't define whether xdivy is now the floor or the
519 * ceiling of the infinitely precise quotient. We want the floor,
520 * and we have it iff the remainder's sign matches y's.
521 */
522 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
523 xmody += y;
524 --xdivy;
525 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000526 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000527 *p_xdivy = xdivy;
528 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000529 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000530}
531
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000533int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000534{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000535 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000536 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000537 CONVERT_TO_LONG(x, xi);
538 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000539 switch (i_divmod(xi, yi, &d, &m)) {
540 case DIVMOD_OK:
541 return PyInt_FromLong(d);
542 case DIVMOD_OVERFLOW:
543 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
544 (PyObject *)y);
545 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000546 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000547 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000548}
549
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000551int_classic_div(PyIntObject *x, PyIntObject *y)
552{
553 long xi, yi;
554 long d, m;
555 CONVERT_TO_LONG(x, xi);
556 CONVERT_TO_LONG(y, yi);
557 if (Py_DivisionWarningFlag &&
558 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
559 return NULL;
560 switch (i_divmod(xi, yi, &d, &m)) {
561 case DIVMOD_OK:
562 return PyInt_FromLong(d);
563 case DIVMOD_OVERFLOW:
564 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
565 (PyObject *)y);
566 default:
567 return NULL;
568 }
569}
570
571static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000572int_true_divide(PyObject *v, PyObject *w)
573{
Tim Peterse2a60002001-09-04 06:17:36 +0000574 /* If they aren't both ints, give someone else a chance. In
575 particular, this lets int/long get handled by longs, which
576 underflows to 0 gracefully if the long is too big to convert
577 to float. */
578 if (PyInt_Check(v) && PyInt_Check(w))
579 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
580 Py_INCREF(Py_NotImplemented);
581 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000582}
583
584static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000585int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000586{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000587 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000588 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000589 CONVERT_TO_LONG(x, xi);
590 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000591 switch (i_divmod(xi, yi, &d, &m)) {
592 case DIVMOD_OK:
593 return PyInt_FromLong(m);
594 case DIVMOD_OVERFLOW:
595 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
596 (PyObject *)y);
597 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000598 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000599 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000600}
601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000603int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000604{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000605 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000606 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000607 CONVERT_TO_LONG(x, xi);
608 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000609 switch (i_divmod(xi, yi, &d, &m)) {
610 case DIVMOD_OK:
611 return Py_BuildValue("(ll)", d, m);
612 case DIVMOD_OVERFLOW:
613 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
614 (PyObject *)y);
615 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000616 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000617 }
Guido van Rossum00466951991-05-05 20:08:27 +0000618}
619
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000621int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000622{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000623 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000624 CONVERT_TO_LONG(v, iv);
625 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000626 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000627 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000628 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
629 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000630 return NULL;
631 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000632 /* Return a float. This works because we know that
633 this calls float_pow() which converts its
634 arguments to double. */
635 return PyFloat_Type.tp_as_number->nb_power(
636 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000637 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000639 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000640 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000642 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000643 return NULL;
644 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000645 }
646 /*
647 * XXX: The original exponentiation code stopped looping
648 * when temp hit zero; this code will continue onwards
649 * unnecessarily, but at least it won't cause any errors.
650 * Hopefully the speed improvement from the fast exponentiation
651 * will compensate for the slight inefficiency.
652 * XXX: Better handling of overflows is desperately needed.
653 */
654 temp = iv;
655 ix = 1;
656 while (iw > 0) {
657 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000658 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000659 ix = ix*temp;
660 if (temp == 0)
661 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000662 if (ix / temp != prev) {
663 if (err_ovf("integer exponentiation"))
664 return NULL;
665 return PyLong_Type.tp_as_number->nb_power(
666 (PyObject *)v,
667 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000668 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000669 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000670 }
671 iw >>= 1; /* Shift exponent down by 1 bit */
672 if (iw==0) break;
673 prev = temp;
674 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000675 if (prev!=0 && temp/prev!=prev) {
676 if (err_ovf("integer exponentiation"))
677 return NULL;
678 return PyLong_Type.tp_as_number->nb_power(
679 (PyObject *)v, (PyObject *)w, (PyObject *)z);
680 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000681 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000682 /* If we did a multiplication, perform a modulo */
683 ix = ix % iz;
684 temp = temp % iz;
685 }
686 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000687 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000688 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000689 switch (i_divmod(ix, iz, &div, &mod)) {
690 case DIVMOD_OK:
691 ix = mod;
692 break;
693 case DIVMOD_OVERFLOW:
694 return PyLong_Type.tp_as_number->nb_power(
695 (PyObject *)v, (PyObject *)w, (PyObject *)z);
696 default:
697 return NULL;
698 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000699 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000701}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000704int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705{
706 register long a, x;
707 a = v->ob_ival;
708 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000709 if (a < 0 && x < 0) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000710 PyObject *o;
Guido van Rossume27f7952001-08-23 02:59:04 +0000711 if (err_ovf("integer negation"))
712 return NULL;
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000713 o = PyLong_FromLong(a);
714 if (o != NULL) {
715 PyObject *result = PyNumber_Negative(o);
716 Py_DECREF(o);
717 return result;
718 }
719 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000720 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722}
723
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000725int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000727 if (PyInt_CheckExact(v)) {
728 Py_INCREF(v);
729 return (PyObject *)v;
730 }
731 else
732 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733}
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000736int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000737{
738 if (v->ob_ival >= 0)
739 return int_pos(v);
740 else
741 return int_neg(v);
742}
743
Guido van Rossum0bff0151991-05-14 12:05:32 +0000744static int
Fred Drakea2f55112000-07-09 15:16:51 +0000745int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000746{
747 return v->ob_ival != 0;
748}
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000751int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000752{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754}
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000757int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000758{
Guido van Rossum078151d2002-08-11 04:24:12 +0000759 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000760 CONVERT_TO_LONG(v, a);
761 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000762 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000764 return NULL;
765 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000766 if (a == 0 || b == 0)
767 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000768 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000769 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000770 "x<<y losing bits or changing sign "
771 "will return a long in Python 2.4 and up") < 0)
772 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000774 }
Tim Petersda1a2212002-08-11 17:54:42 +0000775 c = a << b;
776 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000777 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000778 "x<<y losing bits or changing sign "
779 "will return a long in Python 2.4 and up") < 0)
780 return NULL;
781 }
782 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000783}
784
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000786int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000787{
788 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000789 CONVERT_TO_LONG(v, a);
790 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000791 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000793 return NULL;
794 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000795 if (a == 0 || b == 0)
796 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000797 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000798 if (a < 0)
799 a = -1;
800 else
801 a = 0;
802 }
803 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000804 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000805 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000807}
808
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000810int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000811{
812 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000813 CONVERT_TO_LONG(v, a);
814 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000816}
817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000819int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000820{
821 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000822 CONVERT_TO_LONG(v, a);
823 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000825}
826
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000828int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000829{
830 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000831 CONVERT_TO_LONG(v, a);
832 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000834}
835
Guido van Rossum1952e382001-09-19 01:25:16 +0000836static int
837int_coerce(PyObject **pv, PyObject **pw)
838{
839 if (PyInt_Check(*pw)) {
840 Py_INCREF(*pv);
841 Py_INCREF(*pw);
842 return 0;
843 }
844 return 1; /* Can't do it */
845}
846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000848int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000849{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 Py_INCREF(v);
851 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000852}
853
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000854static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000855int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000856{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000858}
859
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000861int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000862{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000864}
865
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000867int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000868{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000869 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000870 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000871 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000872 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000873 "hex()/oct() of negative int will return "
874 "a signed string in Python 2.4 and up") < 0)
875 return NULL;
876 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000877 if (x == 0)
878 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000879 else
Barry Warsaw61975092001-11-28 20:55:34 +0000880 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000882}
883
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000885int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000886{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000887 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000888 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000889 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000890 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000891 "hex()/oct() of negative int will return "
892 "a signed string in Python 2.4 and up") < 0)
893 return NULL;
894 }
Barry Warsaw61975092001-11-28 20:55:34 +0000895 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000896 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000897}
898
Jeremy Hylton938ace62002-07-17 16:30:39 +0000899static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000900int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
901
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902static PyObject *
903int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
904{
905 PyObject *x = NULL;
906 int base = -909;
907 static char *kwlist[] = {"x", "base", 0};
908
Guido van Rossumbef14172001-08-29 15:47:46 +0000909 if (type != &PyInt_Type)
910 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
912 &x, &base))
913 return NULL;
914 if (x == NULL)
915 return PyInt_FromLong(0L);
916 if (base == -909)
917 return PyNumber_Int(x);
918 if (PyString_Check(x))
919 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000920#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921 if (PyUnicode_Check(x))
922 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
923 PyUnicode_GET_SIZE(x),
924 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000925#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926 PyErr_SetString(PyExc_TypeError,
927 "int() can't convert non-string with explicit base");
928 return NULL;
929}
930
Guido van Rossumbef14172001-08-29 15:47:46 +0000931/* Wimpy, slow approach to tp_new calls for subtypes of int:
932 first create a regular int from whatever arguments we got,
933 then allocate a subtype instance and initialize its ob_ival
934 from the regular int. The regular int is then thrown away.
935*/
936static PyObject *
937int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
938{
939 PyObject *tmp, *new;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000940 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000941
942 assert(PyType_IsSubtype(type, &PyInt_Type));
943 tmp = int_new(&PyInt_Type, args, kwds);
944 if (tmp == NULL)
945 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000946 if (!PyInt_Check(tmp)) {
947 if (!PyLong_Check(tmp)) {
948 PyErr_SetString(PyExc_ValueError,
949 "value must convertable to an int");
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000950 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000951 return NULL;
952 }
953 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000954 if (ival == -1 && PyErr_Occurred()) {
955 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000956 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000957 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000958 } else {
959 ival = ((PyIntObject *)tmp)->ob_ival;
960 }
961
Guido van Rossumd93dce12001-08-30 03:09:31 +0000962 new = type->tp_alloc(type, 0);
Raymond Hettingerf4667932003-06-28 20:04:25 +0000963 if (new == NULL) {
964 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000965 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000966 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000967 ((PyIntObject *)new)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000968 Py_DECREF(tmp);
969 return new;
970}
971
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000972static PyObject *
973int_getnewargs(PyIntObject *v)
974{
975 return Py_BuildValue("(l)", v->ob_ival);
976}
977
978static PyMethodDef int_methods[] = {
979 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
980 {NULL, NULL} /* sentinel */
981};
982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000983PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984"int(x[, base]) -> integer\n\
985\n\
986Convert a string or number to an integer, if possible. A floating point\n\
987argument will be truncated towards zero (this does not include a string\n\
988representation of a floating point number!) When converting a string, use\n\
989the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000990non-string. If the argument is outside the integer range a long object\n\
991will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000994 (binaryfunc)int_add, /*nb_add*/
995 (binaryfunc)int_sub, /*nb_subtract*/
996 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000997 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000998 (binaryfunc)int_mod, /*nb_remainder*/
999 (binaryfunc)int_divmod, /*nb_divmod*/
1000 (ternaryfunc)int_pow, /*nb_power*/
1001 (unaryfunc)int_neg, /*nb_negative*/
1002 (unaryfunc)int_pos, /*nb_positive*/
1003 (unaryfunc)int_abs, /*nb_absolute*/
1004 (inquiry)int_nonzero, /*nb_nonzero*/
1005 (unaryfunc)int_invert, /*nb_invert*/
1006 (binaryfunc)int_lshift, /*nb_lshift*/
1007 (binaryfunc)int_rshift, /*nb_rshift*/
1008 (binaryfunc)int_and, /*nb_and*/
1009 (binaryfunc)int_xor, /*nb_xor*/
1010 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001011 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001012 (unaryfunc)int_int, /*nb_int*/
1013 (unaryfunc)int_long, /*nb_long*/
1014 (unaryfunc)int_float, /*nb_float*/
1015 (unaryfunc)int_oct, /*nb_oct*/
1016 (unaryfunc)int_hex, /*nb_hex*/
1017 0, /*nb_inplace_add*/
1018 0, /*nb_inplace_subtract*/
1019 0, /*nb_inplace_multiply*/
1020 0, /*nb_inplace_divide*/
1021 0, /*nb_inplace_remainder*/
1022 0, /*nb_inplace_power*/
1023 0, /*nb_inplace_lshift*/
1024 0, /*nb_inplace_rshift*/
1025 0, /*nb_inplace_and*/
1026 0, /*nb_inplace_xor*/
1027 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001028 (binaryfunc)int_div, /* nb_floor_divide */
1029 int_true_divide, /* nb_true_divide */
1030 0, /* nb_inplace_floor_divide */
1031 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001032};
1033
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034PyTypeObject PyInt_Type = {
1035 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001036 0,
1037 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040 (destructor)int_dealloc, /* tp_dealloc */
1041 (printfunc)int_print, /* tp_print */
1042 0, /* tp_getattr */
1043 0, /* tp_setattr */
1044 (cmpfunc)int_compare, /* tp_compare */
1045 (reprfunc)int_repr, /* tp_repr */
1046 &int_as_number, /* tp_as_number */
1047 0, /* tp_as_sequence */
1048 0, /* tp_as_mapping */
1049 (hashfunc)int_hash, /* tp_hash */
1050 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001051 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052 PyObject_GenericGetAttr, /* tp_getattro */
1053 0, /* tp_setattro */
1054 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001055 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1056 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057 int_doc, /* tp_doc */
1058 0, /* tp_traverse */
1059 0, /* tp_clear */
1060 0, /* tp_richcompare */
1061 0, /* tp_weaklistoffset */
1062 0, /* tp_iter */
1063 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001064 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065 0, /* tp_members */
1066 0, /* tp_getset */
1067 0, /* tp_base */
1068 0, /* tp_dict */
1069 0, /* tp_descr_get */
1070 0, /* tp_descr_set */
1071 0, /* tp_dictoffset */
1072 0, /* tp_init */
1073 0, /* tp_alloc */
1074 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001075 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001077
Neal Norwitzc91ed402002-12-30 22:29:22 +00001078int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001079_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001080{
1081 PyIntObject *v;
1082 int ival;
1083#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1084 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
1085 if ((free_list = fill_free_list()) == NULL)
1086 return 0;
1087 /* PyObject_New is inlined */
1088 v = free_list;
1089 free_list = (PyIntObject *)v->ob_type;
1090 PyObject_INIT(v, &PyInt_Type);
1091 v->ob_ival = ival;
1092 small_ints[ival + NSMALLNEGINTS] = v;
1093 }
1094#endif
1095 return 1;
1096}
1097
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001098void
Fred Drakea2f55112000-07-09 15:16:51 +00001099PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001100{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001101 PyIntObject *p;
1102 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001103 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001104 int bc, bf; /* block count, number of freed blocks */
1105 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001106
Guido van Rossumda084ed1999-03-10 22:55:24 +00001107#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1108 PyIntObject **q;
1109
1110 i = NSMALLNEGINTS + NSMALLPOSINTS;
1111 q = small_ints;
1112 while (--i >= 0) {
1113 Py_XDECREF(*q);
1114 *q++ = NULL;
1115 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001116#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001117 bc = 0;
1118 bf = 0;
1119 isum = 0;
1120 list = block_list;
1121 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001122 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001123 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001124 bc++;
1125 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001126 for (i = 0, p = &list->objects[0];
1127 i < N_INTOBJECTS;
1128 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001129 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001130 irem++;
1131 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001132 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001133 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001134 list->next = block_list;
1135 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001136 for (i = 0, p = &list->objects[0];
1137 i < N_INTOBJECTS;
1138 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001139 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001140 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001141 p->ob_type = (struct _typeobject *)
1142 free_list;
1143 free_list = p;
1144 }
1145#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1146 else if (-NSMALLNEGINTS <= p->ob_ival &&
1147 p->ob_ival < NSMALLPOSINTS &&
1148 small_ints[p->ob_ival +
1149 NSMALLNEGINTS] == NULL) {
1150 Py_INCREF(p);
1151 small_ints[p->ob_ival +
1152 NSMALLNEGINTS] = p;
1153 }
1154#endif
1155 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001156 }
1157 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001158 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001159 bf++;
1160 }
1161 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001162 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001163 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001164 if (!Py_VerboseFlag)
1165 return;
1166 fprintf(stderr, "# cleanup ints");
1167 if (!isum) {
1168 fprintf(stderr, "\n");
1169 }
1170 else {
1171 fprintf(stderr,
1172 ": %d unfreed int%s in %d out of %d block%s\n",
1173 isum, isum == 1 ? "" : "s",
1174 bc - bf, bc, bc == 1 ? "" : "s");
1175 }
1176 if (Py_VerboseFlag > 1) {
1177 list = block_list;
1178 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001179 for (i = 0, p = &list->objects[0];
1180 i < N_INTOBJECTS;
1181 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001182 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001183 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001184 "# <int at %p, refcnt=%d, val=%ld>\n",
1185 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001186 }
1187 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001188 }
1189 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001190}