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