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