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