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