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