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