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