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 | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | #define N_INTOBJECTS (BLOCK_SIZE / sizeof(PyIntObject)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | static PyIntObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 96 | fill_free_list() |
| 97 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | PyIntObject *p, *q; |
| 99 | p = PyMem_NEW(PyIntObject, N_INTOBJECTS); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 100 | if (p == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | return (PyIntObject *)PyErr_NoMemory(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 102 | q = p + N_INTOBJECTS; |
| 103 | while (--q > p) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | *(PyIntObject **)q = q-1; |
| 105 | *(PyIntObject **)q = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | return p + N_INTOBJECTS - 1; |
| 107 | } |
| 108 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | static PyIntObject *free_list = NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 110 | #ifndef NSMALLPOSINTS |
| 111 | #define NSMALLPOSINTS 100 |
| 112 | #endif |
| 113 | #ifndef NSMALLNEGINTS |
| 114 | #define NSMALLNEGINTS 1 |
| 115 | #endif |
| 116 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 117 | /* References to small integers are saved in this array so that they |
| 118 | can be shared. |
| 119 | The integers that are saved are those in the range |
| 120 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 121 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 123 | #endif |
| 124 | #ifdef COUNT_ALLOCS |
| 125 | int quick_int_allocs, quick_neg_int_allocs; |
| 126 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | PyObject * |
| 129 | PyInt_FromLong(ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | long ival; |
| 131 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 133 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 134 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && |
| 135 | (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 136 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 137 | #ifdef COUNT_ALLOCS |
| 138 | if (ival >= 0) |
| 139 | quick_int_allocs++; |
| 140 | else |
| 141 | quick_neg_int_allocs++; |
| 142 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 144 | } |
| 145 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | if (free_list == NULL) { |
| 147 | if ((free_list = fill_free_list()) == NULL) |
| 148 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 150 | v = free_list; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | free_list = *(PyIntObject **)free_list; |
| 152 | v->ob_type = &PyInt_Type; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 153 | v->ob_ival = ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 154 | _Py_NewReference(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 155 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 156 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 157 | /* save this one for a following allocation */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 159 | small_ints[ival + NSMALLNEGINTS] = v; |
| 160 | } |
| 161 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void |
| 166 | int_dealloc(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | PyIntObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 168 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 169 | *(PyIntObject **)v = free_list; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 170 | free_list = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | long |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 174 | PyInt_AsLong(op) |
| 175 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 177 | PyNumberMethods *nb; |
| 178 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 179 | long val; |
| 180 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | if (op && PyInt_Check(op)) |
| 182 | return PyInt_AS_LONG((PyIntObject*) op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 183 | |
| 184 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 185 | nb->nb_int == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | return -1; |
| 188 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 190 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 191 | if (io == NULL) |
| 192 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 193 | if (!PyInt_Check(io)) { |
| 194 | PyErr_SetString(PyExc_TypeError, |
| 195 | "nb_int should return int object"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 199 | val = PyInt_AS_LONG(io); |
| 200 | Py_DECREF(io); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 201 | |
| 202 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* Methods */ |
| 206 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 207 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 208 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 209 | int_print(v, fp, flags) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | PyIntObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | FILE *fp; |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 212 | int flags; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | { |
| 214 | fprintf(fp, "%ld", v->ob_ival); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 215 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 218 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 219 | int_repr(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 220 | PyIntObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | { |
| 222 | char buf[20]; |
| 223 | sprintf(buf, "%ld", v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | int_compare(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 229 | PyIntObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | { |
| 231 | register long i = v->ob_ival; |
| 232 | register long j = w->ob_ival; |
| 233 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 234 | } |
| 235 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 236 | static long |
| 237 | int_hash(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | PyIntObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 239 | { |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 240 | /* XXX If this is changed, you also need to change the way |
| 241 | Python's long, float and complex types are hashed. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 242 | long x = v -> ob_ival; |
| 243 | if (x == -1) |
| 244 | x = -2; |
| 245 | return x; |
| 246 | } |
| 247 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 249 | int_add(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 250 | PyIntObject *v; |
| 251 | PyIntObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | { |
| 253 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 254 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 255 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | x = a + b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 257 | if ((x^a) < 0 && (x^b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 258 | return err_ovf("integer addition"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 259 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 262 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 263 | int_sub(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 264 | PyIntObject *v; |
| 265 | PyIntObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 266 | { |
| 267 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 269 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | x = a - b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 271 | if ((x^a) < 0 && (x^~b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 272 | return err_ovf("integer subtraction"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 276 | /* |
| 277 | Integer overflow checking used to be done using a double, but on 64 |
| 278 | bit machines (where both long and double are 64 bit) this fails |
| 279 | because the double doesn't have enouvg precision. John Tromp suggests |
| 280 | the following algorithm: |
| 281 | |
| 282 | Suppose again we normalize a and b to be nonnegative. |
| 283 | Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
| 284 | Now we test ah and bh against zero and get essentially 3 possible outcomes. |
| 285 | |
| 286 | 1) both ah and bh > 0 : then report overflow |
| 287 | |
| 288 | 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
| 289 | negative |
| 290 | |
| 291 | 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
| 292 | compute al*bl and report overflow if it's negative |
| 293 | add (ah*bl)<<32 to al*bl and report overflow if |
| 294 | it's negative |
| 295 | |
| 296 | In case of no overflow the result is then negated if necessary. |
| 297 | |
| 298 | The majority of cases will be 2), in which case this method is the same as |
| 299 | what I suggested before. If multiplication is expensive enough, then the |
| 300 | other method is faster on case 3), but also more work to program, so I |
| 301 | guess the above is the preferred solution. |
| 302 | |
| 303 | */ |
| 304 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 306 | int_mul(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | PyIntObject *v; |
| 308 | PyIntObject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 310 | long a, b, ah, bh, x, y; |
| 311 | int s = 1; |
| 312 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 314 | b = w->ob_ival; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 315 | ah = a >> (LONG_BIT/2); |
| 316 | bh = b >> (LONG_BIT/2); |
| 317 | |
| 318 | /* Quick test for common case: two small positive ints */ |
| 319 | |
| 320 | if (ah == 0 && bh == 0) { |
| 321 | x = a*b; |
| 322 | if (x < 0) |
| 323 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | return PyInt_FromLong(x); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* Arrange that a >= b >= 0 */ |
| 328 | |
| 329 | if (a < 0) { |
| 330 | a = -a; |
| 331 | if (a < 0) { |
| 332 | /* Largest negative */ |
| 333 | if (b == 0 || b == 1) { |
| 334 | x = a*b; |
| 335 | goto ok; |
| 336 | } |
| 337 | else |
| 338 | goto bad; |
| 339 | } |
| 340 | s = -s; |
| 341 | ah = a >> (LONG_BIT/2); |
| 342 | } |
| 343 | if (b < 0) { |
| 344 | b = -b; |
| 345 | if (b < 0) { |
| 346 | /* Largest negative */ |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 347 | if (a == 0 || (a == 1 && s == 1)) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 348 | x = a*b; |
| 349 | goto ok; |
| 350 | } |
| 351 | else |
| 352 | goto bad; |
| 353 | } |
| 354 | s = -s; |
| 355 | bh = b >> (LONG_BIT/2); |
| 356 | } |
| 357 | |
| 358 | /* 1) both ah and bh > 0 : then report overflow */ |
| 359 | |
| 360 | if (ah != 0 && bh != 0) |
| 361 | goto bad; |
| 362 | |
| 363 | /* 2) both ah and bh = 0 : then compute a*b and report |
| 364 | overflow if it comes out negative */ |
| 365 | |
| 366 | if (ah == 0 && bh == 0) { |
| 367 | x = a*b; |
| 368 | if (x < 0) |
| 369 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 370 | return PyInt_FromLong(x*s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (a < b) { |
| 374 | /* Swap */ |
| 375 | x = a; |
| 376 | a = b; |
| 377 | b = x; |
| 378 | ah = bh; |
| 379 | /* bh not used beyond this point */ |
| 380 | } |
| 381 | |
| 382 | /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
| 383 | it's >= 2^31 |
| 384 | compute al*bl and report overflow if it's negative |
| 385 | add (ah*bl)<<32 to al*bl and report overflow if |
| 386 | it's negative |
| 387 | (NB b == bl in this case, and we make a = al) */ |
| 388 | |
| 389 | y = ah*b; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 390 | if (y >= (1L << (LONG_BIT/2 - 1))) |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 391 | goto bad; |
| 392 | a &= (1L << (LONG_BIT/2)) - 1; |
| 393 | x = a*b; |
| 394 | if (x < 0) |
| 395 | goto bad; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 396 | x += y << (LONG_BIT/2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 397 | if (x < 0) |
| 398 | goto bad; |
| 399 | ok: |
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 | bad: |
| 403 | return err_ovf("integer multiplication"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 406 | static int |
| 407 | i_divmod(x, y, p_xdivy, p_xmody) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | register PyIntObject *x, *y; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 409 | long *p_xdivy, *p_xmody; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 411 | long xi = x->ob_ival; |
| 412 | long yi = y->ob_ival; |
| 413 | long xdivy, xmody; |
| 414 | |
| 415 | if (yi == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 416 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 417 | "integer division or modulo"); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 418 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 420 | if (yi < 0) { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 421 | if (xi < 0) |
| 422 | xdivy = -xi / -yi; |
| 423 | else |
| 424 | xdivy = - (xi / -yi); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 425 | } |
| 426 | else { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 427 | if (xi < 0) |
| 428 | xdivy = - (-xi / yi); |
| 429 | else |
| 430 | xdivy = xi / yi; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 431 | } |
| 432 | xmody = xi - xdivy*yi; |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 433 | if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) { |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 434 | xmody += yi; |
| 435 | xdivy -= 1; |
| 436 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 437 | *p_xdivy = xdivy; |
| 438 | *p_xmody = xmody; |
| 439 | return 0; |
| 440 | } |
| 441 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 442 | static PyObject * |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 443 | int_div(x, y) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | PyIntObject *x; |
| 445 | PyIntObject *y; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 446 | { |
| 447 | long d, m; |
| 448 | if (i_divmod(x, y, &d, &m) < 0) |
| 449 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 450 | return PyInt_FromLong(d); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 453 | static PyObject * |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 454 | int_mod(x, y) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | PyIntObject *x; |
| 456 | PyIntObject *y; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 457 | { |
| 458 | long d, m; |
| 459 | if (i_divmod(x, y, &d, &m) < 0) |
| 460 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 461 | return PyInt_FromLong(m); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | static PyObject * |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 465 | int_divmod(x, y) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | PyIntObject *x; |
| 467 | PyIntObject *y; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 469 | long d, m; |
| 470 | if (i_divmod(x, y, &d, &m) < 0) |
| 471 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 472 | return Py_BuildValue("(ll)", d, m); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 475 | static PyObject * |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 476 | int_pow(v, w, z) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | PyIntObject *v; |
| 478 | PyIntObject *w; |
| 479 | PyIntObject *z; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 480 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 481 | #if 1 |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 482 | register long iv, iw, iz=0, ix, temp, prev; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 483 | iv = v->ob_ival; |
| 484 | iw = w->ob_ival; |
| 485 | if (iw < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 486 | PyErr_SetString(PyExc_ValueError, |
| 487 | "integer to the negative power"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | if ((PyObject *)z != Py_None) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 491 | iz = z->ob_ival; |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 492 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | PyErr_SetString(PyExc_ValueError, |
| 494 | "pow(x, y, z) with z==0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 495 | return NULL; |
| 496 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 497 | } |
| 498 | /* |
| 499 | * XXX: The original exponentiation code stopped looping |
| 500 | * when temp hit zero; this code will continue onwards |
| 501 | * unnecessarily, but at least it won't cause any errors. |
| 502 | * Hopefully the speed improvement from the fast exponentiation |
| 503 | * will compensate for the slight inefficiency. |
| 504 | * XXX: Better handling of overflows is desperately needed. |
| 505 | */ |
| 506 | temp = iv; |
| 507 | ix = 1; |
| 508 | while (iw > 0) { |
| 509 | prev = ix; /* Save value for overflow check */ |
| 510 | if (iw & 1) { |
| 511 | ix = ix*temp; |
| 512 | if (temp == 0) |
| 513 | break; /* Avoid ix / 0 */ |
| 514 | if (ix / temp != prev) |
| 515 | return err_ovf("integer pow()"); |
| 516 | } |
| 517 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 518 | if (iw==0) break; |
| 519 | prev = temp; |
| 520 | temp *= temp; /* Square the value of temp */ |
| 521 | if (prev!=0 && temp/prev!=prev) |
| 522 | return err_ovf("integer pow()"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 523 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 524 | /* If we did a multiplication, perform a modulo */ |
| 525 | ix = ix % iz; |
| 526 | temp = temp % iz; |
| 527 | } |
| 528 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 529 | if (iz) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 530 | PyObject *t1, *t2; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 531 | long int div, mod; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 532 | t1=PyInt_FromLong(ix); |
| 533 | t2=PyInt_FromLong(iz); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 534 | if (t1==NULL || t2==NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 535 | i_divmod((PyIntObject *)t1, |
| 536 | (PyIntObject *)t2, &div, &mod)<0) |
| 537 | { |
| 538 | Py_XDECREF(t1); |
| 539 | Py_XDECREF(t2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 540 | return(NULL); |
| 541 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 542 | Py_DECREF(t1); |
| 543 | Py_DECREF(t2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 544 | ix=mod; |
| 545 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 546 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 547 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | register long iv, iw, ix; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 549 | iv = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 550 | iw = w->ob_ival; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 551 | if (iw < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | PyErr_SetString(PyExc_ValueError, |
| 553 | "integer to the negative power"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 554 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 556 | if ((PyObject *)z != Py_None) { |
| 557 | PyErr_SetString(PyExc_TypeError, |
| 558 | "pow(int, int, int) not yet supported"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 559 | return NULL; |
| 560 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 561 | ix = 1; |
| 562 | while (--iw >= 0) { |
| 563 | long prev = ix; |
| 564 | ix = ix * iv; |
| 565 | if (iv == 0) |
| 566 | break; /* 0 to some power -- avoid ix / 0 */ |
| 567 | if (ix / iv != prev) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 568 | return err_ovf("integer pow()"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 569 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 571 | #endif |
| 572 | } |
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 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 575 | int_neg(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | PyIntObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 577 | { |
| 578 | register long a, x; |
| 579 | a = v->ob_ival; |
| 580 | x = -a; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 581 | if (a < 0 && x < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 582 | return err_ovf("integer negation"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 583 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 587 | int_pos(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | PyIntObject *v; |
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 | Py_INCREF(v); |
| 591 | return (PyObject *)v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 594 | static PyObject * |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 595 | int_abs(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | PyIntObject *v; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 597 | { |
| 598 | if (v->ob_ival >= 0) |
| 599 | return int_pos(v); |
| 600 | else |
| 601 | return int_neg(v); |
| 602 | } |
| 603 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 604 | static int |
| 605 | int_nonzero(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | PyIntObject *v; |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 607 | { |
| 608 | return v->ob_ival != 0; |
| 609 | } |
| 610 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 611 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 612 | int_invert(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 613 | PyIntObject *v; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 614 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 619 | int_lshift(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | PyIntObject *v; |
| 621 | PyIntObject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 622 | { |
| 623 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 624 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 625 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 626 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 627 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 628 | return NULL; |
| 629 | } |
| 630 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 631 | Py_INCREF(v); |
| 632 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 633 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 634 | if (b >= LONG_BIT) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | return PyInt_FromLong(0L); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 636 | } |
| 637 | a = (unsigned long)a << b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 641 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 642 | int_rshift(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 643 | PyIntObject *v; |
| 644 | PyIntObject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 645 | { |
| 646 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 647 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 648 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 649 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 650 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | Py_INCREF(v); |
| 655 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 656 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 657 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 658 | if (a < 0) |
| 659 | a = -1; |
| 660 | else |
| 661 | a = 0; |
| 662 | } |
| 663 | else { |
| 664 | if (a < 0) |
| 665 | a = ~( ~(unsigned long)a >> b ); |
| 666 | else |
| 667 | a = (unsigned long)a >> b; |
| 668 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 669 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 672 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 673 | int_and(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 674 | PyIntObject *v; |
| 675 | PyIntObject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 676 | { |
| 677 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 678 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 679 | b = w->ob_ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 680 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 683 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 684 | int_xor(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 685 | PyIntObject *v; |
| 686 | PyIntObject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 687 | { |
| 688 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 689 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 690 | b = w->ob_ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 691 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 694 | static PyObject * |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 695 | int_or(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 696 | PyIntObject *v; |
| 697 | PyIntObject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 698 | { |
| 699 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 700 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 701 | b = w->ob_ival; |
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 * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 706 | int_int(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 707 | PyIntObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 708 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 709 | Py_INCREF(v); |
| 710 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 713 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 714 | int_long(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 715 | PyIntObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 716 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 717 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 720 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 721 | int_float(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 722 | PyIntObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 723 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 724 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 727 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 728 | int_oct(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 729 | PyIntObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 730 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 731 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 732 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 733 | if (x == 0) |
| 734 | strcpy(buf, "0"); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 735 | else |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 736 | sprintf(buf, "0%lo", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 737 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 740 | static PyObject * |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 741 | int_hex(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 742 | PyIntObject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 743 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 744 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 745 | long x = v -> ob_ival; |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 746 | sprintf(buf, "0x%lx", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 747 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 750 | static PyNumberMethods int_as_number = { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 751 | (binaryfunc)int_add, /*nb_add*/ |
| 752 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 753 | (binaryfunc)int_mul, /*nb_multiply*/ |
| 754 | (binaryfunc)int_div, /*nb_divide*/ |
| 755 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 756 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 757 | (ternaryfunc)int_pow, /*nb_power*/ |
| 758 | (unaryfunc)int_neg, /*nb_negative*/ |
| 759 | (unaryfunc)int_pos, /*nb_positive*/ |
| 760 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 761 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 762 | (unaryfunc)int_invert, /*nb_invert*/ |
| 763 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 764 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 765 | (binaryfunc)int_and, /*nb_and*/ |
| 766 | (binaryfunc)int_xor, /*nb_xor*/ |
| 767 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 768 | 0, /*nb_coerce*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 769 | (unaryfunc)int_int, /*nb_int*/ |
| 770 | (unaryfunc)int_long, /*nb_long*/ |
| 771 | (unaryfunc)int_float, /*nb_float*/ |
| 772 | (unaryfunc)int_oct, /*nb_oct*/ |
| 773 | (unaryfunc)int_hex, /*nb_hex*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 774 | }; |
| 775 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 776 | PyTypeObject PyInt_Type = { |
| 777 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 778 | 0, |
| 779 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 780 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 781 | 0, |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 782 | (destructor)int_dealloc, /*tp_dealloc*/ |
| 783 | (printfunc)int_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 784 | 0, /*tp_getattr*/ |
| 785 | 0, /*tp_setattr*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 786 | (cmpfunc)int_compare, /*tp_compare*/ |
| 787 | (reprfunc)int_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 788 | &int_as_number, /*tp_as_number*/ |
| 789 | 0, /*tp_as_sequence*/ |
| 790 | 0, /*tp_as_mapping*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 791 | (hashfunc)int_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 793 | |
| 794 | void |
| 795 | PyInt_Fini() |
| 796 | { |
| 797 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 798 | int i; |
| 799 | PyIntObject **p; |
| 800 | |
| 801 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 802 | p = small_ints; |
| 803 | while (--i >= 0) { |
| 804 | Py_XDECREF(*p); |
| 805 | *p++ = NULL; |
| 806 | } |
| 807 | #endif |
| 808 | /* XXX Alas, the free list is not easily and safely freeable */ |
| 809 | } |