Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Integer object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 5 | #include <ctype.h> |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 6 | #include <float.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 8 | static PyObject *int_int(PyIntObject *v); |
| 9 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 10 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 11 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 12 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 13 | return LONG_MAX; /* To initialize sys.maxint */ |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 14 | } |
| 15 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | /* Integers are quite normal objects, to make object handling uniform. |
| 17 | (Using odd pointers to represent integers would save much space |
| 18 | but require extra checks for this special case throughout the code.) |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 19 | Since a typical Python program spends much of its time allocating |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | and deallocating integers, these operations should be very fast. |
| 21 | Therefore we use a dedicated allocation scheme with a much lower |
| 22 | overhead (in space and time) than straight malloc(): a simple |
| 23 | dedicated free list, filled when necessary with memory from malloc(). |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 24 | |
| 25 | block_list is a singly-linked list of all PyIntBlocks ever allocated, |
| 26 | linked via their next members. PyIntBlocks are never returned to the |
| 27 | system before shutdown (PyInt_Fini). |
| 28 | |
| 29 | free_list is a singly-linked list of available PyIntObjects, linked |
| 30 | via abuse of their ob_type members. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 33 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
| 34 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 35 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 37 | struct _intblock { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | struct _intblock *next; |
| 39 | PyIntObject objects[N_INTOBJECTS]; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | typedef struct _intblock PyIntBlock; |
| 43 | |
| 44 | static PyIntBlock *block_list = NULL; |
| 45 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 47 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 48 | fill_free_list(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 50 | PyIntObject *p, *q; |
| 51 | /* Python's object allocator isn't appropriate for large blocks. */ |
| 52 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
| 53 | if (p == NULL) |
| 54 | return (PyIntObject *) PyErr_NoMemory(); |
| 55 | ((PyIntBlock *)p)->next = block_list; |
| 56 | block_list = (PyIntBlock *)p; |
| 57 | /* Link the int objects together, from rear to front, then return |
| 58 | the address of the last int object in the block. */ |
| 59 | p = &((PyIntBlock *)p)->objects[0]; |
| 60 | q = p + N_INTOBJECTS; |
| 61 | while (--q > p) |
| 62 | Py_TYPE(q) = (struct _typeobject *)(q-1); |
| 63 | Py_TYPE(q) = NULL; |
| 64 | return p + N_INTOBJECTS - 1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 67 | #ifndef NSMALLPOSINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 68 | #define NSMALLPOSINTS 257 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #ifndef NSMALLNEGINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 71 | #define NSMALLNEGINTS 5 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 72 | #endif |
| 73 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 74 | /* References to small integers are saved in this array so that they |
| 75 | can be shared. |
| 76 | The integers that are saved are those in the range |
| 77 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 78 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 80 | #endif |
| 81 | #ifdef COUNT_ALLOCS |
Martin v. Löwis | b90304a | 2009-01-07 18:40:40 +0000 | [diff] [blame] | 82 | Py_ssize_t quick_int_allocs; |
| 83 | Py_ssize_t quick_neg_int_allocs; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 87 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 89 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 90 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 91 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 92 | v = small_ints[ival + NSMALLNEGINTS]; |
| 93 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 94 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 95 | if (ival >= 0) |
| 96 | quick_int_allocs++; |
| 97 | else |
| 98 | quick_neg_int_allocs++; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 99 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 100 | return (PyObject *) v; |
| 101 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 102 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 103 | if (free_list == NULL) { |
| 104 | if ((free_list = fill_free_list()) == NULL) |
| 105 | return NULL; |
| 106 | } |
| 107 | /* Inline PyObject_New */ |
| 108 | v = free_list; |
| 109 | free_list = (PyIntObject *)Py_TYPE(v); |
| 110 | PyObject_INIT(v, &PyInt_Type); |
| 111 | v->ob_ival = ival; |
| 112 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | PyInt_FromSize_t(size_t ival) |
| 117 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 118 | if (ival <= LONG_MAX) |
| 119 | return PyInt_FromLong((long)ival); |
| 120 | return _PyLong_FromSize_t(ival); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | PyObject * |
| 124 | PyInt_FromSsize_t(Py_ssize_t ival) |
| 125 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 126 | if (ival >= LONG_MIN && ival <= LONG_MAX) |
| 127 | return PyInt_FromLong((long)ival); |
| 128 | return _PyLong_FromSsize_t(ival); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 132 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 133 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 134 | if (PyInt_CheckExact(v)) { |
| 135 | Py_TYPE(v) = (struct _typeobject *)free_list; |
| 136 | free_list = v; |
| 137 | } |
| 138 | else |
| 139 | Py_TYPE(v)->tp_free((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 142 | static void |
| 143 | int_free(PyIntObject *v) |
| 144 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 145 | Py_TYPE(v) = (struct _typeobject *)free_list; |
| 146 | free_list = v; |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 150 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 152 | PyNumberMethods *nb; |
| 153 | PyIntObject *io; |
| 154 | long val; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 156 | if (op && PyInt_Check(op)) |
| 157 | return PyInt_AS_LONG((PyIntObject*) op); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 159 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
| 160 | nb->nb_int == NULL) { |
| 161 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 162 | return -1; |
| 163 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 165 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 166 | if (io == NULL) |
| 167 | return -1; |
| 168 | if (!PyInt_Check(io)) { |
| 169 | if (PyLong_Check(io)) { |
| 170 | /* got a long? => retry int conversion */ |
| 171 | val = PyLong_AsLong((PyObject *)io); |
| 172 | Py_DECREF(io); |
| 173 | if ((val == -1) && PyErr_Occurred()) |
| 174 | return -1; |
| 175 | return val; |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | Py_DECREF(io); |
| 180 | PyErr_SetString(PyExc_TypeError, |
| 181 | "__int__ method should return an integer"); |
| 182 | return -1; |
| 183 | } |
| 184 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 186 | val = PyInt_AS_LONG(io); |
| 187 | Py_DECREF(io); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 189 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 192 | Py_ssize_t |
| 193 | PyInt_AsSsize_t(register PyObject *op) |
| 194 | { |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 195 | #if SIZEOF_SIZE_T != SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 196 | PyNumberMethods *nb; |
| 197 | PyIntObject *io; |
| 198 | Py_ssize_t val; |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 199 | #endif |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 201 | if (op == NULL) { |
| 202 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 203 | return -1; |
| 204 | } |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 206 | if (PyInt_Check(op)) |
| 207 | return PyInt_AS_LONG((PyIntObject*) op); |
| 208 | if (PyLong_Check(op)) |
| 209 | return _PyLong_AsSsize_t(op); |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 210 | #if SIZEOF_SIZE_T == SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 211 | return PyInt_AsLong(op); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 212 | #else |
| 213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 214 | if ((nb = Py_TYPE(op)->tp_as_number) == NULL || |
| 215 | (nb->nb_int == NULL && nb->nb_long == 0)) { |
| 216 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 217 | return -1; |
| 218 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 220 | if (nb->nb_long != 0) |
| 221 | io = (PyIntObject*) (*nb->nb_long) (op); |
| 222 | else |
| 223 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 224 | if (io == NULL) |
| 225 | return -1; |
| 226 | if (!PyInt_Check(io)) { |
| 227 | if (PyLong_Check(io)) { |
| 228 | /* got a long? => retry int conversion */ |
| 229 | val = _PyLong_AsSsize_t((PyObject *)io); |
| 230 | Py_DECREF(io); |
| 231 | if ((val == -1) && PyErr_Occurred()) |
| 232 | return -1; |
| 233 | return val; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | Py_DECREF(io); |
| 238 | PyErr_SetString(PyExc_TypeError, |
| 239 | "__int__ method should return an integer"); |
| 240 | return -1; |
| 241 | } |
| 242 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 244 | val = PyInt_AS_LONG(io); |
| 245 | Py_DECREF(io); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 247 | return val; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 248 | #endif |
| 249 | } |
| 250 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 251 | unsigned long |
| 252 | PyInt_AsUnsignedLongMask(register PyObject *op) |
| 253 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 254 | PyNumberMethods *nb; |
| 255 | PyIntObject *io; |
| 256 | unsigned long val; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 258 | if (op && PyInt_Check(op)) |
| 259 | return PyInt_AS_LONG((PyIntObject*) op); |
| 260 | if (op && PyLong_Check(op)) |
| 261 | return PyLong_AsUnsignedLongMask(op); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
| 264 | nb->nb_int == NULL) { |
| 265 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 266 | return (unsigned long)-1; |
| 267 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 269 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 270 | if (io == NULL) |
| 271 | return (unsigned long)-1; |
| 272 | if (!PyInt_Check(io)) { |
| 273 | if (PyLong_Check(io)) { |
| 274 | val = PyLong_AsUnsignedLongMask((PyObject *)io); |
| 275 | Py_DECREF(io); |
| 276 | if (PyErr_Occurred()) |
| 277 | return (unsigned long)-1; |
| 278 | return val; |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | Py_DECREF(io); |
| 283 | PyErr_SetString(PyExc_TypeError, |
| 284 | "__int__ method should return an integer"); |
| 285 | return (unsigned long)-1; |
| 286 | } |
| 287 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 289 | val = PyInt_AS_LONG(io); |
| 290 | Py_DECREF(io); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 292 | return val; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | #ifdef HAVE_LONG_LONG |
| 296 | unsigned PY_LONG_LONG |
| 297 | PyInt_AsUnsignedLongLongMask(register PyObject *op) |
| 298 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 299 | PyNumberMethods *nb; |
| 300 | PyIntObject *io; |
| 301 | unsigned PY_LONG_LONG val; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 303 | if (op && PyInt_Check(op)) |
| 304 | return PyInt_AS_LONG((PyIntObject*) op); |
| 305 | if (op && PyLong_Check(op)) |
| 306 | return PyLong_AsUnsignedLongLongMask(op); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 308 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
| 309 | nb->nb_int == NULL) { |
| 310 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 311 | return (unsigned PY_LONG_LONG)-1; |
| 312 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 314 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 315 | if (io == NULL) |
| 316 | return (unsigned PY_LONG_LONG)-1; |
| 317 | if (!PyInt_Check(io)) { |
| 318 | if (PyLong_Check(io)) { |
| 319 | val = PyLong_AsUnsignedLongLongMask((PyObject *)io); |
| 320 | Py_DECREF(io); |
| 321 | if (PyErr_Occurred()) |
| 322 | return (unsigned PY_LONG_LONG)-1; |
| 323 | return val; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | Py_DECREF(io); |
| 328 | PyErr_SetString(PyExc_TypeError, |
| 329 | "__int__ method should return an integer"); |
| 330 | return (unsigned PY_LONG_LONG)-1; |
| 331 | } |
| 332 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | val = PyInt_AS_LONG(io); |
| 335 | Py_DECREF(io); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 337 | return val; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 338 | } |
| 339 | #endif |
| 340 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 341 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 342 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 343 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 344 | char *end; |
| 345 | long x; |
| 346 | Py_ssize_t slen; |
| 347 | PyObject *sobj, *srepr; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | if ((base != 0 && base < 2) || base > 36) { |
| 350 | PyErr_SetString(PyExc_ValueError, |
| 351 | "int() base must be >= 2 and <= 36"); |
| 352 | return NULL; |
| 353 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 354 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 355 | while (*s && isspace(Py_CHARMASK(*s))) |
| 356 | s++; |
| 357 | errno = 0; |
| 358 | if (base == 0 && s[0] == '0') { |
| 359 | x = (long) PyOS_strtoul(s, &end, base); |
| 360 | if (x < 0) |
| 361 | return PyLong_FromString(s, pend, base); |
| 362 | } |
| 363 | else |
| 364 | x = PyOS_strtol(s, &end, base); |
| 365 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
| 366 | goto bad; |
| 367 | while (*end && isspace(Py_CHARMASK(*end))) |
| 368 | end++; |
| 369 | if (*end != '\0') { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 370 | bad: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 371 | slen = strlen(s) < 200 ? strlen(s) : 200; |
| 372 | sobj = PyString_FromStringAndSize(s, slen); |
| 373 | if (sobj == NULL) |
| 374 | return NULL; |
| 375 | srepr = PyObject_Repr(sobj); |
| 376 | Py_DECREF(sobj); |
| 377 | if (srepr == NULL) |
| 378 | return NULL; |
| 379 | PyErr_Format(PyExc_ValueError, |
| 380 | "invalid literal for int() with base %d: %s", |
| 381 | base, PyString_AS_STRING(srepr)); |
| 382 | Py_DECREF(srepr); |
| 383 | return NULL; |
| 384 | } |
| 385 | else if (errno != 0) |
| 386 | return PyLong_FromString(s, pend, base); |
| 387 | if (pend) |
| 388 | *pend = end; |
| 389 | return PyInt_FromLong(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 392 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 393 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 394 | PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 395 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 396 | PyObject *result; |
| 397 | char *buffer = (char *)PyMem_MALLOC(length+1); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 399 | if (buffer == NULL) |
| 400 | return PyErr_NoMemory(); |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 402 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) { |
| 403 | PyMem_FREE(buffer); |
| 404 | return NULL; |
| 405 | } |
| 406 | result = PyInt_FromString(buffer, NULL, base); |
| 407 | PyMem_FREE(buffer); |
| 408 | return result; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 409 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 410 | #endif |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 412 | /* Methods */ |
| 413 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 414 | /* Integers are seen as the "smallest" of all numeric types and thus |
| 415 | don't have any knowledge about conversion of other types to |
| 416 | integers. */ |
| 417 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 418 | #define CONVERT_TO_LONG(obj, lng) \ |
| 419 | if (PyInt_Check(obj)) { \ |
| 420 | lng = PyInt_AS_LONG(obj); \ |
| 421 | } \ |
| 422 | else { \ |
| 423 | Py_INCREF(Py_NotImplemented); \ |
| 424 | return Py_NotImplemented; \ |
| 425 | } |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 427 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 428 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 429 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 430 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 431 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 432 | long int_val = v->ob_ival; |
| 433 | Py_BEGIN_ALLOW_THREADS |
| 434 | fprintf(fp, "%ld", int_val); |
| 435 | Py_END_ALLOW_THREADS |
| 436 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 440 | int_compare(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 442 | register long i = v->ob_ival; |
| 443 | register long j = w->ob_ival; |
| 444 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 447 | static long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 448 | int_hash(PyIntObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 449 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 450 | /* XXX If this is changed, you also need to change the way |
| 451 | Python's long, float and complex types are hashed. */ |
| 452 | long x = v -> ob_ival; |
| 453 | if (x == -1) |
| 454 | x = -2; |
| 455 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 458 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 459 | int_add(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 461 | register long a, b, x; |
| 462 | CONVERT_TO_LONG(v, a); |
| 463 | CONVERT_TO_LONG(w, b); |
| 464 | /* casts in the line below avoid undefined behaviour on overflow */ |
| 465 | x = (long)((unsigned long)a + b); |
| 466 | if ((x^a) >= 0 || (x^b) >= 0) |
| 467 | return PyInt_FromLong(x); |
| 468 | return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 471 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 472 | int_sub(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 473 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 474 | register long a, b, x; |
| 475 | CONVERT_TO_LONG(v, a); |
| 476 | CONVERT_TO_LONG(w, b); |
| 477 | /* casts in the line below avoid undefined behaviour on overflow */ |
| 478 | x = (long)((unsigned long)a - b); |
| 479 | if ((x^a) >= 0 || (x^~b) >= 0) |
| 480 | return PyInt_FromLong(x); |
| 481 | return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v, |
| 482 | (PyObject *)w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 485 | /* |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 486 | Integer overflow checking for * is painful: Python tried a couple ways, but |
| 487 | they didn't work on all platforms, or failed in endcases (a product of |
| 488 | -sys.maxint-1 has been a particular pain). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 489 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 490 | Here's another way: |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 491 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 492 | The native long product x*y is either exactly right or *way* off, being |
| 493 | just the last n bits of the true product, where n is the number of bits |
| 494 | in a long (the delivered product is the true product plus i*2**n for |
| 495 | some integer i). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 496 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 497 | The native double product (double)x * (double)y is subject to three |
| 498 | rounding errors: on a sizeof(long)==8 box, each cast to double can lose |
| 499 | info, and even on a sizeof(long)==4 box, the multiplication can lose info. |
| 500 | But, unlike the native long product, it's not in *range* trouble: even |
| 501 | if sizeof(long)==32 (256-bit longs), the product easily fits in the |
| 502 | dynamic range of a double. So the leading 50 (or so) bits of the double |
| 503 | product are correct. |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 504 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 505 | We check these two ways against each other, and declare victory if they're |
| 506 | approximately the same. Else, because the native long product is the only |
| 507 | one that can lose catastrophic amounts of information, it's the native long |
| 508 | product that must have overflowed. |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 509 | */ |
| 510 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 511 | static PyObject * |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 512 | int_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 513 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 514 | long a, b; |
| 515 | long longprod; /* a*b in native long arithmetic */ |
| 516 | double doubled_longprod; /* (double)longprod */ |
| 517 | double doubleprod; /* (double)a * (double)b */ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 519 | CONVERT_TO_LONG(v, a); |
| 520 | CONVERT_TO_LONG(w, b); |
| 521 | /* casts in the next line avoid undefined behaviour on overflow */ |
| 522 | longprod = (long)((unsigned long)a * b); |
| 523 | doubleprod = (double)a * (double)b; |
| 524 | doubled_longprod = (double)longprod; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 525 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 526 | /* Fast path for normal case: small multiplicands, and no info |
| 527 | is lost in either method. */ |
| 528 | if (doubled_longprod == doubleprod) |
| 529 | return PyInt_FromLong(longprod); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 531 | /* Somebody somewhere lost info. Close enough, or way off? Note |
| 532 | that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0). |
| 533 | The difference either is or isn't significant compared to the |
| 534 | true value (of which doubleprod is a good approximation). |
| 535 | */ |
| 536 | { |
| 537 | const double diff = doubled_longprod - doubleprod; |
| 538 | const double absdiff = diff >= 0.0 ? diff : -diff; |
| 539 | const double absprod = doubleprod >= 0.0 ? doubleprod : |
| 540 | -doubleprod; |
| 541 | /* absdiff/absprod <= 1/32 iff |
| 542 | 32 * absdiff <= absprod -- 5 good bits is "close enough" */ |
| 543 | if (32.0 * absdiff <= absprod) |
| 544 | return PyInt_FromLong(longprod); |
| 545 | else |
| 546 | return PyLong_Type.tp_as_number->nb_multiply(v, w); |
| 547 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 550 | /* Integer overflow checking for unary negation: on a 2's-complement |
| 551 | * box, -x overflows iff x is the most negative long. In this case we |
| 552 | * get -x == x. However, -x is undefined (by C) if x /is/ the most |
| 553 | * negative long (it's a signed overflow case), and some compilers care. |
| 554 | * So we cast x to unsigned long first. However, then other compilers |
| 555 | * warn about applying unary minus to an unsigned operand. Hence the |
| 556 | * weird "0-". |
| 557 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | #define UNARY_NEG_WOULD_OVERFLOW(x) \ |
| 559 | ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x)) |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 561 | /* Return type of i_divmod */ |
| 562 | enum divmod_result { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 563 | DIVMOD_OK, /* Correct result */ |
| 564 | DIVMOD_OVERFLOW, /* Overflow, try again using longs */ |
| 565 | DIVMOD_ERROR /* Exception raised */ |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | static enum divmod_result |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 569 | i_divmod(register long x, register long y, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 570 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 572 | long xdivy, xmody; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 574 | if (y == 0) { |
| 575 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 576 | "integer division or modulo by zero"); |
| 577 | return DIVMOD_ERROR; |
| 578 | } |
| 579 | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
| 580 | if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x)) |
| 581 | return DIVMOD_OVERFLOW; |
| 582 | xdivy = x / y; |
| 583 | /* xdiv*y can overflow on platforms where x/y gives floor(x/y) |
| 584 | * for x and y with differing signs. (This is unusual |
| 585 | * behaviour, and C99 prohibits it, but it's allowed by C89; |
| 586 | * for an example of overflow, take x = LONG_MIN, y = 5 or x = |
| 587 | * LONG_MAX, y = -5.) However, x - xdivy*y is always |
| 588 | * representable as a long, since it lies strictly between |
| 589 | * -abs(y) and abs(y). We add casts to avoid intermediate |
| 590 | * overflow. |
| 591 | */ |
| 592 | xmody = (long)(x - (unsigned long)xdivy * y); |
| 593 | /* If the signs of x and y differ, and the remainder is non-0, |
| 594 | * C89 doesn't define whether xdivy is now the floor or the |
| 595 | * ceiling of the infinitely precise quotient. We want the floor, |
| 596 | * and we have it iff the remainder's sign matches y's. |
| 597 | */ |
| 598 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
| 599 | xmody += y; |
| 600 | --xdivy; |
| 601 | assert(xmody && ((y ^ xmody) >= 0)); |
| 602 | } |
| 603 | *p_xdivy = xdivy; |
| 604 | *p_xmody = xmody; |
| 605 | return DIVMOD_OK; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 609 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 610 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 611 | long xi, yi; |
| 612 | long d, m; |
| 613 | CONVERT_TO_LONG(x, xi); |
| 614 | CONVERT_TO_LONG(y, yi); |
| 615 | switch (i_divmod(xi, yi, &d, &m)) { |
| 616 | case DIVMOD_OK: |
| 617 | return PyInt_FromLong(d); |
| 618 | case DIVMOD_OVERFLOW: |
| 619 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 620 | (PyObject *)y); |
| 621 | default: |
| 622 | return NULL; |
| 623 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 626 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 627 | int_classic_div(PyIntObject *x, PyIntObject *y) |
| 628 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 629 | long xi, yi; |
| 630 | long d, m; |
| 631 | CONVERT_TO_LONG(x, xi); |
| 632 | CONVERT_TO_LONG(y, yi); |
| 633 | if (Py_DivisionWarningFlag && |
| 634 | PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) |
| 635 | return NULL; |
| 636 | switch (i_divmod(xi, yi, &d, &m)) { |
| 637 | case DIVMOD_OK: |
| 638 | return PyInt_FromLong(d); |
| 639 | case DIVMOD_OVERFLOW: |
| 640 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 641 | (PyObject *)y); |
| 642 | default: |
| 643 | return NULL; |
| 644 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static PyObject * |
Mark Dickinson | 4657283 | 2009-12-27 14:55:57 +0000 | [diff] [blame] | 648 | int_true_divide(PyIntObject *x, PyIntObject *y) |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 649 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 650 | long xi, yi; |
| 651 | /* If they aren't both ints, give someone else a chance. In |
| 652 | particular, this lets int/long get handled by longs, which |
| 653 | underflows to 0 gracefully if the long is too big to convert |
| 654 | to float. */ |
| 655 | CONVERT_TO_LONG(x, xi); |
| 656 | CONVERT_TO_LONG(y, yi); |
| 657 | if (yi == 0) { |
| 658 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 659 | "division by zero"); |
| 660 | return NULL; |
| 661 | } |
| 662 | if (xi == 0) |
| 663 | return PyFloat_FromDouble(yi < 0 ? -0.0 : 0.0); |
Mark Dickinson | 4657283 | 2009-12-27 14:55:57 +0000 | [diff] [blame] | 664 | |
| 665 | #define WIDTH_OF_ULONG (CHAR_BIT*SIZEOF_LONG) |
| 666 | #if DBL_MANT_DIG < WIDTH_OF_ULONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 667 | if ((xi >= 0 ? 0UL + xi : 0UL - xi) >> DBL_MANT_DIG || |
| 668 | (yi >= 0 ? 0UL + yi : 0UL - yi) >> DBL_MANT_DIG) |
| 669 | /* Large x or y. Use long integer arithmetic. */ |
| 670 | return PyLong_Type.tp_as_number->nb_true_divide( |
| 671 | (PyObject *)x, (PyObject *)y); |
| 672 | else |
Mark Dickinson | 4657283 | 2009-12-27 14:55:57 +0000 | [diff] [blame] | 673 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 674 | /* Both ints can be exactly represented as doubles. Do a |
| 675 | floating-point division. */ |
| 676 | return PyFloat_FromDouble((double)xi / (double)yi); |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 680 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 681 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | long xi, yi; |
| 683 | long d, m; |
| 684 | CONVERT_TO_LONG(x, xi); |
| 685 | CONVERT_TO_LONG(y, yi); |
| 686 | switch (i_divmod(xi, yi, &d, &m)) { |
| 687 | case DIVMOD_OK: |
| 688 | return PyInt_FromLong(m); |
| 689 | case DIVMOD_OVERFLOW: |
| 690 | return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x, |
| 691 | (PyObject *)y); |
| 692 | default: |
| 693 | return NULL; |
| 694 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 697 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 698 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 699 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | long xi, yi; |
| 701 | long d, m; |
| 702 | CONVERT_TO_LONG(x, xi); |
| 703 | CONVERT_TO_LONG(y, yi); |
| 704 | switch (i_divmod(xi, yi, &d, &m)) { |
| 705 | case DIVMOD_OK: |
| 706 | return Py_BuildValue("(ll)", d, m); |
| 707 | case DIVMOD_OVERFLOW: |
| 708 | return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x, |
| 709 | (PyObject *)y); |
| 710 | default: |
| 711 | return NULL; |
| 712 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 715 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 716 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 717 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 718 | register long iv, iw, iz=0, ix, temp, prev; |
| 719 | CONVERT_TO_LONG(v, iv); |
| 720 | CONVERT_TO_LONG(w, iw); |
| 721 | if (iw < 0) { |
| 722 | if ((PyObject *)z != Py_None) { |
| 723 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
| 724 | "cannot be negative when 3rd argument specified"); |
| 725 | return NULL; |
| 726 | } |
| 727 | /* Return a float. This works because we know that |
| 728 | this calls float_pow() which converts its |
| 729 | arguments to double. */ |
| 730 | return PyFloat_Type.tp_as_number->nb_power( |
| 731 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 732 | } |
| 733 | if ((PyObject *)z != Py_None) { |
| 734 | CONVERT_TO_LONG(z, iz); |
| 735 | if (iz == 0) { |
| 736 | PyErr_SetString(PyExc_ValueError, |
| 737 | "pow() 3rd argument cannot be 0"); |
| 738 | return NULL; |
| 739 | } |
| 740 | } |
| 741 | /* |
| 742 | * XXX: The original exponentiation code stopped looping |
| 743 | * when temp hit zero; this code will continue onwards |
| 744 | * unnecessarily, but at least it won't cause any errors. |
| 745 | * Hopefully the speed improvement from the fast exponentiation |
| 746 | * will compensate for the slight inefficiency. |
| 747 | * XXX: Better handling of overflows is desperately needed. |
| 748 | */ |
| 749 | temp = iv; |
| 750 | ix = 1; |
| 751 | while (iw > 0) { |
| 752 | prev = ix; /* Save value for overflow check */ |
| 753 | if (iw & 1) { |
Mark Dickinson | dbbed04 | 2011-09-19 16:38:08 +0100 | [diff] [blame^] | 754 | /* |
| 755 | * The (unsigned long) cast below ensures that the multiplication |
| 756 | * is interpreted as an unsigned operation rather than a signed one |
| 757 | * (C99 6.3.1.8p1), thus avoiding the perils of undefined behaviour |
| 758 | * from signed arithmetic overflow (C99 6.5p5). See issue #12973. |
| 759 | */ |
| 760 | ix = (unsigned long)ix * temp; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 761 | if (temp == 0) |
| 762 | break; /* Avoid ix / 0 */ |
| 763 | if (ix / temp != prev) { |
| 764 | return PyLong_Type.tp_as_number->nb_power( |
| 765 | (PyObject *)v, |
| 766 | (PyObject *)w, |
| 767 | (PyObject *)z); |
| 768 | } |
| 769 | } |
| 770 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 771 | if (iw==0) break; |
| 772 | prev = temp; |
Mark Dickinson | dbbed04 | 2011-09-19 16:38:08 +0100 | [diff] [blame^] | 773 | temp = (unsigned long)temp * temp; /* Square the value of temp */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 774 | if (prev != 0 && temp / prev != prev) { |
| 775 | return PyLong_Type.tp_as_number->nb_power( |
| 776 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 777 | } |
| 778 | if (iz) { |
| 779 | /* If we did a multiplication, perform a modulo */ |
| 780 | ix = ix % iz; |
| 781 | temp = temp % iz; |
| 782 | } |
| 783 | } |
| 784 | if (iz) { |
| 785 | long div, mod; |
| 786 | switch (i_divmod(ix, iz, &div, &mod)) { |
| 787 | case DIVMOD_OK: |
| 788 | ix = mod; |
| 789 | break; |
| 790 | case DIVMOD_OVERFLOW: |
| 791 | return PyLong_Type.tp_as_number->nb_power( |
| 792 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 793 | default: |
| 794 | return NULL; |
| 795 | } |
| 796 | } |
| 797 | return PyInt_FromLong(ix); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 798 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 799 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 800 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 801 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 803 | register long a; |
| 804 | a = v->ob_ival; |
| 805 | /* check for overflow */ |
| 806 | if (UNARY_NEG_WOULD_OVERFLOW(a)) { |
| 807 | PyObject *o = PyLong_FromLong(a); |
| 808 | if (o != NULL) { |
| 809 | PyObject *result = PyNumber_Negative(o); |
| 810 | Py_DECREF(o); |
| 811 | return result; |
| 812 | } |
| 813 | return NULL; |
| 814 | } |
| 815 | return PyInt_FromLong(-a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 818 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 819 | int_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 820 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 821 | if (v->ob_ival >= 0) |
| 822 | return int_int(v); |
| 823 | else |
| 824 | return int_neg(v); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 827 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 828 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 829 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 830 | return v->ob_ival != 0; |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 833 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 834 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 835 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 836 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 839 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 840 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 841 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | long a, b, c; |
| 843 | PyObject *vv, *ww, *result; |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 845 | CONVERT_TO_LONG(v, a); |
| 846 | CONVERT_TO_LONG(w, b); |
| 847 | if (b < 0) { |
| 848 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 849 | return NULL; |
| 850 | } |
| 851 | if (a == 0 || b == 0) |
| 852 | return int_int(v); |
| 853 | if (b >= LONG_BIT) { |
| 854 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 855 | if (vv == NULL) |
| 856 | return NULL; |
| 857 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 858 | if (ww == NULL) { |
| 859 | Py_DECREF(vv); |
| 860 | return NULL; |
| 861 | } |
| 862 | result = PyNumber_Lshift(vv, ww); |
| 863 | Py_DECREF(vv); |
| 864 | Py_DECREF(ww); |
| 865 | return result; |
| 866 | } |
| 867 | c = a << b; |
| 868 | if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { |
| 869 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 870 | if (vv == NULL) |
| 871 | return NULL; |
| 872 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 873 | if (ww == NULL) { |
| 874 | Py_DECREF(vv); |
| 875 | return NULL; |
| 876 | } |
| 877 | result = PyNumber_Lshift(vv, ww); |
| 878 | Py_DECREF(vv); |
| 879 | Py_DECREF(ww); |
| 880 | return result; |
| 881 | } |
| 882 | return PyInt_FromLong(c); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 886 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 887 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 888 | register long a, b; |
| 889 | CONVERT_TO_LONG(v, a); |
| 890 | CONVERT_TO_LONG(w, b); |
| 891 | if (b < 0) { |
| 892 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 893 | return NULL; |
| 894 | } |
| 895 | if (a == 0 || b == 0) |
| 896 | return int_int(v); |
| 897 | if (b >= LONG_BIT) { |
| 898 | if (a < 0) |
| 899 | a = -1; |
| 900 | else |
| 901 | a = 0; |
| 902 | } |
| 903 | else { |
| 904 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
| 905 | } |
| 906 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 909 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 910 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 911 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 912 | register long a, b; |
| 913 | CONVERT_TO_LONG(v, a); |
| 914 | CONVERT_TO_LONG(w, b); |
| 915 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 918 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 919 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 921 | register long a, b; |
| 922 | CONVERT_TO_LONG(v, a); |
| 923 | CONVERT_TO_LONG(w, b); |
| 924 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 927 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 928 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 929 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 930 | register long a, b; |
| 931 | CONVERT_TO_LONG(v, a); |
| 932 | CONVERT_TO_LONG(w, b); |
| 933 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 936 | static int |
| 937 | int_coerce(PyObject **pv, PyObject **pw) |
| 938 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 939 | if (PyInt_Check(*pw)) { |
| 940 | Py_INCREF(*pv); |
| 941 | Py_INCREF(*pw); |
| 942 | return 0; |
| 943 | } |
| 944 | return 1; /* Can't do it */ |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 948 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 949 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 950 | if (PyInt_CheckExact(v)) |
| 951 | Py_INCREF(v); |
| 952 | else |
| 953 | v = (PyIntObject *)PyInt_FromLong(v->ob_ival); |
| 954 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 957 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 958 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 959 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 960 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 963 | static const unsigned char BitLengthTable[32] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 964 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 965 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 966 | }; |
| 967 | |
| 968 | static int |
| 969 | bits_in_ulong(unsigned long d) |
| 970 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 971 | int d_bits = 0; |
| 972 | while (d >= 32) { |
| 973 | d_bits += 6; |
| 974 | d >>= 6; |
| 975 | } |
| 976 | d_bits += (int)BitLengthTable[d]; |
| 977 | return d_bits; |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | #if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG |
| 981 | /* Every Python int can be exactly represented as a float. */ |
| 982 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 984 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 985 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 986 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 989 | #else |
| 990 | /* Here not all Python ints are exactly representable as floats, so we may |
| 991 | have to round. We do this manually, since the C standards don't specify |
| 992 | whether converting an integer to a float rounds up or down */ |
| 993 | |
| 994 | static PyObject * |
| 995 | int_float(PyIntObject *v) |
| 996 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 997 | unsigned long abs_ival, lsb; |
| 998 | int round_up; |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1000 | if (v->ob_ival < 0) |
| 1001 | abs_ival = 0U-(unsigned long)v->ob_ival; |
| 1002 | else |
| 1003 | abs_ival = (unsigned long)v->ob_ival; |
| 1004 | if (abs_ival < (1L << DBL_MANT_DIG)) |
| 1005 | /* small integer; no need to round */ |
| 1006 | return PyFloat_FromDouble((double)v->ob_ival); |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | /* Round abs_ival to MANT_DIG significant bits, using the |
| 1009 | round-half-to-even rule. abs_ival & lsb picks out the 'rounding' |
| 1010 | bit: the first bit after the most significant MANT_DIG bits of |
| 1011 | abs_ival. We round up if this bit is set, provided that either: |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1013 | (1) abs_ival isn't exactly halfway between two floats, in which |
| 1014 | case at least one of the bits following the rounding bit must be |
| 1015 | set; i.e., abs_ival & lsb-1 != 0, or: |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1017 | (2) the resulting rounded value has least significant bit 0; or |
| 1018 | in other words the bit above the rounding bit is set (this is the |
| 1019 | 'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0 |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1021 | The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */ |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1023 | lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1); |
| 1024 | round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1)); |
| 1025 | abs_ival &= -2*lsb; |
| 1026 | if (round_up) |
| 1027 | abs_ival += 2*lsb; |
| 1028 | return PyFloat_FromDouble(v->ob_ival < 0 ? |
| 1029 | -(double)abs_ival : |
| 1030 | (double)abs_ival); |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | #endif |
| 1034 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1035 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1036 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1037 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1038 | return _PyInt_Format(v, 8, 0); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1041 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1042 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1043 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1044 | return _PyInt_Format(v, 16, 0); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1047 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1048 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1049 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1050 | static PyObject * |
| 1051 | int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1052 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1053 | PyObject *x = NULL; |
| 1054 | int base = -909; |
| 1055 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1057 | if (type != &PyInt_Type) |
| 1058 | return int_subtype_new(type, args, kwds); /* Wimp out */ |
| 1059 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
| 1060 | &x, &base)) |
| 1061 | return NULL; |
| 1062 | if (x == NULL) |
| 1063 | return PyInt_FromLong(0L); |
| 1064 | if (base == -909) |
| 1065 | return PyNumber_Int(x); |
| 1066 | if (PyString_Check(x)) { |
| 1067 | /* Since PyInt_FromString doesn't have a length parameter, |
| 1068 | * check here for possible NULs in the string. */ |
| 1069 | char *string = PyString_AS_STRING(x); |
| 1070 | if (strlen(string) != PyString_Size(x)) { |
| 1071 | /* create a repr() of the input string, |
| 1072 | * just like PyInt_FromString does */ |
| 1073 | PyObject *srepr; |
| 1074 | srepr = PyObject_Repr(x); |
| 1075 | if (srepr == NULL) |
| 1076 | return NULL; |
| 1077 | PyErr_Format(PyExc_ValueError, |
| 1078 | "invalid literal for int() with base %d: %s", |
| 1079 | base, PyString_AS_STRING(srepr)); |
| 1080 | Py_DECREF(srepr); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | return PyInt_FromString(string, NULL, base); |
| 1084 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1085 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1086 | if (PyUnicode_Check(x)) |
| 1087 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 1088 | PyUnicode_GET_SIZE(x), |
| 1089 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1090 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1091 | PyErr_SetString(PyExc_TypeError, |
| 1092 | "int() can't convert non-string with explicit base"); |
| 1093 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1096 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 1097 | first create a regular int from whatever arguments we got, |
| 1098 | then allocate a subtype instance and initialize its ob_ival |
| 1099 | from the regular int. The regular int is then thrown away. |
| 1100 | */ |
| 1101 | static PyObject * |
| 1102 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1103 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1104 | PyObject *tmp, *newobj; |
| 1105 | long ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1107 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
| 1108 | tmp = int_new(&PyInt_Type, args, kwds); |
| 1109 | if (tmp == NULL) |
| 1110 | return NULL; |
| 1111 | if (!PyInt_Check(tmp)) { |
| 1112 | ival = PyLong_AsLong(tmp); |
| 1113 | if (ival == -1 && PyErr_Occurred()) { |
| 1114 | Py_DECREF(tmp); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | } else { |
| 1118 | ival = ((PyIntObject *)tmp)->ob_ival; |
| 1119 | } |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1121 | newobj = type->tp_alloc(type, 0); |
| 1122 | if (newobj == NULL) { |
| 1123 | Py_DECREF(tmp); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | ((PyIntObject *)newobj)->ob_ival = ival; |
| 1127 | Py_DECREF(tmp); |
| 1128 | return newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1131 | static PyObject * |
| 1132 | int_getnewargs(PyIntObject *v) |
| 1133 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1134 | return Py_BuildValue("(l)", v->ob_ival); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1137 | static PyObject * |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1138 | int_get0(PyIntObject *v, void *context) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1139 | return PyInt_FromLong(0L); |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static PyObject * |
| 1143 | int_get1(PyIntObject *v, void *context) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1144 | return PyInt_FromLong(1L); |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame] | 1147 | /* Convert an integer to a decimal string. On many platforms, this |
| 1148 | will be significantly faster than the general arbitrary-base |
| 1149 | conversion machinery in _PyInt_Format, thanks to optimization |
| 1150 | opportunities offered by division by a compile-time constant. */ |
| 1151 | static PyObject * |
| 1152 | int_to_decimal_string(PyIntObject *v) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1153 | char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend; |
| 1154 | long n = v->ob_ival; |
| 1155 | unsigned long absn; |
| 1156 | p = bufend = buf + sizeof(buf); |
| 1157 | absn = n < 0 ? 0UL - n : n; |
| 1158 | do { |
| 1159 | *--p = '0' + (char)(absn % 10); |
| 1160 | absn /= 10; |
| 1161 | } while (absn); |
| 1162 | if (n < 0) |
| 1163 | *--p = '-'; |
| 1164 | return PyString_FromStringAndSize(p, bufend - p); |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1167 | /* Convert an integer to the given base. Returns a string. |
| 1168 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. |
| 1169 | If newstyle is zero, then use the pre-2.6 behavior of octal having |
| 1170 | a leading "0" */ |
| 1171 | PyAPI_FUNC(PyObject*) |
| 1172 | _PyInt_Format(PyIntObject *v, int base, int newstyle) |
| 1173 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1174 | /* There are no doubt many, many ways to optimize this, using code |
| 1175 | similar to _PyLong_Format */ |
| 1176 | long n = v->ob_ival; |
| 1177 | int negative = n < 0; |
| 1178 | int is_zero = n == 0; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1179 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1180 | /* For the reasoning behind this size, see |
| 1181 | http://c-faq.com/misc/hexio.html. Then, add a few bytes for |
| 1182 | the possible sign and prefix "0[box]" */ |
| 1183 | char buf[sizeof(n)*CHAR_BIT+6]; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1185 | /* Start by pointing to the end of the buffer. We fill in from |
| 1186 | the back forward. */ |
| 1187 | char* p = &buf[sizeof(buf)]; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1189 | assert(base >= 2 && base <= 36); |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1190 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1191 | /* Special case base 10, for speed */ |
| 1192 | if (base == 10) |
| 1193 | return int_to_decimal_string(v); |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame] | 1194 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1195 | do { |
| 1196 | /* I'd use i_divmod, except it doesn't produce the results |
| 1197 | I want when n is negative. So just duplicate the salient |
| 1198 | part here. */ |
| 1199 | long div = n / base; |
| 1200 | long mod = n - div * base; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1202 | /* convert abs(mod) to the right character in [0-9, a-z] */ |
| 1203 | char cdigit = (char)(mod < 0 ? -mod : mod); |
| 1204 | cdigit += (cdigit < 10) ? '0' : 'a'-10; |
| 1205 | *--p = cdigit; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1207 | n = div; |
| 1208 | } while(n); |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1210 | if (base == 2) { |
| 1211 | *--p = 'b'; |
| 1212 | *--p = '0'; |
| 1213 | } |
| 1214 | else if (base == 8) { |
| 1215 | if (newstyle) { |
| 1216 | *--p = 'o'; |
| 1217 | *--p = '0'; |
| 1218 | } |
| 1219 | else |
| 1220 | if (!is_zero) |
| 1221 | *--p = '0'; |
| 1222 | } |
| 1223 | else if (base == 16) { |
| 1224 | *--p = 'x'; |
| 1225 | *--p = '0'; |
| 1226 | } |
| 1227 | else { |
| 1228 | *--p = '#'; |
| 1229 | *--p = '0' + base%10; |
| 1230 | if (base > 10) |
| 1231 | *--p = '0' + base/10; |
| 1232 | } |
| 1233 | if (negative) |
| 1234 | *--p = '-'; |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1236 | return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p); |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1239 | static PyObject * |
| 1240 | int__format__(PyObject *self, PyObject *args) |
| 1241 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | PyObject *format_spec; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1244 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 1245 | return NULL; |
| 1246 | if (PyBytes_Check(format_spec)) |
| 1247 | return _PyInt_FormatAdvanced(self, |
| 1248 | PyBytes_AS_STRING(format_spec), |
| 1249 | PyBytes_GET_SIZE(format_spec)); |
| 1250 | if (PyUnicode_Check(format_spec)) { |
| 1251 | /* Convert format_spec to a str */ |
| 1252 | PyObject *result; |
| 1253 | PyObject *str_spec = PyObject_Str(format_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1254 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1255 | if (str_spec == NULL) |
| 1256 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1258 | result = _PyInt_FormatAdvanced(self, |
| 1259 | PyBytes_AS_STRING(str_spec), |
| 1260 | PyBytes_GET_SIZE(str_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1262 | Py_DECREF(str_spec); |
| 1263 | return result; |
| 1264 | } |
| 1265 | PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
| 1266 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1269 | static PyObject * |
| 1270 | int_bit_length(PyIntObject *v) |
| 1271 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1272 | unsigned long n; |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1274 | if (v->ob_ival < 0) |
| 1275 | /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */ |
| 1276 | n = 0U-(unsigned long)v->ob_ival; |
| 1277 | else |
| 1278 | n = (unsigned long)v->ob_ival; |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1280 | return PyInt_FromLong(bits_in_ulong(n)); |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | PyDoc_STRVAR(int_bit_length_doc, |
| 1284 | "int.bit_length() -> int\n\ |
| 1285 | \n\ |
| 1286 | Number of bits necessary to represent self in binary.\n\ |
| 1287 | >>> bin(37)\n\ |
| 1288 | '0b100101'\n\ |
| 1289 | >>> (37).bit_length()\n\ |
| 1290 | 6"); |
| 1291 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1292 | #if 0 |
| 1293 | static PyObject * |
| 1294 | int_is_finite(PyObject *v) |
| 1295 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1296 | Py_RETURN_TRUE; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1297 | } |
| 1298 | #endif |
| 1299 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1300 | static PyMethodDef int_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1301 | {"conjugate", (PyCFunction)int_int, METH_NOARGS, |
| 1302 | "Returns self, the complex conjugate of any int."}, |
| 1303 | {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, |
| 1304 | int_bit_length_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1305 | #if 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1306 | {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS, |
| 1307 | "Returns always True."}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1308 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1309 | {"__trunc__", (PyCFunction)int_int, METH_NOARGS, |
| 1310 | "Truncating an Integral returns itself."}, |
| 1311 | {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS}, |
| 1312 | {"__format__", (PyCFunction)int__format__, METH_VARARGS}, |
| 1313 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1314 | }; |
| 1315 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1316 | static PyGetSetDef int_getset[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1317 | {"real", |
| 1318 | (getter)int_int, (setter)NULL, |
| 1319 | "the real part of a complex number", |
| 1320 | NULL}, |
| 1321 | {"imag", |
| 1322 | (getter)int_get0, (setter)NULL, |
| 1323 | "the imaginary part of a complex number", |
| 1324 | NULL}, |
| 1325 | {"numerator", |
| 1326 | (getter)int_int, (setter)NULL, |
| 1327 | "the numerator of a rational number in lowest terms", |
| 1328 | NULL}, |
| 1329 | {"denominator", |
| 1330 | (getter)int_get1, (setter)NULL, |
| 1331 | "the denominator of a rational number in lowest terms", |
| 1332 | NULL}, |
| 1333 | {NULL} /* Sentinel */ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1334 | }; |
| 1335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1336 | PyDoc_STRVAR(int_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1337 | "int(x[, base]) -> integer\n\ |
| 1338 | \n\ |
| 1339 | Convert a string or number to an integer, if possible. A floating point\n\ |
| 1340 | argument will be truncated towards zero (this does not include a string\n\ |
| 1341 | representation of a floating point number!) When converting a string, use\n\ |
| 1342 | the optional base. It is an error to supply a base when converting a\n\ |
Gustavo Niemeyer | 37e6502 | 2007-01-10 16:15:48 +0000 | [diff] [blame] | 1343 | non-string. If base is zero, the proper base is guessed based on the\n\ |
Gustavo Niemeyer | a443bc8 | 2007-01-10 16:13:40 +0000 | [diff] [blame] | 1344 | string content. If the argument is outside the integer range a\n\ |
| 1345 | long object will be returned instead."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1346 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1347 | static PyNumberMethods int_as_number = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1348 | (binaryfunc)int_add, /*nb_add*/ |
| 1349 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 1350 | (binaryfunc)int_mul, /*nb_multiply*/ |
| 1351 | (binaryfunc)int_classic_div, /*nb_divide*/ |
| 1352 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 1353 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 1354 | (ternaryfunc)int_pow, /*nb_power*/ |
| 1355 | (unaryfunc)int_neg, /*nb_negative*/ |
| 1356 | (unaryfunc)int_int, /*nb_positive*/ |
| 1357 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 1358 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 1359 | (unaryfunc)int_invert, /*nb_invert*/ |
| 1360 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 1361 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 1362 | (binaryfunc)int_and, /*nb_and*/ |
| 1363 | (binaryfunc)int_xor, /*nb_xor*/ |
| 1364 | (binaryfunc)int_or, /*nb_or*/ |
| 1365 | int_coerce, /*nb_coerce*/ |
| 1366 | (unaryfunc)int_int, /*nb_int*/ |
| 1367 | (unaryfunc)int_long, /*nb_long*/ |
| 1368 | (unaryfunc)int_float, /*nb_float*/ |
| 1369 | (unaryfunc)int_oct, /*nb_oct*/ |
| 1370 | (unaryfunc)int_hex, /*nb_hex*/ |
| 1371 | 0, /*nb_inplace_add*/ |
| 1372 | 0, /*nb_inplace_subtract*/ |
| 1373 | 0, /*nb_inplace_multiply*/ |
| 1374 | 0, /*nb_inplace_divide*/ |
| 1375 | 0, /*nb_inplace_remainder*/ |
| 1376 | 0, /*nb_inplace_power*/ |
| 1377 | 0, /*nb_inplace_lshift*/ |
| 1378 | 0, /*nb_inplace_rshift*/ |
| 1379 | 0, /*nb_inplace_and*/ |
| 1380 | 0, /*nb_inplace_xor*/ |
| 1381 | 0, /*nb_inplace_or*/ |
| 1382 | (binaryfunc)int_div, /* nb_floor_divide */ |
| 1383 | (binaryfunc)int_true_divide, /* nb_true_divide */ |
| 1384 | 0, /* nb_inplace_floor_divide */ |
| 1385 | 0, /* nb_inplace_true_divide */ |
| 1386 | (unaryfunc)int_int, /* nb_index */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1387 | }; |
| 1388 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1389 | PyTypeObject PyInt_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1390 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1391 | "int", |
| 1392 | sizeof(PyIntObject), |
| 1393 | 0, |
| 1394 | (destructor)int_dealloc, /* tp_dealloc */ |
| 1395 | (printfunc)int_print, /* tp_print */ |
| 1396 | 0, /* tp_getattr */ |
| 1397 | 0, /* tp_setattr */ |
| 1398 | (cmpfunc)int_compare, /* tp_compare */ |
| 1399 | (reprfunc)int_to_decimal_string, /* tp_repr */ |
| 1400 | &int_as_number, /* tp_as_number */ |
| 1401 | 0, /* tp_as_sequence */ |
| 1402 | 0, /* tp_as_mapping */ |
| 1403 | (hashfunc)int_hash, /* tp_hash */ |
| 1404 | 0, /* tp_call */ |
| 1405 | (reprfunc)int_to_decimal_string, /* tp_str */ |
| 1406 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1407 | 0, /* tp_setattro */ |
| 1408 | 0, /* tp_as_buffer */ |
| 1409 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 1410 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */ |
| 1411 | int_doc, /* tp_doc */ |
| 1412 | 0, /* tp_traverse */ |
| 1413 | 0, /* tp_clear */ |
| 1414 | 0, /* tp_richcompare */ |
| 1415 | 0, /* tp_weaklistoffset */ |
| 1416 | 0, /* tp_iter */ |
| 1417 | 0, /* tp_iternext */ |
| 1418 | int_methods, /* tp_methods */ |
| 1419 | 0, /* tp_members */ |
| 1420 | int_getset, /* tp_getset */ |
| 1421 | 0, /* tp_base */ |
| 1422 | 0, /* tp_dict */ |
| 1423 | 0, /* tp_descr_get */ |
| 1424 | 0, /* tp_descr_set */ |
| 1425 | 0, /* tp_dictoffset */ |
| 1426 | 0, /* tp_init */ |
| 1427 | 0, /* tp_alloc */ |
| 1428 | int_new, /* tp_new */ |
| 1429 | (freefunc)int_free, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1430 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1431 | |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1432 | int |
Neal Norwitz | b2501f4 | 2002-12-31 03:42:13 +0000 | [diff] [blame] | 1433 | _PyInt_Init(void) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1434 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1435 | PyIntObject *v; |
| 1436 | int ival; |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1437 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1438 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) { |
| 1439 | if (!free_list && (free_list = fill_free_list()) == NULL) |
| 1440 | return 0; |
| 1441 | /* PyObject_New is inlined */ |
| 1442 | v = free_list; |
| 1443 | free_list = (PyIntObject *)Py_TYPE(v); |
| 1444 | PyObject_INIT(v, &PyInt_Type); |
| 1445 | v->ob_ival = ival; |
| 1446 | small_ints[ival + NSMALLNEGINTS] = v; |
| 1447 | } |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1448 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1449 | return 1; |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1452 | int |
| 1453 | PyInt_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1454 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1455 | PyIntObject *p; |
| 1456 | PyIntBlock *list, *next; |
| 1457 | int i; |
| 1458 | int u; /* remaining unfreed ints per block */ |
| 1459 | int freelist_size = 0; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1461 | list = block_list; |
| 1462 | block_list = NULL; |
| 1463 | free_list = NULL; |
| 1464 | while (list != NULL) { |
| 1465 | u = 0; |
| 1466 | for (i = 0, p = &list->objects[0]; |
| 1467 | i < N_INTOBJECTS; |
| 1468 | i++, p++) { |
| 1469 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
| 1470 | u++; |
| 1471 | } |
| 1472 | next = list->next; |
| 1473 | if (u) { |
| 1474 | list->next = block_list; |
| 1475 | block_list = list; |
| 1476 | for (i = 0, p = &list->objects[0]; |
| 1477 | i < N_INTOBJECTS; |
| 1478 | i++, p++) { |
| 1479 | if (!PyInt_CheckExact(p) || |
| 1480 | p->ob_refcnt == 0) { |
| 1481 | Py_TYPE(p) = (struct _typeobject *) |
| 1482 | free_list; |
| 1483 | free_list = p; |
| 1484 | } |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1485 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1486 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 1487 | p->ob_ival < NSMALLPOSINTS && |
| 1488 | small_ints[p->ob_ival + |
| 1489 | NSMALLNEGINTS] == NULL) { |
| 1490 | Py_INCREF(p); |
| 1491 | small_ints[p->ob_ival + |
| 1492 | NSMALLNEGINTS] = p; |
| 1493 | } |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1494 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | else { |
| 1498 | PyMem_FREE(list); |
| 1499 | } |
| 1500 | freelist_size += u; |
| 1501 | list = next; |
| 1502 | } |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1504 | return freelist_size; |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void |
| 1508 | PyInt_Fini(void) |
| 1509 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1510 | PyIntObject *p; |
| 1511 | PyIntBlock *list; |
| 1512 | int i; |
| 1513 | int u; /* total unfreed ints per block */ |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1514 | |
| 1515 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1516 | PyIntObject **q; |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1517 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1518 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 1519 | q = small_ints; |
| 1520 | while (--i >= 0) { |
| 1521 | Py_XDECREF(*q); |
| 1522 | *q++ = NULL; |
| 1523 | } |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1524 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1525 | u = PyInt_ClearFreeList(); |
| 1526 | if (!Py_VerboseFlag) |
| 1527 | return; |
| 1528 | fprintf(stderr, "# cleanup ints"); |
| 1529 | if (!u) { |
| 1530 | fprintf(stderr, "\n"); |
| 1531 | } |
| 1532 | else { |
| 1533 | fprintf(stderr, |
| 1534 | ": %d unfreed int%s\n", |
| 1535 | u, u == 1 ? "" : "s"); |
| 1536 | } |
| 1537 | if (Py_VerboseFlag > 1) { |
| 1538 | list = block_list; |
| 1539 | while (list != NULL) { |
| 1540 | for (i = 0, p = &list->objects[0]; |
| 1541 | i < N_INTOBJECTS; |
| 1542 | i++, p++) { |
| 1543 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
| 1544 | /* XXX(twouters) cast refcount to |
| 1545 | long until %zd is universally |
| 1546 | available |
| 1547 | */ |
| 1548 | fprintf(stderr, |
| 1549 | "# <int at %p, refcnt=%ld, val=%ld>\n", |
| 1550 | p, (long)p->ob_refcnt, |
| 1551 | p->ob_ival); |
| 1552 | } |
| 1553 | list = list->next; |
| 1554 | } |
| 1555 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1556 | } |