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> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 7 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 8 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 9 | { |
| 10 | return LONG_MAX; /* To initialize sys.maxint */ |
| 11 | } |
| 12 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | /* Standard Booleans */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 15 | PyIntObject _Py_ZeroStruct = { |
| 16 | PyObject_HEAD_INIT(&PyInt_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | 0 |
| 18 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 20 | PyIntObject _Py_TrueStruct = { |
| 21 | PyObject_HEAD_INIT(&PyInt_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 22 | 1 |
| 23 | }; |
| 24 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 26 | err_ovf(char *msg) |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 27 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 28 | PyErr_SetString(PyExc_OverflowError, msg); |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 29 | return NULL; |
| 30 | } |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Integers are quite normal objects, to make object handling uniform. |
| 33 | (Using odd pointers to represent integers would save much space |
| 34 | but require extra checks for this special case throughout the code.) |
| 35 | Since, a typical Python program spends much of its time allocating |
| 36 | and deallocating integers, these operations should be very fast. |
| 37 | Therefore we use a dedicated allocation scheme with a much lower |
| 38 | overhead (in space and time) than straight malloc(): a simple |
| 39 | dedicated free list, filled when necessary with memory from malloc(). |
| 40 | */ |
| 41 | |
| 42 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 43 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 44 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 46 | struct _intblock { |
| 47 | struct _intblock *next; |
| 48 | PyIntObject objects[N_INTOBJECTS]; |
| 49 | }; |
| 50 | |
| 51 | typedef struct _intblock PyIntBlock; |
| 52 | |
| 53 | static PyIntBlock *block_list = NULL; |
| 54 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 56 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 57 | fill_free_list(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 58 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | PyIntObject *p, *q; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 60 | /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */ |
| 61 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 62 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 63 | return (PyIntObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 64 | ((PyIntBlock *)p)->next = block_list; |
| 65 | block_list = (PyIntBlock *)p; |
| 66 | p = &((PyIntBlock *)p)->objects[0]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | q = p + N_INTOBJECTS; |
| 68 | while (--q > p) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 69 | q->ob_type = (struct _typeobject *)(q-1); |
| 70 | q->ob_type = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 71 | return p + N_INTOBJECTS - 1; |
| 72 | } |
| 73 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 74 | #ifndef NSMALLPOSINTS |
| 75 | #define NSMALLPOSINTS 100 |
| 76 | #endif |
| 77 | #ifndef NSMALLNEGINTS |
| 78 | #define NSMALLNEGINTS 1 |
| 79 | #endif |
| 80 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 81 | /* References to small integers are saved in this array so that they |
| 82 | can be shared. |
| 83 | The integers that are saved are those in the range |
| 84 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 85 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 87 | #endif |
| 88 | #ifdef COUNT_ALLOCS |
| 89 | int quick_int_allocs, quick_neg_int_allocs; |
| 90 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 92 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 93 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 96 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 97 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && |
| 98 | (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 100 | #ifdef COUNT_ALLOCS |
| 101 | if (ival >= 0) |
| 102 | quick_int_allocs++; |
| 103 | else |
| 104 | quick_neg_int_allocs++; |
| 105 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 107 | } |
| 108 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 109 | if (free_list == NULL) { |
| 110 | if ((free_list = fill_free_list()) == NULL) |
| 111 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 113 | /* PyObject_New is inlined */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 114 | v = free_list; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 115 | free_list = (PyIntObject *)v->ob_type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 116 | PyObject_INIT(v, &PyInt_Type); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | v->ob_ival = ival; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 118 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 119 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 120 | /* save this one for a following allocation */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 122 | small_ints[ival + NSMALLNEGINTS] = v; |
| 123 | } |
| 124 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 129 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 131 | v->ob_type = (struct _typeobject *)free_list; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | free_list = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 136 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 137 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 138 | PyNumberMethods *nb; |
| 139 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 140 | long val; |
| 141 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | if (op && PyInt_Check(op)) |
| 143 | return PyInt_AS_LONG((PyIntObject*) op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 144 | |
| 145 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 146 | nb->nb_int == NULL) { |
Guido van Rossum | c18a6f4 | 2000-05-09 14:27:48 +0000 | [diff] [blame] | 147 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | return -1; |
| 149 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 152 | if (io == NULL) |
| 153 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 154 | if (!PyInt_Check(io)) { |
| 155 | PyErr_SetString(PyExc_TypeError, |
| 156 | "nb_int should return int object"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 157 | return -1; |
| 158 | } |
| 159 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 160 | val = PyInt_AS_LONG(io); |
| 161 | Py_DECREF(io); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 162 | |
| 163 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 166 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 167 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 168 | { |
| 169 | char *end; |
| 170 | long x; |
| 171 | char buffer[256]; /* For errors */ |
| 172 | |
| 173 | if ((base != 0 && base < 2) || base > 36) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 174 | PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | while (*s && isspace(Py_CHARMASK(*s))) |
| 179 | s++; |
| 180 | errno = 0; |
| 181 | if (base == 0 && s[0] == '0') |
| 182 | x = (long) PyOS_strtoul(s, &end, base); |
| 183 | else |
| 184 | x = PyOS_strtol(s, &end, base); |
Martin v. Löwis | 2b6727b | 2001-03-06 12:12:02 +0000 | [diff] [blame] | 185 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 186 | goto bad; |
| 187 | while (*end && isspace(Py_CHARMASK(*end))) |
| 188 | end++; |
| 189 | if (*end != '\0') { |
| 190 | bad: |
| 191 | sprintf(buffer, "invalid literal for int(): %.200s", s); |
| 192 | PyErr_SetString(PyExc_ValueError, buffer); |
| 193 | return NULL; |
| 194 | } |
| 195 | else if (errno != 0) { |
| 196 | sprintf(buffer, "int() literal too large: %.200s", s); |
| 197 | PyErr_SetString(PyExc_ValueError, buffer); |
| 198 | return NULL; |
| 199 | } |
| 200 | if (pend) |
| 201 | *pend = end; |
| 202 | return PyInt_FromLong(x); |
| 203 | } |
| 204 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 205 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 206 | PyInt_FromUnicode(Py_UNICODE *s, int length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 207 | { |
| 208 | char buffer[256]; |
| 209 | |
| 210 | if (length >= sizeof(buffer)) { |
| 211 | PyErr_SetString(PyExc_ValueError, |
| 212 | "int() literal too large to convert"); |
| 213 | return NULL; |
| 214 | } |
| 215 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) |
| 216 | return NULL; |
| 217 | return PyInt_FromString(buffer, NULL, base); |
| 218 | } |
| 219 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | /* Methods */ |
| 221 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 222 | /* Integers are seen as the "smallest" of all numeric types and thus |
| 223 | don't have any knowledge about conversion of other types to |
| 224 | integers. */ |
| 225 | |
| 226 | #define CONVERT_TO_LONG(obj, lng) \ |
| 227 | if (PyInt_Check(obj)) { \ |
| 228 | lng = PyInt_AS_LONG(obj); \ |
| 229 | } \ |
| 230 | else { \ |
| 231 | Py_INCREF(Py_NotImplemented); \ |
| 232 | return Py_NotImplemented; \ |
| 233 | } |
| 234 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 235 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 236 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 237 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 238 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | { |
| 240 | fprintf(fp, "%ld", v->ob_ival); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 241 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 244 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 245 | int_repr(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | { |
| 247 | char buf[20]; |
| 248 | sprintf(buf, "%ld", v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 249 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 253 | int_compare(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 254 | { |
| 255 | register long i = v->ob_ival; |
| 256 | register long j = w->ob_ival; |
| 257 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 258 | } |
| 259 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 260 | static long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 261 | int_hash(PyIntObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 262 | { |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 263 | /* XXX If this is changed, you also need to change the way |
| 264 | Python's long, float and complex types are hashed. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 265 | long x = v -> ob_ival; |
| 266 | if (x == -1) |
| 267 | x = -2; |
| 268 | return x; |
| 269 | } |
| 270 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 272 | int_add(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | { |
| 274 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 275 | CONVERT_TO_LONG(v, a); |
| 276 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 277 | x = a + b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 278 | if ((x^a) < 0 && (x^b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 279 | return err_ovf("integer addition"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 280 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 284 | int_sub(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | { |
| 286 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 287 | CONVERT_TO_LONG(v, a); |
| 288 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | x = a - b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 290 | if ((x^a) < 0 && (x^~b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 291 | return err_ovf("integer subtraction"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 292 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 295 | /* |
| 296 | Integer overflow checking used to be done using a double, but on 64 |
| 297 | bit machines (where both long and double are 64 bit) this fails |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 298 | because the double doesn't have enough precision. John Tromp suggests |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 299 | the following algorithm: |
| 300 | |
| 301 | Suppose again we normalize a and b to be nonnegative. |
| 302 | Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
| 303 | Now we test ah and bh against zero and get essentially 3 possible outcomes. |
| 304 | |
| 305 | 1) both ah and bh > 0 : then report overflow |
| 306 | |
| 307 | 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
| 308 | negative |
| 309 | |
| 310 | 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
| 311 | compute al*bl and report overflow if it's negative |
| 312 | add (ah*bl)<<32 to al*bl and report overflow if |
| 313 | it's negative |
| 314 | |
| 315 | In case of no overflow the result is then negated if necessary. |
| 316 | |
| 317 | The majority of cases will be 2), in which case this method is the same as |
| 318 | what I suggested before. If multiplication is expensive enough, then the |
| 319 | other method is faster on case 3), but also more work to program, so I |
| 320 | guess the above is the preferred solution. |
| 321 | |
| 322 | */ |
| 323 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | static PyObject * |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 325 | int_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 327 | long a, b, ah, bh, x, y; |
| 328 | int s = 1; |
| 329 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 330 | if (v->ob_type->tp_as_sequence && |
| 331 | v->ob_type->tp_as_sequence->sq_repeat) { |
| 332 | /* sequence * int */ |
| 333 | a = PyInt_AsLong(w); |
| 334 | return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a); |
| 335 | } |
| 336 | else if (w->ob_type->tp_as_sequence && |
| 337 | w->ob_type->tp_as_sequence->sq_repeat) { |
| 338 | /* int * sequence */ |
| 339 | a = PyInt_AsLong(v); |
| 340 | return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a); |
| 341 | } |
| 342 | |
| 343 | CONVERT_TO_LONG(v, a); |
| 344 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 345 | ah = a >> (LONG_BIT/2); |
| 346 | bh = b >> (LONG_BIT/2); |
| 347 | |
| 348 | /* Quick test for common case: two small positive ints */ |
| 349 | |
| 350 | if (ah == 0 && bh == 0) { |
| 351 | x = a*b; |
| 352 | if (x < 0) |
| 353 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | return PyInt_FromLong(x); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* Arrange that a >= b >= 0 */ |
| 358 | |
| 359 | if (a < 0) { |
| 360 | a = -a; |
| 361 | if (a < 0) { |
| 362 | /* Largest negative */ |
| 363 | if (b == 0 || b == 1) { |
| 364 | x = a*b; |
| 365 | goto ok; |
| 366 | } |
| 367 | else |
| 368 | goto bad; |
| 369 | } |
| 370 | s = -s; |
| 371 | ah = a >> (LONG_BIT/2); |
| 372 | } |
| 373 | if (b < 0) { |
| 374 | b = -b; |
| 375 | if (b < 0) { |
| 376 | /* Largest negative */ |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 377 | if (a == 0 || (a == 1 && s == 1)) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 378 | x = a*b; |
| 379 | goto ok; |
| 380 | } |
| 381 | else |
| 382 | goto bad; |
| 383 | } |
| 384 | s = -s; |
| 385 | bh = b >> (LONG_BIT/2); |
| 386 | } |
| 387 | |
| 388 | /* 1) both ah and bh > 0 : then report overflow */ |
| 389 | |
| 390 | if (ah != 0 && bh != 0) |
| 391 | goto bad; |
| 392 | |
| 393 | /* 2) both ah and bh = 0 : then compute a*b and report |
| 394 | overflow if it comes out negative */ |
| 395 | |
| 396 | if (ah == 0 && bh == 0) { |
| 397 | x = a*b; |
| 398 | if (x < 0) |
| 399 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | return PyInt_FromLong(x*s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (a < b) { |
| 404 | /* Swap */ |
| 405 | x = a; |
| 406 | a = b; |
| 407 | b = x; |
| 408 | ah = bh; |
| 409 | /* bh not used beyond this point */ |
| 410 | } |
| 411 | |
| 412 | /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
| 413 | it's >= 2^31 |
| 414 | compute al*bl and report overflow if it's negative |
| 415 | add (ah*bl)<<32 to al*bl and report overflow if |
| 416 | it's negative |
| 417 | (NB b == bl in this case, and we make a = al) */ |
| 418 | |
| 419 | y = ah*b; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 420 | if (y >= (1L << (LONG_BIT/2 - 1))) |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 421 | goto bad; |
| 422 | a &= (1L << (LONG_BIT/2)) - 1; |
| 423 | x = a*b; |
| 424 | if (x < 0) |
| 425 | goto bad; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 426 | x += y << (LONG_BIT/2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 427 | if (x < 0) |
| 428 | goto bad; |
| 429 | ok: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | return PyInt_FromLong(x * s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 431 | |
| 432 | bad: |
| 433 | return err_ovf("integer multiplication"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 436 | static int |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 437 | i_divmod(register long x, register long y, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 438 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 440 | long xdivy, xmody; |
| 441 | |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 442 | if (y == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 444 | "integer division or modulo by zero"); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 445 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 446 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 447 | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
| 448 | if (y == -1 && x < 0 && x == -x) { |
| 449 | err_ovf("integer division"); |
| 450 | return -1; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 451 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 452 | xdivy = x / y; |
| 453 | xmody = x - xdivy * y; |
| 454 | /* If the signs of x and y differ, and the remainder is non-0, |
| 455 | * C89 doesn't define whether xdivy is now the floor or the |
| 456 | * ceiling of the infinitely precise quotient. We want the floor, |
| 457 | * and we have it iff the remainder's sign matches y's. |
| 458 | */ |
| 459 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
| 460 | xmody += y; |
| 461 | --xdivy; |
| 462 | assert(xmody && ((y ^ xmody) >= 0)); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 463 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 464 | *p_xdivy = xdivy; |
| 465 | *p_xmody = xmody; |
| 466 | return 0; |
| 467 | } |
| 468 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 469 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 470 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 471 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 472 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 473 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 474 | CONVERT_TO_LONG(x, xi); |
| 475 | CONVERT_TO_LONG(y, yi); |
| 476 | if (i_divmod(xi, yi, &d, &m) < 0) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 477 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 478 | return PyInt_FromLong(d); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 481 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 482 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 483 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 484 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 485 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 486 | CONVERT_TO_LONG(x, xi); |
| 487 | CONVERT_TO_LONG(y, yi); |
| 488 | if (i_divmod(xi, yi, &d, &m) < 0) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 489 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | return PyInt_FromLong(m); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 494 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 495 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 496 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 497 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 498 | CONVERT_TO_LONG(x, xi); |
| 499 | CONVERT_TO_LONG(y, yi); |
| 500 | if (i_divmod(xi, yi, &d, &m) < 0) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 501 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | return Py_BuildValue("(ll)", d, m); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 506 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 508 | #if 1 |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 509 | register long iv, iw, iz=0, ix, temp, prev; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 510 | CONVERT_TO_LONG(v, iv); |
| 511 | CONVERT_TO_LONG(w, iw); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 512 | if (iw < 0) { |
Guido van Rossum | b82fedc | 2001-07-12 11:19:45 +0000 | [diff] [blame] | 513 | /* Return a float. This works because we know that |
| 514 | this calls float_pow() which converts its |
| 515 | arguments to double. */ |
| 516 | return PyFloat_Type.tp_as_number->nb_power( |
| 517 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 518 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 519 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 520 | CONVERT_TO_LONG(z, iz); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 521 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 522 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 523 | "pow() arg 3 cannot be 0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 526 | } |
| 527 | /* |
| 528 | * XXX: The original exponentiation code stopped looping |
| 529 | * when temp hit zero; this code will continue onwards |
| 530 | * unnecessarily, but at least it won't cause any errors. |
| 531 | * Hopefully the speed improvement from the fast exponentiation |
| 532 | * will compensate for the slight inefficiency. |
| 533 | * XXX: Better handling of overflows is desperately needed. |
| 534 | */ |
| 535 | temp = iv; |
| 536 | ix = 1; |
| 537 | while (iw > 0) { |
| 538 | prev = ix; /* Save value for overflow check */ |
| 539 | if (iw & 1) { |
| 540 | ix = ix*temp; |
| 541 | if (temp == 0) |
| 542 | break; /* Avoid ix / 0 */ |
| 543 | if (ix / temp != prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 544 | return err_ovf("integer exponentiation"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 545 | } |
| 546 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 547 | if (iw==0) break; |
| 548 | prev = temp; |
| 549 | temp *= temp; /* Square the value of temp */ |
| 550 | if (prev!=0 && temp/prev!=prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 551 | return err_ovf("integer exponentiation"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 552 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 553 | /* If we did a multiplication, perform a modulo */ |
| 554 | ix = ix % iz; |
| 555 | temp = temp % iz; |
| 556 | } |
| 557 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 558 | if (iz) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 559 | long div, mod; |
| 560 | if (i_divmod(ix, iz, &div, &mod) < 0) |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 561 | return(NULL); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 562 | ix=mod; |
| 563 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 565 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | register long iv, iw, ix; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 567 | CONVERT_TO_LONG(v, iv); |
| 568 | CONVERT_TO_LONG(w, iw); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 569 | if (iw < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | PyErr_SetString(PyExc_ValueError, |
| 571 | "integer to the negative power"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 572 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 573 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 574 | if ((PyObject *)z != Py_None) { |
| 575 | PyErr_SetString(PyExc_TypeError, |
| 576 | "pow(int, int, int) not yet supported"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 579 | ix = 1; |
| 580 | while (--iw >= 0) { |
| 581 | long prev = ix; |
| 582 | ix = ix * iv; |
| 583 | if (iv == 0) |
| 584 | break; /* 0 to some power -- avoid ix / 0 */ |
| 585 | if (ix / iv != prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 586 | return err_ovf("integer exponentiation"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 587 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 589 | #endif |
| 590 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 593 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | { |
| 595 | register long a, x; |
| 596 | a = v->ob_ival; |
| 597 | x = -a; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 598 | if (a < 0 && x < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 599 | return err_ovf("integer negation"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 600 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 604 | int_pos(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | Py_INCREF(v); |
| 607 | return (PyObject *)v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 611 | int_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 612 | { |
| 613 | if (v->ob_ival >= 0) |
| 614 | return int_pos(v); |
| 615 | else |
| 616 | return int_neg(v); |
| 617 | } |
| 618 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 619 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 620 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 621 | { |
| 622 | return v->ob_ival != 0; |
| 623 | } |
| 624 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 626 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 627 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 628 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 631 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 632 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 633 | { |
| 634 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 635 | CONVERT_TO_LONG(v, a); |
| 636 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 637 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 639 | return NULL; |
| 640 | } |
| 641 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | Py_INCREF(v); |
| 643 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 644 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 645 | if (b >= LONG_BIT) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 646 | return PyInt_FromLong(0L); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 647 | } |
| 648 | a = (unsigned long)a << b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 649 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 653 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 654 | { |
| 655 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 656 | CONVERT_TO_LONG(v, a); |
| 657 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 658 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 659 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 660 | return NULL; |
| 661 | } |
| 662 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 663 | Py_INCREF(v); |
| 664 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 665 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 666 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 667 | if (a < 0) |
| 668 | a = -1; |
| 669 | else |
| 670 | a = 0; |
| 671 | } |
| 672 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 673 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 674 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 675 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 678 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 679 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 680 | { |
| 681 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 682 | CONVERT_TO_LONG(v, a); |
| 683 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 687 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 688 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 689 | { |
| 690 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 691 | CONVERT_TO_LONG(v, a); |
| 692 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 693 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 696 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 697 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 698 | { |
| 699 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 700 | CONVERT_TO_LONG(v, a); |
| 701 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 702 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 705 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 706 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 707 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 708 | Py_INCREF(v); |
| 709 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 712 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 713 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 714 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 715 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 718 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 719 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 720 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 721 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 724 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 725 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 726 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 727 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 728 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 729 | if (x == 0) |
| 730 | strcpy(buf, "0"); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 731 | else |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 732 | sprintf(buf, "0%lo", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 733 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 736 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 737 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 738 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 739 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 740 | long x = v -> ob_ival; |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 741 | sprintf(buf, "0x%lx", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 742 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 745 | static PyNumberMethods int_as_number = { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 746 | (binaryfunc)int_add, /*nb_add*/ |
| 747 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 748 | (binaryfunc)int_mul, /*nb_multiply*/ |
| 749 | (binaryfunc)int_div, /*nb_divide*/ |
| 750 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 751 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 752 | (ternaryfunc)int_pow, /*nb_power*/ |
| 753 | (unaryfunc)int_neg, /*nb_negative*/ |
| 754 | (unaryfunc)int_pos, /*nb_positive*/ |
| 755 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 756 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 757 | (unaryfunc)int_invert, /*nb_invert*/ |
| 758 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 759 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 760 | (binaryfunc)int_and, /*nb_and*/ |
| 761 | (binaryfunc)int_xor, /*nb_xor*/ |
| 762 | (binaryfunc)int_or, /*nb_or*/ |
| 763 | 0, /*nb_coerce*/ |
| 764 | (unaryfunc)int_int, /*nb_int*/ |
| 765 | (unaryfunc)int_long, /*nb_long*/ |
| 766 | (unaryfunc)int_float, /*nb_float*/ |
| 767 | (unaryfunc)int_oct, /*nb_oct*/ |
| 768 | (unaryfunc)int_hex, /*nb_hex*/ |
| 769 | 0, /*nb_inplace_add*/ |
| 770 | 0, /*nb_inplace_subtract*/ |
| 771 | 0, /*nb_inplace_multiply*/ |
| 772 | 0, /*nb_inplace_divide*/ |
| 773 | 0, /*nb_inplace_remainder*/ |
| 774 | 0, /*nb_inplace_power*/ |
| 775 | 0, /*nb_inplace_lshift*/ |
| 776 | 0, /*nb_inplace_rshift*/ |
| 777 | 0, /*nb_inplace_and*/ |
| 778 | 0, /*nb_inplace_xor*/ |
| 779 | 0, /*nb_inplace_or*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 782 | PyTypeObject PyInt_Type = { |
| 783 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 784 | 0, |
| 785 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 786 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 787 | 0, |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 788 | (destructor)int_dealloc, /*tp_dealloc*/ |
| 789 | (printfunc)int_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | 0, /*tp_getattr*/ |
| 791 | 0, /*tp_setattr*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 792 | (cmpfunc)int_compare, /*tp_compare*/ |
| 793 | (reprfunc)int_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 794 | &int_as_number, /*tp_as_number*/ |
| 795 | 0, /*tp_as_sequence*/ |
| 796 | 0, /*tp_as_mapping*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 797 | (hashfunc)int_hash, /*tp_hash*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 798 | 0, /*tp_call*/ |
| 799 | 0, /*tp_str*/ |
| 800 | 0, /*tp_getattro*/ |
| 801 | 0, /*tp_setattro*/ |
| 802 | 0, /*tp_as_buffer*/ |
Guido van Rossum | 3968e4c | 2001-01-17 15:32:23 +0000 | [diff] [blame] | 803 | Py_TPFLAGS_CHECKTYPES /*tp_flags*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 805 | |
| 806 | void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 807 | PyInt_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 809 | PyIntObject *p; |
| 810 | PyIntBlock *list, *next; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 811 | int i; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 812 | int bc, bf; /* block count, number of freed blocks */ |
| 813 | int irem, isum; /* remaining unfreed ints per block, total */ |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 814 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 815 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 816 | PyIntObject **q; |
| 817 | |
| 818 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 819 | q = small_ints; |
| 820 | while (--i >= 0) { |
| 821 | Py_XDECREF(*q); |
| 822 | *q++ = NULL; |
| 823 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 824 | #endif |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 825 | bc = 0; |
| 826 | bf = 0; |
| 827 | isum = 0; |
| 828 | list = block_list; |
| 829 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 830 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 831 | while (list != NULL) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 832 | bc++; |
| 833 | irem = 0; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 834 | for (i = 0, p = &list->objects[0]; |
| 835 | i < N_INTOBJECTS; |
| 836 | i++, p++) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 837 | if (PyInt_Check(p) && p->ob_refcnt != 0) |
| 838 | irem++; |
| 839 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 840 | next = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 841 | if (irem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 842 | list->next = block_list; |
| 843 | block_list = list; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 844 | for (i = 0, p = &list->objects[0]; |
| 845 | i < N_INTOBJECTS; |
| 846 | i++, p++) { |
| 847 | if (!PyInt_Check(p) || p->ob_refcnt == 0) { |
| 848 | p->ob_type = (struct _typeobject *) |
| 849 | free_list; |
| 850 | free_list = p; |
| 851 | } |
| 852 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 853 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 854 | p->ob_ival < NSMALLPOSINTS && |
| 855 | small_ints[p->ob_ival + |
| 856 | NSMALLNEGINTS] == NULL) { |
| 857 | Py_INCREF(p); |
| 858 | small_ints[p->ob_ival + |
| 859 | NSMALLNEGINTS] = p; |
| 860 | } |
| 861 | #endif |
| 862 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 863 | } |
| 864 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 865 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 866 | bf++; |
| 867 | } |
| 868 | isum += irem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 869 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 870 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 871 | if (!Py_VerboseFlag) |
| 872 | return; |
| 873 | fprintf(stderr, "# cleanup ints"); |
| 874 | if (!isum) { |
| 875 | fprintf(stderr, "\n"); |
| 876 | } |
| 877 | else { |
| 878 | fprintf(stderr, |
| 879 | ": %d unfreed int%s in %d out of %d block%s\n", |
| 880 | isum, isum == 1 ? "" : "s", |
| 881 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 882 | } |
| 883 | if (Py_VerboseFlag > 1) { |
| 884 | list = block_list; |
| 885 | while (list != NULL) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 886 | for (i = 0, p = &list->objects[0]; |
| 887 | i < N_INTOBJECTS; |
| 888 | i++, p++) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 889 | if (PyInt_Check(p) && p->ob_refcnt != 0) |
| 890 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 891 | "# <int at %p, refcnt=%d, val=%ld>\n", |
| 892 | p, p->ob_refcnt, p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 893 | } |
| 894 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 897 | } |