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