blob: 611aedf9f46c3a38ce8e3824ec53c0ee0236e1ce [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 {
172 PyErr_SetString(PyExc_TypeError,
173 "nb_int should return int object");
174 return -1;
175 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000176 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000177
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 val = PyInt_AS_LONG(io);
179 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000180
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000181 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000185PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186{
187 char *end;
188 long x;
189 char buffer[256]; /* For errors */
Guido van Rossum47710652003-02-12 20:48:22 +0000190 int warn = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191
192 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000193 PyErr_SetString(PyExc_ValueError,
194 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000195 return NULL;
196 }
197
198 while (*s && isspace(Py_CHARMASK(*s)))
199 s++;
200 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000201 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000202 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000203 if (x < 0)
204 warn = 1;
205 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000206 else
207 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000208 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000209 goto bad;
210 while (*end && isspace(Py_CHARMASK(*end)))
211 end++;
212 if (*end != '\0') {
213 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000214 PyOS_snprintf(buffer, sizeof(buffer),
215 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000216 PyErr_SetString(PyExc_ValueError, buffer);
217 return NULL;
218 }
219 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000220 if (err_ovf("string/unicode conversion"))
221 return NULL;
222 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000223 }
Guido van Rossum47710652003-02-12 20:48:22 +0000224 if (warn) {
225 if (PyErr_Warn(PyExc_FutureWarning,
226 "int('0...', 0): sign will change in Python 2.4") < 0)
227 return NULL;
228 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000229 if (pend)
230 *pend = end;
231 return PyInt_FromLong(x);
232}
233
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000234#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000235PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000236PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000237{
Walter Dörwald07e14762002-11-06 16:15:14 +0000238 PyObject *result;
239 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000240
Walter Dörwald07e14762002-11-06 16:15:14 +0000241 if (buffer == NULL)
242 return NULL;
243
244 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
245 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000246 return NULL;
247 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000248 result = PyInt_FromString(buffer, NULL, base);
249 PyMem_FREE(buffer);
250 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000251}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000252#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000253
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254/* Methods */
255
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000256/* Integers are seen as the "smallest" of all numeric types and thus
257 don't have any knowledge about conversion of other types to
258 integers. */
259
260#define CONVERT_TO_LONG(obj, lng) \
261 if (PyInt_Check(obj)) { \
262 lng = PyInt_AS_LONG(obj); \
263 } \
264 else { \
265 Py_INCREF(Py_NotImplemented); \
266 return Py_NotImplemented; \
267 }
268
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000269/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000270static int
Fred Drakea2f55112000-07-09 15:16:51 +0000271int_print(PyIntObject *v, FILE *fp, int flags)
272 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273{
274 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000275 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000279int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280{
Tim Peters42221042001-12-01 02:52:56 +0000281 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000282 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284}
285
286static int
Fred Drakea2f55112000-07-09 15:16:51 +0000287int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288{
289 register long i = v->ob_ival;
290 register long j = w->ob_ival;
291 return (i < j) ? -1 : (i > j) ? 1 : 0;
292}
293
Guido van Rossum9bfef441993-03-29 10:43:31 +0000294static long
Fred Drakea2f55112000-07-09 15:16:51 +0000295int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000297 /* XXX If this is changed, you also need to change the way
298 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000299 long x = v -> ob_ival;
300 if (x == -1)
301 x = -2;
302 return x;
303}
304
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000306int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
308 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000309 CONVERT_TO_LONG(v, a);
310 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000312 if ((x^a) >= 0 || (x^b) >= 0)
313 return PyInt_FromLong(x);
314 if (err_ovf("integer addition"))
315 return NULL;
316 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317}
318
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000320int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321{
322 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000323 CONVERT_TO_LONG(v, a);
324 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000326 if ((x^a) >= 0 || (x^~b) >= 0)
327 return PyInt_FromLong(x);
328 if (err_ovf("integer subtraction"))
329 return NULL;
330 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
331 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
333
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000334/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000335Integer overflow checking for * is painful: Python tried a couple ways, but
336they didn't work on all platforms, or failed in endcases (a product of
337-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000338
Tim Petersa3c01ce2001-12-04 23:05:10 +0000339Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000340
Tim Petersa3c01ce2001-12-04 23:05:10 +0000341The native long product x*y is either exactly right or *way* off, being
342just the last n bits of the true product, where n is the number of bits
343in a long (the delivered product is the true product plus i*2**n for
344some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000345
Tim Petersa3c01ce2001-12-04 23:05:10 +0000346The native double product (double)x * (double)y is subject to three
347rounding errors: on a sizeof(long)==8 box, each cast to double can lose
348info, and even on a sizeof(long)==4 box, the multiplication can lose info.
349But, unlike the native long product, it's not in *range* trouble: even
350if sizeof(long)==32 (256-bit longs), the product easily fits in the
351dynamic range of a double. So the leading 50 (or so) bits of the double
352product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000353
Tim Petersa3c01ce2001-12-04 23:05:10 +0000354We check these two ways against each other, and declare victory if they're
355approximately the same. Else, because the native long product is the only
356one that can lose catastrophic amounts of information, it's the native long
357product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000358*/
359
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000360static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000361int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000363 long a, b;
364 long longprod; /* a*b in native long arithmetic */
365 double doubled_longprod; /* (double)longprod */
366 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000367
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000368 CONVERT_TO_LONG(v, a);
369 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000370 longprod = a * b;
371 doubleprod = (double)a * (double)b;
372 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000373
Tim Petersa3c01ce2001-12-04 23:05:10 +0000374 /* Fast path for normal case: small multiplicands, and no info
375 is lost in either method. */
376 if (doubled_longprod == doubleprod)
377 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000378
Tim Petersa3c01ce2001-12-04 23:05:10 +0000379 /* Somebody somewhere lost info. Close enough, or way off? Note
380 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
381 The difference either is or isn't significant compared to the
382 true value (of which doubleprod is a good approximation).
383 */
384 {
385 const double diff = doubled_longprod - doubleprod;
386 const double absdiff = diff >= 0.0 ? diff : -diff;
387 const double absprod = doubleprod >= 0.0 ? doubleprod :
388 -doubleprod;
389 /* absdiff/absprod <= 1/32 iff
390 32 * absdiff <= absprod -- 5 good bits is "close enough" */
391 if (32.0 * absdiff <= absprod)
392 return PyInt_FromLong(longprod);
393 else if (err_ovf("integer multiplication"))
394 return NULL;
395 else
396 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000397 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Guido van Rossume27f7952001-08-23 02:59:04 +0000400/* Return type of i_divmod */
401enum divmod_result {
402 DIVMOD_OK, /* Correct result */
403 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
404 DIVMOD_ERROR /* Exception raised */
405};
406
407static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000408i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000409 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000411 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000412
Tim Peters1dad6a82001-06-18 19:21:11 +0000413 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000415 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000416 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000418 /* (-sys.maxint-1)/-1 is the only overflow case. */
419 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000420 if (err_ovf("integer division"))
421 return DIVMOD_ERROR;
422 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000423 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000424 xdivy = x / y;
425 xmody = x - xdivy * y;
426 /* If the signs of x and y differ, and the remainder is non-0,
427 * C89 doesn't define whether xdivy is now the floor or the
428 * ceiling of the infinitely precise quotient. We want the floor,
429 * and we have it iff the remainder's sign matches y's.
430 */
431 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
432 xmody += y;
433 --xdivy;
434 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000435 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000436 *p_xdivy = xdivy;
437 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000438 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000439}
440
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000442int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000443{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000444 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000445 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000446 CONVERT_TO_LONG(x, xi);
447 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000448 switch (i_divmod(xi, yi, &d, &m)) {
449 case DIVMOD_OK:
450 return PyInt_FromLong(d);
451 case DIVMOD_OVERFLOW:
452 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
453 (PyObject *)y);
454 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000455 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000456 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000457}
458
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000460int_classic_div(PyIntObject *x, PyIntObject *y)
461{
462 long xi, yi;
463 long d, m;
464 CONVERT_TO_LONG(x, xi);
465 CONVERT_TO_LONG(y, yi);
466 if (Py_DivisionWarningFlag &&
467 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
468 return NULL;
469 switch (i_divmod(xi, yi, &d, &m)) {
470 case DIVMOD_OK:
471 return PyInt_FromLong(d);
472 case DIVMOD_OVERFLOW:
473 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
474 (PyObject *)y);
475 default:
476 return NULL;
477 }
478}
479
480static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000481int_true_divide(PyObject *v, PyObject *w)
482{
Tim Peterse2a60002001-09-04 06:17:36 +0000483 /* If they aren't both ints, give someone else a chance. In
484 particular, this lets int/long get handled by longs, which
485 underflows to 0 gracefully if the long is too big to convert
486 to float. */
487 if (PyInt_Check(v) && PyInt_Check(w))
488 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
489 Py_INCREF(Py_NotImplemented);
490 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000491}
492
493static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000494int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000495{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000496 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000497 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000498 CONVERT_TO_LONG(x, xi);
499 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000500 switch (i_divmod(xi, yi, &d, &m)) {
501 case DIVMOD_OK:
502 return PyInt_FromLong(m);
503 case DIVMOD_OVERFLOW:
504 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
505 (PyObject *)y);
506 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000507 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000508 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000509}
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000512int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000513{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000514 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000515 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000516 CONVERT_TO_LONG(x, xi);
517 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000518 switch (i_divmod(xi, yi, &d, &m)) {
519 case DIVMOD_OK:
520 return Py_BuildValue("(ll)", d, m);
521 case DIVMOD_OVERFLOW:
522 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
523 (PyObject *)y);
524 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000525 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000526 }
Guido van Rossum00466951991-05-05 20:08:27 +0000527}
528
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000530int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000532 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000533 CONVERT_TO_LONG(v, iv);
534 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000535 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000536 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000537 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
538 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000539 return NULL;
540 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000541 /* Return a float. This works because we know that
542 this calls float_pow() which converts its
543 arguments to double. */
544 return PyFloat_Type.tp_as_number->nb_power(
545 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000546 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000548 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000549 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000551 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000552 return NULL;
553 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000554 }
555 /*
556 * XXX: The original exponentiation code stopped looping
557 * when temp hit zero; this code will continue onwards
558 * unnecessarily, but at least it won't cause any errors.
559 * Hopefully the speed improvement from the fast exponentiation
560 * will compensate for the slight inefficiency.
561 * XXX: Better handling of overflows is desperately needed.
562 */
563 temp = iv;
564 ix = 1;
565 while (iw > 0) {
566 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000567 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000568 ix = ix*temp;
569 if (temp == 0)
570 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000571 if (ix / temp != prev) {
572 if (err_ovf("integer exponentiation"))
573 return NULL;
574 return PyLong_Type.tp_as_number->nb_power(
575 (PyObject *)v,
576 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000577 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000578 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000579 }
580 iw >>= 1; /* Shift exponent down by 1 bit */
581 if (iw==0) break;
582 prev = temp;
583 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000584 if (prev!=0 && temp/prev!=prev) {
585 if (err_ovf("integer exponentiation"))
586 return NULL;
587 return PyLong_Type.tp_as_number->nb_power(
588 (PyObject *)v, (PyObject *)w, (PyObject *)z);
589 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000590 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591 /* If we did a multiplication, perform a modulo */
592 ix = ix % iz;
593 temp = temp % iz;
594 }
595 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000596 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000597 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000598 switch (i_divmod(ix, iz, &div, &mod)) {
599 case DIVMOD_OK:
600 ix = mod;
601 break;
602 case DIVMOD_OVERFLOW:
603 return PyLong_Type.tp_as_number->nb_power(
604 (PyObject *)v, (PyObject *)w, (PyObject *)z);
605 default:
606 return NULL;
607 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000608 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000610}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000613int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614{
615 register long a, x;
616 a = v->ob_ival;
617 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000618 if (a < 0 && x < 0) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000619 PyObject *o;
Guido van Rossume27f7952001-08-23 02:59:04 +0000620 if (err_ovf("integer negation"))
621 return NULL;
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000622 o = PyLong_FromLong(a);
623 if (o != NULL) {
624 PyObject *result = PyNumber_Negative(o);
625 Py_DECREF(o);
626 return result;
627 }
628 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000629 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000634int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000636 if (PyInt_CheckExact(v)) {
637 Py_INCREF(v);
638 return (PyObject *)v;
639 }
640 else
641 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000645int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000646{
647 if (v->ob_ival >= 0)
648 return int_pos(v);
649 else
650 return int_neg(v);
651}
652
Guido van Rossum0bff0151991-05-14 12:05:32 +0000653static int
Fred Drakea2f55112000-07-09 15:16:51 +0000654int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000655{
656 return v->ob_ival != 0;
657}
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000660int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000663}
664
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000666int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667{
Guido van Rossum078151d2002-08-11 04:24:12 +0000668 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000669 CONVERT_TO_LONG(v, a);
670 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000671 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 return NULL;
674 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000675 if (a == 0 || b == 0)
676 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000677 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000678 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000679 "x<<y losing bits or changing sign "
680 "will return a long in Python 2.4 and up") < 0)
681 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000683 }
Tim Petersda1a2212002-08-11 17:54:42 +0000684 c = a << b;
685 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000686 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000687 "x<<y losing bits or changing sign "
688 "will return a long in Python 2.4 and up") < 0)
689 return NULL;
690 }
691 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000692}
693
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000695int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000696{
697 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000698 CONVERT_TO_LONG(v, a);
699 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000700 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000702 return NULL;
703 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000704 if (a == 0 || b == 0)
705 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000706 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000707 if (a < 0)
708 a = -1;
709 else
710 a = 0;
711 }
712 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000713 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000714 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716}
717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000719int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000720{
721 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000722 CONVERT_TO_LONG(v, a);
723 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000725}
726
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000728int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000729{
730 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000731 CONVERT_TO_LONG(v, a);
732 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000734}
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000737int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000738{
739 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000740 CONVERT_TO_LONG(v, a);
741 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000743}
744
Guido van Rossum1952e382001-09-19 01:25:16 +0000745static int
746int_coerce(PyObject **pv, PyObject **pw)
747{
748 if (PyInt_Check(*pw)) {
749 Py_INCREF(*pv);
750 Py_INCREF(*pw);
751 return 0;
752 }
753 return 1; /* Can't do it */
754}
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000757int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000758{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759 Py_INCREF(v);
760 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000761}
762
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000764int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000765{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767}
768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000770int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000771{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000773}
774
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000776int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000777{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000778 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000779 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000780 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000781 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000782 "hex()/oct() of negative int will return "
783 "a signed string in Python 2.4 and up") < 0)
784 return NULL;
785 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000786 if (x == 0)
787 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000788 else
Barry Warsaw61975092001-11-28 20:55:34 +0000789 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000791}
792
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000794int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000795{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000796 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000797 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000798 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000799 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000800 "hex()/oct() of negative int will return "
801 "a signed string in Python 2.4 and up") < 0)
802 return NULL;
803 }
Barry Warsaw61975092001-11-28 20:55:34 +0000804 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000806}
807
Jeremy Hylton938ace62002-07-17 16:30:39 +0000808static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000809int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
810
Tim Peters6d6c1a32001-08-02 04:15:00 +0000811static PyObject *
812int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
813{
814 PyObject *x = NULL;
815 int base = -909;
816 static char *kwlist[] = {"x", "base", 0};
817
Guido van Rossumbef14172001-08-29 15:47:46 +0000818 if (type != &PyInt_Type)
819 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
821 &x, &base))
822 return NULL;
823 if (x == NULL)
824 return PyInt_FromLong(0L);
825 if (base == -909)
826 return PyNumber_Int(x);
827 if (PyString_Check(x))
828 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000829#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830 if (PyUnicode_Check(x))
831 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
832 PyUnicode_GET_SIZE(x),
833 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000834#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000835 PyErr_SetString(PyExc_TypeError,
836 "int() can't convert non-string with explicit base");
837 return NULL;
838}
839
Guido van Rossumbef14172001-08-29 15:47:46 +0000840/* Wimpy, slow approach to tp_new calls for subtypes of int:
841 first create a regular int from whatever arguments we got,
842 then allocate a subtype instance and initialize its ob_ival
843 from the regular int. The regular int is then thrown away.
844*/
845static PyObject *
846int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
847{
848 PyObject *tmp, *new;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000849 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000850
851 assert(PyType_IsSubtype(type, &PyInt_Type));
852 tmp = int_new(&PyInt_Type, args, kwds);
853 if (tmp == NULL)
854 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000855 if (!PyInt_Check(tmp)) {
856 if (!PyLong_Check(tmp)) {
857 PyErr_SetString(PyExc_ValueError,
858 "value must convertable to an int");
859 return NULL;
860 }
861 ival = PyLong_AsLong(tmp);
862 if (ival == -1 && PyErr_Occurred())
863 return NULL;
864
865 } else {
866 ival = ((PyIntObject *)tmp)->ob_ival;
867 }
868
Guido van Rossumd93dce12001-08-30 03:09:31 +0000869 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000870 if (new == NULL)
871 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000872 ((PyIntObject *)new)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000873 Py_DECREF(tmp);
874 return new;
875}
876
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000877static PyObject *
878int_getnewargs(PyIntObject *v)
879{
880 return Py_BuildValue("(l)", v->ob_ival);
881}
882
883static PyMethodDef int_methods[] = {
884 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
885 {NULL, NULL} /* sentinel */
886};
887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889"int(x[, base]) -> integer\n\
890\n\
891Convert a string or number to an integer, if possible. A floating point\n\
892argument will be truncated towards zero (this does not include a string\n\
893representation of a floating point number!) When converting a string, use\n\
894the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000895non-string. If the argument is outside the integer range a long object\n\
896will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000899 (binaryfunc)int_add, /*nb_add*/
900 (binaryfunc)int_sub, /*nb_subtract*/
901 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000902 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000903 (binaryfunc)int_mod, /*nb_remainder*/
904 (binaryfunc)int_divmod, /*nb_divmod*/
905 (ternaryfunc)int_pow, /*nb_power*/
906 (unaryfunc)int_neg, /*nb_negative*/
907 (unaryfunc)int_pos, /*nb_positive*/
908 (unaryfunc)int_abs, /*nb_absolute*/
909 (inquiry)int_nonzero, /*nb_nonzero*/
910 (unaryfunc)int_invert, /*nb_invert*/
911 (binaryfunc)int_lshift, /*nb_lshift*/
912 (binaryfunc)int_rshift, /*nb_rshift*/
913 (binaryfunc)int_and, /*nb_and*/
914 (binaryfunc)int_xor, /*nb_xor*/
915 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000916 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000917 (unaryfunc)int_int, /*nb_int*/
918 (unaryfunc)int_long, /*nb_long*/
919 (unaryfunc)int_float, /*nb_float*/
920 (unaryfunc)int_oct, /*nb_oct*/
921 (unaryfunc)int_hex, /*nb_hex*/
922 0, /*nb_inplace_add*/
923 0, /*nb_inplace_subtract*/
924 0, /*nb_inplace_multiply*/
925 0, /*nb_inplace_divide*/
926 0, /*nb_inplace_remainder*/
927 0, /*nb_inplace_power*/
928 0, /*nb_inplace_lshift*/
929 0, /*nb_inplace_rshift*/
930 0, /*nb_inplace_and*/
931 0, /*nb_inplace_xor*/
932 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000933 (binaryfunc)int_div, /* nb_floor_divide */
934 int_true_divide, /* nb_true_divide */
935 0, /* nb_inplace_floor_divide */
936 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000937};
938
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939PyTypeObject PyInt_Type = {
940 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000941 0,
942 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945 (destructor)int_dealloc, /* tp_dealloc */
946 (printfunc)int_print, /* tp_print */
947 0, /* tp_getattr */
948 0, /* tp_setattr */
949 (cmpfunc)int_compare, /* tp_compare */
950 (reprfunc)int_repr, /* tp_repr */
951 &int_as_number, /* tp_as_number */
952 0, /* tp_as_sequence */
953 0, /* tp_as_mapping */
954 (hashfunc)int_hash, /* tp_hash */
955 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000956 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957 PyObject_GenericGetAttr, /* tp_getattro */
958 0, /* tp_setattro */
959 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000960 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
961 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000962 int_doc, /* tp_doc */
963 0, /* tp_traverse */
964 0, /* tp_clear */
965 0, /* tp_richcompare */
966 0, /* tp_weaklistoffset */
967 0, /* tp_iter */
968 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000969 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970 0, /* tp_members */
971 0, /* tp_getset */
972 0, /* tp_base */
973 0, /* tp_dict */
974 0, /* tp_descr_get */
975 0, /* tp_descr_set */
976 0, /* tp_dictoffset */
977 0, /* tp_init */
978 0, /* tp_alloc */
979 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000980 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000982
Neal Norwitzc91ed402002-12-30 22:29:22 +0000983int
Neal Norwitzb2501f42002-12-31 03:42:13 +0000984_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +0000985{
986 PyIntObject *v;
987 int ival;
988#if NSMALLNEGINTS + NSMALLPOSINTS > 0
989 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
990 if ((free_list = fill_free_list()) == NULL)
991 return 0;
992 /* PyObject_New is inlined */
993 v = free_list;
994 free_list = (PyIntObject *)v->ob_type;
995 PyObject_INIT(v, &PyInt_Type);
996 v->ob_ival = ival;
997 small_ints[ival + NSMALLNEGINTS] = v;
998 }
999#endif
1000 return 1;
1001}
1002
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001003void
Fred Drakea2f55112000-07-09 15:16:51 +00001004PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001005{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001006 PyIntObject *p;
1007 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001008 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001009 int bc, bf; /* block count, number of freed blocks */
1010 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001011
Guido van Rossumda084ed1999-03-10 22:55:24 +00001012#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1013 PyIntObject **q;
1014
1015 i = NSMALLNEGINTS + NSMALLPOSINTS;
1016 q = small_ints;
1017 while (--i >= 0) {
1018 Py_XDECREF(*q);
1019 *q++ = NULL;
1020 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001021#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001022 bc = 0;
1023 bf = 0;
1024 isum = 0;
1025 list = block_list;
1026 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001027 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001028 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001029 bc++;
1030 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001031 for (i = 0, p = &list->objects[0];
1032 i < N_INTOBJECTS;
1033 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001034 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001035 irem++;
1036 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001037 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001038 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001039 list->next = block_list;
1040 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001041 for (i = 0, p = &list->objects[0];
1042 i < N_INTOBJECTS;
1043 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001044 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001045 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001046 p->ob_type = (struct _typeobject *)
1047 free_list;
1048 free_list = p;
1049 }
1050#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1051 else if (-NSMALLNEGINTS <= p->ob_ival &&
1052 p->ob_ival < NSMALLPOSINTS &&
1053 small_ints[p->ob_ival +
1054 NSMALLNEGINTS] == NULL) {
1055 Py_INCREF(p);
1056 small_ints[p->ob_ival +
1057 NSMALLNEGINTS] = p;
1058 }
1059#endif
1060 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001061 }
1062 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001063 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001064 bf++;
1065 }
1066 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001067 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001068 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001069 if (!Py_VerboseFlag)
1070 return;
1071 fprintf(stderr, "# cleanup ints");
1072 if (!isum) {
1073 fprintf(stderr, "\n");
1074 }
1075 else {
1076 fprintf(stderr,
1077 ": %d unfreed int%s in %d out of %d block%s\n",
1078 isum, isum == 1 ? "" : "s",
1079 bc - bf, bc, bc == 1 ? "" : "s");
1080 }
1081 if (Py_VerboseFlag > 1) {
1082 list = block_list;
1083 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001084 for (i = 0, p = &list->objects[0];
1085 i < N_INTOBJECTS;
1086 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001087 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001088 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001089 "# <int at %p, refcnt=%d, val=%ld>\n",
1090 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001091 }
1092 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001093 }
1094 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001095}