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