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