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