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