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