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