Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1 | /* Long (arbitrary precision) integer object implementation */ |
| 2 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3 | /* XXX The functional organization of this file is terrible */ |
| 4 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 6 | #include "longintrepr.h" |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 8 | #include <ctype.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 10 | #ifndef NSMALLPOSINTS |
| 11 | #define NSMALLPOSINTS 257 |
| 12 | #endif |
| 13 | #ifndef NSMALLNEGINTS |
| 14 | #define NSMALLNEGINTS 5 |
| 15 | #endif |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 16 | |
| 17 | #define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(x)->ob_digit[0] : \ |
| 18 | (Py_SIZE(x) == 0 ? 0 : (x)->ob_digit[0])) |
| 19 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 20 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 21 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 22 | /* Small integers are preallocated in this array so that they |
| 23 | can be shared. |
| 24 | The integers that are preallocated are those in the range |
| 25 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 26 | */ |
| 27 | static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
| 28 | #ifdef COUNT_ALLOCS |
| 29 | int quick_int_allocs, quick_neg_int_allocs; |
| 30 | #endif |
| 31 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 32 | static PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 33 | get_small_int(int ival) |
| 34 | { |
| 35 | PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); |
| 36 | Py_INCREF(v); |
| 37 | #ifdef COUNT_ALLOCS |
| 38 | if (ival >= 0) |
| 39 | quick_int_allocs++; |
| 40 | else |
| 41 | quick_neg_int_allocs++; |
| 42 | #endif |
| 43 | return v; |
| 44 | } |
| 45 | #define CHECK_SMALL_INT(ival) \ |
| 46 | do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ |
| 47 | return get_small_int(ival); \ |
| 48 | } while(0) |
| 49 | |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 50 | static PyLongObject * |
| 51 | maybe_small_long(PyLongObject *v) |
| 52 | { |
| 53 | if (v && ABS(Py_SIZE(v)) <= 1) { |
| 54 | int ival = MEDIUM_VALUE(v); |
| 55 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 56 | Py_DECREF(v); |
| 57 | return (PyLongObject *)get_small_int(ival); |
| 58 | } |
| 59 | } |
| 60 | return v; |
| 61 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 62 | #else |
| 63 | #define CHECK_SMALL_INT(ival) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 64 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 67 | /* If a freshly-allocated long is already shared, it must |
| 68 | be a small integer, so negating it must go to PyLong_FromLong */ |
| 69 | #define NEGATE(x) \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 70 | do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 71 | else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 72 | Py_DECREF(x); (x) = (PyLongObject*)tmp; } \ |
| 73 | while(0) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 74 | /* For long multiplication, use the O(N**2) school algorithm unless |
| 75 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
| 76 | * being an internal Python long digit, in base BASE). |
| 77 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 78 | #define KARATSUBA_CUTOFF 70 |
| 79 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 80 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 81 | /* For exponentiation, use the binary left-to-right algorithm |
| 82 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 83 | * In that case, do 5 bits at a time. The potential drawback is that |
| 84 | * a table of 2**5 intermediate results is computed. |
| 85 | */ |
| 86 | #define FIVEARY_CUTOFF 8 |
| 87 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 88 | #undef MIN |
| 89 | #undef MAX |
| 90 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 91 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 92 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 93 | /* Forward */ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 94 | static PyLongObject *long_normalize(PyLongObject *); |
| 95 | static PyLongObject *mul1(PyLongObject *, wdigit); |
| 96 | static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 97 | static PyLongObject *divrem1(PyLongObject *, digit, digit *); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | #define SIGCHECK(PyTryBlock) \ |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 100 | if (--_Py_Ticker < 0) { \ |
| 101 | _Py_Ticker = _Py_CheckInterval; \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 102 | if (PyErr_CheckSignals()) PyTryBlock \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 105 | /* Normalize (remove leading zeros from) a long int object. |
| 106 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 107 | of the algorithms used, this could save at most be one word anyway. */ |
| 108 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 110 | long_normalize(register PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 111 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 112 | Py_ssize_t j = ABS(Py_SIZE(v)); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 113 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 115 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 116 | --i; |
| 117 | if (i != j) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 118 | Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 119 | return v; |
| 120 | } |
| 121 | |
| 122 | /* Allocate a new long int object with size digits. |
| 123 | Return NULL and set exception if we run out of memory. */ |
| 124 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 126 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 127 | { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 128 | PyLongObject *result; |
| 129 | /* Can't use sizeof(PyLongObject) here, since the |
| 130 | compiler takes padding at the end into account. |
| 131 | As the consequence, this would waste 2 bytes on |
| 132 | a 32-bit system, and 6 bytes on a 64-bit system. |
| 133 | This computation would be incorrect on systems |
| 134 | which have padding before the digits; with 16-bit |
| 135 | digits this should not happen. */ |
| 136 | result = PyObject_MALLOC(sizeof(PyVarObject) + |
| 137 | size*sizeof(digit)); |
| 138 | if (!result) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 139 | PyErr_NoMemory(); |
| 140 | return NULL; |
| 141 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 142 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 145 | PyObject * |
| 146 | _PyLong_Copy(PyLongObject *src) |
| 147 | { |
| 148 | PyLongObject *result; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 149 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 150 | |
| 151 | assert(src != NULL); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 152 | i = Py_SIZE(src); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 153 | if (i < 0) |
| 154 | i = -(i); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 155 | if (i < 2) { |
| 156 | int ival = src->ob_digit[0]; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 157 | if (Py_SIZE(src) < 0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 158 | ival = -ival; |
| 159 | CHECK_SMALL_INT(ival); |
| 160 | } |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 161 | result = _PyLong_New(i); |
| 162 | if (result != NULL) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 163 | Py_SIZE(result) = Py_SIZE(src); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 164 | while (--i >= 0) |
| 165 | result->ob_digit[i] = src->ob_digit[i]; |
| 166 | } |
| 167 | return (PyObject *)result; |
| 168 | } |
| 169 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 170 | /* Create a new long int object from a C long int */ |
| 171 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 173 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 174 | { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 175 | PyLongObject *v; |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 176 | unsigned long abs_ival; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 177 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 178 | int ndigits = 0; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 179 | int sign = 1; |
| 180 | |
| 181 | CHECK_SMALL_INT(ival); |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 182 | |
| 183 | if (ival < 0) { |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 184 | /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then |
| 185 | ANSI C says that the result of -ival is undefined when ival |
| 186 | == LONG_MIN. Hence the following workaround. */ |
| 187 | abs_ival = (unsigned long)(-1-ival) + 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 188 | sign = -1; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 189 | } |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 190 | else { |
| 191 | abs_ival = (unsigned long)ival; |
| 192 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 194 | /* Fast path for single-digits ints */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 195 | if (!(ival>>PyLong_SHIFT)) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 196 | v = _PyLong_New(1); |
| 197 | if (v) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 198 | Py_SIZE(v) = sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 199 | v->ob_digit[0] = ival; |
| 200 | } |
| 201 | return (PyObject*)v; |
| 202 | } |
| 203 | |
| 204 | /* 2 digits */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 205 | if (!(ival >> 2*PyLong_SHIFT)) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 206 | v = _PyLong_New(2); |
| 207 | if (v) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 208 | Py_SIZE(v) = 2*sign; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 209 | v->ob_digit[0] = (digit)ival & PyLong_MASK; |
| 210 | v->ob_digit[1] = ival >> PyLong_SHIFT; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 211 | } |
| 212 | return (PyObject*)v; |
| 213 | } |
| 214 | |
| 215 | /* Larger numbers: loop to determine number of digits */ |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 216 | t = abs_ival; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 217 | while (t) { |
| 218 | ++ndigits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 219 | t >>= PyLong_SHIFT; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 220 | } |
| 221 | v = _PyLong_New(ndigits); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 222 | if (v != NULL) { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 223 | digit *p = v->ob_digit; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 224 | Py_SIZE(v) = ndigits*sign; |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 225 | t = abs_ival; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 226 | while (t) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 227 | *p++ = (digit)(t & PyLong_MASK); |
| 228 | t >>= PyLong_SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 229 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 231 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 234 | /* Create a new long int object from a C unsigned long int */ |
| 235 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 236 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 237 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 238 | { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 239 | PyLongObject *v; |
| 240 | unsigned long t; |
| 241 | int ndigits = 0; |
| 242 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 243 | if (ival < PyLong_BASE) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 244 | return PyLong_FromLong(ival); |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 245 | /* Count the number of Python digits. */ |
| 246 | t = (unsigned long)ival; |
| 247 | while (t) { |
| 248 | ++ndigits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 249 | t >>= PyLong_SHIFT; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 250 | } |
| 251 | v = _PyLong_New(ndigits); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 252 | if (v != NULL) { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 253 | digit *p = v->ob_digit; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 254 | Py_SIZE(v) = ndigits; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 255 | while (ival) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 256 | *p++ = (digit)(ival & PyLong_MASK); |
| 257 | ival >>= PyLong_SHIFT; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 258 | } |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 259 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 260 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 263 | /* Create a new long int object from a C double */ |
| 264 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 265 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 266 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 267 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 268 | PyLongObject *v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 269 | double frac; |
| 270 | int i, ndig, expo, neg; |
| 271 | neg = 0; |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 272 | if (Py_IS_INFINITY(dval)) { |
Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 273 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 274 | "cannot convert float infinity to integer"); |
Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | } |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 277 | if (Py_IS_NAN(dval)) { |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 278 | PyErr_SetString(PyExc_ValueError, |
| 279 | "cannot convert float NaN to integer"); |
Christian Heimes | 386cd1e | 2008-01-15 02:01:20 +0000 | [diff] [blame] | 280 | return NULL; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 281 | } |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 282 | if (dval < 0.0) { |
| 283 | neg = 1; |
| 284 | dval = -dval; |
| 285 | } |
| 286 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 287 | if (expo <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 288 | return PyLong_FromLong(0L); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 289 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | v = _PyLong_New(ndig); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 291 | if (v == NULL) |
| 292 | return NULL; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 293 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 294 | for (i = ndig; --i >= 0; ) { |
| 295 | long bits = (long)frac; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 296 | v->ob_digit[i] = (digit) bits; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 297 | frac = frac - (double)bits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 298 | frac = ldexp(frac, PyLong_SHIFT); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 299 | } |
| 300 | if (neg) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 301 | Py_SIZE(v) = -(Py_SIZE(v)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 305 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 306 | * anything about what happens when a signed integer operation overflows, |
| 307 | * and some compilers think they're doing you a favor by being "clever" |
| 308 | * then. The bit pattern for the largest postive signed long is |
| 309 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 310 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 311 | * However, some other compilers warn about applying unary minus to an |
| 312 | * unsigned operand. Hence the weird "0-". |
| 313 | */ |
| 314 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 315 | #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN) |
| 316 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 317 | /* Get a C long int from a long int object. |
| 318 | Returns -1 and sets an error condition if overflow occurs. */ |
| 319 | |
| 320 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 321 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 322 | { |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 323 | /* This version by Tim Peters */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | register PyLongObject *v; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 325 | unsigned long x, prev; |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 326 | long res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 327 | Py_ssize_t i; |
| 328 | int sign; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 329 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 330 | |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 331 | *overflow = 0; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 332 | if (vv == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 334 | return -1; |
| 335 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 336 | |
| 337 | if (!PyLong_Check(vv)) { |
| 338 | PyNumberMethods *nb; |
| 339 | if ((nb = vv->ob_type->tp_as_number) == NULL || |
| 340 | nb->nb_int == NULL) { |
| 341 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 342 | return -1; |
| 343 | } |
| 344 | vv = (*nb->nb_int) (vv); |
| 345 | if (vv == NULL) |
| 346 | return -1; |
| 347 | do_decref = 1; |
| 348 | if (!PyLong_Check(vv)) { |
| 349 | Py_DECREF(vv); |
| 350 | PyErr_SetString(PyExc_TypeError, |
| 351 | "nb_int should return int object"); |
| 352 | return -1; |
| 353 | } |
| 354 | } |
| 355 | |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 356 | res = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 358 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 359 | |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 360 | switch (i) { |
| 361 | case -1: |
| 362 | res = -v->ob_digit[0]; |
| 363 | break; |
| 364 | case 0: |
| 365 | res = 0; |
| 366 | break; |
| 367 | case 1: |
| 368 | res = v->ob_digit[0]; |
| 369 | break; |
| 370 | default: |
| 371 | sign = 1; |
| 372 | x = 0; |
| 373 | if (i < 0) { |
| 374 | sign = -1; |
| 375 | i = -(i); |
| 376 | } |
| 377 | while (--i >= 0) { |
| 378 | prev = x; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 379 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 380 | if ((x >> PyLong_SHIFT) != prev) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 381 | *overflow = Py_SIZE(v) > 0 ? 1 : -1; |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 382 | goto exit; |
| 383 | } |
| 384 | } |
| 385 | /* Haven't lost any bits, but casting to long requires extra care |
| 386 | * (see comment above). |
| 387 | */ |
| 388 | if (x <= (unsigned long)LONG_MAX) { |
| 389 | res = (long)x * sign; |
| 390 | } |
| 391 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 392 | res = LONG_MIN; |
| 393 | } |
| 394 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 395 | *overflow = Py_SIZE(v) > 0 ? 1 : -1; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 396 | /* res is already set to -1 */ |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | exit: |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 400 | if (do_decref) { |
| 401 | Py_DECREF(vv); |
| 402 | } |
Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 403 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 406 | long |
| 407 | PyLong_AsLong(PyObject *obj) |
| 408 | { |
| 409 | int overflow; |
| 410 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 411 | if (overflow) { |
| 412 | /* XXX: could be cute and give a different |
| 413 | message for overflow == -1 */ |
| 414 | PyErr_SetString(PyExc_OverflowError, |
| 415 | "Python int too large to convert to C long"); |
| 416 | } |
| 417 | return result; |
| 418 | } |
| 419 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 420 | /* Get a Py_ssize_t from a long int object. |
| 421 | Returns -1 and sets an error condition if overflow occurs. */ |
| 422 | |
| 423 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 424 | PyLong_AsSsize_t(PyObject *vv) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 425 | register PyLongObject *v; |
| 426 | size_t x, prev; |
| 427 | Py_ssize_t i; |
| 428 | int sign; |
| 429 | |
| 430 | if (vv == NULL || !PyLong_Check(vv)) { |
| 431 | PyErr_BadInternalCall(); |
| 432 | return -1; |
| 433 | } |
| 434 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 435 | i = Py_SIZE(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 436 | switch (i) { |
| 437 | case -1: return -v->ob_digit[0]; |
| 438 | case 0: return 0; |
| 439 | case 1: return v->ob_digit[0]; |
| 440 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 441 | sign = 1; |
| 442 | x = 0; |
| 443 | if (i < 0) { |
| 444 | sign = -1; |
| 445 | i = -(i); |
| 446 | } |
| 447 | while (--i >= 0) { |
| 448 | prev = x; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 449 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 450 | if ((x >> PyLong_SHIFT) != prev) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 451 | goto overflow; |
| 452 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 453 | /* Haven't lost any bits, but casting to a signed type requires |
| 454 | * extra care (see comment above). |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 455 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 456 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 457 | return (Py_ssize_t)x * sign; |
| 458 | } |
| 459 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 460 | return PY_SSIZE_T_MIN; |
| 461 | } |
| 462 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 463 | |
| 464 | overflow: |
| 465 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 466 | "Python int too large to convert to C ssize_t"); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 467 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Guido van Rossum | d8c8048 | 2002-08-13 00:24:58 +0000 | [diff] [blame] | 470 | /* Get a C unsigned long int from a long int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 471 | Returns -1 and sets an error condition if overflow occurs. */ |
| 472 | |
| 473 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 474 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 475 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 476 | register PyLongObject *v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 477 | unsigned long x, prev; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 478 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | if (vv == NULL || !PyLong_Check(vv)) { |
| 481 | PyErr_BadInternalCall(); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 482 | return (unsigned long) -1; |
| 483 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 485 | i = Py_SIZE(v); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 486 | x = 0; |
| 487 | if (i < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 488 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 489 | "can't convert negative value to unsigned int"); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 490 | return (unsigned long) -1; |
| 491 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 492 | switch (i) { |
| 493 | case 0: return 0; |
| 494 | case 1: return v->ob_digit[0]; |
| 495 | } |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 496 | while (--i >= 0) { |
| 497 | prev = x; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 498 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 499 | if ((x >> PyLong_SHIFT) != prev) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 500 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 501 | "python int too large to convert to C unsigned long"); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 502 | return (unsigned long) -1; |
| 503 | } |
| 504 | } |
| 505 | return x; |
| 506 | } |
| 507 | |
| 508 | /* Get a C unsigned long int from a long int object. |
| 509 | Returns -1 and sets an error condition if overflow occurs. */ |
| 510 | |
| 511 | size_t |
| 512 | PyLong_AsSize_t(PyObject *vv) |
| 513 | { |
| 514 | register PyLongObject *v; |
| 515 | size_t x, prev; |
| 516 | Py_ssize_t i; |
| 517 | |
| 518 | if (vv == NULL || !PyLong_Check(vv)) { |
| 519 | PyErr_BadInternalCall(); |
| 520 | return (unsigned long) -1; |
| 521 | } |
| 522 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 523 | i = Py_SIZE(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 524 | x = 0; |
| 525 | if (i < 0) { |
| 526 | PyErr_SetString(PyExc_OverflowError, |
| 527 | "can't convert negative value to size_t"); |
| 528 | return (size_t) -1; |
| 529 | } |
| 530 | switch (i) { |
| 531 | case 0: return 0; |
| 532 | case 1: return v->ob_digit[0]; |
| 533 | } |
| 534 | while (--i >= 0) { |
| 535 | prev = x; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 536 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 537 | if ((x >> PyLong_SHIFT) != prev) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 538 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 539 | "Python int too large to convert to C size_t"); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 540 | return (unsigned long) -1; |
| 541 | } |
| 542 | } |
| 543 | return x; |
| 544 | } |
| 545 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 546 | /* Get a C unsigned long int from a long int object, ignoring the high bits. |
| 547 | Returns -1 and sets an error condition if an error occurs. */ |
| 548 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 549 | static unsigned long |
| 550 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 551 | { |
| 552 | register PyLongObject *v; |
| 553 | unsigned long x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 554 | Py_ssize_t i; |
| 555 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 556 | |
| 557 | if (vv == NULL || !PyLong_Check(vv)) { |
| 558 | PyErr_BadInternalCall(); |
| 559 | return (unsigned long) -1; |
| 560 | } |
| 561 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 562 | i = Py_SIZE(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 563 | switch (i) { |
| 564 | case 0: return 0; |
| 565 | case 1: return v->ob_digit[0]; |
| 566 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 567 | sign = 1; |
| 568 | x = 0; |
| 569 | if (i < 0) { |
| 570 | sign = -1; |
| 571 | i = -i; |
| 572 | } |
| 573 | while (--i >= 0) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 574 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 575 | } |
| 576 | return x * sign; |
| 577 | } |
| 578 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 579 | unsigned long |
| 580 | PyLong_AsUnsignedLongMask(register PyObject *op) |
| 581 | { |
| 582 | PyNumberMethods *nb; |
| 583 | PyLongObject *lo; |
| 584 | unsigned long val; |
| 585 | |
| 586 | if (op && PyLong_Check(op)) |
| 587 | return _PyLong_AsUnsignedLongMask(op); |
| 588 | |
| 589 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 590 | nb->nb_int == NULL) { |
| 591 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 592 | return (unsigned long)-1; |
| 593 | } |
| 594 | |
| 595 | lo = (PyLongObject*) (*nb->nb_int) (op); |
| 596 | if (lo == NULL) |
| 597 | return (unsigned long)-1; |
| 598 | if (PyLong_Check(lo)) { |
| 599 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 600 | Py_DECREF(lo); |
| 601 | if (PyErr_Occurred()) |
| 602 | return (unsigned long)-1; |
| 603 | return val; |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | Py_DECREF(lo); |
| 608 | PyErr_SetString(PyExc_TypeError, |
| 609 | "nb_int should return int object"); |
| 610 | return (unsigned long)-1; |
| 611 | } |
| 612 | } |
| 613 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 614 | int |
| 615 | _PyLong_Sign(PyObject *vv) |
| 616 | { |
| 617 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 618 | |
| 619 | assert(v != NULL); |
| 620 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 621 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 622 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 625 | size_t |
| 626 | _PyLong_NumBits(PyObject *vv) |
| 627 | { |
| 628 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 629 | size_t result = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 630 | Py_ssize_t ndigits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 631 | |
| 632 | assert(v != NULL); |
| 633 | assert(PyLong_Check(v)); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 634 | ndigits = ABS(Py_SIZE(v)); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 635 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 636 | if (ndigits > 0) { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 637 | digit msd = v->ob_digit[ndigits - 1]; |
| 638 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 639 | result = (ndigits - 1) * PyLong_SHIFT; |
| 640 | if (result / PyLong_SHIFT != (size_t)(ndigits - 1)) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 641 | goto Overflow; |
| 642 | do { |
| 643 | ++result; |
| 644 | if (result == 0) |
| 645 | goto Overflow; |
| 646 | msd >>= 1; |
| 647 | } while (msd); |
| 648 | } |
| 649 | return result; |
| 650 | |
| 651 | Overflow: |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 652 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 653 | "to express in a platform size_t"); |
| 654 | return (size_t)-1; |
| 655 | } |
| 656 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 657 | PyObject * |
| 658 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
| 659 | int little_endian, int is_signed) |
| 660 | { |
| 661 | const unsigned char* pstartbyte;/* LSB of bytes */ |
| 662 | int incr; /* direction to move pstartbyte */ |
| 663 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 664 | size_t numsignificantbytes; /* number of bytes that matter */ |
| 665 | size_t ndigits; /* number of Python long digits */ |
| 666 | PyLongObject* v; /* result */ |
| 667 | int idigit = 0; /* next free index in v->ob_digit */ |
| 668 | |
| 669 | if (n == 0) |
| 670 | return PyLong_FromLong(0L); |
| 671 | |
| 672 | if (little_endian) { |
| 673 | pstartbyte = bytes; |
| 674 | pendbyte = bytes + n - 1; |
| 675 | incr = 1; |
| 676 | } |
| 677 | else { |
| 678 | pstartbyte = bytes + n - 1; |
| 679 | pendbyte = bytes; |
| 680 | incr = -1; |
| 681 | } |
| 682 | |
| 683 | if (is_signed) |
| 684 | is_signed = *pendbyte >= 0x80; |
| 685 | |
| 686 | /* Compute numsignificantbytes. This consists of finding the most |
| 687 | significant byte. Leading 0 bytes are insignficant if the number |
| 688 | is positive, and leading 0xff bytes if negative. */ |
| 689 | { |
| 690 | size_t i; |
| 691 | const unsigned char* p = pendbyte; |
| 692 | const int pincr = -incr; /* search MSB to LSB */ |
| 693 | const unsigned char insignficant = is_signed ? 0xff : 0x00; |
| 694 | |
| 695 | for (i = 0; i < n; ++i, p += pincr) { |
| 696 | if (*p != insignficant) |
| 697 | break; |
| 698 | } |
| 699 | numsignificantbytes = n - i; |
| 700 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 701 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 702 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 703 | do for 0xffff = -0x0001. To be safe without bothering to |
| 704 | check every case, bump it regardless. */ |
| 705 | if (is_signed && numsignificantbytes < n) |
| 706 | ++numsignificantbytes; |
| 707 | } |
| 708 | |
| 709 | /* How many Python long digits do we need? We have |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 710 | 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 711 | bits, so it's the ceiling of the quotient. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 712 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 713 | if (ndigits > (size_t)INT_MAX) |
| 714 | return PyErr_NoMemory(); |
| 715 | v = _PyLong_New((int)ndigits); |
| 716 | if (v == NULL) |
| 717 | return NULL; |
| 718 | |
| 719 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 720 | the fly for signed numbers, and dealing with the mismatch between |
| 721 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 722 | { |
| 723 | size_t i; |
Tim Peters | f251d06 | 2001-06-13 21:09:15 +0000 | [diff] [blame] | 724 | twodigits carry = 1; /* for 2's-comp calculation */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 725 | twodigits accum = 0; /* sliding register */ |
| 726 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 727 | const unsigned char* p = pstartbyte; |
| 728 | |
| 729 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 730 | twodigits thisbyte = *p; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 731 | /* Compute correction for 2's comp, if needed. */ |
| 732 | if (is_signed) { |
| 733 | thisbyte = (0xff ^ thisbyte) + carry; |
| 734 | carry = thisbyte >> 8; |
| 735 | thisbyte &= 0xff; |
| 736 | } |
| 737 | /* Because we're going LSB to MSB, thisbyte is |
| 738 | more significant than what's already in accum, |
| 739 | so needs to be prepended to accum. */ |
| 740 | accum |= thisbyte << accumbits; |
| 741 | accumbits += 8; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 742 | if (accumbits >= PyLong_SHIFT) { |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 743 | /* There's enough to fill a Python digit. */ |
| 744 | assert(idigit < (int)ndigits); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 745 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 746 | ++idigit; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 747 | accum >>= PyLong_SHIFT; |
| 748 | accumbits -= PyLong_SHIFT; |
| 749 | assert(accumbits < PyLong_SHIFT); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 752 | assert(accumbits < PyLong_SHIFT); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 753 | if (accumbits) { |
| 754 | assert(idigit < (int)ndigits); |
| 755 | v->ob_digit[idigit] = (digit)accum; |
| 756 | ++idigit; |
| 757 | } |
| 758 | } |
| 759 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 760 | Py_SIZE(v) = is_signed ? -idigit : idigit; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 761 | return (PyObject *)long_normalize(v); |
| 762 | } |
| 763 | |
| 764 | int |
| 765 | _PyLong_AsByteArray(PyLongObject* v, |
| 766 | unsigned char* bytes, size_t n, |
| 767 | int little_endian, int is_signed) |
| 768 | { |
| 769 | int i; /* index into v->ob_digit */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 770 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 771 | twodigits accum; /* sliding register */ |
| 772 | unsigned int accumbits; /* # bits in accum */ |
| 773 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 774 | twodigits carry; /* for computing 2's-comp */ |
| 775 | size_t j; /* # bytes filled */ |
| 776 | unsigned char* p; /* pointer to next byte in bytes */ |
| 777 | int pincr; /* direction to move p */ |
| 778 | |
| 779 | assert(v != NULL && PyLong_Check(v)); |
| 780 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 781 | if (Py_SIZE(v) < 0) { |
| 782 | ndigits = -(Py_SIZE(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 783 | if (!is_signed) { |
| 784 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 785 | "can't convert negative int to unsigned"); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 786 | return -1; |
| 787 | } |
| 788 | do_twos_comp = 1; |
| 789 | } |
| 790 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 791 | ndigits = Py_SIZE(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 792 | do_twos_comp = 0; |
| 793 | } |
| 794 | |
| 795 | if (little_endian) { |
| 796 | p = bytes; |
| 797 | pincr = 1; |
| 798 | } |
| 799 | else { |
| 800 | p = bytes + n - 1; |
| 801 | pincr = -1; |
| 802 | } |
| 803 | |
Tim Peters | 898cf85 | 2001-06-13 20:50:08 +0000 | [diff] [blame] | 804 | /* Copy over all the Python digits. |
| 805 | It's crucial that every Python digit except for the MSD contribute |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 806 | exactly PyLong_SHIFT bits to the total, so first assert that the long is |
Tim Peters | 898cf85 | 2001-06-13 20:50:08 +0000 | [diff] [blame] | 807 | normalized. */ |
| 808 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 809 | j = 0; |
| 810 | accum = 0; |
| 811 | accumbits = 0; |
| 812 | carry = do_twos_comp ? 1 : 0; |
| 813 | for (i = 0; i < ndigits; ++i) { |
| 814 | twodigits thisdigit = v->ob_digit[i]; |
| 815 | if (do_twos_comp) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 816 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 817 | carry = thisdigit >> PyLong_SHIFT; |
| 818 | thisdigit &= PyLong_MASK; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 819 | } |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 820 | /* Because we're going LSB to MSB, thisdigit is more |
| 821 | significant than what's already in accum, so needs to be |
| 822 | prepended to accum. */ |
| 823 | accum |= thisdigit << accumbits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 824 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 825 | |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 826 | /* The most-significant digit may be (probably is) at least |
| 827 | partly empty. */ |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 828 | if (i == ndigits - 1) { |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 829 | /* Count # of sign bits -- they needn't be stored, |
| 830 | * although for signed conversion we need later to |
| 831 | * make sure at least one sign bit gets stored. |
| 832 | * First shift conceptual sign bit to real sign bit. |
| 833 | */ |
| 834 | stwodigits s = (stwodigits)(thisdigit << |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 835 | (8*sizeof(stwodigits) - PyLong_SHIFT)); |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 836 | unsigned int nsignbits = 0; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 837 | while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) { |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 838 | ++nsignbits; |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 839 | s <<= 1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 840 | } |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 841 | accumbits -= nsignbits; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 842 | } |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 843 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 844 | /* Store as many bytes as possible. */ |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 845 | while (accumbits >= 8) { |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 846 | if (j >= n) |
| 847 | goto Overflow; |
| 848 | ++j; |
| 849 | *p = (unsigned char)(accum & 0xff); |
| 850 | p += pincr; |
| 851 | accumbits -= 8; |
| 852 | accum >>= 8; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 853 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* Store the straggler (if any). */ |
| 857 | assert(accumbits < 8); |
| 858 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 859 | if (accumbits > 0) { |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 860 | if (j >= n) |
| 861 | goto Overflow; |
| 862 | ++j; |
| 863 | if (do_twos_comp) { |
| 864 | /* Fill leading bits of the byte with sign bits |
| 865 | (appropriately pretending that the long had an |
| 866 | infinite supply of sign bits). */ |
| 867 | accum |= (~(twodigits)0) << accumbits; |
| 868 | } |
| 869 | *p = (unsigned char)(accum & 0xff); |
| 870 | p += pincr; |
| 871 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 872 | else if (j == n && n > 0 && is_signed) { |
| 873 | /* The main loop filled the byte array exactly, so the code |
| 874 | just above didn't get to ensure there's a sign bit, and the |
| 875 | loop below wouldn't add one either. Make sure a sign bit |
| 876 | exists. */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 877 | unsigned char msb = *(p - pincr); |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 878 | int sign_bit_set = msb >= 0x80; |
| 879 | assert(accumbits == 0); |
| 880 | if (sign_bit_set == do_twos_comp) |
| 881 | return 0; |
| 882 | else |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 883 | goto Overflow; |
| 884 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 885 | |
| 886 | /* Fill remaining bytes with copies of the sign bit. */ |
| 887 | { |
| 888 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 889 | for ( ; j < n; ++j, p += pincr) |
| 890 | *p = signbyte; |
| 891 | } |
| 892 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 893 | return 0; |
| 894 | |
| 895 | Overflow: |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 896 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 897 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 898 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 901 | double |
| 902 | _PyLong_AsScaledDouble(PyObject *vv, int *exponent) |
| 903 | { |
| 904 | /* NBITS_WANTED should be > the number of bits in a double's precision, |
| 905 | but small enough so that 2**NBITS_WANTED is within the normal double |
| 906 | range. nbitsneeded is set to 1 less than that because the most-significant |
| 907 | Python digit contains at least 1 significant bit, but we don't want to |
| 908 | bother counting them (catering to the worst case cheaply). |
| 909 | |
| 910 | 57 is one more than VAX-D double precision; I (Tim) don't know of a double |
| 911 | format with more precision than that; it's 1 larger so that we add in at |
| 912 | least one round bit to stand in for the ignored least-significant bits. |
| 913 | */ |
| 914 | #define NBITS_WANTED 57 |
| 915 | PyLongObject *v; |
| 916 | double x; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 917 | const double multiplier = (double)(1L << PyLong_SHIFT); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 918 | Py_ssize_t i; |
| 919 | int sign; |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 920 | int nbitsneeded; |
| 921 | |
| 922 | if (vv == NULL || !PyLong_Check(vv)) { |
| 923 | PyErr_BadInternalCall(); |
| 924 | return -1; |
| 925 | } |
| 926 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 927 | i = Py_SIZE(v); |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 928 | sign = 1; |
| 929 | if (i < 0) { |
| 930 | sign = -1; |
| 931 | i = -(i); |
| 932 | } |
| 933 | else if (i == 0) { |
| 934 | *exponent = 0; |
| 935 | return 0.0; |
| 936 | } |
| 937 | --i; |
| 938 | x = (double)v->ob_digit[i]; |
| 939 | nbitsneeded = NBITS_WANTED - 1; |
| 940 | /* Invariant: i Python digits remain unaccounted for. */ |
| 941 | while (i > 0 && nbitsneeded > 0) { |
| 942 | --i; |
| 943 | x = x * multiplier + (double)v->ob_digit[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 944 | nbitsneeded -= PyLong_SHIFT; |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 945 | } |
| 946 | /* There are i digits we didn't shift in. Pretending they're all |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 947 | zeroes, the true value is x * 2**(i*PyLong_SHIFT). */ |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 948 | *exponent = i; |
| 949 | assert(x > 0.0); |
| 950 | return x * sign; |
| 951 | #undef NBITS_WANTED |
| 952 | } |
| 953 | |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 954 | /* Get a C double from a long int object. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 955 | |
| 956 | double |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 957 | PyLong_AsDouble(PyObject *vv) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 958 | { |
Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 959 | int e = -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 960 | double x; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 961 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 962 | if (vv == NULL || !PyLong_Check(vv)) { |
| 963 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 964 | return -1; |
| 965 | } |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 966 | x = _PyLong_AsScaledDouble(vv, &e); |
| 967 | if (x == -1.0 && PyErr_Occurred()) |
| 968 | return -1.0; |
Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 969 | /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be |
| 970 | set correctly after a successful _PyLong_AsScaledDouble() call */ |
| 971 | assert(e >= 0); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 972 | if (e > INT_MAX / PyLong_SHIFT) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 973 | goto overflow; |
| 974 | errno = 0; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 975 | x = ldexp(x, e * PyLong_SHIFT); |
Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 976 | if (Py_OVERFLOWED(x)) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 977 | goto overflow; |
| 978 | return x; |
| 979 | |
| 980 | overflow: |
| 981 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 982 | "Python int too large to convert to C double"); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 983 | return -1.0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 986 | /* Create a new long (or int) object from a C pointer */ |
| 987 | |
| 988 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 989 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 990 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 991 | #ifndef HAVE_LONG_LONG |
| 992 | # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 993 | #endif |
| 994 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 995 | # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 996 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 997 | /* special-case null pointer */ |
| 998 | if (!p) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 999 | return PyLong_FromLong(0); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1000 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* Get a C pointer from a long object (or an int object in some cases) */ |
| 1005 | |
| 1006 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1007 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1008 | { |
| 1009 | /* This function will allow int or long objects. If vv is neither, |
| 1010 | then the PyLong_AsLong*() functions will raise the exception: |
| 1011 | PyExc_SystemError, "bad argument to internal function" |
| 1012 | */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1013 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1014 | long x; |
| 1015 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1016 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1017 | x = PyLong_AsLong(vv); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1018 | else |
| 1019 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1020 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1021 | |
| 1022 | #ifndef HAVE_LONG_LONG |
| 1023 | # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 1024 | #endif |
| 1025 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1026 | # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1027 | #endif |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1028 | PY_LONG_LONG x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1029 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1030 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1031 | x = PyLong_AsLongLong(vv); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1032 | else |
| 1033 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1034 | |
| 1035 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1036 | |
| 1037 | if (x == -1 && PyErr_Occurred()) |
| 1038 | return NULL; |
| 1039 | return (void *)x; |
| 1040 | } |
| 1041 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1042 | #ifdef HAVE_LONG_LONG |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1043 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1044 | /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1045 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1046 | */ |
| 1047 | |
Tim Peters | cf37dfc | 2001-06-14 18:42:50 +0000 | [diff] [blame] | 1048 | #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1049 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1050 | /* Create a new long int object from a C PY_LONG_LONG int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1051 | |
| 1052 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1053 | PyLong_FromLongLong(PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1054 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1055 | PyLongObject *v; |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 1056 | unsigned PY_LONG_LONG abs_ival; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1057 | unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */ |
| 1058 | int ndigits = 0; |
| 1059 | int negative = 0; |
| 1060 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1061 | CHECK_SMALL_INT(ival); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1062 | if (ival < 0) { |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 1063 | /* avoid signed overflow on negation; see comments |
| 1064 | in PyLong_FromLong above. */ |
| 1065 | abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1066 | negative = 1; |
| 1067 | } |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 1068 | else { |
| 1069 | abs_ival = (unsigned PY_LONG_LONG)ival; |
| 1070 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* Count the number of Python digits. |
| 1073 | We used to pick 5 ("big enough for anything"), but that's a |
| 1074 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1075 | needed. */ |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 1076 | t = abs_ival; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1077 | while (t) { |
| 1078 | ++ndigits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1079 | t >>= PyLong_SHIFT; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1080 | } |
| 1081 | v = _PyLong_New(ndigits); |
| 1082 | if (v != NULL) { |
| 1083 | digit *p = v->ob_digit; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1084 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 1085 | t = abs_ival; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1086 | while (t) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1087 | *p++ = (digit)(t & PyLong_MASK); |
| 1088 | t >>= PyLong_SHIFT; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1094 | /* Create a new long int object from a C unsigned PY_LONG_LONG int. */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1095 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1096 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1097 | PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1098 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1099 | PyLongObject *v; |
| 1100 | unsigned PY_LONG_LONG t; |
| 1101 | int ndigits = 0; |
| 1102 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1103 | if (ival < PyLong_BASE) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1104 | return PyLong_FromLong(ival); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1105 | /* Count the number of Python digits. */ |
| 1106 | t = (unsigned PY_LONG_LONG)ival; |
| 1107 | while (t) { |
| 1108 | ++ndigits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1109 | t >>= PyLong_SHIFT; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1110 | } |
| 1111 | v = _PyLong_New(ndigits); |
| 1112 | if (v != NULL) { |
| 1113 | digit *p = v->ob_digit; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1114 | Py_SIZE(v) = ndigits; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1115 | while (ival) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1116 | *p++ = (digit)(ival & PyLong_MASK); |
| 1117 | ival >>= PyLong_SHIFT; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1123 | /* Create a new long int object from a C Py_ssize_t. */ |
| 1124 | |
| 1125 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1126 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1127 | { |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1128 | PyLongObject *v; |
| 1129 | size_t abs_ival; |
| 1130 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1131 | int ndigits = 0; |
| 1132 | int negative = 0; |
| 1133 | |
| 1134 | CHECK_SMALL_INT(ival); |
| 1135 | if (ival < 0) { |
| 1136 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1137 | abs_ival = (size_t)(-1-ival)+1; |
| 1138 | negative = 1; |
| 1139 | } |
| 1140 | else { |
| 1141 | abs_ival = (size_t)ival; |
| 1142 | } |
| 1143 | |
| 1144 | /* Count the number of Python digits. */ |
| 1145 | t = abs_ival; |
| 1146 | while (t) { |
| 1147 | ++ndigits; |
| 1148 | t >>= PyLong_SHIFT; |
| 1149 | } |
| 1150 | v = _PyLong_New(ndigits); |
| 1151 | if (v != NULL) { |
| 1152 | digit *p = v->ob_digit; |
| 1153 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1154 | t = abs_ival; |
| 1155 | while (t) { |
| 1156 | *p++ = (digit)(t & PyLong_MASK); |
| 1157 | t >>= PyLong_SHIFT; |
| 1158 | } |
| 1159 | } |
| 1160 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /* Create a new long int object from a C size_t. */ |
| 1164 | |
| 1165 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1166 | PyLong_FromSize_t(size_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1167 | { |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1168 | PyLongObject *v; |
| 1169 | size_t t; |
| 1170 | int ndigits = 0; |
| 1171 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1172 | if (ival < PyLong_BASE) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1173 | return PyLong_FromLong(ival); |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1174 | /* Count the number of Python digits. */ |
| 1175 | t = ival; |
| 1176 | while (t) { |
| 1177 | ++ndigits; |
| 1178 | t >>= PyLong_SHIFT; |
| 1179 | } |
| 1180 | v = _PyLong_New(ndigits); |
| 1181 | if (v != NULL) { |
| 1182 | digit *p = v->ob_digit; |
| 1183 | Py_SIZE(v) = ndigits; |
| 1184 | while (ival) { |
| 1185 | *p++ = (digit)(ival & PyLong_MASK); |
| 1186 | ival >>= PyLong_SHIFT; |
| 1187 | } |
| 1188 | } |
| 1189 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1192 | /* Get a C PY_LONG_LONG int from a long int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1193 | Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1194 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1195 | PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1196 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1197 | { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1198 | PyLongObject *v; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1199 | PY_LONG_LONG bytes; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1200 | int one = 1; |
| 1201 | int res; |
| 1202 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 1203 | if (vv == NULL) { |
| 1204 | PyErr_BadInternalCall(); |
| 1205 | return -1; |
| 1206 | } |
| 1207 | if (!PyLong_Check(vv)) { |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1208 | PyNumberMethods *nb; |
| 1209 | PyObject *io; |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1210 | if ((nb = vv->ob_type->tp_as_number) == NULL || |
| 1211 | nb->nb_int == NULL) { |
| 1212 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1213 | return -1; |
| 1214 | } |
| 1215 | io = (*nb->nb_int) (vv); |
| 1216 | if (io == NULL) |
| 1217 | return -1; |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1218 | if (PyLong_Check(io)) { |
| 1219 | bytes = PyLong_AsLongLong(io); |
| 1220 | Py_DECREF(io); |
| 1221 | return bytes; |
| 1222 | } |
| 1223 | Py_DECREF(io); |
| 1224 | PyErr_SetString(PyExc_TypeError, "integer conversion failed"); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1225 | return -1; |
| 1226 | } |
| 1227 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1228 | v = (PyLongObject*)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1229 | switch(Py_SIZE(v)) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1230 | case -1: return -v->ob_digit[0]; |
| 1231 | case 0: return 0; |
| 1232 | case 1: return v->ob_digit[0]; |
| 1233 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1234 | res = _PyLong_AsByteArray( |
| 1235 | (PyLongObject *)vv, (unsigned char *)&bytes, |
| 1236 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1237 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1238 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1239 | if (res < 0) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1240 | return (PY_LONG_LONG)-1; |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1241 | else |
| 1242 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1245 | /* Get a C unsigned PY_LONG_LONG int from a long int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1246 | Return -1 and set an error if overflow occurs. */ |
| 1247 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1248 | unsigned PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1249 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1250 | { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1251 | PyLongObject *v; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1252 | unsigned PY_LONG_LONG bytes; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1253 | int one = 1; |
| 1254 | int res; |
| 1255 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1256 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1257 | PyErr_BadInternalCall(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1258 | return (unsigned PY_LONG_LONG)-1; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1261 | v = (PyLongObject*)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1262 | switch(Py_SIZE(v)) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1263 | case 0: return 0; |
| 1264 | case 1: return v->ob_digit[0]; |
| 1265 | } |
| 1266 | |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1267 | res = _PyLong_AsByteArray( |
| 1268 | (PyLongObject *)vv, (unsigned char *)&bytes, |
| 1269 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1270 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1271 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1272 | if (res < 0) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1273 | return (unsigned PY_LONG_LONG)res; |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1274 | else |
| 1275 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1276 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1277 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1278 | /* Get a C unsigned long int from a long int object, ignoring the high bits. |
| 1279 | Returns -1 and sets an error condition if an error occurs. */ |
| 1280 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1281 | static unsigned PY_LONG_LONG |
| 1282 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1283 | { |
| 1284 | register PyLongObject *v; |
| 1285 | unsigned PY_LONG_LONG x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1286 | Py_ssize_t i; |
| 1287 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1290 | PyErr_BadInternalCall(); |
| 1291 | return (unsigned long) -1; |
| 1292 | } |
| 1293 | v = (PyLongObject *)vv; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1294 | switch(Py_SIZE(v)) { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1295 | case 0: return 0; |
| 1296 | case 1: return v->ob_digit[0]; |
| 1297 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1298 | i = Py_SIZE(v); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1299 | sign = 1; |
| 1300 | x = 0; |
| 1301 | if (i < 0) { |
| 1302 | sign = -1; |
| 1303 | i = -i; |
| 1304 | } |
| 1305 | while (--i >= 0) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1306 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1307 | } |
| 1308 | return x * sign; |
| 1309 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1310 | |
| 1311 | unsigned PY_LONG_LONG |
| 1312 | PyLong_AsUnsignedLongLongMask(register PyObject *op) |
| 1313 | { |
| 1314 | PyNumberMethods *nb; |
| 1315 | PyLongObject *lo; |
| 1316 | unsigned PY_LONG_LONG val; |
| 1317 | |
| 1318 | if (op && PyLong_Check(op)) |
| 1319 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1320 | |
| 1321 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 1322 | nb->nb_int == NULL) { |
| 1323 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1324 | return (unsigned PY_LONG_LONG)-1; |
| 1325 | } |
| 1326 | |
| 1327 | lo = (PyLongObject*) (*nb->nb_int) (op); |
| 1328 | if (lo == NULL) |
| 1329 | return (unsigned PY_LONG_LONG)-1; |
| 1330 | if (PyLong_Check(lo)) { |
| 1331 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1332 | Py_DECREF(lo); |
| 1333 | if (PyErr_Occurred()) |
| 1334 | return (unsigned PY_LONG_LONG)-1; |
| 1335 | return val; |
| 1336 | } |
| 1337 | else |
| 1338 | { |
| 1339 | Py_DECREF(lo); |
| 1340 | PyErr_SetString(PyExc_TypeError, |
| 1341 | "nb_int should return int object"); |
| 1342 | return (unsigned PY_LONG_LONG)-1; |
| 1343 | } |
| 1344 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1345 | #undef IS_LITTLE_ENDIAN |
| 1346 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1347 | #endif /* HAVE_LONG_LONG */ |
| 1348 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 1349 | #define CHECK_BINOP(v,w) \ |
| 1350 | if (!PyLong_Check(v) || !PyLong_Check(w)) { \ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1351 | Py_INCREF(Py_NotImplemented); \ |
| 1352 | return Py_NotImplemented; \ |
| 1353 | } |
| 1354 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1355 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1356 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1357 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1358 | */ |
| 1359 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1360 | v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1361 | { |
| 1362 | int i; |
| 1363 | digit carry = 0; |
| 1364 | |
| 1365 | assert(m >= n); |
| 1366 | for (i = 0; i < n; ++i) { |
| 1367 | carry += x[i] + y[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1368 | x[i] = carry & PyLong_MASK; |
| 1369 | carry >>= PyLong_SHIFT; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1370 | assert((carry & 1) == carry); |
| 1371 | } |
| 1372 | for (; carry && i < m; ++i) { |
| 1373 | carry += x[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1374 | x[i] = carry & PyLong_MASK; |
| 1375 | carry >>= PyLong_SHIFT; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1376 | assert((carry & 1) == carry); |
| 1377 | } |
| 1378 | return carry; |
| 1379 | } |
| 1380 | |
| 1381 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1382 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1383 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1384 | */ |
| 1385 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1386 | v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1387 | { |
| 1388 | int i; |
| 1389 | digit borrow = 0; |
| 1390 | |
| 1391 | assert(m >= n); |
| 1392 | for (i = 0; i < n; ++i) { |
| 1393 | borrow = x[i] - y[i] - borrow; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1394 | x[i] = borrow & PyLong_MASK; |
| 1395 | borrow >>= PyLong_SHIFT; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1396 | borrow &= 1; /* keep only 1 sign bit */ |
| 1397 | } |
| 1398 | for (; borrow && i < m; ++i) { |
| 1399 | borrow = x[i] - borrow; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1400 | x[i] = borrow & PyLong_MASK; |
| 1401 | borrow >>= PyLong_SHIFT; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1402 | borrow &= 1; |
| 1403 | } |
| 1404 | return borrow; |
| 1405 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1406 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1407 | /* Multiply by a single digit, ignoring the sign. */ |
| 1408 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1409 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1410 | mul1(PyLongObject *a, wdigit n) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1411 | { |
| 1412 | return muladd1(a, n, (digit)0); |
| 1413 | } |
| 1414 | |
| 1415 | /* Multiply by a single digit and add a single digit, ignoring the sign. */ |
| 1416 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1417 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1418 | muladd1(PyLongObject *a, wdigit n, wdigit extra) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1419 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1420 | Py_ssize_t size_a = ABS(Py_SIZE(a)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1421 | PyLongObject *z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1422 | twodigits carry = extra; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1423 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1424 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1425 | if (z == NULL) |
| 1426 | return NULL; |
| 1427 | for (i = 0; i < size_a; ++i) { |
| 1428 | carry += (twodigits)a->ob_digit[i] * n; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1429 | z->ob_digit[i] = (digit) (carry & PyLong_MASK); |
| 1430 | carry >>= PyLong_SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1431 | } |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1432 | z->ob_digit[i] = (digit) carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1433 | return long_normalize(z); |
| 1434 | } |
| 1435 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1436 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1437 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1438 | It's OK for pin == pout on entry, which saves oodles of mallocs/frees in |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1439 | _PyLong_Format, but that should be done with great care since longs are |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1440 | immutable. */ |
| 1441 | |
| 1442 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1443 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1444 | { |
| 1445 | twodigits rem = 0; |
| 1446 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1447 | assert(n > 0 && n <= PyLong_MASK); |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1448 | pin += size; |
| 1449 | pout += size; |
| 1450 | while (--size >= 0) { |
| 1451 | digit hi; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1452 | rem = (rem << PyLong_SHIFT) + *--pin; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1453 | *--pout = hi = (digit)(rem / n); |
| 1454 | rem -= hi * n; |
| 1455 | } |
| 1456 | return (digit)rem; |
| 1457 | } |
| 1458 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1459 | /* Divide a long integer by a digit, returning both the quotient |
| 1460 | (as function result) and the remainder (through *prem). |
| 1461 | The sign of a is ignored; n should not be zero. */ |
| 1462 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1463 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1464 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1465 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1466 | const Py_ssize_t size = ABS(Py_SIZE(a)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1467 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1468 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1469 | assert(n > 0 && n <= PyLong_MASK); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1470 | z = _PyLong_New(size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1471 | if (z == NULL) |
| 1472 | return NULL; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1473 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1474 | return long_normalize(z); |
| 1475 | } |
| 1476 | |
| 1477 | /* Convert a long int object to a string, using a given conversion base. |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 1478 | Return a string object. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1479 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1480 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1481 | PyObject * |
| 1482 | _PyLong_Format(PyObject *aa, int base) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1483 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1484 | register PyLongObject *a = (PyLongObject *)aa; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1485 | PyObject *str; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1486 | Py_ssize_t i, j, sz; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1487 | Py_ssize_t size_a; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1488 | Py_UNICODE *p; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1489 | int bits; |
| 1490 | char sign = '\0'; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1491 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1492 | if (a == NULL || !PyLong_Check(a)) { |
| 1493 | PyErr_BadInternalCall(); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1494 | return NULL; |
| 1495 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1496 | assert(base >= 2 && base <= 36); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1497 | size_a = ABS(Py_SIZE(a)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1498 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1499 | /* Compute a rough upper bound for the length of the string */ |
| 1500 | i = base; |
| 1501 | bits = 0; |
| 1502 | while (i > 1) { |
| 1503 | ++bits; |
| 1504 | i >>= 1; |
| 1505 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1506 | i = 5; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1507 | j = size_a*PyLong_SHIFT + bits-1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1508 | sz = i + j / bits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1509 | if (j / PyLong_SHIFT < size_a || sz < i) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1510 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1511 | "int is too large to format"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1512 | return NULL; |
| 1513 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1514 | str = PyUnicode_FromUnicode(NULL, sz); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1515 | if (str == NULL) |
| 1516 | return NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1517 | p = PyUnicode_AS_UNICODE(str) + sz; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1518 | *p = '\0'; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1519 | if (Py_SIZE(a) < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1520 | sign = '-'; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1521 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1522 | if (Py_SIZE(a) == 0) { |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1523 | *--p = '0'; |
| 1524 | } |
| 1525 | else if ((base & (base - 1)) == 0) { |
| 1526 | /* JRH: special case for power-of-2 bases */ |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1527 | twodigits accum = 0; |
| 1528 | int accumbits = 0; /* # of bits in accum */ |
| 1529 | int basebits = 1; /* # of bits in base-1 */ |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1530 | i = base; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1531 | while ((i >>= 1) > 1) |
| 1532 | ++basebits; |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1533 | |
| 1534 | for (i = 0; i < size_a; ++i) { |
Tim Peters | 0d2d87d | 2002-08-20 19:00:22 +0000 | [diff] [blame] | 1535 | accum |= (twodigits)a->ob_digit[i] << accumbits; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1536 | accumbits += PyLong_SHIFT; |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1537 | assert(accumbits >= basebits); |
| 1538 | do { |
Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 1539 | char cdigit = (char)(accum & (base - 1)); |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 1540 | cdigit += (cdigit < 10) ? '0' : 'a'-10; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1541 | assert(p > PyUnicode_AS_UNICODE(str)); |
Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 1542 | *--p = cdigit; |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1543 | accumbits -= basebits; |
| 1544 | accum >>= basebits; |
| 1545 | } while (i < size_a-1 ? accumbits >= basebits : |
| 1546 | accum > 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1547 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1548 | } |
| 1549 | else { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1550 | /* Not 0, and base not a power of 2. Divide repeatedly by |
| 1551 | base, but for speed use the highest power of base that |
| 1552 | fits in a digit. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1553 | Py_ssize_t size = size_a; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1554 | digit *pin = a->ob_digit; |
| 1555 | PyLongObject *scratch; |
| 1556 | /* powbasw <- largest power of base that fits in a digit. */ |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1557 | digit powbase = base; /* powbase == base ** power */ |
| 1558 | int power = 1; |
| 1559 | for (;;) { |
| 1560 | unsigned long newpow = powbase * (unsigned long)base; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1561 | if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */ |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | powbase = (digit)newpow; |
| 1564 | ++power; |
| 1565 | } |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* Get a scratch area for repeated division. */ |
| 1568 | scratch = _PyLong_New(size); |
| 1569 | if (scratch == NULL) { |
| 1570 | Py_DECREF(str); |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | |
| 1574 | /* Repeatedly divide by powbase. */ |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1575 | do { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1576 | int ntostore = power; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1577 | digit rem = inplace_divrem1(scratch->ob_digit, |
| 1578 | pin, size, powbase); |
| 1579 | pin = scratch->ob_digit; /* no need to use a again */ |
| 1580 | if (pin[size - 1] == 0) |
| 1581 | --size; |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1582 | SIGCHECK({ |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1583 | Py_DECREF(scratch); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1584 | Py_DECREF(str); |
| 1585 | return NULL; |
| 1586 | }) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1587 | |
| 1588 | /* Break rem into digits. */ |
Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 1589 | assert(ntostore > 0); |
| 1590 | do { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1591 | digit nextrem = (digit)(rem / base); |
| 1592 | char c = (char)(rem - nextrem * base); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1593 | assert(p > PyUnicode_AS_UNICODE(str)); |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 1594 | c += (c < 10) ? '0' : 'a'-10; |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1595 | *--p = c; |
| 1596 | rem = nextrem; |
Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 1597 | --ntostore; |
| 1598 | /* Termination is a bit delicate: must not |
| 1599 | store leading zeroes, so must get out if |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1600 | remaining quotient and rem are both 0. */ |
| 1601 | } while (ntostore && (size || rem)); |
| 1602 | } while (size != 0); |
| 1603 | Py_DECREF(scratch); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1606 | if (base == 16) { |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 1607 | *--p = 'x'; |
| 1608 | *--p = '0'; |
| 1609 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1610 | else if (base == 8) { |
| 1611 | *--p = 'o'; |
| 1612 | *--p = '0'; |
| 1613 | } |
| 1614 | else if (base == 2) { |
| 1615 | *--p = 'b'; |
| 1616 | *--p = '0'; |
| 1617 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1618 | else if (base != 10) { |
| 1619 | *--p = '#'; |
| 1620 | *--p = '0' + base%10; |
| 1621 | if (base > 10) |
| 1622 | *--p = '0' + base/10; |
| 1623 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1624 | if (sign) |
| 1625 | *--p = sign; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1626 | if (p != PyUnicode_AS_UNICODE(str)) { |
| 1627 | Py_UNICODE *q = PyUnicode_AS_UNICODE(str); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1628 | assert(p > q); |
| 1629 | do { |
| 1630 | } while ((*q++ = *p++) != '\0'); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1631 | q--; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1632 | if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) { |
| 1633 | Py_DECREF(str); |
| 1634 | return NULL; |
| 1635 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1636 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1637 | return (PyObject *)str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1640 | /* Table of digit values for 8-bit string -> integer conversion. |
| 1641 | * '0' maps to 0, ..., '9' maps to 9. |
| 1642 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 1643 | * All other indices map to 37. |
| 1644 | * Note that when converting a base B string, a char c is a legitimate |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1645 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1646 | */ |
| 1647 | int _PyLong_DigitValue[256] = { |
| 1648 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1649 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1650 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1651 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 1652 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1653 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1654 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1655 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1656 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1657 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1658 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1659 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1660 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1661 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1662 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1663 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1664 | }; |
| 1665 | |
| 1666 | /* *str points to the first digit in a string of base `base` digits. base |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1667 | * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first |
| 1668 | * non-digit (which may be *str!). A normalized long is returned. |
| 1669 | * The point to this routine is that it takes time linear in the number of |
| 1670 | * string characters. |
| 1671 | */ |
| 1672 | static PyLongObject * |
| 1673 | long_from_binary_base(char **str, int base) |
| 1674 | { |
| 1675 | char *p = *str; |
| 1676 | char *start = p; |
| 1677 | int bits_per_char; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1678 | Py_ssize_t n; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1679 | PyLongObject *z; |
| 1680 | twodigits accum; |
| 1681 | int bits_in_accum; |
| 1682 | digit *pdigit; |
| 1683 | |
| 1684 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 1685 | n = base; |
| 1686 | for (bits_per_char = -1; n; ++bits_per_char) |
| 1687 | n >>= 1; |
| 1688 | /* n <- total # of bits needed, while setting p to end-of-string */ |
| 1689 | n = 0; |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1690 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1691 | ++p; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1692 | *str = p; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1693 | /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ |
| 1694 | n = (p - start) * bits_per_char + PyLong_SHIFT - 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1695 | if (n / bits_per_char < p - start) { |
Tim Peters | 1a3b19a | 2003-02-02 17:33:53 +0000 | [diff] [blame] | 1696 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1697 | "int string too large to convert"); |
Tim Peters | 1a3b19a | 2003-02-02 17:33:53 +0000 | [diff] [blame] | 1698 | return NULL; |
| 1699 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1700 | n = n / PyLong_SHIFT; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1701 | z = _PyLong_New(n); |
| 1702 | if (z == NULL) |
| 1703 | return NULL; |
| 1704 | /* Read string from right, and fill in long from left; i.e., |
| 1705 | * from least to most significant in both. |
| 1706 | */ |
| 1707 | accum = 0; |
| 1708 | bits_in_accum = 0; |
| 1709 | pdigit = z->ob_digit; |
| 1710 | while (--p >= start) { |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1711 | int k = _PyLong_DigitValue[Py_CHARMASK(*p)]; |
Tim Peters | c7bc0b9 | 2003-05-05 20:39:43 +0000 | [diff] [blame] | 1712 | assert(k >= 0 && k < base); |
| 1713 | accum |= (twodigits)(k << bits_in_accum); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1714 | bits_in_accum += bits_per_char; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1715 | if (bits_in_accum >= PyLong_SHIFT) { |
| 1716 | *pdigit++ = (digit)(accum & PyLong_MASK); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1717 | assert(pdigit - z->ob_digit <= (int)n); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1718 | accum >>= PyLong_SHIFT; |
| 1719 | bits_in_accum -= PyLong_SHIFT; |
| 1720 | assert(bits_in_accum < PyLong_SHIFT); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | if (bits_in_accum) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1724 | assert(bits_in_accum <= PyLong_SHIFT); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1725 | *pdigit++ = (digit)accum; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1726 | assert(pdigit - z->ob_digit <= (int)n); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1727 | } |
| 1728 | while (pdigit - z->ob_digit < n) |
| 1729 | *pdigit++ = 0; |
| 1730 | return long_normalize(z); |
| 1731 | } |
| 1732 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1733 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1734 | PyLong_FromString(char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1735 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1736 | int sign = 1, error_if_nonzero = 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1737 | char *start, *orig_str = str; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1738 | PyLongObject *z = NULL; |
Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 1739 | PyObject *strobj; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1740 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1741 | |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 1742 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1743 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1744 | "int() arg 2 must be >= 2 and <= 36"); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1745 | return NULL; |
| 1746 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1747 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1748 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1749 | if (*str == '+') |
| 1750 | ++str; |
| 1751 | else if (*str == '-') { |
| 1752 | ++str; |
| 1753 | sign = -1; |
| 1754 | } |
| 1755 | if (base == 0) { |
| 1756 | if (str[0] != '0') |
| 1757 | base = 10; |
| 1758 | else if (str[1] == 'x' || str[1] == 'X') |
| 1759 | base = 16; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1760 | else if (str[1] == 'o' || str[1] == 'O') |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1761 | base = 8; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1762 | else if (str[1] == 'b' || str[1] == 'B') |
| 1763 | base = 2; |
| 1764 | else { |
| 1765 | /* "old" (C-style) octal literal, now invalid. |
| 1766 | it might still be zero though */ |
| 1767 | error_if_nonzero = 1; |
| 1768 | base = 10; |
| 1769 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1770 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1771 | if (str[0] == '0' && |
| 1772 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 1773 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
| 1774 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1775 | str += 2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1776 | |
Guido van Rossum | e676297 | 1998-06-22 03:54:46 +0000 | [diff] [blame] | 1777 | start = str; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1778 | if ((base & (base - 1)) == 0) |
| 1779 | z = long_from_binary_base(&str, base); |
| 1780 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1781 | /*** |
| 1782 | Binary bases can be converted in time linear in the number of digits, because |
| 1783 | Python's representation base is binary. Other bases (including decimal!) use |
| 1784 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1785 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1786 | First some math: the largest integer that can be expressed in N base-B digits |
| 1787 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 1788 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 1789 | |
| 1790 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 1791 | BASE**n >= B**N [taking logs to base BASE] |
| 1792 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 1793 | |
| 1794 | The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute |
| 1795 | this quickly. A Python long with that much space is reserved near the start, |
| 1796 | and the result is computed into it. |
| 1797 | |
| 1798 | The input string is actually treated as being in base base**i (i.e., i digits |
| 1799 | are processed at a time), where two more static arrays hold: |
| 1800 | |
| 1801 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 1802 | convmultmax_base[base] = base ** convwidth_base[base] |
| 1803 | |
| 1804 | The first of these is the largest i such that i consecutive input digits |
| 1805 | must fit in a single Python digit. The second is effectively the input |
| 1806 | base we're really using. |
| 1807 | |
| 1808 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 1809 | convmultmax_base[base], the result is "simply" |
| 1810 | |
| 1811 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 1812 | |
| 1813 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1814 | |
| 1815 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 1816 | case |
| 1817 | |
| 1818 | n >= N * log(B)/log(BASE) |
| 1819 | |
| 1820 | where `N` is the number of input digits in base `B`. This is computed via |
| 1821 | |
| 1822 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 1823 | |
| 1824 | below. Two numeric concerns are how much space this can waste, and whether |
| 1825 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 1826 | which is the default (and it's unlikely anyone changes that). |
| 1827 | |
| 1828 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 1829 | between the worst-case input with N digits and the smallest input with N |
| 1830 | digits is about a factor of B, but B is small compared to BASE so at most |
| 1831 | one allocated Python digit can remain unused on that count. If |
| 1832 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 1833 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 1834 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 1835 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 1836 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 1837 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 1838 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 1839 | |
| 1840 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 1841 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 1842 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 1843 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 1844 | close can a transcendental function get to an integer over some range?" |
| 1845 | questions are generally theoretically intractable. Computer analysis via |
| 1846 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 1847 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 1848 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 1849 | we can get very close to being in trouble, but very rarely. For example, |
| 1850 | 76573 is a denominator in one of the continued-fraction approximations to |
| 1851 | log(10)/log(2**15), and indeed: |
| 1852 | |
| 1853 | >>> log(10)/log(2**15)*76573 |
| 1854 | 16958.000000654003 |
| 1855 | |
| 1856 | is very close to an integer. If we were working with IEEE single-precision, |
| 1857 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 1858 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 1859 | Instead the code checks to see whether the allocated space is enough as each |
| 1860 | new Python digit is added, and copies the whole thing to a larger long if not. |
| 1861 | This should happen extremely rarely, and in fact I don't have a test case |
| 1862 | that triggers it(!). Instead the code was tested by artificially allocating |
| 1863 | just 1 digit at the start, so that the copying code was exercised for every |
| 1864 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1865 | ***/ |
| 1866 | register twodigits c; /* current input character */ |
| 1867 | Py_ssize_t size_z; |
| 1868 | int i; |
| 1869 | int convwidth; |
| 1870 | twodigits convmultmax, convmult; |
| 1871 | digit *pz, *pzstop; |
| 1872 | char* scan; |
| 1873 | |
| 1874 | static double log_base_BASE[37] = {0.0e0,}; |
| 1875 | static int convwidth_base[37] = {0,}; |
| 1876 | static twodigits convmultmax_base[37] = {0,}; |
| 1877 | |
| 1878 | if (log_base_BASE[base] == 0.0) { |
| 1879 | twodigits convmax = base; |
| 1880 | int i = 1; |
| 1881 | |
| 1882 | log_base_BASE[base] = log((double)base) / |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1883 | log((double)PyLong_BASE); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1884 | for (;;) { |
| 1885 | twodigits next = convmax * base; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1886 | if (next > PyLong_BASE) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1887 | break; |
| 1888 | convmax = next; |
| 1889 | ++i; |
| 1890 | } |
| 1891 | convmultmax_base[base] = convmax; |
| 1892 | assert(i > 0); |
| 1893 | convwidth_base[base] = i; |
| 1894 | } |
| 1895 | |
| 1896 | /* Find length of the string of numeric characters. */ |
| 1897 | scan = str; |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1898 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1899 | ++scan; |
| 1900 | |
| 1901 | /* Create a long object that can contain the largest possible |
| 1902 | * integer with this base and length. Note that there's no |
| 1903 | * need to initialize z->ob_digit -- no slot is read up before |
| 1904 | * being stored into. |
| 1905 | */ |
| 1906 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1907 | /* Uncomment next line to test exceedingly rare copy code */ |
| 1908 | /* size_z = 1; */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1909 | assert(size_z > 0); |
| 1910 | z = _PyLong_New(size_z); |
| 1911 | if (z == NULL) |
| 1912 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1913 | Py_SIZE(z) = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1914 | |
| 1915 | /* `convwidth` consecutive input digits are treated as a single |
| 1916 | * digit in base `convmultmax`. |
| 1917 | */ |
| 1918 | convwidth = convwidth_base[base]; |
| 1919 | convmultmax = convmultmax_base[base]; |
| 1920 | |
| 1921 | /* Work ;-) */ |
| 1922 | while (str < scan) { |
| 1923 | /* grab up to convwidth digits from the input string */ |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1924 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1925 | for (i = 1; i < convwidth && str != scan; ++i, ++str) { |
| 1926 | c = (twodigits)(c * base + |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1927 | _PyLong_DigitValue[Py_CHARMASK(*str)]); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1928 | assert(c < PyLong_BASE); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | convmult = convmultmax; |
| 1932 | /* Calculate the shift only if we couldn't get |
| 1933 | * convwidth digits. |
| 1934 | */ |
| 1935 | if (i != convwidth) { |
| 1936 | convmult = base; |
| 1937 | for ( ; i > 1; --i) |
| 1938 | convmult *= base; |
| 1939 | } |
| 1940 | |
| 1941 | /* Multiply z by convmult, and add c. */ |
| 1942 | pz = z->ob_digit; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1943 | pzstop = pz + Py_SIZE(z); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1944 | for (; pz < pzstop; ++pz) { |
| 1945 | c += (twodigits)*pz * convmult; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1946 | *pz = (digit)(c & PyLong_MASK); |
| 1947 | c >>= PyLong_SHIFT; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1948 | } |
| 1949 | /* carry off the current end? */ |
| 1950 | if (c) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1951 | assert(c < PyLong_BASE); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1952 | if (Py_SIZE(z) < size_z) { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1953 | *pz = (digit)c; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1954 | ++Py_SIZE(z); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1955 | } |
| 1956 | else { |
| 1957 | PyLongObject *tmp; |
| 1958 | /* Extremely rare. Get more space. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1959 | assert(Py_SIZE(z) == size_z); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1960 | tmp = _PyLong_New(size_z + 1); |
| 1961 | if (tmp == NULL) { |
| 1962 | Py_DECREF(z); |
| 1963 | return NULL; |
| 1964 | } |
| 1965 | memcpy(tmp->ob_digit, |
| 1966 | z->ob_digit, |
| 1967 | sizeof(digit) * size_z); |
| 1968 | Py_DECREF(z); |
| 1969 | z = tmp; |
| 1970 | z->ob_digit[size_z] = (digit)c; |
| 1971 | ++size_z; |
| 1972 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1973 | } |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1974 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1975 | } |
Guido van Rossum | ac6a37a | 1998-08-04 15:04:06 +0000 | [diff] [blame] | 1976 | if (z == NULL) |
| 1977 | return NULL; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1978 | if (error_if_nonzero) { |
| 1979 | /* reset the base to 0, else the exception message |
| 1980 | doesn't make too much sense */ |
| 1981 | base = 0; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1982 | if (Py_SIZE(z) != 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1983 | goto onError; |
| 1984 | /* there might still be other problems, therefore base |
| 1985 | remains zero here for the same reason */ |
| 1986 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1987 | if (str == start) |
| 1988 | goto onError; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1989 | if (sign < 0) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1990 | Py_SIZE(z) = -(Py_SIZE(z)); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1991 | if (*str == 'L' || *str == 'l') |
| 1992 | str++; |
| 1993 | while (*str && isspace(Py_CHARMASK(*str))) |
| 1994 | str++; |
| 1995 | if (*str != '\0') |
| 1996 | goto onError; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1997 | if (pend) |
| 1998 | *pend = str; |
Martin v. Löwis | 029656f | 2008-06-30 04:06:08 +0000 | [diff] [blame] | 1999 | long_normalize(z); |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 2000 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2001 | |
| 2002 | onError: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2003 | Py_XDECREF(z); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2004 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 2005 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2006 | if (strobj == NULL) |
| 2007 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2008 | PyErr_Format(PyExc_ValueError, |
Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 2009 | "invalid literal for int() with base %d: %R", |
| 2010 | base, strobj); |
| 2011 | Py_DECREF(strobj); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2012 | return NULL; |
| 2013 | } |
| 2014 | |
| 2015 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2016 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2017 | { |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 2018 | PyObject *result; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2019 | char *buffer = (char *)PyMem_MALLOC(length+1); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2020 | |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 2021 | if (buffer == NULL) |
| 2022 | return NULL; |
| 2023 | |
| 2024 | if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) { |
| 2025 | PyMem_FREE(buffer); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2026 | return NULL; |
| 2027 | } |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 2028 | result = PyLong_FromString(buffer, NULL, base); |
| 2029 | PyMem_FREE(buffer); |
| 2030 | return result; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2033 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2034 | static PyLongObject *x_divrem |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2035 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 2036 | static PyObject *long_long(PyObject *v); |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2037 | static int long_divrem(PyLongObject *, PyLongObject *, |
| 2038 | PyLongObject **, PyLongObject **); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2039 | |
| 2040 | /* Long division with remainder, top-level routine */ |
| 2041 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2042 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2043 | long_divrem(PyLongObject *a, PyLongObject *b, |
| 2044 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2045 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2046 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2047 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2048 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2049 | if (size_b == 0) { |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_ZeroDivisionError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2051 | "integer division or modulo by zero"); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2052 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2053 | } |
| 2054 | if (size_a < size_b || |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 2055 | (size_a == size_b && |
| 2056 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2057 | /* |a| < |b|. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2058 | *pdiv = (PyLongObject*)PyLong_FromLong(0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2059 | if (*pdiv == NULL) |
| 2060 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2061 | Py_INCREF(a); |
| 2062 | *prem = (PyLongObject *) a; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2063 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2064 | } |
| 2065 | if (size_b == 1) { |
| 2066 | digit rem = 0; |
| 2067 | z = divrem1(a, b->ob_digit[0], &rem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2068 | if (z == NULL) |
| 2069 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2070 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2071 | if (*prem == NULL) { |
| 2072 | Py_DECREF(z); |
| 2073 | return -1; |
| 2074 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2075 | } |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2076 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2077 | z = x_divrem(a, b, prem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2078 | if (z == NULL) |
| 2079 | return -1; |
| 2080 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2081 | /* Set the signs. |
| 2082 | The quotient z has the sign of a*b; |
| 2083 | the remainder r has the sign of a, |
| 2084 | so a = b*z + r. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2085 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2086 | NEGATE(z); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2087 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2088 | NEGATE(*prem); |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 2089 | *pdiv = maybe_small_long(z); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2090 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2093 | /* Unsigned long division with remainder -- the algorithm */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2094 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2095 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2096 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2097 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2098 | Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1)); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2099 | digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2100 | PyLongObject *v = mul1(v1, d); |
| 2101 | PyLongObject *w = mul1(w1, d); |
| 2102 | PyLongObject *a; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2103 | Py_ssize_t j, k; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2104 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2105 | if (v == NULL || w == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2106 | Py_XDECREF(v); |
| 2107 | Py_XDECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2108 | return NULL; |
| 2109 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2110 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2111 | assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2112 | assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */ |
| 2113 | assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2114 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2115 | size_v = ABS(Py_SIZE(v)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2116 | k = size_v - size_w; |
| 2117 | a = _PyLong_New(k + 1); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2118 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2119 | for (j = size_v; a != NULL && k >= 0; --j, --k) { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2120 | digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; |
| 2121 | twodigits q; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2122 | stwodigits carry = 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2123 | int i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2124 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2125 | SIGCHECK({ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2126 | Py_DECREF(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2127 | a = NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2128 | break; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2129 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2130 | if (vj == w->ob_digit[size_w-1]) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2131 | q = PyLong_MASK; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2132 | else |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2133 | q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) / |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2134 | w->ob_digit[size_w-1]; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2135 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2136 | while (w->ob_digit[size_w-2]*q > |
| 2137 | (( |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2138 | ((twodigits)vj << PyLong_SHIFT) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2139 | + v->ob_digit[j-1] |
| 2140 | - q*w->ob_digit[size_w-1] |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2141 | ) << PyLong_SHIFT) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2142 | + v->ob_digit[j-2]) |
| 2143 | --q; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2144 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2145 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 2146 | twodigits z = w->ob_digit[i] * q; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2147 | digit zz = (digit) (z >> PyLong_SHIFT); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2148 | carry += v->ob_digit[i+k] - z |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2149 | + ((twodigits)zz << PyLong_SHIFT); |
| 2150 | v->ob_digit[i+k] = (digit)(carry & PyLong_MASK); |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2151 | carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2152 | carry, PyLong_SHIFT); |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2153 | carry -= zz; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2154 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2155 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2156 | if (i+k < size_v) { |
| 2157 | carry += v->ob_digit[i+k]; |
| 2158 | v->ob_digit[i+k] = 0; |
| 2159 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2160 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2161 | if (carry == 0) |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 2162 | a->ob_digit[k] = (digit) q; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2163 | else { |
| 2164 | assert(carry == -1); |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 2165 | a->ob_digit[k] = (digit) q-1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2166 | carry = 0; |
| 2167 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 2168 | carry += v->ob_digit[i+k] + w->ob_digit[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2169 | v->ob_digit[i+k] = (digit)(carry & PyLong_MASK); |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2170 | carry = Py_ARITHMETIC_RIGHT_SHIFT( |
| 2171 | BASE_TWODIGITS_TYPE, |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2172 | carry, PyLong_SHIFT); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | } /* for j, k */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2176 | |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 2177 | if (a == NULL) |
| 2178 | *prem = NULL; |
| 2179 | else { |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2180 | a = long_normalize(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2181 | *prem = divrem1(v, d, &d); |
| 2182 | /* d receives the (unused) remainder */ |
| 2183 | if (*prem == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2184 | Py_DECREF(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2185 | a = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2186 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2187 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2188 | Py_DECREF(v); |
| 2189 | Py_DECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2190 | return a; |
| 2191 | } |
| 2192 | |
| 2193 | /* Methods */ |
| 2194 | |
| 2195 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2196 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2197 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2198 | Py_TYPE(v)->tp_free(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2201 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2202 | long_repr(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2203 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2204 | return _PyLong_Format(v, 10); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2208 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2209 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2210 | Py_ssize_t sign; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2211 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2212 | if (Py_SIZE(a) != Py_SIZE(b)) { |
| 2213 | if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2214 | sign = 0; |
| 2215 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2216 | sign = Py_SIZE(a) - Py_SIZE(b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2217 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2218 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2219 | Py_ssize_t i = ABS(Py_SIZE(a)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2220 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2221 | ; |
| 2222 | if (i < 0) |
| 2223 | sign = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 2224 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2225 | sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2226 | if (Py_SIZE(a) < 0) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 2227 | sign = -sign; |
| 2228 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2229 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2230 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2233 | static PyObject * |
| 2234 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 2235 | { |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2236 | PyObject *result; |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2237 | CHECK_BINOP(self, other); |
| 2238 | result = Py_CmpToRich(op, long_compare((PyLongObject*)self, |
| 2239 | (PyLongObject*)other)); |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2240 | return result; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2243 | static long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2244 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2245 | { |
| 2246 | long x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2247 | Py_ssize_t i; |
| 2248 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* This is designed so that Python ints and longs with the |
| 2251 | same value hash to the same value, otherwise comparisons |
| 2252 | of mapping keys will turn out weird */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2253 | i = Py_SIZE(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2254 | switch(i) { |
| 2255 | case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0]; |
| 2256 | case 0: return 0; |
| 2257 | case 1: return v->ob_digit[0]; |
| 2258 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2259 | sign = 1; |
| 2260 | x = 0; |
| 2261 | if (i < 0) { |
| 2262 | sign = -1; |
| 2263 | i = -(i); |
| 2264 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2265 | #define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT) |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 2266 | /* The following loop produces a C long x such that (unsigned long)x |
| 2267 | is congruent to the absolute value of v modulo ULONG_MAX. The |
| 2268 | resulting x is nonzero if and only if v is. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2269 | while (--i >= 0) { |
Neal Norwitz | d5a65a7 | 2003-02-23 23:11:41 +0000 | [diff] [blame] | 2270 | /* Force a native long #-bits (32 or 64) circular shift */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2271 | x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2272 | x += v->ob_digit[i]; |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 2273 | /* If the addition above overflowed (thinking of x as |
| 2274 | unsigned), we compensate by incrementing. This preserves |
| 2275 | the value modulo ULONG_MAX. */ |
| 2276 | if ((unsigned long)x < v->ob_digit[i]) |
| 2277 | x++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2278 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2279 | #undef LONG_BIT_PyLong_SHIFT |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2280 | x = x * sign; |
| 2281 | if (x == -1) |
| 2282 | x = -2; |
| 2283 | return x; |
| 2284 | } |
| 2285 | |
| 2286 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2287 | /* Add the absolute values of two long integers. */ |
| 2288 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2289 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2290 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2291 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2292 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2293 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2294 | int i; |
| 2295 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2296 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2297 | /* Ensure a is the larger of the two: */ |
| 2298 | if (size_a < size_b) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2299 | { PyLongObject *temp = a; a = b; b = temp; } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2300 | { Py_ssize_t size_temp = size_a; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2301 | size_a = size_b; |
| 2302 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2303 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2304 | z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2305 | if (z == NULL) |
| 2306 | return NULL; |
| 2307 | for (i = 0; i < size_b; ++i) { |
| 2308 | carry += a->ob_digit[i] + b->ob_digit[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2309 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2310 | carry >>= PyLong_SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2311 | } |
| 2312 | for (; i < size_a; ++i) { |
| 2313 | carry += a->ob_digit[i]; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2314 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2315 | carry >>= PyLong_SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2316 | } |
| 2317 | z->ob_digit[i] = carry; |
| 2318 | return long_normalize(z); |
| 2319 | } |
| 2320 | |
| 2321 | /* Subtract the absolute values of two integers. */ |
| 2322 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2323 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2324 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2325 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2326 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2327 | PyLongObject *z; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2328 | Py_ssize_t i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2329 | int sign = 1; |
| 2330 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2331 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2332 | /* Ensure a is the larger of the two: */ |
| 2333 | if (size_a < size_b) { |
| 2334 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2335 | { PyLongObject *temp = a; a = b; b = temp; } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2336 | { Py_ssize_t size_temp = size_a; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2337 | size_a = size_b; |
| 2338 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2339 | } |
| 2340 | else if (size_a == size_b) { |
| 2341 | /* Find highest digit where a and b differ: */ |
| 2342 | i = size_a; |
| 2343 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2344 | ; |
| 2345 | if (i < 0) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 2346 | return (PyLongObject *)PyLong_FromLong(0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2347 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 2348 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2349 | { PyLongObject *temp = a; a = b; b = temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2350 | } |
| 2351 | size_a = size_b = i+1; |
| 2352 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2353 | z = _PyLong_New(size_a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2354 | if (z == NULL) |
| 2355 | return NULL; |
| 2356 | for (i = 0; i < size_b; ++i) { |
| 2357 | /* The following assumes unsigned arithmetic |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2358 | works module 2**N for some N>PyLong_SHIFT. */ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2359 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2360 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2361 | borrow >>= PyLong_SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2362 | borrow &= 1; /* Keep only one sign bit */ |
| 2363 | } |
| 2364 | for (; i < size_a; ++i) { |
| 2365 | borrow = a->ob_digit[i] - borrow; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2366 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2367 | borrow >>= PyLong_SHIFT; |
Tim Peters | 43f04a3 | 2000-07-08 02:26:47 +0000 | [diff] [blame] | 2368 | borrow &= 1; /* Keep only one sign bit */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2369 | } |
| 2370 | assert(borrow == 0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2371 | if (sign < 0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2372 | NEGATE(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2373 | return long_normalize(z); |
| 2374 | } |
| 2375 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2376 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2377 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2378 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2379 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2380 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2381 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2382 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2383 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2384 | PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) + |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2385 | MEDIUM_VALUE(b)); |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2386 | return result; |
| 2387 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2388 | if (Py_SIZE(a) < 0) { |
| 2389 | if (Py_SIZE(b) < 0) { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2390 | z = x_add(a, b); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2391 | if (z != NULL && Py_SIZE(z) != 0) |
| 2392 | Py_SIZE(z) = -(Py_SIZE(z)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2393 | } |
| 2394 | else |
| 2395 | z = x_sub(b, a); |
| 2396 | } |
| 2397 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2398 | if (Py_SIZE(b) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2399 | z = x_sub(a, b); |
| 2400 | else |
| 2401 | z = x_add(a, b); |
| 2402 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2403 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2406 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2407 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2408 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2409 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2410 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2411 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2412 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2413 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2414 | PyObject* r; |
| 2415 | r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2416 | return r; |
| 2417 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2418 | if (Py_SIZE(a) < 0) { |
| 2419 | if (Py_SIZE(b) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2420 | z = x_sub(a, b); |
| 2421 | else |
| 2422 | z = x_add(a, b); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2423 | if (z != NULL && Py_SIZE(z) != 0) |
| 2424 | Py_SIZE(z) = -(Py_SIZE(z)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2425 | } |
| 2426 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2427 | if (Py_SIZE(b) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2428 | z = x_add(a, b); |
| 2429 | else |
| 2430 | z = x_sub(a, b); |
| 2431 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2432 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2435 | /* Grade school multiplication, ignoring the signs. |
| 2436 | * Returns the absolute value of the product, or NULL if error. |
| 2437 | */ |
| 2438 | static PyLongObject * |
| 2439 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2440 | { |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2441 | PyLongObject *z; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2442 | Py_ssize_t size_a = ABS(Py_SIZE(a)); |
| 2443 | Py_ssize_t size_b = ABS(Py_SIZE(b)); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2444 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2445 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2446 | z = _PyLong_New(size_a + size_b); |
| 2447 | if (z == NULL) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2448 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2449 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2450 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2451 | if (a == b) { |
| 2452 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 2453 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 2454 | * Gives slightly less than a 2x speedup when a == b, |
| 2455 | * via exploiting that each entry in the multiplication |
| 2456 | * pyramid appears twice (except for the size_a squares). |
| 2457 | */ |
| 2458 | for (i = 0; i < size_a; ++i) { |
| 2459 | twodigits carry; |
| 2460 | twodigits f = a->ob_digit[i]; |
| 2461 | digit *pz = z->ob_digit + (i << 1); |
| 2462 | digit *pa = a->ob_digit + i + 1; |
| 2463 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2464 | |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2465 | SIGCHECK({ |
| 2466 | Py_DECREF(z); |
| 2467 | return NULL; |
| 2468 | }) |
| 2469 | |
| 2470 | carry = *pz + f * f; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2471 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2472 | carry >>= PyLong_SHIFT; |
| 2473 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2474 | |
| 2475 | /* Now f is added in twice in each column of the |
| 2476 | * pyramid it appears. Same as adding f<<1 once. |
| 2477 | */ |
| 2478 | f <<= 1; |
| 2479 | while (pa < paend) { |
| 2480 | carry += *pz + *pa++ * f; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2481 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2482 | carry >>= PyLong_SHIFT; |
| 2483 | assert(carry <= (PyLong_MASK << 1)); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2484 | } |
| 2485 | if (carry) { |
| 2486 | carry += *pz; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2487 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2488 | carry >>= PyLong_SHIFT; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2489 | } |
| 2490 | if (carry) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2491 | *pz += (digit)(carry & PyLong_MASK); |
| 2492 | assert((carry >> PyLong_SHIFT) == 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2493 | } |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2494 | } |
| 2495 | else { /* a is not the same as b -- gradeschool long mult */ |
| 2496 | for (i = 0; i < size_a; ++i) { |
| 2497 | twodigits carry = 0; |
| 2498 | twodigits f = a->ob_digit[i]; |
| 2499 | digit *pz = z->ob_digit + i; |
| 2500 | digit *pb = b->ob_digit; |
| 2501 | digit *pbend = b->ob_digit + size_b; |
| 2502 | |
| 2503 | SIGCHECK({ |
| 2504 | Py_DECREF(z); |
| 2505 | return NULL; |
| 2506 | }) |
| 2507 | |
| 2508 | while (pb < pbend) { |
| 2509 | carry += *pz + *pb++ * f; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2510 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2511 | carry >>= PyLong_SHIFT; |
| 2512 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2513 | } |
| 2514 | if (carry) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2515 | *pz += (digit)(carry & PyLong_MASK); |
| 2516 | assert((carry >> PyLong_SHIFT) == 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2517 | } |
| 2518 | } |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2519 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* A helper for Karatsuba multiplication (k_mul). |
| 2523 | Takes a long "n" and an integer "size" representing the place to |
| 2524 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 2525 | viewing the shift as being by digits. The sign bit is ignored, and |
| 2526 | the return values are >= 0. |
| 2527 | Returns 0 on success, -1 on failure. |
| 2528 | */ |
| 2529 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2530 | kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2531 | { |
| 2532 | PyLongObject *hi, *lo; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2533 | Py_ssize_t size_lo, size_hi; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2534 | const Py_ssize_t size_n = ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2535 | |
| 2536 | size_lo = MIN(size_n, size); |
| 2537 | size_hi = size_n - size_lo; |
| 2538 | |
| 2539 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 2540 | return -1; |
| 2541 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 2542 | Py_DECREF(hi); |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 2547 | memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)); |
| 2548 | |
| 2549 | *high = long_normalize(hi); |
| 2550 | *low = long_normalize(lo); |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2554 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 2555 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2556 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 2557 | * absolute value of the product (or NULL if error). |
| 2558 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 2559 | */ |
| 2560 | static PyLongObject * |
| 2561 | k_mul(PyLongObject *a, PyLongObject *b) |
| 2562 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2563 | Py_ssize_t asize = ABS(Py_SIZE(a)); |
| 2564 | Py_ssize_t bsize = ABS(Py_SIZE(b)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2565 | PyLongObject *ah = NULL; |
| 2566 | PyLongObject *al = NULL; |
| 2567 | PyLongObject *bh = NULL; |
| 2568 | PyLongObject *bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2569 | PyLongObject *ret = NULL; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2570 | PyLongObject *t1, *t2, *t3; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2571 | Py_ssize_t shift; /* the number of digits we split off */ |
| 2572 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2573 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2574 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 2575 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 2576 | * Then the original product is |
Tim Peters | 18c15b9 | 2002-08-12 02:43:58 +0000 | [diff] [blame] | 2577 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2578 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 2579 | * been reduced to 3 multiplies on numbers half the size. |
| 2580 | */ |
| 2581 | |
Tim Peters | fc07e56 | 2002-08-12 02:54:10 +0000 | [diff] [blame] | 2582 | /* We want to split based on the larger number; fiddle so that b |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2583 | * is largest. |
| 2584 | */ |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2585 | if (asize > bsize) { |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2586 | t1 = a; |
| 2587 | a = b; |
| 2588 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2589 | |
| 2590 | i = asize; |
| 2591 | asize = bsize; |
| 2592 | bsize = i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | /* Use gradeschool math when either number is too small. */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2596 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 2597 | if (asize <= i) { |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2598 | if (asize == 0) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 2599 | return (PyLongObject *)PyLong_FromLong(0); |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2600 | else |
| 2601 | return x_mul(a, b); |
| 2602 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2603 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2604 | /* If a is small compared to b, splitting on b gives a degenerate |
| 2605 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 2606 | * than "grade school" then. However, we can still win, by viewing |
| 2607 | * b as a string of "big digits", each of width a->ob_size. That |
| 2608 | * leads to a sequence of balanced calls to k_mul. |
| 2609 | */ |
| 2610 | if (2 * asize <= bsize) |
| 2611 | return k_lopsided_mul(a, b); |
| 2612 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2613 | /* Split a & b into hi & lo pieces. */ |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2614 | shift = bsize >> 1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2615 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2616 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2617 | |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2618 | if (a == b) { |
| 2619 | bh = ah; |
| 2620 | bl = al; |
| 2621 | Py_INCREF(bh); |
| 2622 | Py_INCREF(bl); |
| 2623 | } |
| 2624 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2625 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2626 | /* The plan: |
| 2627 | * 1. Allocate result space (asize + bsize digits: that's always |
| 2628 | * enough). |
| 2629 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 2630 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 2631 | * can't overlap with #2. |
| 2632 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 2633 | * underflow (borrow out of the high digit), but we don't care: |
| 2634 | * we're effectively doing unsigned arithmetic mod |
| 2635 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 2636 | * borrows and carries out of the high digit can be ignored. |
| 2637 | * 5. Subtract ah*bh from the result, starting at shift. |
| 2638 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 2639 | * at shift. |
| 2640 | */ |
| 2641 | |
| 2642 | /* 1. Allocate result space. */ |
Tim Peters | 70b041b | 2002-08-12 19:38:01 +0000 | [diff] [blame] | 2643 | ret = _PyLong_New(asize + bsize); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2644 | if (ret == NULL) goto fail; |
| 2645 | #ifdef Py_DEBUG |
| 2646 | /* Fill with trash, to catch reference to uninitialized digits. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2647 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2648 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2649 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2650 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2651 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2652 | assert(Py_SIZE(t1) >= 0); |
| 2653 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2654 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2655 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2656 | |
| 2657 | /* Zero-out the digits higher than the ah*bh copy. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2658 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2659 | if (i) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2660 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2661 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2662 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2663 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2664 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 2665 | Py_DECREF(t1); |
| 2666 | goto fail; |
| 2667 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2668 | assert(Py_SIZE(t2) >= 0); |
| 2669 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 2670 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2671 | |
| 2672 | /* Zero out remaining digits. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2673 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2674 | if (i) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2675 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2676 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2677 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 2678 | * because it's fresher in cache. |
| 2679 | */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2680 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 2681 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2682 | Py_DECREF(t2); |
| 2683 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2684 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2685 | Py_DECREF(t1); |
| 2686 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2687 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2688 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 2689 | Py_DECREF(ah); |
| 2690 | Py_DECREF(al); |
| 2691 | ah = al = NULL; |
| 2692 | |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2693 | if (a == b) { |
| 2694 | t2 = t1; |
| 2695 | Py_INCREF(t2); |
| 2696 | } |
| 2697 | else if ((t2 = x_add(bh, bl)) == NULL) { |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2698 | Py_DECREF(t1); |
| 2699 | goto fail; |
| 2700 | } |
| 2701 | Py_DECREF(bh); |
| 2702 | Py_DECREF(bl); |
| 2703 | bh = bl = NULL; |
| 2704 | |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2705 | t3 = k_mul(t1, t2); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2706 | Py_DECREF(t1); |
| 2707 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2708 | if (t3 == NULL) goto fail; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2709 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2710 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2711 | /* Add t3. It's not obvious why we can't run out of room here. |
| 2712 | * See the (*) comment after this function. |
Tim Peters | d8b2173 | 2002-08-12 19:30:26 +0000 | [diff] [blame] | 2713 | */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2714 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2715 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2716 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2717 | return long_normalize(ret); |
| 2718 | |
| 2719 | fail: |
| 2720 | Py_XDECREF(ret); |
| 2721 | Py_XDECREF(ah); |
| 2722 | Py_XDECREF(al); |
| 2723 | Py_XDECREF(bh); |
| 2724 | Py_XDECREF(bl); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2725 | return NULL; |
| 2726 | } |
| 2727 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2728 | /* (*) Why adding t3 can't "run out of room" above. |
| 2729 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2730 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 2731 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2732 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2733 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 2734 | bsize = c(bsize/2) + f(bsize/2). |
| 2735 | 2. shift = f(bsize/2) |
| 2736 | 3. asize <= bsize |
| 2737 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 2738 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2739 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2740 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 2741 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 2742 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 2743 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2744 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2745 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 2746 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2747 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2748 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 2749 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 2750 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2751 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2752 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2753 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2754 | c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2755 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2756 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 2757 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 2758 | the question reduces to whether asize digits is enough to hold |
| 2759 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 2760 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 2761 | asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1 |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2762 | digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2763 | asize == bsize, then we're asking whether bsize digits is enough to hold |
Tim Peters | e417de0 | 2002-08-15 20:10:45 +0000 | [diff] [blame] | 2764 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 2765 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 2766 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 2767 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 2768 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 2769 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 2770 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2771 | */ |
| 2772 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2773 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 2774 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 2775 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 2776 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 2777 | * also cache-friendly (we compute one double-width slice of the result |
| 2778 | * at a time, then move on, never bactracking except for the helpful |
| 2779 | * single-width slice overlap between successive partial sums). |
| 2780 | */ |
| 2781 | static PyLongObject * |
| 2782 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 2783 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2784 | const Py_ssize_t asize = ABS(Py_SIZE(a)); |
| 2785 | Py_ssize_t bsize = ABS(Py_SIZE(b)); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2786 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2787 | PyLongObject *ret; |
| 2788 | PyLongObject *bslice = NULL; |
| 2789 | |
| 2790 | assert(asize > KARATSUBA_CUTOFF); |
| 2791 | assert(2 * asize <= bsize); |
| 2792 | |
| 2793 | /* Allocate result space, and zero it out. */ |
| 2794 | ret = _PyLong_New(asize + bsize); |
| 2795 | if (ret == NULL) |
| 2796 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2797 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2798 | |
| 2799 | /* Successive slices of b are copied into bslice. */ |
Tim Peters | 1203403 | 2002-08-12 22:10:00 +0000 | [diff] [blame] | 2800 | bslice = _PyLong_New(asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2801 | if (bslice == NULL) |
| 2802 | goto fail; |
| 2803 | |
| 2804 | nbdone = 0; |
| 2805 | while (bsize > 0) { |
| 2806 | PyLongObject *product; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2807 | const Py_ssize_t nbtouse = MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2808 | |
| 2809 | /* Multiply the next slice of b by a. */ |
| 2810 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 2811 | nbtouse * sizeof(digit)); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2812 | Py_SIZE(bslice) = nbtouse; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2813 | product = k_mul(a, bslice); |
| 2814 | if (product == NULL) |
| 2815 | goto fail; |
| 2816 | |
| 2817 | /* Add into result. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2818 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 2819 | product->ob_digit, Py_SIZE(product)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2820 | Py_DECREF(product); |
| 2821 | |
| 2822 | bsize -= nbtouse; |
| 2823 | nbdone += nbtouse; |
| 2824 | } |
| 2825 | |
| 2826 | Py_DECREF(bslice); |
| 2827 | return long_normalize(ret); |
| 2828 | |
| 2829 | fail: |
| 2830 | Py_DECREF(ret); |
| 2831 | Py_XDECREF(bslice); |
| 2832 | return NULL; |
| 2833 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2834 | |
| 2835 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2836 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2837 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2838 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2839 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2840 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2841 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2842 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2843 | PyObject *r; |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2844 | r = PyLong_FromLong(MEDIUM_VALUE(a)*MEDIUM_VALUE(b)); |
Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2845 | return r; |
| 2846 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2847 | |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2848 | z = k_mul(a, b); |
Tim Peters | 9973d74 | 2002-08-15 19:41:06 +0000 | [diff] [blame] | 2849 | /* Negate if exactly one of the inputs is negative. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2850 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2851 | NEGATE(z); |
Tim Peters | 9973d74 | 2002-08-15 19:41:06 +0000 | [diff] [blame] | 2852 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2855 | /* The / and % operators are now defined in terms of divmod(). |
| 2856 | The expression a mod b has the value a - b*floor(a/b). |
| 2857 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2858 | |a| by |b|, with the sign of a. This is also expressed |
| 2859 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 2860 | Some examples: |
| 2861 | a b a rem b a mod b |
| 2862 | 13 10 3 3 |
| 2863 | -13 10 -3 7 |
| 2864 | 13 -10 3 -7 |
| 2865 | -13 -10 -3 -3 |
| 2866 | So, to get from rem to mod, we have to add b if a and b |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2867 | have different signs. We then subtract one from the 'div' |
| 2868 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2869 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2870 | /* Compute |
| 2871 | * *pdiv, *pmod = divmod(v, w) |
| 2872 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 2873 | * the result is simply thrown away. The caller owns a reference to |
| 2874 | * each of these it requests (does not pass NULL for). |
| 2875 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2876 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2877 | l_divmod(PyLongObject *v, PyLongObject *w, |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2878 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2879 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2880 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2881 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2882 | if (long_divrem(v, w, &div, &mod) < 0) |
| 2883 | return -1; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2884 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 2885 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2886 | PyLongObject *temp; |
| 2887 | PyLongObject *one; |
| 2888 | temp = (PyLongObject *) long_add(mod, w); |
| 2889 | Py_DECREF(mod); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2890 | mod = temp; |
| 2891 | if (mod == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2892 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2893 | return -1; |
| 2894 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2895 | one = (PyLongObject *) PyLong_FromLong(1L); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2896 | if (one == NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2897 | (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
| 2898 | Py_DECREF(mod); |
| 2899 | Py_DECREF(div); |
| 2900 | Py_XDECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2901 | return -1; |
| 2902 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2903 | Py_DECREF(one); |
| 2904 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2905 | div = temp; |
| 2906 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2907 | if (pdiv != NULL) |
| 2908 | *pdiv = div; |
| 2909 | else |
| 2910 | Py_DECREF(div); |
| 2911 | |
| 2912 | if (pmod != NULL) |
| 2913 | *pmod = mod; |
| 2914 | else |
| 2915 | Py_DECREF(mod); |
| 2916 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2917 | return 0; |
| 2918 | } |
| 2919 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2920 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2921 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2922 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2923 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2924 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2925 | CHECK_BINOP(a, b); |
| 2926 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2927 | div = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2928 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2931 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2932 | long_true_divide(PyObject *a, PyObject *b) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 2933 | { |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2934 | double ad, bd; |
Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 2935 | int failed, aexp = -1, bexp = -1; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2936 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2937 | CHECK_BINOP(a, b); |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2938 | ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp); |
| 2939 | bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp); |
Tim Peters | 6f97e49 | 2001-11-04 23:09:40 +0000 | [diff] [blame] | 2940 | failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred(); |
Tim Peters | 6f97e49 | 2001-11-04 23:09:40 +0000 | [diff] [blame] | 2941 | if (failed) |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2942 | return NULL; |
Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 2943 | /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x, |
| 2944 | but should really be set correctly after sucessful calls to |
| 2945 | _PyLong_AsScaledDouble() */ |
| 2946 | assert(aexp >= 0 && bexp >= 0); |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2947 | |
| 2948 | if (bd == 0.0) { |
| 2949 | PyErr_SetString(PyExc_ZeroDivisionError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2950 | "int division or modulo by zero"); |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2951 | return NULL; |
| 2952 | } |
| 2953 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2954 | /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */ |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2955 | ad /= bd; /* overflow/underflow impossible here */ |
| 2956 | aexp -= bexp; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2957 | if (aexp > INT_MAX / PyLong_SHIFT) |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2958 | goto overflow; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2959 | else if (aexp < -(INT_MAX / PyLong_SHIFT)) |
Tim Peters | e56ed9b | 2001-09-06 21:59:14 +0000 | [diff] [blame] | 2960 | return PyFloat_FromDouble(0.0); /* underflow to 0 */ |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2961 | errno = 0; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2962 | ad = ldexp(ad, aexp * PyLong_SHIFT); |
Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 2963 | if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */ |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2964 | goto overflow; |
| 2965 | return PyFloat_FromDouble(ad); |
| 2966 | |
| 2967 | overflow: |
| 2968 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2969 | "int/int too large for a float"); |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2970 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2971 | |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2975 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2976 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2977 | PyLongObject *mod; |
| 2978 | |
| 2979 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2980 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2981 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2982 | mod = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2983 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2986 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2987 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2988 | { |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2989 | PyLongObject *div, *mod; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2990 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2991 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2992 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2993 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2994 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2995 | return NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2996 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2997 | z = PyTuple_New(2); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2998 | if (z != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2999 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 3000 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3001 | } |
| 3002 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3003 | Py_DECREF(div); |
| 3004 | Py_DECREF(mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3005 | } |
| 3006 | return z; |
| 3007 | } |
| 3008 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3009 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3010 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3011 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3012 | { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3013 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 3014 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3015 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3016 | PyLongObject *z = NULL; /* accumulated result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3017 | Py_ssize_t i, j, k; /* counters */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3018 | PyLongObject *temp = NULL; |
| 3019 | |
| 3020 | /* 5-ary values. If the exponent is large enough, table is |
| 3021 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 3022 | */ |
| 3023 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 3024 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
| 3025 | |
| 3026 | /* a, b, c = v, w, x */ |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3027 | CHECK_BINOP(v, w); |
| 3028 | a = (PyLongObject*)v; Py_INCREF(a); |
| 3029 | b = (PyLongObject*)w; Py_INCREF(b); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3030 | if (PyLong_Check(x)) { |
| 3031 | c = (PyLongObject *)x; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3032 | Py_INCREF(x); |
| 3033 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3034 | else if (x == Py_None) |
| 3035 | c = NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3036 | else { |
| 3037 | Py_DECREF(a); |
| 3038 | Py_DECREF(b); |
| 3039 | Py_INCREF(Py_NotImplemented); |
| 3040 | return Py_NotImplemented; |
| 3041 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3042 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3043 | if (Py_SIZE(b) < 0) { /* if exponent is negative */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3044 | if (c) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3045 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3046 | "cannot be negative when 3rd argument specified"); |
Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3047 | goto Error; |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 3048 | } |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 3049 | else { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3050 | /* else return a float. This works because we know |
| 3051 | that this calls float_pow() which converts its |
| 3052 | arguments to double. */ |
| 3053 | Py_DECREF(a); |
| 3054 | Py_DECREF(b); |
| 3055 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 3056 | } |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 3057 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3058 | |
| 3059 | if (c) { |
| 3060 | /* if modulus == 0: |
| 3061 | raise ValueError() */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3062 | if (Py_SIZE(c) == 0) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3063 | PyErr_SetString(PyExc_ValueError, |
| 3064 | "pow() 3rd argument cannot be 0"); |
Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3065 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | /* if modulus < 0: |
| 3069 | negativeOutput = True |
| 3070 | modulus = -modulus */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3071 | if (Py_SIZE(c) < 0) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3072 | negativeOutput = 1; |
| 3073 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 3074 | if (temp == NULL) |
| 3075 | goto Error; |
| 3076 | Py_DECREF(c); |
| 3077 | c = temp; |
| 3078 | temp = NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3079 | NEGATE(c); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | /* if modulus == 1: |
| 3083 | return 0 */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3084 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3085 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 3086 | goto Done; |
| 3087 | } |
| 3088 | |
| 3089 | /* if base < 0: |
| 3090 | base = base % modulus |
| 3091 | Having the base positive just makes things easier. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3092 | if (Py_SIZE(a) < 0) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3093 | if (l_divmod(a, c, NULL, &temp) < 0) |
Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3094 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3095 | Py_DECREF(a); |
| 3096 | a = temp; |
| 3097 | temp = NULL; |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 3102 | c is NULL, in which case a may be negative. */ |
| 3103 | |
| 3104 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 3105 | if (z == NULL) |
| 3106 | goto Error; |
| 3107 | |
| 3108 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 3109 | * is NULL. |
| 3110 | */ |
| 3111 | #define REDUCE(X) \ |
| 3112 | if (c != NULL) { \ |
| 3113 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 3114 | goto Error; \ |
| 3115 | Py_XDECREF(X); \ |
| 3116 | X = temp; \ |
| 3117 | temp = NULL; \ |
| 3118 | } |
| 3119 | |
| 3120 | /* Multiply two values, then reduce the result: |
| 3121 | result = X*Y % c. If c is NULL, skip the mod. */ |
| 3122 | #define MULT(X, Y, result) \ |
| 3123 | { \ |
| 3124 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 3125 | if (temp == NULL) \ |
| 3126 | goto Error; \ |
| 3127 | Py_XDECREF(result); \ |
| 3128 | result = temp; \ |
| 3129 | temp = NULL; \ |
| 3130 | REDUCE(result) \ |
| 3131 | } |
| 3132 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3133 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3134 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 3135 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3136 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3137 | digit bi = b->ob_digit[i]; |
| 3138 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3139 | for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3140 | MULT(z, z, z) |
| 3141 | if (bi & j) |
| 3142 | MULT(z, a, z) |
| 3143 | } |
| 3144 | } |
| 3145 | } |
| 3146 | else { |
| 3147 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 3148 | Py_INCREF(z); /* still holds 1L */ |
| 3149 | table[0] = z; |
| 3150 | for (i = 1; i < 32; ++i) |
| 3151 | MULT(table[i-1], a, table[i]) |
| 3152 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3153 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3154 | const digit bi = b->ob_digit[i]; |
| 3155 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3156 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3157 | const int index = (bi >> j) & 0x1f; |
| 3158 | for (k = 0; k < 5; ++k) |
| 3159 | MULT(z, z, z) |
| 3160 | if (index) |
| 3161 | MULT(z, table[index], z) |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3166 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3167 | temp = (PyLongObject *)long_sub(z, c); |
| 3168 | if (temp == NULL) |
| 3169 | goto Error; |
| 3170 | Py_DECREF(z); |
| 3171 | z = temp; |
| 3172 | temp = NULL; |
| 3173 | } |
| 3174 | goto Done; |
| 3175 | |
| 3176 | Error: |
| 3177 | if (z != NULL) { |
| 3178 | Py_DECREF(z); |
| 3179 | z = NULL; |
| 3180 | } |
| 3181 | /* fall through */ |
| 3182 | Done: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3183 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3184 | for (i = 0; i < 32; ++i) |
| 3185 | Py_XDECREF(table[i]); |
| 3186 | } |
Tim Peters | de7990b | 2005-07-17 23:45:23 +0000 | [diff] [blame] | 3187 | Py_DECREF(a); |
| 3188 | Py_DECREF(b); |
| 3189 | Py_XDECREF(c); |
| 3190 | Py_XDECREF(temp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3191 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3194 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3195 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3196 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3197 | /* Implement ~x as -(x+1) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3198 | PyLongObject *x; |
| 3199 | PyLongObject *w; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3200 | if (ABS(Py_SIZE(v)) <=1) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3201 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3202 | w = (PyLongObject *)PyLong_FromLong(1L); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3203 | if (w == NULL) |
| 3204 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3205 | x = (PyLongObject *) long_add(v, w); |
| 3206 | Py_DECREF(w); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3207 | if (x == NULL) |
| 3208 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3209 | Py_SIZE(x) = -(Py_SIZE(x)); |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 3210 | return (PyObject *)maybe_small_long(x); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3213 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3214 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3215 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3216 | PyLongObject *z; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3217 | if (ABS(Py_SIZE(v)) <= 1) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3218 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3219 | z = (PyLongObject *)_PyLong_Copy(v); |
| 3220 | if (z != NULL) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3221 | Py_SIZE(z) = -(Py_SIZE(v)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3222 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3225 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3226 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3227 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3228 | if (Py_SIZE(v) < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3229 | return long_neg(v); |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3230 | else |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3231 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3234 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3235 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3236 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3237 | return ABS(Py_SIZE(v)) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3240 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3241 | long_rshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3242 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3243 | PyLongObject *z = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3244 | long shiftby; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3245 | Py_ssize_t newsize, wordshift, loshift, hishift, i, j; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3246 | digit lomask, himask; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3247 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3248 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3249 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3250 | if (Py_SIZE(a) < 0) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3251 | /* Right shifting negative numbers is harder */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3252 | PyLongObject *a1, *a2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3253 | a1 = (PyLongObject *) long_invert(a); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3254 | if (a1 == NULL) |
| 3255 | goto rshift_error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3256 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 3257 | Py_DECREF(a1); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3258 | if (a2 == NULL) |
| 3259 | goto rshift_error; |
| 3260 | z = (PyLongObject *) long_invert(a2); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3261 | Py_DECREF(a2); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3262 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3263 | else { |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3264 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3265 | shiftby = PyLong_AsLong((PyObject *)b); |
| 3266 | if (shiftby == -1L && PyErr_Occurred()) |
| 3267 | goto rshift_error; |
| 3268 | if (shiftby < 0) { |
| 3269 | PyErr_SetString(PyExc_ValueError, |
| 3270 | "negative shift count"); |
| 3271 | goto rshift_error; |
| 3272 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3273 | wordshift = shiftby / PyLong_SHIFT; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3274 | newsize = ABS(Py_SIZE(a)) - wordshift; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 3275 | if (newsize <= 0) |
| 3276 | return PyLong_FromLong(0); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3277 | loshift = shiftby % PyLong_SHIFT; |
| 3278 | hishift = PyLong_SHIFT - loshift; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3279 | lomask = ((digit)1 << hishift) - 1; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3280 | himask = PyLong_MASK ^ lomask; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3281 | z = _PyLong_New(newsize); |
| 3282 | if (z == NULL) |
| 3283 | goto rshift_error; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3284 | if (Py_SIZE(a) < 0) |
| 3285 | Py_SIZE(z) = -(Py_SIZE(z)); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3286 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 3287 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 3288 | if (i+1 < newsize) |
| 3289 | z->ob_digit[i] |= |
| 3290 | (a->ob_digit[j+1] << hishift) & himask; |
| 3291 | } |
| 3292 | z = long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3293 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3294 | rshift_error: |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 3295 | return (PyObject *) maybe_small_long(z); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3296 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3299 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3300 | long_lshift(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3301 | { |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3302 | /* This version due to Tim Peters */ |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3303 | PyLongObject *a = (PyLongObject*)v; |
| 3304 | PyLongObject *b = (PyLongObject*)w; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3305 | PyLongObject *z = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3306 | long shiftby; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3307 | Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3308 | twodigits accum; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3309 | |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3310 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3311 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3312 | shiftby = PyLong_AsLong((PyObject *)b); |
| 3313 | if (shiftby == -1L && PyErr_Occurred()) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3314 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3315 | if (shiftby < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3316 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3317 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3318 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3319 | if ((long)(int)shiftby != shiftby) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3320 | PyErr_SetString(PyExc_ValueError, |
| 3321 | "outrageous left shift count"); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3322 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3323 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3324 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 3325 | wordshift = (int)shiftby / PyLong_SHIFT; |
| 3326 | remshift = (int)shiftby - wordshift * PyLong_SHIFT; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3327 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3328 | oldsize = ABS(Py_SIZE(a)); |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3329 | newsize = oldsize + wordshift; |
| 3330 | if (remshift) |
| 3331 | ++newsize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3332 | z = _PyLong_New(newsize); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3333 | if (z == NULL) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3334 | goto lshift_error; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3335 | if (Py_SIZE(a) < 0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3336 | NEGATE(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3337 | for (i = 0; i < wordshift; i++) |
| 3338 | z->ob_digit[i] = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3339 | accum = 0; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3340 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
Tim Peters | 0d2d87d | 2002-08-20 19:00:22 +0000 | [diff] [blame] | 3341 | accum |= (twodigits)a->ob_digit[j] << remshift; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3342 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 3343 | accum >>= PyLong_SHIFT; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3344 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3345 | if (remshift) |
| 3346 | z->ob_digit[newsize-1] = (digit)accum; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3347 | else |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3348 | assert(!accum); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3349 | z = long_normalize(z); |
| 3350 | lshift_error: |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 3351 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3354 | |
| 3355 | /* Bitwise and/xor/or operations */ |
| 3356 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3357 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3358 | long_bitwise(PyLongObject *a, |
| 3359 | int op, /* '&', '|', '^' */ |
| 3360 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3361 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3362 | digit maska, maskb; /* 0 or PyLong_MASK */ |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3363 | int negz; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3364 | Py_ssize_t size_a, size_b, size_z; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3365 | PyLongObject *z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3366 | int i; |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 3367 | digit diga, digb; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3368 | PyObject *v; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3369 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3370 | if (Py_SIZE(a) < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3371 | a = (PyLongObject *) long_invert(a); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3372 | if (a == NULL) |
| 3373 | return NULL; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3374 | maska = PyLong_MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3375 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3376 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3377 | Py_INCREF(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3378 | maska = 0; |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 3379 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3380 | if (Py_SIZE(b) < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3381 | b = (PyLongObject *) long_invert(b); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3382 | if (b == NULL) { |
| 3383 | Py_DECREF(a); |
| 3384 | return NULL; |
| 3385 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3386 | maskb = PyLong_MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3387 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3388 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3389 | Py_INCREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3390 | maskb = 0; |
| 3391 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3392 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3393 | negz = 0; |
| 3394 | switch (op) { |
| 3395 | case '^': |
| 3396 | if (maska != maskb) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3397 | maska ^= PyLong_MASK; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3398 | negz = -1; |
| 3399 | } |
| 3400 | break; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 3401 | case '&': |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3402 | if (maska && maskb) { |
| 3403 | op = '|'; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3404 | maska ^= PyLong_MASK; |
| 3405 | maskb ^= PyLong_MASK; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3406 | negz = -1; |
| 3407 | } |
| 3408 | break; |
| 3409 | case '|': |
| 3410 | if (maska || maskb) { |
| 3411 | op = '&'; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3412 | maska ^= PyLong_MASK; |
| 3413 | maskb ^= PyLong_MASK; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3414 | negz = -1; |
| 3415 | } |
| 3416 | break; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3417 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3418 | |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3419 | /* JRH: The original logic here was to allocate the result value (z) |
| 3420 | as the longer of the two operands. However, there are some cases |
| 3421 | where the result is guaranteed to be shorter than that: AND of two |
| 3422 | positives, OR of two negatives: use the shorter number. AND with |
| 3423 | mixed signs: use the positive number. OR with mixed signs: use the |
| 3424 | negative number. After the transformations above, op will be '&' |
| 3425 | iff one of these cases applies, and mask will be non-0 for operands |
| 3426 | whose length should be ignored. |
| 3427 | */ |
| 3428 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3429 | size_a = Py_SIZE(a); |
| 3430 | size_b = Py_SIZE(b); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3431 | size_z = op == '&' |
| 3432 | ? (maska |
| 3433 | ? size_b |
| 3434 | : (maskb ? size_a : MIN(size_a, size_b))) |
| 3435 | : MAX(size_a, size_b); |
| 3436 | z = _PyLong_New(size_z); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3437 | if (z == NULL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3438 | Py_DECREF(a); |
| 3439 | Py_DECREF(b); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3440 | return NULL; |
| 3441 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3442 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3443 | for (i = 0; i < size_z; ++i) { |
| 3444 | diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; |
| 3445 | digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; |
| 3446 | switch (op) { |
| 3447 | case '&': z->ob_digit[i] = diga & digb; break; |
| 3448 | case '|': z->ob_digit[i] = diga | digb; break; |
| 3449 | case '^': z->ob_digit[i] = diga ^ digb; break; |
| 3450 | } |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 3451 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3452 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3453 | Py_DECREF(a); |
| 3454 | Py_DECREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3455 | z = long_normalize(z); |
| 3456 | if (negz == 0) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 3457 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3458 | v = long_invert(z); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3459 | Py_DECREF(z); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3460 | return v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3463 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3464 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3465 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3466 | PyObject *c; |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3467 | CHECK_BINOP(a, b); |
| 3468 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3469 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3472 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3473 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3474 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3475 | PyObject *c; |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3476 | CHECK_BINOP(a, b); |
| 3477 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3478 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3481 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3482 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3483 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3484 | PyObject *c; |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3485 | CHECK_BINOP(a, b); |
| 3486 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3487 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3490 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3491 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3492 | { |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 3493 | if (PyLong_CheckExact(v)) |
| 3494 | Py_INCREF(v); |
| 3495 | else |
| 3496 | v = _PyLong_Copy((PyLongObject *)v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3497 | return v; |
| 3498 | } |
| 3499 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3500 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3501 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3502 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 3503 | double result; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3504 | result = PyLong_AsDouble(v); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 3505 | if (result == -1.0 && PyErr_Occurred()) |
| 3506 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3507 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3510 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3511 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3512 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3513 | static PyObject * |
| 3514 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3515 | { |
| 3516 | PyObject *x = NULL; |
| 3517 | int base = -909; /* unlikely! */ |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3518 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3519 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3520 | if (type != &PyLong_Type) |
| 3521 | return long_subtype_new(type, args, kwds); /* Wimp out */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3522 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3523 | &x, &base)) |
| 3524 | return NULL; |
| 3525 | if (x == NULL) |
| 3526 | return PyLong_FromLong(0L); |
| 3527 | if (base == -909) |
| 3528 | return PyNumber_Long(x); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3529 | else if (PyUnicode_Check(x)) |
| 3530 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 3531 | PyUnicode_GET_SIZE(x), |
| 3532 | base); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3533 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3534 | /* Since PyLong_FromString doesn't have a length parameter, |
| 3535 | * check here for possible NULs in the string. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3536 | char *string; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3537 | int size = Py_SIZE(x); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3538 | if (PyByteArray_Check(x)) |
| 3539 | string = PyByteArray_AS_STRING(x); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3540 | else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3541 | string = PyBytes_AS_STRING(x); |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 3542 | if (strlen(string) != size) { |
Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 3543 | /* We only see this if there's a null byte in x, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3544 | x is a bytes or buffer, *and* a base is given. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3545 | PyErr_Format(PyExc_ValueError, |
Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 3546 | "invalid literal for int() with base %d: %R", |
| 3547 | base, x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3548 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3549 | } |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 3550 | return PyLong_FromString(string, NULL, base); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3551 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3552 | else { |
| 3553 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3554 | "int() can't convert non-string with explicit base"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3555 | return NULL; |
| 3556 | } |
| 3557 | } |
| 3558 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3559 | /* Wimpy, slow approach to tp_new calls for subtypes of long: |
| 3560 | first create a regular long from whatever arguments we got, |
| 3561 | then allocate a subtype instance and initialize it from |
| 3562 | the regular long. The regular long is then thrown away. |
| 3563 | */ |
| 3564 | static PyObject * |
| 3565 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3566 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3567 | PyLongObject *tmp, *newobj; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3568 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3569 | |
| 3570 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
| 3571 | tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); |
| 3572 | if (tmp == NULL) |
| 3573 | return NULL; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 3574 | assert(PyLong_CheckExact(tmp)); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3575 | n = Py_SIZE(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3576 | if (n < 0) |
| 3577 | n = -n; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3578 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 3579 | if (newobj == NULL) { |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 3580 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3581 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 3582 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3583 | assert(PyLong_Check(newobj)); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3584 | Py_SIZE(newobj) = Py_SIZE(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3585 | for (i = 0; i < n; i++) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3586 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3587 | Py_DECREF(tmp); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3588 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3591 | static PyObject * |
| 3592 | long_getnewargs(PyLongObject *v) |
| 3593 | { |
| 3594 | return Py_BuildValue("(N)", _PyLong_Copy(v)); |
| 3595 | } |
| 3596 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3597 | static PyObject * |
| 3598 | long_getN(PyLongObject *v, void *context) { |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 3599 | return PyLong_FromLong((Py_intptr_t)context); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3602 | static PyObject * |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3603 | long__format__(PyObject *self, PyObject *args) |
| 3604 | { |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 3605 | PyObject *format_spec; |
| 3606 | |
| 3607 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3608 | return NULL; |
| 3609 | return _PyLong_FormatAdvanced(self, |
| 3610 | PyUnicode_AS_UNICODE(format_spec), |
| 3611 | PyUnicode_GET_SIZE(format_spec)); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | |
| 3615 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3616 | long_round(PyObject *self, PyObject *args) |
| 3617 | { |
| 3618 | #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */ |
| 3619 | int ndigits = UNDEF_NDIGITS; |
| 3620 | double x; |
| 3621 | PyObject *res; |
| 3622 | |
| 3623 | if (!PyArg_ParseTuple(args, "|i", &ndigits)) |
| 3624 | return NULL; |
| 3625 | |
| 3626 | if (ndigits == UNDEF_NDIGITS) |
| 3627 | return long_long(self); |
| 3628 | |
| 3629 | /* If called with two args, defer to float.__round__(). */ |
| 3630 | x = PyLong_AsDouble(self); |
| 3631 | if (x == -1.0 && PyErr_Occurred()) |
| 3632 | return NULL; |
| 3633 | self = PyFloat_FromDouble(x); |
| 3634 | if (self == NULL) |
| 3635 | return NULL; |
| 3636 | res = PyObject_CallMethod(self, "__round__", "i", ndigits); |
| 3637 | Py_DECREF(self); |
| 3638 | return res; |
| 3639 | #undef UNDEF_NDIGITS |
| 3640 | } |
| 3641 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3642 | static PyObject * |
| 3643 | long_sizeof(PyLongObject *v) |
| 3644 | { |
| 3645 | Py_ssize_t res; |
| 3646 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 3647 | res = sizeof(PyVarObject) + abs(Py_SIZE(v))*sizeof(digit); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3648 | return PyLong_FromSsize_t(res); |
| 3649 | } |
| 3650 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 3651 | #if 0 |
| 3652 | static PyObject * |
| 3653 | long_is_finite(PyObject *v) |
| 3654 | { |
| 3655 | Py_RETURN_TRUE; |
| 3656 | } |
| 3657 | #endif |
| 3658 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3659 | static PyMethodDef long_methods[] = { |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3660 | {"conjugate", (PyCFunction)long_long, METH_NOARGS, |
| 3661 | "Returns self, the complex conjugate of any int."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 3662 | #if 0 |
| 3663 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
| 3664 | "Returns always True."}, |
| 3665 | #endif |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3666 | {"__trunc__", (PyCFunction)long_long, METH_NOARGS, |
| 3667 | "Truncating an Integral returns itself."}, |
| 3668 | {"__floor__", (PyCFunction)long_long, METH_NOARGS, |
| 3669 | "Flooring an Integral returns itself."}, |
| 3670 | {"__ceil__", (PyCFunction)long_long, METH_NOARGS, |
| 3671 | "Ceiling of an Integral returns itself."}, |
| 3672 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 3673 | "Rounding an Integral returns itself.\n" |
| 3674 | "Rounding with an ndigits arguments defers to float.__round__."}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3675 | {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3676 | {"__format__", (PyCFunction)long__format__, METH_VARARGS}, |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3677 | {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, |
| 3678 | "Returns size in memory, in bytes"}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3679 | {NULL, NULL} /* sentinel */ |
| 3680 | }; |
| 3681 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3682 | static PyGetSetDef long_getset[] = { |
| 3683 | {"real", |
| 3684 | (getter)long_long, (setter)NULL, |
| 3685 | "the real part of a complex number", |
| 3686 | NULL}, |
| 3687 | {"imag", |
| 3688 | (getter)long_getN, (setter)NULL, |
| 3689 | "the imaginary part of a complex number", |
| 3690 | (void*)0}, |
| 3691 | {"numerator", |
| 3692 | (getter)long_long, (setter)NULL, |
| 3693 | "the numerator of a rational number in lowest terms", |
| 3694 | NULL}, |
| 3695 | {"denominator", |
| 3696 | (getter)long_getN, (setter)NULL, |
| 3697 | "the denominator of a rational number in lowest terms", |
| 3698 | (void*)1}, |
| 3699 | {NULL} /* Sentinel */ |
| 3700 | }; |
| 3701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3702 | PyDoc_STRVAR(long_doc, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3703 | "int(x[, base]) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3704 | \n\ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3705 | Convert a string or number to an integer, if possible. A floating\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3706 | point argument will be truncated towards zero (this does not include a\n\ |
| 3707 | string representation of a floating point number!) When converting a\n\ |
| 3708 | string, use the optional base. It is an error to supply a base when\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3709 | converting a non-string."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3710 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3711 | static PyNumberMethods long_as_number = { |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3712 | (binaryfunc) long_add, /*nb_add*/ |
| 3713 | (binaryfunc) long_sub, /*nb_subtract*/ |
| 3714 | (binaryfunc) long_mul, /*nb_multiply*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3715 | long_mod, /*nb_remainder*/ |
| 3716 | long_divmod, /*nb_divmod*/ |
| 3717 | long_pow, /*nb_power*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3718 | (unaryfunc) long_neg, /*nb_negative*/ |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3719 | (unaryfunc) long_long, /*tp_positive*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3720 | (unaryfunc) long_abs, /*tp_absolute*/ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3721 | (inquiry) long_bool, /*tp_bool*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3722 | (unaryfunc) long_invert, /*nb_invert*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3723 | long_lshift, /*nb_lshift*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3724 | (binaryfunc) long_rshift, /*nb_rshift*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3725 | long_and, /*nb_and*/ |
| 3726 | long_xor, /*nb_xor*/ |
| 3727 | long_or, /*nb_or*/ |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3728 | long_long, /*nb_int*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3729 | long_long, /*nb_long*/ |
| 3730 | long_float, /*nb_float*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3731 | 0, /* nb_inplace_add */ |
| 3732 | 0, /* nb_inplace_subtract */ |
| 3733 | 0, /* nb_inplace_multiply */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3734 | 0, /* nb_inplace_remainder */ |
| 3735 | 0, /* nb_inplace_power */ |
| 3736 | 0, /* nb_inplace_lshift */ |
| 3737 | 0, /* nb_inplace_rshift */ |
| 3738 | 0, /* nb_inplace_and */ |
| 3739 | 0, /* nb_inplace_xor */ |
| 3740 | 0, /* nb_inplace_or */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3741 | long_div, /* nb_floor_divide */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3742 | long_true_divide, /* nb_true_divide */ |
| 3743 | 0, /* nb_inplace_floor_divide */ |
| 3744 | 0, /* nb_inplace_true_divide */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3745 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3746 | }; |
| 3747 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3748 | PyTypeObject PyLong_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3749 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3750 | "int", /* tp_name */ |
| 3751 | /* See _PyLong_New for why this isn't |
| 3752 | sizeof(PyLongObject) - sizeof(digit) */ |
| 3753 | sizeof(PyVarObject), /* tp_basicsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3754 | sizeof(digit), /* tp_itemsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3755 | long_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3756 | 0, /* tp_print */ |
| 3757 | 0, /* tp_getattr */ |
| 3758 | 0, /* tp_setattr */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3759 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3760 | long_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3761 | &long_as_number, /* tp_as_number */ |
| 3762 | 0, /* tp_as_sequence */ |
| 3763 | 0, /* tp_as_mapping */ |
| 3764 | (hashfunc)long_hash, /* tp_hash */ |
| 3765 | 0, /* tp_call */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3766 | long_repr, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3767 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3768 | 0, /* tp_setattro */ |
| 3769 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3770 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 3771 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3772 | long_doc, /* tp_doc */ |
| 3773 | 0, /* tp_traverse */ |
| 3774 | 0, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3775 | long_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3776 | 0, /* tp_weaklistoffset */ |
| 3777 | 0, /* tp_iter */ |
| 3778 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3779 | long_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3780 | 0, /* tp_members */ |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3781 | long_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3782 | 0, /* tp_base */ |
| 3783 | 0, /* tp_dict */ |
| 3784 | 0, /* tp_descr_get */ |
| 3785 | 0, /* tp_descr_set */ |
| 3786 | 0, /* tp_dictoffset */ |
| 3787 | 0, /* tp_init */ |
| 3788 | 0, /* tp_alloc */ |
| 3789 | long_new, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 3790 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3791 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3792 | |
| 3793 | int |
| 3794 | _PyLong_Init(void) |
| 3795 | { |
| 3796 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3797 | int ival, size; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3798 | PyLongObject *v = small_ints; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3799 | |
| 3800 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { |
| 3801 | size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
| 3802 | if (Py_TYPE(v) == &PyLong_Type) { |
| 3803 | /* The element is already initialized, most likely |
| 3804 | * the Python interpreter was initialized before. |
| 3805 | */ |
Christian Heimes | 48aa4b1 | 2008-02-01 16:56:30 +0000 | [diff] [blame] | 3806 | Py_ssize_t refcnt; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3807 | PyObject* op = (PyObject*)v; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3808 | |
Christian Heimes | 48aa4b1 | 2008-02-01 16:56:30 +0000 | [diff] [blame] | 3809 | refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); |
| 3810 | _Py_NewReference(op); |
| 3811 | /* _Py_NewReference sets the ref count to 1 but |
| 3812 | * the ref count might be larger. Set the refcnt |
| 3813 | * to the original refcnt + 1 */ |
| 3814 | Py_REFCNT(op) = refcnt + 1; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3815 | assert(Py_SIZE(op) == size); |
| 3816 | assert(v->ob_digit[0] == abs(ival)); |
| 3817 | } |
| 3818 | else { |
| 3819 | PyObject_INIT(v, &PyLong_Type); |
| 3820 | } |
| 3821 | Py_SIZE(v) = size; |
| 3822 | v->ob_digit[0] = abs(ival); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3823 | } |
| 3824 | #endif |
| 3825 | return 1; |
| 3826 | } |
| 3827 | |
| 3828 | void |
| 3829 | PyLong_Fini(void) |
| 3830 | { |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3831 | /* Integers are currently statically allocated. Py_DECREF is not |
| 3832 | needed, but Python must forget about the reference or multiple |
| 3833 | reinitializations will fail. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3834 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 3835 | int i; |
| 3836 | PyLongObject *v = small_ints; |
| 3837 | for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { |
| 3838 | _Py_DEC_REFTOTAL; |
| 3839 | _Py_ForgetReference((PyObject*)v); |
| 3840 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3841 | #endif |
| 3842 | } |