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