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