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