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 | |
Mark Dickinson | c630039 | 2009-04-20 21:38:00 +0000 | [diff] [blame] | 8 | #include <float.h> |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 10 | #include <stddef.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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | #define NSMALLPOSINTS 257 |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 14 | #endif |
| 15 | #ifndef NSMALLNEGINTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | #define NSMALLNEGINTS 5 |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 17 | #endif |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 18 | |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 19 | /* convert a PyLong of size 1, 0 or -1 to an sdigit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | #define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
| 21 | (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
| 22 | (sdigit)(x)->ob_digit[0])) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 23 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 24 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 25 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 26 | /* Small integers are preallocated in this array so that they |
| 27 | can be shared. |
| 28 | The integers that are preallocated are those in the range |
| 29 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 30 | */ |
| 31 | static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
| 32 | #ifdef COUNT_ALLOCS |
Mark Dickinson | c286e58 | 2012-09-20 21:29:28 +0100 | [diff] [blame] | 33 | Py_ssize_t quick_int_allocs, quick_neg_int_allocs; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 36 | static PyObject * |
Mark Dickinson | e441674 | 2009-02-15 15:14:57 +0000 | [diff] [blame] | 37 | get_small_int(sdigit ival) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 38 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); |
| 40 | Py_INCREF(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 41 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | if (ival >= 0) |
| 43 | quick_int_allocs++; |
| 44 | else |
| 45 | quick_neg_int_allocs++; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 46 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | return v; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 48 | } |
| 49 | #define CHECK_SMALL_INT(ival) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ |
| 51 | return get_small_int((sdigit)ival); \ |
| 52 | } while(0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | static PyLongObject * |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 55 | maybe_small_long(PyLongObject *v) |
| 56 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | if (v && ABS(Py_SIZE(v)) <= 1) { |
| 58 | sdigit ival = MEDIUM_VALUE(v); |
| 59 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 60 | Py_DECREF(v); |
| 61 | return (PyLongObject *)get_small_int(ival); |
| 62 | } |
| 63 | } |
| 64 | return v; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 65 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 66 | #else |
| 67 | #define CHECK_SMALL_INT(ival) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 68 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 71 | /* If a freshly-allocated long is already shared, it must |
| 72 | be a small integer, so negating it must go to PyLong_FromLong */ |
| 73 | #define NEGATE(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \ |
| 75 | else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \ |
| 76 | Py_DECREF(x); (x) = (PyLongObject*)tmp; } \ |
| 77 | while(0) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 78 | /* For long multiplication, use the O(N**2) school algorithm unless |
| 79 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
| 80 | * being an internal Python long digit, in base BASE). |
| 81 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 82 | #define KARATSUBA_CUTOFF 70 |
| 83 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 84 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 85 | /* For exponentiation, use the binary left-to-right algorithm |
| 86 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 87 | * In that case, do 5 bits at a time. The potential drawback is that |
| 88 | * a table of 2**5 intermediate results is computed. |
| 89 | */ |
| 90 | #define FIVEARY_CUTOFF 8 |
| 91 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 92 | #undef MIN |
| 93 | #undef MAX |
| 94 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 95 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 96 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 97 | #define SIGCHECK(PyTryBlock) \ |
| 98 | do { \ |
| 99 | if (PyErr_CheckSignals()) PyTryBlock \ |
| 100 | } while(0) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 102 | /* Normalize (remove leading zeros from) a long int object. |
| 103 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 104 | of the algorithms used, this could save at most be one word anyway. */ |
| 105 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 107 | long_normalize(register PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | Py_ssize_t j = ABS(Py_SIZE(v)); |
| 110 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 113 | --i; |
| 114 | if (i != j) |
| 115 | Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
| 116 | return v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Allocate a new long int object with size digits. |
| 120 | Return NULL and set exception if we run out of memory. */ |
| 121 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 122 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 126 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | PyLongObject *result; |
| 129 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 130 | sizeof(digit)*size. Previous incarnations of this code used |
| 131 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 132 | incorrect in the presence of padding between the PyVarObject header |
| 133 | and the digits. */ |
| 134 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 135 | PyErr_SetString(PyExc_OverflowError, |
| 136 | "too many digits in integer"); |
| 137 | return NULL; |
| 138 | } |
| 139 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 140 | size*sizeof(digit)); |
| 141 | if (!result) { |
| 142 | PyErr_NoMemory(); |
| 143 | return NULL; |
| 144 | } |
| 145 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 148 | PyObject * |
| 149 | _PyLong_Copy(PyLongObject *src) |
| 150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | PyLongObject *result; |
| 152 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | assert(src != NULL); |
| 155 | i = Py_SIZE(src); |
| 156 | if (i < 0) |
| 157 | i = -(i); |
| 158 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 159 | sdigit ival = MEDIUM_VALUE(src); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | CHECK_SMALL_INT(ival); |
| 161 | } |
| 162 | result = _PyLong_New(i); |
| 163 | if (result != NULL) { |
| 164 | Py_SIZE(result) = Py_SIZE(src); |
| 165 | while (--i >= 0) |
| 166 | result->ob_digit[i] = src->ob_digit[i]; |
| 167 | } |
| 168 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 171 | /* Create a new long int object from a C long int */ |
| 172 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 173 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 174 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | PyLongObject *v; |
| 177 | unsigned long abs_ival; |
| 178 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 179 | int ndigits = 0; |
| 180 | int sign = 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | CHECK_SMALL_INT(ival); |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | if (ival < 0) { |
| 185 | /* negate: can't write this as abs_ival = -ival since that |
| 186 | invokes undefined behaviour when ival is LONG_MIN */ |
| 187 | abs_ival = 0U-(unsigned long)ival; |
| 188 | sign = -1; |
| 189 | } |
| 190 | else { |
| 191 | abs_ival = (unsigned long)ival; |
| 192 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | /* Fast path for single-digit ints */ |
| 195 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 196 | v = _PyLong_New(1); |
| 197 | if (v) { |
| 198 | Py_SIZE(v) = sign; |
| 199 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 200 | abs_ival, unsigned long, digit); |
| 201 | } |
| 202 | return (PyObject*)v; |
| 203 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 204 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 205 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | /* 2 digits */ |
| 207 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 208 | v = _PyLong_New(2); |
| 209 | if (v) { |
| 210 | Py_SIZE(v) = 2*sign; |
| 211 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 212 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 213 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 214 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 215 | } |
| 216 | return (PyObject*)v; |
| 217 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | /* Larger numbers: loop to determine number of digits */ |
| 221 | t = abs_ival; |
| 222 | while (t) { |
| 223 | ++ndigits; |
| 224 | t >>= PyLong_SHIFT; |
| 225 | } |
| 226 | v = _PyLong_New(ndigits); |
| 227 | if (v != NULL) { |
| 228 | digit *p = v->ob_digit; |
| 229 | Py_SIZE(v) = ndigits*sign; |
| 230 | t = abs_ival; |
| 231 | while (t) { |
| 232 | *p++ = Py_SAFE_DOWNCAST( |
| 233 | t & PyLong_MASK, unsigned long, digit); |
| 234 | t >>= PyLong_SHIFT; |
| 235 | } |
| 236 | } |
| 237 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 240 | /* Create a new long int object from a C unsigned long int */ |
| 241 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 243 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyLongObject *v; |
| 246 | unsigned long t; |
| 247 | int ndigits = 0; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | if (ival < PyLong_BASE) |
| 250 | return PyLong_FromLong(ival); |
| 251 | /* Count the number of Python digits. */ |
| 252 | t = (unsigned long)ival; |
| 253 | while (t) { |
| 254 | ++ndigits; |
| 255 | t >>= PyLong_SHIFT; |
| 256 | } |
| 257 | v = _PyLong_New(ndigits); |
| 258 | if (v != NULL) { |
| 259 | digit *p = v->ob_digit; |
| 260 | Py_SIZE(v) = ndigits; |
| 261 | while (ival) { |
| 262 | *p++ = (digit)(ival & PyLong_MASK); |
| 263 | ival >>= PyLong_SHIFT; |
| 264 | } |
| 265 | } |
| 266 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 269 | /* Create a new long int object from a C double */ |
| 270 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 272 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | PyLongObject *v; |
| 275 | double frac; |
| 276 | int i, ndig, expo, neg; |
| 277 | neg = 0; |
| 278 | if (Py_IS_INFINITY(dval)) { |
| 279 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 280 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | return NULL; |
| 282 | } |
| 283 | if (Py_IS_NAN(dval)) { |
| 284 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 285 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | return NULL; |
| 287 | } |
| 288 | if (dval < 0.0) { |
| 289 | neg = 1; |
| 290 | dval = -dval; |
| 291 | } |
| 292 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 293 | if (expo <= 0) |
| 294 | return PyLong_FromLong(0L); |
| 295 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 296 | v = _PyLong_New(ndig); |
| 297 | if (v == NULL) |
| 298 | return NULL; |
| 299 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 300 | for (i = ndig; --i >= 0; ) { |
| 301 | digit bits = (digit)frac; |
| 302 | v->ob_digit[i] = bits; |
| 303 | frac = frac - (double)bits; |
| 304 | frac = ldexp(frac, PyLong_SHIFT); |
| 305 | } |
| 306 | if (neg) |
| 307 | Py_SIZE(v) = -(Py_SIZE(v)); |
| 308 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 311 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 312 | * anything about what happens when a signed integer operation overflows, |
| 313 | * and some compilers think they're doing you a favor by being "clever" |
| 314 | * then. The bit pattern for the largest postive signed long is |
| 315 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 316 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 317 | * However, some other compilers warn about applying unary minus to an |
| 318 | * unsigned operand. Hence the weird "0-". |
| 319 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 321 | #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 322 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 323 | /* Get a C long int from a long int object or any object that has an __int__ |
| 324 | method. |
| 325 | |
| 326 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 327 | the result. Otherwise *overflow is 0. |
| 328 | |
| 329 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 330 | In this case *overflow will be 0. |
| 331 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 332 | |
| 333 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 334 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | /* This version by Tim Peters */ |
| 337 | register PyLongObject *v; |
| 338 | unsigned long x, prev; |
| 339 | long res; |
| 340 | Py_ssize_t i; |
| 341 | int sign; |
| 342 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | *overflow = 0; |
| 345 | if (vv == NULL) { |
| 346 | PyErr_BadInternalCall(); |
| 347 | return -1; |
| 348 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | if (!PyLong_Check(vv)) { |
| 351 | PyNumberMethods *nb; |
| 352 | nb = vv->ob_type->tp_as_number; |
| 353 | if (nb == NULL || nb->nb_int == NULL) { |
| 354 | PyErr_SetString(PyExc_TypeError, |
| 355 | "an integer is required"); |
| 356 | return -1; |
| 357 | } |
| 358 | vv = (*nb->nb_int) (vv); |
| 359 | if (vv == NULL) |
| 360 | return -1; |
| 361 | do_decref = 1; |
| 362 | if (!PyLong_Check(vv)) { |
| 363 | Py_DECREF(vv); |
| 364 | PyErr_SetString(PyExc_TypeError, |
| 365 | "nb_int should return int object"); |
| 366 | return -1; |
| 367 | } |
| 368 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | res = -1; |
| 371 | v = (PyLongObject *)vv; |
| 372 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | switch (i) { |
| 375 | case -1: |
| 376 | res = -(sdigit)v->ob_digit[0]; |
| 377 | break; |
| 378 | case 0: |
| 379 | res = 0; |
| 380 | break; |
| 381 | case 1: |
| 382 | res = v->ob_digit[0]; |
| 383 | break; |
| 384 | default: |
| 385 | sign = 1; |
| 386 | x = 0; |
| 387 | if (i < 0) { |
| 388 | sign = -1; |
| 389 | i = -(i); |
| 390 | } |
| 391 | while (--i >= 0) { |
| 392 | prev = x; |
| 393 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 394 | if ((x >> PyLong_SHIFT) != prev) { |
| 395 | *overflow = sign; |
| 396 | goto exit; |
| 397 | } |
| 398 | } |
| 399 | /* Haven't lost any bits, but casting to long requires extra |
| 400 | * care (see comment above). |
| 401 | */ |
| 402 | if (x <= (unsigned long)LONG_MAX) { |
| 403 | res = (long)x * sign; |
| 404 | } |
| 405 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 406 | res = LONG_MIN; |
| 407 | } |
| 408 | else { |
| 409 | *overflow = sign; |
| 410 | /* res is already set to -1 */ |
| 411 | } |
| 412 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 413 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | if (do_decref) { |
| 415 | Py_DECREF(vv); |
| 416 | } |
| 417 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 420 | /* Get a C long int from a long int object or any object that has an __int__ |
| 421 | method. Return -1 and set an error if overflow occurs. */ |
| 422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 424 | PyLong_AsLong(PyObject *obj) |
| 425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | int overflow; |
| 427 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 428 | if (overflow) { |
| 429 | /* XXX: could be cute and give a different |
| 430 | message for overflow == -1 */ |
| 431 | PyErr_SetString(PyExc_OverflowError, |
| 432 | "Python int too large to convert to C long"); |
| 433 | } |
| 434 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 437 | /* Get a C int from a long int object or any object that has an __int__ |
| 438 | method. Return -1 and set an error if overflow occurs. */ |
| 439 | |
| 440 | int |
| 441 | _PyLong_AsInt(PyObject *obj) |
| 442 | { |
| 443 | int overflow; |
| 444 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 445 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 446 | /* XXX: could be cute and give a different |
| 447 | message for overflow == -1 */ |
| 448 | PyErr_SetString(PyExc_OverflowError, |
| 449 | "Python int too large to convert to C int"); |
| 450 | return -1; |
| 451 | } |
| 452 | return (int)result; |
| 453 | } |
| 454 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 455 | /* Get a Py_ssize_t from a long int object. |
| 456 | Returns -1 and sets an error condition if overflow occurs. */ |
| 457 | |
| 458 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 459 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | register PyLongObject *v; |
| 461 | size_t x, prev; |
| 462 | Py_ssize_t i; |
| 463 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (vv == NULL) { |
| 466 | PyErr_BadInternalCall(); |
| 467 | return -1; |
| 468 | } |
| 469 | if (!PyLong_Check(vv)) { |
| 470 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 471 | return -1; |
| 472 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | v = (PyLongObject *)vv; |
| 475 | i = Py_SIZE(v); |
| 476 | switch (i) { |
| 477 | case -1: return -(sdigit)v->ob_digit[0]; |
| 478 | case 0: return 0; |
| 479 | case 1: return v->ob_digit[0]; |
| 480 | } |
| 481 | sign = 1; |
| 482 | x = 0; |
| 483 | if (i < 0) { |
| 484 | sign = -1; |
| 485 | i = -(i); |
| 486 | } |
| 487 | while (--i >= 0) { |
| 488 | prev = x; |
| 489 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 490 | if ((x >> PyLong_SHIFT) != prev) |
| 491 | goto overflow; |
| 492 | } |
| 493 | /* Haven't lost any bits, but casting to a signed type requires |
| 494 | * extra care (see comment above). |
| 495 | */ |
| 496 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 497 | return (Py_ssize_t)x * sign; |
| 498 | } |
| 499 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 500 | return PY_SSIZE_T_MIN; |
| 501 | } |
| 502 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 503 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 504 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | PyErr_SetString(PyExc_OverflowError, |
| 506 | "Python int too large to convert to C ssize_t"); |
| 507 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Guido van Rossum | d8c8048 | 2002-08-13 00:24:58 +0000 | [diff] [blame] | 510 | /* Get a C unsigned long int from a long int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 511 | Returns -1 and sets an error condition if overflow occurs. */ |
| 512 | |
| 513 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 514 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | register PyLongObject *v; |
| 517 | unsigned long x, prev; |
| 518 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (vv == NULL) { |
| 521 | PyErr_BadInternalCall(); |
| 522 | return (unsigned long)-1; |
| 523 | } |
| 524 | if (!PyLong_Check(vv)) { |
| 525 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 526 | return (unsigned long)-1; |
| 527 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | v = (PyLongObject *)vv; |
| 530 | i = Py_SIZE(v); |
| 531 | x = 0; |
| 532 | if (i < 0) { |
| 533 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 534 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | return (unsigned long) -1; |
| 536 | } |
| 537 | switch (i) { |
| 538 | case 0: return 0; |
| 539 | case 1: return v->ob_digit[0]; |
| 540 | } |
| 541 | while (--i >= 0) { |
| 542 | prev = x; |
| 543 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 544 | if ((x >> PyLong_SHIFT) != prev) { |
| 545 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 546 | "python int too large to convert " |
| 547 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | return (unsigned long) -1; |
| 549 | } |
| 550 | } |
| 551 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 554 | /* Get a C size_t from a long int object. Returns (size_t)-1 and sets |
| 555 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 556 | |
| 557 | size_t |
| 558 | PyLong_AsSize_t(PyObject *vv) |
| 559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | register PyLongObject *v; |
| 561 | size_t x, prev; |
| 562 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | if (vv == NULL) { |
| 565 | PyErr_BadInternalCall(); |
| 566 | return (size_t) -1; |
| 567 | } |
| 568 | if (!PyLong_Check(vv)) { |
| 569 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 570 | return (size_t)-1; |
| 571 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | v = (PyLongObject *)vv; |
| 574 | i = Py_SIZE(v); |
| 575 | x = 0; |
| 576 | if (i < 0) { |
| 577 | PyErr_SetString(PyExc_OverflowError, |
| 578 | "can't convert negative value to size_t"); |
| 579 | return (size_t) -1; |
| 580 | } |
| 581 | switch (i) { |
| 582 | case 0: return 0; |
| 583 | case 1: return v->ob_digit[0]; |
| 584 | } |
| 585 | while (--i >= 0) { |
| 586 | prev = x; |
| 587 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 588 | if ((x >> PyLong_SHIFT) != prev) { |
| 589 | PyErr_SetString(PyExc_OverflowError, |
| 590 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 591 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 597 | /* Get a C unsigned long int from a long int object, ignoring the high bits. |
| 598 | Returns -1 and sets an error condition if an error occurs. */ |
| 599 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 600 | static unsigned long |
| 601 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | register PyLongObject *v; |
| 604 | unsigned long x; |
| 605 | Py_ssize_t i; |
| 606 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | if (vv == NULL || !PyLong_Check(vv)) { |
| 609 | PyErr_BadInternalCall(); |
| 610 | return (unsigned long) -1; |
| 611 | } |
| 612 | v = (PyLongObject *)vv; |
| 613 | i = Py_SIZE(v); |
| 614 | switch (i) { |
| 615 | case 0: return 0; |
| 616 | case 1: return v->ob_digit[0]; |
| 617 | } |
| 618 | sign = 1; |
| 619 | x = 0; |
| 620 | if (i < 0) { |
| 621 | sign = -1; |
| 622 | i = -i; |
| 623 | } |
| 624 | while (--i >= 0) { |
| 625 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 626 | } |
| 627 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 630 | unsigned long |
| 631 | PyLong_AsUnsignedLongMask(register PyObject *op) |
| 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | PyNumberMethods *nb; |
| 634 | PyLongObject *lo; |
| 635 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | if (op && PyLong_Check(op)) |
| 638 | return _PyLong_AsUnsignedLongMask(op); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 641 | nb->nb_int == NULL) { |
| 642 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 643 | return (unsigned long)-1; |
| 644 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | lo = (PyLongObject*) (*nb->nb_int) (op); |
| 647 | if (lo == NULL) |
| 648 | return (unsigned long)-1; |
| 649 | if (PyLong_Check(lo)) { |
| 650 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 651 | Py_DECREF(lo); |
| 652 | if (PyErr_Occurred()) |
| 653 | return (unsigned long)-1; |
| 654 | return val; |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | Py_DECREF(lo); |
| 659 | PyErr_SetString(PyExc_TypeError, |
| 660 | "nb_int should return int object"); |
| 661 | return (unsigned long)-1; |
| 662 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 665 | int |
| 666 | _PyLong_Sign(PyObject *vv) |
| 667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | assert(v != NULL); |
| 671 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 676 | size_t |
| 677 | _PyLong_NumBits(PyObject *vv) |
| 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyLongObject *v = (PyLongObject *)vv; |
| 680 | size_t result = 0; |
| 681 | Py_ssize_t ndigits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | assert(v != NULL); |
| 684 | assert(PyLong_Check(v)); |
| 685 | ndigits = ABS(Py_SIZE(v)); |
| 686 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 687 | if (ndigits > 0) { |
| 688 | digit msd = v->ob_digit[ndigits - 1]; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 689 | if ((size_t)(ndigits - 1) > PY_SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 691 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | do { |
| 693 | ++result; |
| 694 | if (result == 0) |
| 695 | goto Overflow; |
| 696 | msd >>= 1; |
| 697 | } while (msd); |
| 698 | } |
| 699 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 700 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 701 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 703 | "to express in a platform size_t"); |
| 704 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 707 | PyObject * |
| 708 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 710 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 711 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | int incr; /* direction to move pstartbyte */ |
| 713 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 714 | size_t numsignificantbytes; /* number of bytes that matter */ |
| 715 | Py_ssize_t ndigits; /* number of Python long digits */ |
| 716 | PyLongObject* v; /* result */ |
| 717 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | if (n == 0) |
| 720 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | if (little_endian) { |
| 723 | pstartbyte = bytes; |
| 724 | pendbyte = bytes + n - 1; |
| 725 | incr = 1; |
| 726 | } |
| 727 | else { |
| 728 | pstartbyte = bytes + n - 1; |
| 729 | pendbyte = bytes; |
| 730 | incr = -1; |
| 731 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | if (is_signed) |
| 734 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 737 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | is positive, and leading 0xff bytes if negative. */ |
| 739 | { |
| 740 | size_t i; |
| 741 | const unsigned char* p = pendbyte; |
| 742 | const int pincr = -incr; /* search MSB to LSB */ |
| 743 | const unsigned char insignficant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | for (i = 0; i < n; ++i, p += pincr) { |
| 746 | if (*p != insignficant) |
| 747 | break; |
| 748 | } |
| 749 | numsignificantbytes = n - i; |
| 750 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 751 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 752 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 753 | do for 0xffff = -0x0001. To be safe without bothering to |
| 754 | check every case, bump it regardless. */ |
| 755 | if (is_signed && numsignificantbytes < n) |
| 756 | ++numsignificantbytes; |
| 757 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | /* How many Python long digits do we need? We have |
| 760 | 8*numsignificantbytes bits, and each Python long digit has |
| 761 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 762 | /* catch overflow before it happens */ |
| 763 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 764 | PyErr_SetString(PyExc_OverflowError, |
| 765 | "byte array too long to convert to int"); |
| 766 | return NULL; |
| 767 | } |
| 768 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 769 | v = _PyLong_New(ndigits); |
| 770 | if (v == NULL) |
| 771 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 774 | the fly for signed numbers, and dealing with the mismatch between |
| 775 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 776 | { |
| 777 | size_t i; |
| 778 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 779 | twodigits accum = 0; /* sliding register */ |
| 780 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 781 | const unsigned char* p = pstartbyte; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 784 | twodigits thisbyte = *p; |
| 785 | /* Compute correction for 2's comp, if needed. */ |
| 786 | if (is_signed) { |
| 787 | thisbyte = (0xff ^ thisbyte) + carry; |
| 788 | carry = thisbyte >> 8; |
| 789 | thisbyte &= 0xff; |
| 790 | } |
| 791 | /* Because we're going LSB to MSB, thisbyte is |
| 792 | more significant than what's already in accum, |
| 793 | so needs to be prepended to accum. */ |
| 794 | accum |= (twodigits)thisbyte << accumbits; |
| 795 | accumbits += 8; |
| 796 | if (accumbits >= PyLong_SHIFT) { |
| 797 | /* There's enough to fill a Python digit. */ |
| 798 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 799 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | ++idigit; |
| 801 | accum >>= PyLong_SHIFT; |
| 802 | accumbits -= PyLong_SHIFT; |
| 803 | assert(accumbits < PyLong_SHIFT); |
| 804 | } |
| 805 | } |
| 806 | assert(accumbits < PyLong_SHIFT); |
| 807 | if (accumbits) { |
| 808 | assert(idigit < ndigits); |
| 809 | v->ob_digit[idigit] = (digit)accum; |
| 810 | ++idigit; |
| 811 | } |
| 812 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | Py_SIZE(v) = is_signed ? -idigit : idigit; |
| 815 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | int |
| 819 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | unsigned char* bytes, size_t n, |
| 821 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 824 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 826 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 828 | digit carry; /* for computing 2's-comp */ |
| 829 | size_t j; /* # bytes filled */ |
| 830 | unsigned char* p; /* pointer to next byte in bytes */ |
| 831 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | if (Py_SIZE(v) < 0) { |
| 836 | ndigits = -(Py_SIZE(v)); |
| 837 | if (!is_signed) { |
| 838 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 839 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | return -1; |
| 841 | } |
| 842 | do_twos_comp = 1; |
| 843 | } |
| 844 | else { |
| 845 | ndigits = Py_SIZE(v); |
| 846 | do_twos_comp = 0; |
| 847 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | if (little_endian) { |
| 850 | p = bytes; |
| 851 | pincr = 1; |
| 852 | } |
| 853 | else { |
| 854 | p = bytes + n - 1; |
| 855 | pincr = -1; |
| 856 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | /* Copy over all the Python digits. |
| 859 | It's crucial that every Python digit except for the MSD contribute |
| 860 | exactly PyLong_SHIFT bits to the total, so first assert that the long is |
| 861 | normalized. */ |
| 862 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 863 | j = 0; |
| 864 | accum = 0; |
| 865 | accumbits = 0; |
| 866 | carry = do_twos_comp ? 1 : 0; |
| 867 | for (i = 0; i < ndigits; ++i) { |
| 868 | digit thisdigit = v->ob_digit[i]; |
| 869 | if (do_twos_comp) { |
| 870 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 871 | carry = thisdigit >> PyLong_SHIFT; |
| 872 | thisdigit &= PyLong_MASK; |
| 873 | } |
| 874 | /* Because we're going LSB to MSB, thisdigit is more |
| 875 | significant than what's already in accum, so needs to be |
| 876 | prepended to accum. */ |
| 877 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | /* The most-significant digit may be (probably is) at least |
| 880 | partly empty. */ |
| 881 | if (i == ndigits - 1) { |
| 882 | /* Count # of sign bits -- they needn't be stored, |
| 883 | * although for signed conversion we need later to |
| 884 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 885 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | while (s != 0) { |
| 887 | s >>= 1; |
| 888 | accumbits++; |
| 889 | } |
| 890 | } |
| 891 | else |
| 892 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | /* Store as many bytes as possible. */ |
| 895 | while (accumbits >= 8) { |
| 896 | if (j >= n) |
| 897 | goto Overflow; |
| 898 | ++j; |
| 899 | *p = (unsigned char)(accum & 0xff); |
| 900 | p += pincr; |
| 901 | accumbits -= 8; |
| 902 | accum >>= 8; |
| 903 | } |
| 904 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | /* Store the straggler (if any). */ |
| 907 | assert(accumbits < 8); |
| 908 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 909 | if (accumbits > 0) { |
| 910 | if (j >= n) |
| 911 | goto Overflow; |
| 912 | ++j; |
| 913 | if (do_twos_comp) { |
| 914 | /* Fill leading bits of the byte with sign bits |
| 915 | (appropriately pretending that the long had an |
| 916 | infinite supply of sign bits). */ |
| 917 | accum |= (~(twodigits)0) << accumbits; |
| 918 | } |
| 919 | *p = (unsigned char)(accum & 0xff); |
| 920 | p += pincr; |
| 921 | } |
| 922 | else if (j == n && n > 0 && is_signed) { |
| 923 | /* The main loop filled the byte array exactly, so the code |
| 924 | just above didn't get to ensure there's a sign bit, and the |
| 925 | loop below wouldn't add one either. Make sure a sign bit |
| 926 | exists. */ |
| 927 | unsigned char msb = *(p - pincr); |
| 928 | int sign_bit_set = msb >= 0x80; |
| 929 | assert(accumbits == 0); |
| 930 | if (sign_bit_set == do_twos_comp) |
| 931 | return 0; |
| 932 | else |
| 933 | goto Overflow; |
| 934 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | /* Fill remaining bytes with copies of the sign bit. */ |
| 937 | { |
| 938 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 939 | for ( ; j < n; ++j, p += pincr) |
| 940 | *p = signbyte; |
| 941 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 944 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 945 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 947 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 948 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 951 | /* Create a new long int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 952 | |
| 953 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 954 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 955 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 956 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 957 | return PyLong_FromUnsignedLong((unsigned long)(Py_uintptr_t)p); |
| 958 | #else |
| 959 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 960 | #ifndef HAVE_LONG_LONG |
| 961 | # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 962 | #endif |
| 963 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 964 | # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 965 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 967 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 968 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 971 | /* Get a C pointer from a long int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 972 | |
| 973 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 974 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 975 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 976 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 980 | x = PyLong_AsLong(vv); |
| 981 | else |
| 982 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 983 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 984 | |
| 985 | #ifndef HAVE_LONG_LONG |
| 986 | # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 987 | #endif |
| 988 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 989 | # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 990 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | PY_LONG_LONG x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 994 | x = PyLong_AsLongLong(vv); |
| 995 | else |
| 996 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 997 | |
| 998 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | if (x == -1 && PyErr_Occurred()) |
| 1001 | return NULL; |
| 1002 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1005 | #ifdef HAVE_LONG_LONG |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1006 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1007 | /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1008 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1009 | */ |
| 1010 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1011 | #define PY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN) |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1012 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1013 | /* 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] | 1014 | |
| 1015 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1016 | PyLong_FromLongLong(PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1017 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | PyLongObject *v; |
| 1019 | unsigned PY_LONG_LONG abs_ival; |
| 1020 | unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */ |
| 1021 | int ndigits = 0; |
| 1022 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | CHECK_SMALL_INT(ival); |
| 1025 | if (ival < 0) { |
| 1026 | /* avoid signed overflow on negation; see comments |
| 1027 | in PyLong_FromLong above. */ |
| 1028 | abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1; |
| 1029 | negative = 1; |
| 1030 | } |
| 1031 | else { |
| 1032 | abs_ival = (unsigned PY_LONG_LONG)ival; |
| 1033 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | /* Count the number of Python digits. |
| 1036 | We used to pick 5 ("big enough for anything"), but that's a |
| 1037 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1038 | needed. */ |
| 1039 | t = abs_ival; |
| 1040 | while (t) { |
| 1041 | ++ndigits; |
| 1042 | t >>= PyLong_SHIFT; |
| 1043 | } |
| 1044 | v = _PyLong_New(ndigits); |
| 1045 | if (v != NULL) { |
| 1046 | digit *p = v->ob_digit; |
| 1047 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1048 | t = abs_ival; |
| 1049 | while (t) { |
| 1050 | *p++ = (digit)(t & PyLong_MASK); |
| 1051 | t >>= PyLong_SHIFT; |
| 1052 | } |
| 1053 | } |
| 1054 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1057 | /* 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] | 1058 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1059 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1060 | PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | PyLongObject *v; |
| 1063 | unsigned PY_LONG_LONG t; |
| 1064 | int ndigits = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | if (ival < PyLong_BASE) |
| 1067 | return PyLong_FromLong((long)ival); |
| 1068 | /* Count the number of Python digits. */ |
| 1069 | t = (unsigned PY_LONG_LONG)ival; |
| 1070 | while (t) { |
| 1071 | ++ndigits; |
| 1072 | t >>= PyLong_SHIFT; |
| 1073 | } |
| 1074 | v = _PyLong_New(ndigits); |
| 1075 | if (v != NULL) { |
| 1076 | digit *p = v->ob_digit; |
| 1077 | Py_SIZE(v) = ndigits; |
| 1078 | while (ival) { |
| 1079 | *p++ = (digit)(ival & PyLong_MASK); |
| 1080 | ival >>= PyLong_SHIFT; |
| 1081 | } |
| 1082 | } |
| 1083 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | /* Create a new long int object from a C Py_ssize_t. */ |
| 1087 | |
| 1088 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1089 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1090 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | PyLongObject *v; |
| 1092 | size_t abs_ival; |
| 1093 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1094 | int ndigits = 0; |
| 1095 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | CHECK_SMALL_INT(ival); |
| 1098 | if (ival < 0) { |
| 1099 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1100 | abs_ival = (size_t)(-1-ival)+1; |
| 1101 | negative = 1; |
| 1102 | } |
| 1103 | else { |
| 1104 | abs_ival = (size_t)ival; |
| 1105 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | /* Count the number of Python digits. */ |
| 1108 | t = abs_ival; |
| 1109 | while (t) { |
| 1110 | ++ndigits; |
| 1111 | t >>= PyLong_SHIFT; |
| 1112 | } |
| 1113 | v = _PyLong_New(ndigits); |
| 1114 | if (v != NULL) { |
| 1115 | digit *p = v->ob_digit; |
| 1116 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1117 | t = abs_ival; |
| 1118 | while (t) { |
| 1119 | *p++ = (digit)(t & PyLong_MASK); |
| 1120 | t >>= PyLong_SHIFT; |
| 1121 | } |
| 1122 | } |
| 1123 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* Create a new long int object from a C size_t. */ |
| 1127 | |
| 1128 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1129 | PyLong_FromSize_t(size_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | PyLongObject *v; |
| 1132 | size_t t; |
| 1133 | int ndigits = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | if (ival < PyLong_BASE) |
| 1136 | return PyLong_FromLong((long)ival); |
| 1137 | /* Count the number of Python digits. */ |
| 1138 | t = ival; |
| 1139 | while (t) { |
| 1140 | ++ndigits; |
| 1141 | t >>= PyLong_SHIFT; |
| 1142 | } |
| 1143 | v = _PyLong_New(ndigits); |
| 1144 | if (v != NULL) { |
| 1145 | digit *p = v->ob_digit; |
| 1146 | Py_SIZE(v) = ndigits; |
| 1147 | while (ival) { |
| 1148 | *p++ = (digit)(ival & PyLong_MASK); |
| 1149 | ival >>= PyLong_SHIFT; |
| 1150 | } |
| 1151 | } |
| 1152 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1155 | /* Get a C long long int from a long int object or any object that has an |
| 1156 | __int__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1157 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1158 | PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1159 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | PyLongObject *v; |
| 1162 | PY_LONG_LONG bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | if (vv == NULL) { |
| 1166 | PyErr_BadInternalCall(); |
| 1167 | return -1; |
| 1168 | } |
| 1169 | if (!PyLong_Check(vv)) { |
| 1170 | PyNumberMethods *nb; |
| 1171 | PyObject *io; |
| 1172 | if ((nb = vv->ob_type->tp_as_number) == NULL || |
| 1173 | nb->nb_int == NULL) { |
| 1174 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1175 | return -1; |
| 1176 | } |
| 1177 | io = (*nb->nb_int) (vv); |
| 1178 | if (io == NULL) |
| 1179 | return -1; |
| 1180 | if (PyLong_Check(io)) { |
| 1181 | bytes = PyLong_AsLongLong(io); |
| 1182 | Py_DECREF(io); |
| 1183 | return bytes; |
| 1184 | } |
| 1185 | Py_DECREF(io); |
| 1186 | PyErr_SetString(PyExc_TypeError, "integer conversion failed"); |
| 1187 | return -1; |
| 1188 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | v = (PyLongObject*)vv; |
| 1191 | switch(Py_SIZE(v)) { |
| 1192 | case -1: return -(sdigit)v->ob_digit[0]; |
| 1193 | case 0: return 0; |
| 1194 | case 1: return v->ob_digit[0]; |
| 1195 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1196 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1197 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
| 1200 | if (res < 0) |
| 1201 | return (PY_LONG_LONG)-1; |
| 1202 | else |
| 1203 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1206 | /* 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] | 1207 | Return -1 and set an error if overflow occurs. */ |
| 1208 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1209 | unsigned PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1210 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | PyLongObject *v; |
| 1213 | unsigned PY_LONG_LONG bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1215 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1216 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | PyErr_BadInternalCall(); |
| 1218 | return (unsigned PY_LONG_LONG)-1; |
| 1219 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1220 | if (!PyLong_Check(vv)) { |
| 1221 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1222 | return (unsigned PY_LONG_LONG)-1; |
| 1223 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | v = (PyLongObject*)vv; |
| 1226 | switch(Py_SIZE(v)) { |
| 1227 | case 0: return 0; |
| 1228 | case 1: return v->ob_digit[0]; |
| 1229 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1230 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1231 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1232 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
| 1235 | if (res < 0) |
| 1236 | return (unsigned PY_LONG_LONG)res; |
| 1237 | else |
| 1238 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1239 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1240 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1241 | /* Get a C unsigned long int from a long int object, ignoring the high bits. |
| 1242 | Returns -1 and sets an error condition if an error occurs. */ |
| 1243 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1244 | static unsigned PY_LONG_LONG |
| 1245 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | register PyLongObject *v; |
| 1248 | unsigned PY_LONG_LONG x; |
| 1249 | Py_ssize_t i; |
| 1250 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1253 | PyErr_BadInternalCall(); |
| 1254 | return (unsigned long) -1; |
| 1255 | } |
| 1256 | v = (PyLongObject *)vv; |
| 1257 | switch(Py_SIZE(v)) { |
| 1258 | case 0: return 0; |
| 1259 | case 1: return v->ob_digit[0]; |
| 1260 | } |
| 1261 | i = Py_SIZE(v); |
| 1262 | sign = 1; |
| 1263 | x = 0; |
| 1264 | if (i < 0) { |
| 1265 | sign = -1; |
| 1266 | i = -i; |
| 1267 | } |
| 1268 | while (--i >= 0) { |
| 1269 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1270 | } |
| 1271 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1272 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1273 | |
| 1274 | unsigned PY_LONG_LONG |
| 1275 | PyLong_AsUnsignedLongLongMask(register PyObject *op) |
| 1276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | PyNumberMethods *nb; |
| 1278 | PyLongObject *lo; |
| 1279 | unsigned PY_LONG_LONG val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | if (op && PyLong_Check(op)) |
| 1282 | return _PyLong_AsUnsignedLongLongMask(op); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 1285 | nb->nb_int == NULL) { |
| 1286 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1287 | return (unsigned PY_LONG_LONG)-1; |
| 1288 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | lo = (PyLongObject*) (*nb->nb_int) (op); |
| 1291 | if (lo == NULL) |
| 1292 | return (unsigned PY_LONG_LONG)-1; |
| 1293 | if (PyLong_Check(lo)) { |
| 1294 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1295 | Py_DECREF(lo); |
| 1296 | if (PyErr_Occurred()) |
| 1297 | return (unsigned PY_LONG_LONG)-1; |
| 1298 | return val; |
| 1299 | } |
| 1300 | else |
| 1301 | { |
| 1302 | Py_DECREF(lo); |
| 1303 | PyErr_SetString(PyExc_TypeError, |
| 1304 | "nb_int should return int object"); |
| 1305 | return (unsigned PY_LONG_LONG)-1; |
| 1306 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1307 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1308 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1309 | /* Get a C long long int from a long int object or any object that has an |
| 1310 | __int__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1311 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1312 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1313 | the result. Otherwise *overflow is 0. |
| 1314 | |
| 1315 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1316 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1317 | */ |
| 1318 | |
| 1319 | PY_LONG_LONG |
| 1320 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | /* This version by Tim Peters */ |
| 1323 | register PyLongObject *v; |
| 1324 | unsigned PY_LONG_LONG x, prev; |
| 1325 | PY_LONG_LONG res; |
| 1326 | Py_ssize_t i; |
| 1327 | int sign; |
| 1328 | int do_decref = 0; /* if nb_int was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | *overflow = 0; |
| 1331 | if (vv == NULL) { |
| 1332 | PyErr_BadInternalCall(); |
| 1333 | return -1; |
| 1334 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | if (!PyLong_Check(vv)) { |
| 1337 | PyNumberMethods *nb; |
| 1338 | nb = vv->ob_type->tp_as_number; |
| 1339 | if (nb == NULL || nb->nb_int == NULL) { |
| 1340 | PyErr_SetString(PyExc_TypeError, |
| 1341 | "an integer is required"); |
| 1342 | return -1; |
| 1343 | } |
| 1344 | vv = (*nb->nb_int) (vv); |
| 1345 | if (vv == NULL) |
| 1346 | return -1; |
| 1347 | do_decref = 1; |
| 1348 | if (!PyLong_Check(vv)) { |
| 1349 | Py_DECREF(vv); |
| 1350 | PyErr_SetString(PyExc_TypeError, |
| 1351 | "nb_int should return int object"); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | res = -1; |
| 1357 | v = (PyLongObject *)vv; |
| 1358 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | switch (i) { |
| 1361 | case -1: |
| 1362 | res = -(sdigit)v->ob_digit[0]; |
| 1363 | break; |
| 1364 | case 0: |
| 1365 | res = 0; |
| 1366 | break; |
| 1367 | case 1: |
| 1368 | res = v->ob_digit[0]; |
| 1369 | break; |
| 1370 | default: |
| 1371 | sign = 1; |
| 1372 | x = 0; |
| 1373 | if (i < 0) { |
| 1374 | sign = -1; |
| 1375 | i = -(i); |
| 1376 | } |
| 1377 | while (--i >= 0) { |
| 1378 | prev = x; |
| 1379 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1380 | if ((x >> PyLong_SHIFT) != prev) { |
| 1381 | *overflow = sign; |
| 1382 | goto exit; |
| 1383 | } |
| 1384 | } |
| 1385 | /* Haven't lost any bits, but casting to long requires extra |
| 1386 | * care (see comment above). |
| 1387 | */ |
| 1388 | if (x <= (unsigned PY_LONG_LONG)PY_LLONG_MAX) { |
| 1389 | res = (PY_LONG_LONG)x * sign; |
| 1390 | } |
| 1391 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
| 1392 | res = PY_LLONG_MIN; |
| 1393 | } |
| 1394 | else { |
| 1395 | *overflow = sign; |
| 1396 | /* res is already set to -1 */ |
| 1397 | } |
| 1398 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1399 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | if (do_decref) { |
| 1401 | Py_DECREF(vv); |
| 1402 | } |
| 1403 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1406 | #endif /* HAVE_LONG_LONG */ |
| 1407 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1408 | #define CHECK_BINOP(v,w) \ |
| 1409 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1410 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1411 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1412 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1413 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1414 | /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < |
| 1415 | 2**k if d is nonzero, else 0. */ |
| 1416 | |
| 1417 | static const unsigned char BitLengthTable[32] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 1419 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1420 | }; |
| 1421 | |
| 1422 | static int |
| 1423 | bits_in_digit(digit d) |
| 1424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | int d_bits = 0; |
| 1426 | while (d >= 32) { |
| 1427 | d_bits += 6; |
| 1428 | d >>= 6; |
| 1429 | } |
| 1430 | d_bits += (int)BitLengthTable[d]; |
| 1431 | return d_bits; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1434 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1435 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1436 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1437 | */ |
| 1438 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1439 | 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] | 1440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | Py_ssize_t i; |
| 1442 | digit carry = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | assert(m >= n); |
| 1445 | for (i = 0; i < n; ++i) { |
| 1446 | carry += x[i] + y[i]; |
| 1447 | x[i] = carry & PyLong_MASK; |
| 1448 | carry >>= PyLong_SHIFT; |
| 1449 | assert((carry & 1) == carry); |
| 1450 | } |
| 1451 | for (; carry && i < m; ++i) { |
| 1452 | carry += x[i]; |
| 1453 | x[i] = carry & PyLong_MASK; |
| 1454 | carry >>= PyLong_SHIFT; |
| 1455 | assert((carry & 1) == carry); |
| 1456 | } |
| 1457 | return carry; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1461 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1462 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1463 | */ |
| 1464 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1465 | 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] | 1466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | Py_ssize_t i; |
| 1468 | digit borrow = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | assert(m >= n); |
| 1471 | for (i = 0; i < n; ++i) { |
| 1472 | borrow = x[i] - y[i] - borrow; |
| 1473 | x[i] = borrow & PyLong_MASK; |
| 1474 | borrow >>= PyLong_SHIFT; |
| 1475 | borrow &= 1; /* keep only 1 sign bit */ |
| 1476 | } |
| 1477 | for (; borrow && i < m; ++i) { |
| 1478 | borrow = x[i] - borrow; |
| 1479 | x[i] = borrow & PyLong_MASK; |
| 1480 | borrow >>= PyLong_SHIFT; |
| 1481 | borrow &= 1; |
| 1482 | } |
| 1483 | return borrow; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1484 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1485 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1486 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
| 1487 | * result in z[0:m], and return the d bits shifted out of the top. |
| 1488 | */ |
| 1489 | static digit |
| 1490 | v_lshift(digit *z, digit *a, Py_ssize_t m, int d) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | Py_ssize_t i; |
| 1493 | digit carry = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | assert(0 <= d && d < PyLong_SHIFT); |
| 1496 | for (i=0; i < m; i++) { |
| 1497 | twodigits acc = (twodigits)a[i] << d | carry; |
| 1498 | z[i] = (digit)acc & PyLong_MASK; |
| 1499 | carry = (digit)(acc >> PyLong_SHIFT); |
| 1500 | } |
| 1501 | return carry; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
| 1505 | * result in z[0:m], and return the d bits shifted out of the bottom. |
| 1506 | */ |
| 1507 | static digit |
| 1508 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
| 1509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | Py_ssize_t i; |
| 1511 | digit carry = 0; |
| 1512 | digit mask = ((digit)1 << d) - 1U; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | assert(0 <= d && d < PyLong_SHIFT); |
| 1515 | for (i=m; i-- > 0;) { |
| 1516 | twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
| 1517 | carry = (digit)acc & mask; |
| 1518 | z[i] = (digit)(acc >> d); |
| 1519 | } |
| 1520 | return carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1523 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1524 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1525 | 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] | 1526 | _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] | 1527 | immutable. */ |
| 1528 | |
| 1529 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1530 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | twodigits rem = 0; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | assert(n > 0 && n <= PyLong_MASK); |
| 1535 | pin += size; |
| 1536 | pout += size; |
| 1537 | while (--size >= 0) { |
| 1538 | digit hi; |
| 1539 | rem = (rem << PyLong_SHIFT) | *--pin; |
| 1540 | *--pout = hi = (digit)(rem / n); |
| 1541 | rem -= (twodigits)hi * n; |
| 1542 | } |
| 1543 | return (digit)rem; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1546 | /* Divide a long integer by a digit, returning both the quotient |
| 1547 | (as function result) and the remainder (through *prem). |
| 1548 | The sign of a is ignored; n should not be zero. */ |
| 1549 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1550 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1551 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | const Py_ssize_t size = ABS(Py_SIZE(a)); |
| 1554 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | assert(n > 0 && n <= PyLong_MASK); |
| 1557 | z = _PyLong_New(size); |
| 1558 | if (z == NULL) |
| 1559 | return NULL; |
| 1560 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 1561 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1564 | /* Convert a long integer to a base 10 string. Returns a new non-shared |
| 1565 | string. (Return value is non-shared so that callers can modify the |
| 1566 | returned value if necessary.) */ |
| 1567 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1568 | static int |
| 1569 | long_to_decimal_string_internal(PyObject *aa, |
| 1570 | PyObject **p_output, |
| 1571 | _PyUnicodeWriter *writer) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | PyLongObject *scratch, *a; |
| 1574 | PyObject *str; |
| 1575 | Py_ssize_t size, strlen, size_a, i, j; |
| 1576 | digit *pout, *pin, rem, tenpow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | int negative; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1578 | enum PyUnicode_Kind kind; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | a = (PyLongObject *)aa; |
| 1581 | if (a == NULL || !PyLong_Check(a)) { |
| 1582 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1583 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | } |
| 1585 | size_a = ABS(Py_SIZE(a)); |
| 1586 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | /* quick and dirty upper bound for the number of digits |
| 1589 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1594 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
| 1595 | > 3 * _PyLong_DECIMAL_SHIFT |
| 1596 | */ |
| 1597 | if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { |
| 1598 | PyErr_SetString(PyExc_OverflowError, |
| 1599 | "long is too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1600 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | } |
| 1602 | /* the expression size_a * PyLong_SHIFT is now safe from overflow */ |
| 1603 | size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); |
| 1604 | scratch = _PyLong_New(size); |
| 1605 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1606 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1609 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1610 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1611 | pin = a->ob_digit; |
| 1612 | pout = scratch->ob_digit; |
| 1613 | size = 0; |
| 1614 | for (i = size_a; --i >= 0; ) { |
| 1615 | digit hi = pin[i]; |
| 1616 | for (j = 0; j < size; j++) { |
| 1617 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1618 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1619 | pout[j] = (digit)(z - (twodigits)hi * |
| 1620 | _PyLong_DECIMAL_BASE); |
| 1621 | } |
| 1622 | while (hi) { |
| 1623 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1624 | hi /= _PyLong_DECIMAL_BASE; |
| 1625 | } |
| 1626 | /* check for keyboard interrupt */ |
| 1627 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1628 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1629 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1630 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | } |
| 1632 | /* pout should have at least one digit, so that the case when a = 0 |
| 1633 | works correctly */ |
| 1634 | if (size == 0) |
| 1635 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | /* calculate exact length of output string, and allocate */ |
| 1638 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1639 | tenpow = 10; |
| 1640 | rem = pout[size-1]; |
| 1641 | while (rem >= tenpow) { |
| 1642 | tenpow *= 10; |
| 1643 | strlen++; |
| 1644 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1645 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1646 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1647 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1648 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1649 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1650 | kind = writer->kind; |
| 1651 | str = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1653 | else { |
| 1654 | str = PyUnicode_New(strlen, '9'); |
| 1655 | if (str == NULL) { |
| 1656 | Py_DECREF(scratch); |
| 1657 | return -1; |
| 1658 | } |
| 1659 | kind = PyUnicode_KIND(str); |
| 1660 | } |
| 1661 | |
| 1662 | #define WRITE_DIGITS(TYPE) \ |
| 1663 | do { \ |
| 1664 | if (writer) \ |
| 1665 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1666 | else \ |
| 1667 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1668 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1669 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1670 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1671 | for (i=0; i < size - 1; i++) { \ |
| 1672 | rem = pout[i]; \ |
| 1673 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1674 | *--p = '0' + rem % 10; \ |
| 1675 | rem /= 10; \ |
| 1676 | } \ |
| 1677 | } \ |
| 1678 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1679 | rem = pout[i]; \ |
| 1680 | do { \ |
| 1681 | *--p = '0' + rem % 10; \ |
| 1682 | rem /= 10; \ |
| 1683 | } while (rem != 0); \ |
| 1684 | \ |
| 1685 | /* and sign */ \ |
| 1686 | if (negative) \ |
| 1687 | *--p = '-'; \ |
| 1688 | \ |
| 1689 | /* check we've counted correctly */ \ |
| 1690 | if (writer) \ |
| 1691 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1692 | else \ |
| 1693 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1694 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | /* fill the string right-to-left */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1697 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1698 | Py_UCS1 *p; |
| 1699 | WRITE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1701 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1702 | Py_UCS2 *p; |
| 1703 | WRITE_DIGITS(Py_UCS2); |
| 1704 | } |
| 1705 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1706 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1707 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1708 | WRITE_DIGITS(Py_UCS4); |
| 1709 | } |
| 1710 | #undef WRITE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1713 | if (writer) { |
| 1714 | writer->pos += strlen; |
| 1715 | } |
| 1716 | else { |
| 1717 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1718 | *p_output = (PyObject *)str; |
| 1719 | } |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | static PyObject * |
| 1724 | long_to_decimal_string(PyObject *aa) |
| 1725 | { |
| 1726 | PyObject *v; |
| 1727 | if (long_to_decimal_string_internal(aa, &v, NULL) == -1) |
| 1728 | return NULL; |
| 1729 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Mark Dickinson | cd06812 | 2009-09-18 14:53:08 +0000 | [diff] [blame] | 1732 | /* Convert a long int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1733 | which should be one of 2, 8 or 16. Return a string object. |
| 1734 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1735 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1736 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1737 | static int |
| 1738 | long_format_binary(PyObject *aa, int base, int alternate, |
| 1739 | PyObject **p_output, _PyUnicodeWriter *writer) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | register PyLongObject *a = (PyLongObject *)aa; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1742 | PyObject *v; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1743 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1745 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1746 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1748 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1749 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | if (a == NULL || !PyLong_Check(a)) { |
| 1751 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1752 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | } |
| 1754 | size_a = ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1755 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | /* Compute a rough upper bound for the length of the string */ |
| 1758 | switch (base) { |
| 1759 | case 16: |
| 1760 | bits = 4; |
| 1761 | break; |
| 1762 | case 8: |
| 1763 | bits = 3; |
| 1764 | break; |
| 1765 | case 2: |
| 1766 | bits = 1; |
| 1767 | break; |
| 1768 | default: |
| 1769 | assert(0); /* shouldn't ever get here */ |
| 1770 | bits = 0; /* to silence gcc warning */ |
| 1771 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1772 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1773 | /* Compute exact length 'sz' of output string. */ |
| 1774 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1775 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1776 | } |
| 1777 | else { |
| 1778 | Py_ssize_t size_a_in_bits; |
| 1779 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1780 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1781 | PyErr_SetString(PyExc_OverflowError, |
| 1782 | "int is too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1783 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1784 | } |
| 1785 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
| 1786 | bits_in_digit(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1787 | /* Allow 1 character for a '-' sign. */ |
| 1788 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1789 | } |
| 1790 | if (alternate) { |
| 1791 | /* 2 characters for prefix */ |
| 1792 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1793 | } |
| 1794 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1795 | if (writer) { |
| 1796 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1797 | return -1; |
| 1798 | kind = writer->kind; |
| 1799 | v = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | } |
| 1801 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1802 | v = PyUnicode_New(sz, 'x'); |
| 1803 | if (v == NULL) |
| 1804 | return -1; |
| 1805 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1807 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1808 | #define WRITE_DIGITS(TYPE) \ |
| 1809 | do { \ |
| 1810 | if (writer) \ |
| 1811 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 1812 | else \ |
| 1813 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 1814 | \ |
| 1815 | if (size_a == 0) { \ |
| 1816 | *--p = '0'; \ |
| 1817 | } \ |
| 1818 | else { \ |
| 1819 | /* JRH: special case for power-of-2 bases */ \ |
| 1820 | twodigits accum = 0; \ |
| 1821 | int accumbits = 0; /* # of bits in accum */ \ |
| 1822 | Py_ssize_t i; \ |
| 1823 | for (i = 0; i < size_a; ++i) { \ |
| 1824 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1825 | accumbits += PyLong_SHIFT; \ |
| 1826 | assert(accumbits >= bits); \ |
| 1827 | do { \ |
| 1828 | char cdigit; \ |
| 1829 | cdigit = (char)(accum & (base - 1)); \ |
| 1830 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1831 | *--p = cdigit; \ |
| 1832 | accumbits -= bits; \ |
| 1833 | accum >>= bits; \ |
| 1834 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1835 | } \ |
| 1836 | } \ |
| 1837 | \ |
| 1838 | if (alternate) { \ |
| 1839 | if (base == 16) \ |
| 1840 | *--p = 'x'; \ |
| 1841 | else if (base == 8) \ |
| 1842 | *--p = 'o'; \ |
| 1843 | else /* (base == 2) */ \ |
| 1844 | *--p = 'b'; \ |
| 1845 | *--p = '0'; \ |
| 1846 | } \ |
| 1847 | if (negative) \ |
| 1848 | *--p = '-'; \ |
| 1849 | if (writer) \ |
| 1850 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1851 | else \ |
| 1852 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 1853 | } while (0) |
| 1854 | |
| 1855 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1856 | Py_UCS1 *p; |
| 1857 | WRITE_DIGITS(Py_UCS1); |
| 1858 | } |
| 1859 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1860 | Py_UCS2 *p; |
| 1861 | WRITE_DIGITS(Py_UCS2); |
| 1862 | } |
| 1863 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1864 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1865 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1866 | WRITE_DIGITS(Py_UCS4); |
| 1867 | } |
| 1868 | #undef WRITE_DIGITS |
| 1869 | |
| 1870 | if (writer) { |
| 1871 | writer->pos += sz; |
| 1872 | } |
| 1873 | else { |
| 1874 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 1875 | *p_output = v; |
| 1876 | } |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | PyObject * |
| 1881 | _PyLong_Format(PyObject *obj, int base) |
| 1882 | { |
| 1883 | PyObject *str; |
| 1884 | int err; |
| 1885 | if (base == 10) |
| 1886 | err = long_to_decimal_string_internal(obj, &str, NULL); |
| 1887 | else |
| 1888 | err = long_format_binary(obj, base, 1, &str, NULL); |
| 1889 | if (err == -1) |
| 1890 | return NULL; |
| 1891 | return str; |
| 1892 | } |
| 1893 | |
| 1894 | int |
| 1895 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 1896 | PyObject *obj, |
| 1897 | int base, int alternate) |
| 1898 | { |
| 1899 | if (base == 10) |
| 1900 | return long_to_decimal_string_internal(obj, NULL, writer); |
| 1901 | else |
| 1902 | return long_format_binary(obj, base, alternate, NULL, writer); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1905 | /* Table of digit values for 8-bit string -> integer conversion. |
| 1906 | * '0' maps to 0, ..., '9' maps to 9. |
| 1907 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 1908 | * All other indices map to 37. |
| 1909 | * 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] | 1910 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1911 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 1912 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1914 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1915 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1916 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 1917 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1918 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1919 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1920 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1921 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1922 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1923 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1924 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1925 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1926 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1927 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1928 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1929 | }; |
| 1930 | |
| 1931 | /* *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] | 1932 | * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first |
| 1933 | * non-digit (which may be *str!). A normalized long is returned. |
| 1934 | * The point to this routine is that it takes time linear in the number of |
| 1935 | * string characters. |
| 1936 | */ |
| 1937 | static PyLongObject * |
| 1938 | long_from_binary_base(char **str, int base) |
| 1939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | char *p = *str; |
| 1941 | char *start = p; |
| 1942 | int bits_per_char; |
| 1943 | Py_ssize_t n; |
| 1944 | PyLongObject *z; |
| 1945 | twodigits accum; |
| 1946 | int bits_in_accum; |
| 1947 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 1950 | n = base; |
| 1951 | for (bits_per_char = -1; n; ++bits_per_char) |
| 1952 | n >>= 1; |
| 1953 | /* n <- total # of bits needed, while setting p to end-of-string */ |
| 1954 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) |
| 1955 | ++p; |
| 1956 | *str = p; |
| 1957 | /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ |
| 1958 | n = (p - start) * bits_per_char + PyLong_SHIFT - 1; |
| 1959 | if (n / bits_per_char < p - start) { |
| 1960 | PyErr_SetString(PyExc_ValueError, |
| 1961 | "int string too large to convert"); |
| 1962 | return NULL; |
| 1963 | } |
| 1964 | n = n / PyLong_SHIFT; |
| 1965 | z = _PyLong_New(n); |
| 1966 | if (z == NULL) |
| 1967 | return NULL; |
| 1968 | /* Read string from right, and fill in long from left; i.e., |
| 1969 | * from least to most significant in both. |
| 1970 | */ |
| 1971 | accum = 0; |
| 1972 | bits_in_accum = 0; |
| 1973 | pdigit = z->ob_digit; |
| 1974 | while (--p >= start) { |
| 1975 | int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
| 1976 | assert(k >= 0 && k < base); |
| 1977 | accum |= (twodigits)k << bits_in_accum; |
| 1978 | bits_in_accum += bits_per_char; |
| 1979 | if (bits_in_accum >= PyLong_SHIFT) { |
| 1980 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 1981 | assert(pdigit - z->ob_digit <= n); |
| 1982 | accum >>= PyLong_SHIFT; |
| 1983 | bits_in_accum -= PyLong_SHIFT; |
| 1984 | assert(bits_in_accum < PyLong_SHIFT); |
| 1985 | } |
| 1986 | } |
| 1987 | if (bits_in_accum) { |
| 1988 | assert(bits_in_accum <= PyLong_SHIFT); |
| 1989 | *pdigit++ = (digit)accum; |
| 1990 | assert(pdigit - z->ob_digit <= n); |
| 1991 | } |
| 1992 | while (pdigit - z->ob_digit < n) |
| 1993 | *pdigit++ = 0; |
| 1994 | return long_normalize(z); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1997 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1998 | PyLong_FromString(char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | int sign = 1, error_if_nonzero = 0; |
| 2001 | char *start, *orig_str = str; |
| 2002 | PyLongObject *z = NULL; |
| 2003 | PyObject *strobj; |
| 2004 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | if ((base != 0 && base < 2) || base > 36) { |
| 2007 | PyErr_SetString(PyExc_ValueError, |
| 2008 | "int() arg 2 must be >= 2 and <= 36"); |
| 2009 | return NULL; |
| 2010 | } |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 2011 | while (*str != '\0' && Py_ISSPACE(Py_CHARMASK(*str))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | str++; |
| 2013 | if (*str == '+') |
| 2014 | ++str; |
| 2015 | else if (*str == '-') { |
| 2016 | ++str; |
| 2017 | sign = -1; |
| 2018 | } |
| 2019 | if (base == 0) { |
| 2020 | if (str[0] != '0') |
| 2021 | base = 10; |
| 2022 | else if (str[1] == 'x' || str[1] == 'X') |
| 2023 | base = 16; |
| 2024 | else if (str[1] == 'o' || str[1] == 'O') |
| 2025 | base = 8; |
| 2026 | else if (str[1] == 'b' || str[1] == 'B') |
| 2027 | base = 2; |
| 2028 | else { |
| 2029 | /* "old" (C-style) octal literal, now invalid. |
| 2030 | it might still be zero though */ |
| 2031 | error_if_nonzero = 1; |
| 2032 | base = 10; |
| 2033 | } |
| 2034 | } |
| 2035 | if (str[0] == '0' && |
| 2036 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2037 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
| 2038 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) |
| 2039 | str += 2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | start = str; |
| 2042 | if ((base & (base - 1)) == 0) |
| 2043 | z = long_from_binary_base(&str, base); |
| 2044 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2045 | /*** |
| 2046 | Binary bases can be converted in time linear in the number of digits, because |
| 2047 | Python's representation base is binary. Other bases (including decimal!) use |
| 2048 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2049 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2050 | First some math: the largest integer that can be expressed in N base-B digits |
| 2051 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2052 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2053 | |
| 2054 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2055 | BASE**n >= B**N [taking logs to base BASE] |
| 2056 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2057 | |
| 2058 | The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute |
| 2059 | this quickly. A Python long with that much space is reserved near the start, |
| 2060 | and the result is computed into it. |
| 2061 | |
| 2062 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2063 | are processed at a time), where two more static arrays hold: |
| 2064 | |
| 2065 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2066 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2067 | |
| 2068 | The first of these is the largest i such that i consecutive input digits |
| 2069 | must fit in a single Python digit. The second is effectively the input |
| 2070 | base we're really using. |
| 2071 | |
| 2072 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2073 | convmultmax_base[base], the result is "simply" |
| 2074 | |
| 2075 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2076 | |
| 2077 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2078 | |
| 2079 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2080 | case |
| 2081 | |
| 2082 | n >= N * log(B)/log(BASE) |
| 2083 | |
| 2084 | where `N` is the number of input digits in base `B`. This is computed via |
| 2085 | |
| 2086 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2087 | |
| 2088 | below. Two numeric concerns are how much space this can waste, and whether |
| 2089 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2090 | which is the default (and it's unlikely anyone changes that). |
| 2091 | |
| 2092 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2093 | between the worst-case input with N digits and the smallest input with N |
| 2094 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2095 | one allocated Python digit can remain unused on that count. If |
| 2096 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2097 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2098 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2099 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2100 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2101 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2102 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2103 | |
| 2104 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2105 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2106 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2107 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2108 | close can a transcendental function get to an integer over some range?" |
| 2109 | questions are generally theoretically intractable. Computer analysis via |
| 2110 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2111 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2112 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2113 | we can get very close to being in trouble, but very rarely. For example, |
| 2114 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2115 | log(10)/log(2**15), and indeed: |
| 2116 | |
| 2117 | >>> log(10)/log(2**15)*76573 |
| 2118 | 16958.000000654003 |
| 2119 | |
| 2120 | is very close to an integer. If we were working with IEEE single-precision, |
| 2121 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2122 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2123 | Instead the code checks to see whether the allocated space is enough as each |
| 2124 | new Python digit is added, and copies the whole thing to a larger long if not. |
| 2125 | This should happen extremely rarely, and in fact I don't have a test case |
| 2126 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2127 | just 1 digit at the start, so that the copying code was exercised for every |
| 2128 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2129 | ***/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | register twodigits c; /* current input character */ |
| 2131 | Py_ssize_t size_z; |
| 2132 | int i; |
| 2133 | int convwidth; |
| 2134 | twodigits convmultmax, convmult; |
| 2135 | digit *pz, *pzstop; |
| 2136 | char* scan; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | static double log_base_BASE[37] = {0.0e0,}; |
| 2139 | static int convwidth_base[37] = {0,}; |
| 2140 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | if (log_base_BASE[base] == 0.0) { |
| 2143 | twodigits convmax = base; |
| 2144 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2145 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2146 | log_base_BASE[base] = (log((double)base) / |
| 2147 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | for (;;) { |
| 2149 | twodigits next = convmax * base; |
| 2150 | if (next > PyLong_BASE) |
| 2151 | break; |
| 2152 | convmax = next; |
| 2153 | ++i; |
| 2154 | } |
| 2155 | convmultmax_base[base] = convmax; |
| 2156 | assert(i > 0); |
| 2157 | convwidth_base[base] = i; |
| 2158 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | /* Find length of the string of numeric characters. */ |
| 2161 | scan = str; |
| 2162 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) |
| 2163 | ++scan; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | /* Create a long object that can contain the largest possible |
| 2166 | * integer with this base and length. Note that there's no |
| 2167 | * need to initialize z->ob_digit -- no slot is read up before |
| 2168 | * being stored into. |
| 2169 | */ |
| 2170 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2171 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2172 | /* size_z = 1; */ |
| 2173 | assert(size_z > 0); |
| 2174 | z = _PyLong_New(size_z); |
| 2175 | if (z == NULL) |
| 2176 | return NULL; |
| 2177 | Py_SIZE(z) = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | /* `convwidth` consecutive input digits are treated as a single |
| 2180 | * digit in base `convmultmax`. |
| 2181 | */ |
| 2182 | convwidth = convwidth_base[base]; |
| 2183 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | /* Work ;-) */ |
| 2186 | while (str < scan) { |
| 2187 | /* grab up to convwidth digits from the input string */ |
| 2188 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
| 2189 | for (i = 1; i < convwidth && str != scan; ++i, ++str) { |
| 2190 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2191 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | assert(c < PyLong_BASE); |
| 2193 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | convmult = convmultmax; |
| 2196 | /* Calculate the shift only if we couldn't get |
| 2197 | * convwidth digits. |
| 2198 | */ |
| 2199 | if (i != convwidth) { |
| 2200 | convmult = base; |
| 2201 | for ( ; i > 1; --i) |
| 2202 | convmult *= base; |
| 2203 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | /* Multiply z by convmult, and add c. */ |
| 2206 | pz = z->ob_digit; |
| 2207 | pzstop = pz + Py_SIZE(z); |
| 2208 | for (; pz < pzstop; ++pz) { |
| 2209 | c += (twodigits)*pz * convmult; |
| 2210 | *pz = (digit)(c & PyLong_MASK); |
| 2211 | c >>= PyLong_SHIFT; |
| 2212 | } |
| 2213 | /* carry off the current end? */ |
| 2214 | if (c) { |
| 2215 | assert(c < PyLong_BASE); |
| 2216 | if (Py_SIZE(z) < size_z) { |
| 2217 | *pz = (digit)c; |
| 2218 | ++Py_SIZE(z); |
| 2219 | } |
| 2220 | else { |
| 2221 | PyLongObject *tmp; |
| 2222 | /* Extremely rare. Get more space. */ |
| 2223 | assert(Py_SIZE(z) == size_z); |
| 2224 | tmp = _PyLong_New(size_z + 1); |
| 2225 | if (tmp == NULL) { |
| 2226 | Py_DECREF(z); |
| 2227 | return NULL; |
| 2228 | } |
| 2229 | memcpy(tmp->ob_digit, |
| 2230 | z->ob_digit, |
| 2231 | sizeof(digit) * size_z); |
| 2232 | Py_DECREF(z); |
| 2233 | z = tmp; |
| 2234 | z->ob_digit[size_z] = (digit)c; |
| 2235 | ++size_z; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | if (z == NULL) |
| 2241 | return NULL; |
| 2242 | if (error_if_nonzero) { |
| 2243 | /* reset the base to 0, else the exception message |
| 2244 | doesn't make too much sense */ |
| 2245 | base = 0; |
| 2246 | if (Py_SIZE(z) != 0) |
| 2247 | goto onError; |
| 2248 | /* there might still be other problems, therefore base |
| 2249 | remains zero here for the same reason */ |
| 2250 | } |
| 2251 | if (str == start) |
| 2252 | goto onError; |
| 2253 | if (sign < 0) |
| 2254 | Py_SIZE(z) = -(Py_SIZE(z)); |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 2255 | while (*str && Py_ISSPACE(Py_CHARMASK(*str))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | str++; |
| 2257 | if (*str != '\0') |
| 2258 | goto onError; |
| 2259 | if (pend) |
| 2260 | *pend = str; |
| 2261 | long_normalize(z); |
| 2262 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2263 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2264 | onError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | Py_XDECREF(z); |
| 2266 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2267 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
| 2268 | if (strobj == NULL) |
| 2269 | return NULL; |
| 2270 | PyErr_Format(PyExc_ValueError, |
| 2271 | "invalid literal for int() with base %d: %R", |
| 2272 | base, strobj); |
| 2273 | Py_DECREF(strobj); |
| 2274 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2278 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2279 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2280 | PyObject *v, *unicode = PyUnicode_FromUnicode(u, length); |
| 2281 | if (unicode == NULL) |
| 2282 | return NULL; |
| 2283 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2284 | Py_DECREF(unicode); |
| 2285 | return v; |
| 2286 | } |
| 2287 | |
| 2288 | PyObject * |
| 2289 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | PyObject *result; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2292 | PyObject *asciidig; |
| 2293 | char *buffer, *end; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2294 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2295 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2296 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2297 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2299 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2300 | if (buffer == NULL) { |
| 2301 | Py_DECREF(asciidig); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | return NULL; |
| 2303 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2304 | result = PyLong_FromString(buffer, &end, base); |
| 2305 | if (result != NULL && end != buffer + buflen) { |
| 2306 | PyErr_SetString(PyExc_ValueError, |
| 2307 | "null byte in argument for int()"); |
| 2308 | Py_DECREF(result); |
| 2309 | result = NULL; |
| 2310 | } |
| 2311 | Py_DECREF(asciidig); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | return result; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2315 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2316 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 2318 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2319 | |
| 2320 | /* Long division with remainder, top-level routine */ |
| 2321 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2322 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2323 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
| 2327 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | if (size_b == 0) { |
| 2330 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2331 | "integer division or modulo by zero"); |
| 2332 | return -1; |
| 2333 | } |
| 2334 | if (size_a < size_b || |
| 2335 | (size_a == size_b && |
| 2336 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2337 | /* |a| < |b|. */ |
| 2338 | *pdiv = (PyLongObject*)PyLong_FromLong(0); |
| 2339 | if (*pdiv == NULL) |
| 2340 | return -1; |
| 2341 | Py_INCREF(a); |
| 2342 | *prem = (PyLongObject *) a; |
| 2343 | return 0; |
| 2344 | } |
| 2345 | if (size_b == 1) { |
| 2346 | digit rem = 0; |
| 2347 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2348 | if (z == NULL) |
| 2349 | return -1; |
| 2350 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2351 | if (*prem == NULL) { |
| 2352 | Py_DECREF(z); |
| 2353 | return -1; |
| 2354 | } |
| 2355 | } |
| 2356 | else { |
| 2357 | z = x_divrem(a, b, prem); |
| 2358 | if (z == NULL) |
| 2359 | return -1; |
| 2360 | } |
| 2361 | /* Set the signs. |
| 2362 | The quotient z has the sign of a*b; |
| 2363 | the remainder r has the sign of a, |
| 2364 | so a = b*z + r. */ |
| 2365 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) |
| 2366 | NEGATE(z); |
| 2367 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) |
| 2368 | NEGATE(*prem); |
| 2369 | *pdiv = maybe_small_long(z); |
| 2370 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2373 | /* Unsigned long division with remainder -- the algorithm. The arguments v1 |
| 2374 | and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2375 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2376 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2377 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | PyLongObject *v, *w, *a; |
| 2380 | Py_ssize_t i, k, size_v, size_w; |
| 2381 | int d; |
| 2382 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2383 | twodigits vv; |
| 2384 | sdigit zhi; |
| 2385 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2388 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2389 | handle the special case when the initial estimate q for a quotient |
| 2390 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2391 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | /* allocate space; w will also be used to hold the final remainder */ |
| 2394 | size_v = ABS(Py_SIZE(v1)); |
| 2395 | size_w = ABS(Py_SIZE(w1)); |
| 2396 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2397 | v = _PyLong_New(size_v+1); |
| 2398 | if (v == NULL) { |
| 2399 | *prem = NULL; |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | w = _PyLong_New(size_w); |
| 2403 | if (w == NULL) { |
| 2404 | Py_DECREF(v); |
| 2405 | *prem = NULL; |
| 2406 | return NULL; |
| 2407 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2409 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2410 | shift v1 left by the same amount. Results go into w and v. */ |
| 2411 | d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]); |
| 2412 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2413 | assert(carry == 0); |
| 2414 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2415 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2416 | v->ob_digit[size_v] = carry; |
| 2417 | size_v++; |
| 2418 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2421 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2422 | k = size_v - size_w; |
| 2423 | assert(k >= 0); |
| 2424 | a = _PyLong_New(k); |
| 2425 | if (a == NULL) { |
| 2426 | Py_DECREF(w); |
| 2427 | Py_DECREF(v); |
| 2428 | *prem = NULL; |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | v0 = v->ob_digit; |
| 2432 | w0 = w->ob_digit; |
| 2433 | wm1 = w0[size_w-1]; |
| 2434 | wm2 = w0[size_w-2]; |
| 2435 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2436 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2437 | single-digit quotient q, remainder in vk[0:size_w]. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2440 | Py_DECREF(a); |
| 2441 | Py_DECREF(w); |
| 2442 | Py_DECREF(v); |
| 2443 | *prem = NULL; |
| 2444 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2445 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2448 | vtop = vk[size_w]; |
| 2449 | assert(vtop <= wm1); |
| 2450 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2451 | q = (digit)(vv / wm1); |
| 2452 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2453 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2454 | | vk[size_w-2])) { |
| 2455 | --q; |
| 2456 | r += wm1; |
| 2457 | if (r >= PyLong_BASE) |
| 2458 | break; |
| 2459 | } |
| 2460 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2463 | zhi = 0; |
| 2464 | for (i = 0; i < size_w; ++i) { |
| 2465 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2466 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2467 | z = (sdigit)vk[i] + zhi - |
| 2468 | (stwodigits)q * (stwodigits)w0[i]; |
| 2469 | vk[i] = (digit)z & PyLong_MASK; |
| 2470 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2471 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | /* add w back if q was too large (this branch taken rarely) */ |
| 2475 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2476 | if ((sdigit)vtop + zhi < 0) { |
| 2477 | carry = 0; |
| 2478 | for (i = 0; i < size_w; ++i) { |
| 2479 | carry += vk[i] + w0[i]; |
| 2480 | vk[i] = carry & PyLong_MASK; |
| 2481 | carry >>= PyLong_SHIFT; |
| 2482 | } |
| 2483 | --q; |
| 2484 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | /* store quotient digit */ |
| 2487 | assert(q < PyLong_BASE); |
| 2488 | *--ak = q; |
| 2489 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | /* unshift remainder; we reuse w to store the result */ |
| 2492 | carry = v_rshift(w0, v0, size_w, d); |
| 2493 | assert(carry==0); |
| 2494 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | *prem = long_normalize(w); |
| 2497 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2500 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2501 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2502 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2503 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2504 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2505 | -1.0. */ |
| 2506 | |
| 2507 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2508 | #if DBL_MANT_DIG == 53 |
| 2509 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2510 | #else |
| 2511 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2512 | #endif |
| 2513 | |
| 2514 | double |
| 2515 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2518 | /* See below for why x_digits is always large enough. */ |
| 2519 | digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; |
| 2520 | double dx; |
| 2521 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2522 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2523 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2524 | static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1}; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | a_size = ABS(Py_SIZE(a)); |
| 2527 | if (a_size == 0) { |
| 2528 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2529 | *e = 0; |
| 2530 | return 0.0; |
| 2531 | } |
| 2532 | a_bits = bits_in_digit(a->ob_digit[a_size-1]); |
| 2533 | /* The following is an overflow-free version of the check |
| 2534 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2535 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2536 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2537 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2538 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2542 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | Number of digits needed for result: write // for floor division. |
| 2545 | Then if shifting left, we end up using |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | digits. If shifting right, we use |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2554 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2557 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2558 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | valid for any integers m and n, we find that x_size satisfies |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | in both cases. |
| 2565 | */ |
| 2566 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2567 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2568 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
| 2569 | x_size = 0; |
| 2570 | while (x_size < shift_digits) |
| 2571 | x_digits[x_size++] = 0; |
| 2572 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2573 | (int)shift_bits); |
| 2574 | x_size += a_size; |
| 2575 | x_digits[x_size++] = rem; |
| 2576 | } |
| 2577 | else { |
| 2578 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2579 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2580 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2581 | a_size - shift_digits, (int)shift_bits); |
| 2582 | x_size = a_size - shift_digits; |
| 2583 | /* For correct rounding below, we need the least significant |
| 2584 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2585 | shifted out was nonzero, we set the least significant bit |
| 2586 | of x. */ |
| 2587 | if (rem) |
| 2588 | x_digits[0] |= 1; |
| 2589 | else |
| 2590 | while (shift_digits > 0) |
| 2591 | if (a->ob_digit[--shift_digits]) { |
| 2592 | x_digits[0] |= 1; |
| 2593 | break; |
| 2594 | } |
| 2595 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2596 | assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits)); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | /* Round, and convert to double. */ |
| 2599 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2600 | dx = x_digits[--x_size]; |
| 2601 | while (x_size > 0) |
| 2602 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | /* Rescale; make correction if result is 1.0. */ |
| 2605 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2606 | if (dx == 1.0) { |
| 2607 | if (a_bits == PY_SSIZE_T_MAX) |
| 2608 | goto overflow; |
| 2609 | dx = 0.5; |
| 2610 | a_bits += 1; |
| 2611 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | *e = a_bits; |
| 2614 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2615 | |
| 2616 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | /* exponent > PY_SSIZE_T_MAX */ |
| 2618 | PyErr_SetString(PyExc_OverflowError, |
| 2619 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2620 | *e = 0; |
| 2621 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | /* Get a C double from a long int object. Rounds to the nearest double, |
| 2625 | using the round-half-to-even rule in the case of a tie. */ |
| 2626 | |
| 2627 | double |
| 2628 | PyLong_AsDouble(PyObject *v) |
| 2629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | Py_ssize_t exponent; |
| 2631 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2632 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2633 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | PyErr_BadInternalCall(); |
| 2635 | return -1.0; |
| 2636 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2637 | if (!PyLong_Check(v)) { |
| 2638 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2639 | return -1.0; |
| 2640 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2642 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2643 | PyErr_SetString(PyExc_OverflowError, |
| 2644 | "long int too large to convert to float"); |
| 2645 | return -1.0; |
| 2646 | } |
| 2647 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2650 | /* Methods */ |
| 2651 | |
| 2652 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2653 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | Py_TYPE(v)->tp_free(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2658 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2659 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | Py_ssize_t sign; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | if (Py_SIZE(a) != Py_SIZE(b)) { |
| 2664 | sign = Py_SIZE(a) - Py_SIZE(b); |
| 2665 | } |
| 2666 | else { |
| 2667 | Py_ssize_t i = ABS(Py_SIZE(a)); |
| 2668 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2669 | ; |
| 2670 | if (i < 0) |
| 2671 | sign = 0; |
| 2672 | else { |
| 2673 | sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i]; |
| 2674 | if (Py_SIZE(a) < 0) |
| 2675 | sign = -sign; |
| 2676 | } |
| 2677 | } |
| 2678 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2681 | #define TEST_COND(cond) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2683 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2684 | static PyObject * |
| 2685 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 2686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | int result; |
| 2688 | PyObject *v; |
| 2689 | CHECK_BINOP(self, other); |
| 2690 | if (self == other) |
| 2691 | result = 0; |
| 2692 | else |
| 2693 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
| 2694 | /* Convert the return value to a Boolean */ |
| 2695 | switch (op) { |
| 2696 | case Py_EQ: |
| 2697 | v = TEST_COND(result == 0); |
| 2698 | break; |
| 2699 | case Py_NE: |
| 2700 | v = TEST_COND(result != 0); |
| 2701 | break; |
| 2702 | case Py_LE: |
| 2703 | v = TEST_COND(result <= 0); |
| 2704 | break; |
| 2705 | case Py_GE: |
| 2706 | v = TEST_COND(result >= 0); |
| 2707 | break; |
| 2708 | case Py_LT: |
| 2709 | v = TEST_COND(result == -1); |
| 2710 | break; |
| 2711 | case Py_GT: |
| 2712 | v = TEST_COND(result == 1); |
| 2713 | break; |
| 2714 | default: |
| 2715 | PyErr_BadArgument(); |
| 2716 | return NULL; |
| 2717 | } |
| 2718 | Py_INCREF(v); |
| 2719 | return v; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2722 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2723 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2724 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2725 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | Py_ssize_t i; |
| 2727 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | i = Py_SIZE(v); |
| 2730 | switch(i) { |
| 2731 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 2732 | case 0: return 0; |
| 2733 | case 1: return v->ob_digit[0]; |
| 2734 | } |
| 2735 | sign = 1; |
| 2736 | x = 0; |
| 2737 | if (i < 0) { |
| 2738 | sign = -1; |
| 2739 | i = -(i); |
| 2740 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2742 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 2743 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 2744 | _PyHASH_MODULUS. |
| 2745 | |
| 2746 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 2747 | amounts to a rotation of the bits of x. To see this, write |
| 2748 | |
| 2749 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 2750 | |
| 2751 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 2752 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 2753 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 2754 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 2755 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 2756 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 2757 | congruent to y modulo _PyHASH_MODULUS. So |
| 2758 | |
| 2759 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 2760 | |
| 2761 | The right-hand side is just the result of rotating the |
| 2762 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 2763 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 2764 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 2765 | the reduction of x*2**PyLong_SHIFT modulo |
| 2766 | _PyHASH_MODULUS. */ |
| 2767 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 2768 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2770 | if (x >= _PyHASH_MODULUS) |
| 2771 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | } |
| 2773 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2774 | if (x == (Py_uhash_t)-1) |
| 2775 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2776 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2780 | /* Add the absolute values of two long integers. */ |
| 2781 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2782 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2783 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
| 2786 | PyLongObject *z; |
| 2787 | Py_ssize_t i; |
| 2788 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | /* Ensure a is the larger of the two: */ |
| 2791 | if (size_a < size_b) { |
| 2792 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2793 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2794 | size_a = size_b; |
| 2795 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | } |
| 2797 | z = _PyLong_New(size_a+1); |
| 2798 | if (z == NULL) |
| 2799 | return NULL; |
| 2800 | for (i = 0; i < size_b; ++i) { |
| 2801 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 2802 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2803 | carry >>= PyLong_SHIFT; |
| 2804 | } |
| 2805 | for (; i < size_a; ++i) { |
| 2806 | carry += a->ob_digit[i]; |
| 2807 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2808 | carry >>= PyLong_SHIFT; |
| 2809 | } |
| 2810 | z->ob_digit[i] = carry; |
| 2811 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | /* Subtract the absolute values of two integers. */ |
| 2815 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2816 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2817 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
| 2820 | PyLongObject *z; |
| 2821 | Py_ssize_t i; |
| 2822 | int sign = 1; |
| 2823 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | /* Ensure a is the larger of the two: */ |
| 2826 | if (size_a < size_b) { |
| 2827 | sign = -1; |
| 2828 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2829 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2830 | size_a = size_b; |
| 2831 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | } |
| 2833 | else if (size_a == size_b) { |
| 2834 | /* Find highest digit where a and b differ: */ |
| 2835 | i = size_a; |
| 2836 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2837 | ; |
| 2838 | if (i < 0) |
| 2839 | return (PyLongObject *)PyLong_FromLong(0); |
| 2840 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 2841 | sign = -1; |
| 2842 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2843 | } |
| 2844 | size_a = size_b = i+1; |
| 2845 | } |
| 2846 | z = _PyLong_New(size_a); |
| 2847 | if (z == NULL) |
| 2848 | return NULL; |
| 2849 | for (i = 0; i < size_b; ++i) { |
| 2850 | /* The following assumes unsigned arithmetic |
| 2851 | works module 2**N for some N>PyLong_SHIFT. */ |
| 2852 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 2853 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2854 | borrow >>= PyLong_SHIFT; |
| 2855 | borrow &= 1; /* Keep only one sign bit */ |
| 2856 | } |
| 2857 | for (; i < size_a; ++i) { |
| 2858 | borrow = a->ob_digit[i] - borrow; |
| 2859 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2860 | borrow >>= PyLong_SHIFT; |
| 2861 | borrow &= 1; /* Keep only one sign bit */ |
| 2862 | } |
| 2863 | assert(borrow == 0); |
| 2864 | if (sign < 0) |
| 2865 | NEGATE(z); |
| 2866 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2869 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2870 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
| 2877 | PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) + |
| 2878 | MEDIUM_VALUE(b)); |
| 2879 | return result; |
| 2880 | } |
| 2881 | if (Py_SIZE(a) < 0) { |
| 2882 | if (Py_SIZE(b) < 0) { |
| 2883 | z = x_add(a, b); |
| 2884 | if (z != NULL && Py_SIZE(z) != 0) |
| 2885 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 2886 | } |
| 2887 | else |
| 2888 | z = x_sub(b, a); |
| 2889 | } |
| 2890 | else { |
| 2891 | if (Py_SIZE(b) < 0) |
| 2892 | z = x_sub(a, b); |
| 2893 | else |
| 2894 | z = x_add(a, b); |
| 2895 | } |
| 2896 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2899 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 2900 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
| 2907 | PyObject* r; |
| 2908 | r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); |
| 2909 | return r; |
| 2910 | } |
| 2911 | if (Py_SIZE(a) < 0) { |
| 2912 | if (Py_SIZE(b) < 0) |
| 2913 | z = x_sub(a, b); |
| 2914 | else |
| 2915 | z = x_add(a, b); |
| 2916 | if (z != NULL && Py_SIZE(z) != 0) |
| 2917 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 2918 | } |
| 2919 | else { |
| 2920 | if (Py_SIZE(b) < 0) |
| 2921 | z = x_add(a, b); |
| 2922 | else |
| 2923 | z = x_sub(a, b); |
| 2924 | } |
| 2925 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2928 | /* Grade school multiplication, ignoring the signs. |
| 2929 | * Returns the absolute value of the product, or NULL if error. |
| 2930 | */ |
| 2931 | static PyLongObject * |
| 2932 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | PyLongObject *z; |
| 2935 | Py_ssize_t size_a = ABS(Py_SIZE(a)); |
| 2936 | Py_ssize_t size_b = ABS(Py_SIZE(b)); |
| 2937 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | z = _PyLong_New(size_a + size_b); |
| 2940 | if (z == NULL) |
| 2941 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 2944 | if (a == b) { |
| 2945 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 2946 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 2947 | * Gives slightly less than a 2x speedup when a == b, |
| 2948 | * via exploiting that each entry in the multiplication |
| 2949 | * pyramid appears twice (except for the size_a squares). |
| 2950 | */ |
| 2951 | for (i = 0; i < size_a; ++i) { |
| 2952 | twodigits carry; |
| 2953 | twodigits f = a->ob_digit[i]; |
| 2954 | digit *pz = z->ob_digit + (i << 1); |
| 2955 | digit *pa = a->ob_digit + i + 1; |
| 2956 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2959 | Py_DECREF(z); |
| 2960 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2961 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | carry = *pz + f * f; |
| 2964 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2965 | carry >>= PyLong_SHIFT; |
| 2966 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2968 | /* Now f is added in twice in each column of the |
| 2969 | * pyramid it appears. Same as adding f<<1 once. |
| 2970 | */ |
| 2971 | f <<= 1; |
| 2972 | while (pa < paend) { |
| 2973 | carry += *pz + *pa++ * f; |
| 2974 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2975 | carry >>= PyLong_SHIFT; |
| 2976 | assert(carry <= (PyLong_MASK << 1)); |
| 2977 | } |
| 2978 | if (carry) { |
| 2979 | carry += *pz; |
| 2980 | *pz++ = (digit)(carry & PyLong_MASK); |
| 2981 | carry >>= PyLong_SHIFT; |
| 2982 | } |
| 2983 | if (carry) |
| 2984 | *pz += (digit)(carry & PyLong_MASK); |
| 2985 | assert((carry >> PyLong_SHIFT) == 0); |
| 2986 | } |
| 2987 | } |
| 2988 | else { /* a is not the same as b -- gradeschool long mult */ |
| 2989 | for (i = 0; i < size_a; ++i) { |
| 2990 | twodigits carry = 0; |
| 2991 | twodigits f = a->ob_digit[i]; |
| 2992 | digit *pz = z->ob_digit + i; |
| 2993 | digit *pb = b->ob_digit; |
| 2994 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2997 | Py_DECREF(z); |
| 2998 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2999 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | while (pb < pbend) { |
| 3002 | carry += *pz + *pb++ * f; |
| 3003 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3004 | carry >>= PyLong_SHIFT; |
| 3005 | assert(carry <= PyLong_MASK); |
| 3006 | } |
| 3007 | if (carry) |
| 3008 | *pz += (digit)(carry & PyLong_MASK); |
| 3009 | assert((carry >> PyLong_SHIFT) == 0); |
| 3010 | } |
| 3011 | } |
| 3012 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /* A helper for Karatsuba multiplication (k_mul). |
| 3016 | Takes a long "n" and an integer "size" representing the place to |
| 3017 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3018 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3019 | the return values are >= 0. |
| 3020 | Returns 0 on success, -1 on failure. |
| 3021 | */ |
| 3022 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3023 | kmul_split(PyLongObject *n, |
| 3024 | Py_ssize_t size, |
| 3025 | PyLongObject **high, |
| 3026 | PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | PyLongObject *hi, *lo; |
| 3029 | Py_ssize_t size_lo, size_hi; |
| 3030 | const Py_ssize_t size_n = ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | size_lo = MIN(size_n, size); |
| 3033 | size_hi = size_n - size_lo; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3036 | return -1; |
| 3037 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3038 | Py_DECREF(hi); |
| 3039 | return -1; |
| 3040 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3043 | memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | *high = long_normalize(hi); |
| 3046 | *low = long_normalize(lo); |
| 3047 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3050 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3051 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3052 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3053 | * absolute value of the product (or NULL if error). |
| 3054 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3055 | */ |
| 3056 | static PyLongObject * |
| 3057 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3058 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | Py_ssize_t asize = ABS(Py_SIZE(a)); |
| 3060 | Py_ssize_t bsize = ABS(Py_SIZE(b)); |
| 3061 | PyLongObject *ah = NULL; |
| 3062 | PyLongObject *al = NULL; |
| 3063 | PyLongObject *bh = NULL; |
| 3064 | PyLongObject *bl = NULL; |
| 3065 | PyLongObject *ret = NULL; |
| 3066 | PyLongObject *t1, *t2, *t3; |
| 3067 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3068 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3071 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3072 | * Then the original product is |
| 3073 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3074 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3075 | * been reduced to 3 multiplies on numbers half the size. |
| 3076 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | /* We want to split based on the larger number; fiddle so that b |
| 3079 | * is largest. |
| 3080 | */ |
| 3081 | if (asize > bsize) { |
| 3082 | t1 = a; |
| 3083 | a = b; |
| 3084 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | i = asize; |
| 3087 | asize = bsize; |
| 3088 | bsize = i; |
| 3089 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | /* Use gradeschool math when either number is too small. */ |
| 3092 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3093 | if (asize <= i) { |
| 3094 | if (asize == 0) |
| 3095 | return (PyLongObject *)PyLong_FromLong(0); |
| 3096 | else |
| 3097 | return x_mul(a, b); |
| 3098 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3101 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3102 | * than "grade school" then. However, we can still win, by viewing |
| 3103 | * b as a string of "big digits", each of width a->ob_size. That |
| 3104 | * leads to a sequence of balanced calls to k_mul. |
| 3105 | */ |
| 3106 | if (2 * asize <= bsize) |
| 3107 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | /* Split a & b into hi & lo pieces. */ |
| 3110 | shift = bsize >> 1; |
| 3111 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3112 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | if (a == b) { |
| 3115 | bh = ah; |
| 3116 | bl = al; |
| 3117 | Py_INCREF(bh); |
| 3118 | Py_INCREF(bl); |
| 3119 | } |
| 3120 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | /* The plan: |
| 3123 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3124 | * enough). |
| 3125 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3126 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3127 | * can't overlap with #2. |
| 3128 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3129 | * underflow (borrow out of the high digit), but we don't care: |
| 3130 | * we're effectively doing unsigned arithmetic mod |
| 3131 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3132 | * borrows and carries out of the high digit can be ignored. |
| 3133 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3134 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3135 | * at shift. |
| 3136 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | /* 1. Allocate result space. */ |
| 3139 | ret = _PyLong_New(asize + bsize); |
| 3140 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3141 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3143 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3144 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3147 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3148 | assert(Py_SIZE(t1) >= 0); |
| 3149 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3150 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3151 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3154 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3155 | if (i) |
| 3156 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3157 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3160 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3161 | Py_DECREF(t1); |
| 3162 | goto fail; |
| 3163 | } |
| 3164 | assert(Py_SIZE(t2) >= 0); |
| 3165 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3166 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | /* Zero out remaining digits. */ |
| 3169 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3170 | if (i) |
| 3171 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3174 | * because it's fresher in cache. |
| 3175 | */ |
| 3176 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3177 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3178 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3181 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3184 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3185 | Py_DECREF(ah); |
| 3186 | Py_DECREF(al); |
| 3187 | ah = al = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | if (a == b) { |
| 3190 | t2 = t1; |
| 3191 | Py_INCREF(t2); |
| 3192 | } |
| 3193 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3194 | Py_DECREF(t1); |
| 3195 | goto fail; |
| 3196 | } |
| 3197 | Py_DECREF(bh); |
| 3198 | Py_DECREF(bl); |
| 3199 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | t3 = k_mul(t1, t2); |
| 3202 | Py_DECREF(t1); |
| 3203 | Py_DECREF(t2); |
| 3204 | if (t3 == NULL) goto fail; |
| 3205 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3208 | * See the (*) comment after this function. |
| 3209 | */ |
| 3210 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3211 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3214 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3215 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | Py_XDECREF(ret); |
| 3217 | Py_XDECREF(ah); |
| 3218 | Py_XDECREF(al); |
| 3219 | Py_XDECREF(bh); |
| 3220 | Py_XDECREF(bl); |
| 3221 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3224 | /* (*) Why adding t3 can't "run out of room" above. |
| 3225 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3226 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3227 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3228 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3229 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3230 | bsize = c(bsize/2) + f(bsize/2). |
| 3231 | 2. shift = f(bsize/2) |
| 3232 | 3. asize <= bsize |
| 3233 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3234 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3235 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3236 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3237 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3238 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3239 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3240 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3241 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3242 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3243 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3244 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3245 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3246 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3247 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3248 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3249 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3250 | 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] | 3251 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3252 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3253 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3254 | the question reduces to whether asize digits is enough to hold |
| 3255 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3256 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3257 | 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] | 3258 | 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] | 3259 | 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] | 3260 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3261 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3262 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3263 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3264 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3265 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3266 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3267 | */ |
| 3268 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3269 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3270 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3271 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3272 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3273 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3274 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3275 | * single-width slice overlap between successive partial sums). |
| 3276 | */ |
| 3277 | static PyLongObject * |
| 3278 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | const Py_ssize_t asize = ABS(Py_SIZE(a)); |
| 3281 | Py_ssize_t bsize = ABS(Py_SIZE(b)); |
| 3282 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3283 | PyLongObject *ret; |
| 3284 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | assert(asize > KARATSUBA_CUTOFF); |
| 3287 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | /* Allocate result space, and zero it out. */ |
| 3290 | ret = _PyLong_New(asize + bsize); |
| 3291 | if (ret == NULL) |
| 3292 | return NULL; |
| 3293 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | /* Successive slices of b are copied into bslice. */ |
| 3296 | bslice = _PyLong_New(asize); |
| 3297 | if (bslice == NULL) |
| 3298 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | nbdone = 0; |
| 3301 | while (bsize > 0) { |
| 3302 | PyLongObject *product; |
| 3303 | const Py_ssize_t nbtouse = MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | /* Multiply the next slice of b by a. */ |
| 3306 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3307 | nbtouse * sizeof(digit)); |
| 3308 | Py_SIZE(bslice) = nbtouse; |
| 3309 | product = k_mul(a, bslice); |
| 3310 | if (product == NULL) |
| 3311 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | /* Add into result. */ |
| 3314 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3315 | product->ob_digit, Py_SIZE(product)); |
| 3316 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | bsize -= nbtouse; |
| 3319 | nbdone += nbtouse; |
| 3320 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | Py_DECREF(bslice); |
| 3323 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3324 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3325 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | Py_DECREF(ret); |
| 3327 | Py_XDECREF(bslice); |
| 3328 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3329 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3330 | |
| 3331 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3332 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | /* fast path for single-digit multiplication */ |
| 3339 | if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
| 3340 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3341 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | return PyLong_FromLongLong((PY_LONG_LONG)v); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3343 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | /* if we don't have long long then we're almost certainly |
| 3345 | using 15-bit digits, so v will fit in a long. In the |
| 3346 | unlikely event that we're using 30-bit digits on a platform |
| 3347 | without long long, a large v will just cause us to fall |
| 3348 | through to the general multiplication code below. */ |
| 3349 | if (v >= LONG_MIN && v <= LONG_MAX) |
| 3350 | return PyLong_FromLong((long)v); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3351 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | z = k_mul(a, b); |
| 3355 | /* Negate if exactly one of the inputs is negative. */ |
| 3356 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) |
| 3357 | NEGATE(z); |
| 3358 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3361 | /* The / and % operators are now defined in terms of divmod(). |
| 3362 | The expression a mod b has the value a - b*floor(a/b). |
| 3363 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3364 | |a| by |b|, with the sign of a. This is also expressed |
| 3365 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3366 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | a b a rem b a mod b |
| 3368 | 13 10 3 3 |
| 3369 | -13 10 -3 7 |
| 3370 | 13 -10 3 -7 |
| 3371 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3372 | 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] | 3373 | have different signs. We then subtract one from the 'div' |
| 3374 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3375 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3376 | /* Compute |
| 3377 | * *pdiv, *pmod = divmod(v, w) |
| 3378 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3379 | * the result is simply thrown away. The caller owns a reference to |
| 3380 | * each of these it requests (does not pass NULL for). |
| 3381 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3382 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3383 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3389 | return -1; |
| 3390 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3391 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3392 | PyLongObject *temp; |
| 3393 | PyLongObject *one; |
| 3394 | temp = (PyLongObject *) long_add(mod, w); |
| 3395 | Py_DECREF(mod); |
| 3396 | mod = temp; |
| 3397 | if (mod == NULL) { |
| 3398 | Py_DECREF(div); |
| 3399 | return -1; |
| 3400 | } |
| 3401 | one = (PyLongObject *) PyLong_FromLong(1L); |
| 3402 | if (one == NULL || |
| 3403 | (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
| 3404 | Py_DECREF(mod); |
| 3405 | Py_DECREF(div); |
| 3406 | Py_XDECREF(one); |
| 3407 | return -1; |
| 3408 | } |
| 3409 | Py_DECREF(one); |
| 3410 | Py_DECREF(div); |
| 3411 | div = temp; |
| 3412 | } |
| 3413 | if (pdiv != NULL) |
| 3414 | *pdiv = div; |
| 3415 | else |
| 3416 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | if (pmod != NULL) |
| 3419 | *pmod = mod; |
| 3420 | else |
| 3421 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3426 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3427 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | CHECK_BINOP(a, b); |
| 3432 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3433 | div = NULL; |
| 3434 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3437 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3438 | |
| 3439 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3440 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3441 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3442 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3443 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | PyLongObject *a, *b, *x; |
| 3446 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3447 | digit mask, low; |
| 3448 | int inexact, negate, a_is_small, b_is_small; |
| 3449 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | CHECK_BINOP(v, w); |
| 3452 | a = (PyLongObject *)v; |
| 3453 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | /* |
| 3456 | Method in a nutshell: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3459 | 1. choose a suitable integer 'shift' |
| 3460 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3461 | 3. adjust x for correct rounding |
| 3462 | 4. convert x to a double dx with the same value |
| 3463 | 5. return ldexp(dx, shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | In more detail: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3467 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3468 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3469 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3470 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3471 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3472 | for b). Then |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3474 | 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3477 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3478 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3479 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3484 | bits for a double, plus two or three extra bits that will be used |
| 3485 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3486 | number of significant bits in a and b respectively, a |
| 3487 | straightforward formula for shift is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | This is fine in the usual case, but if a/b is smaller than the |
| 3492 | smallest normal float then it can lead to double rounding on an |
| 3493 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3494 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3499 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3500 | b. For both the shift and the division, we keep track of whether |
| 3501 | the result is inexact, in a flag 'inexact'; this information is |
| 3502 | needed at the rounding stage. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | With the choice of shift above, together with our assumption that |
| 3505 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3506 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3509 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | For float representability, we need x/2**extra_bits < |
| 3514 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3515 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | To round, we just modify the bottom digit of x in-place; this can |
| 3520 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3521 | problem since digits can hold values up to 2*PyLONG_MASK+1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | With the original choices for shift above, extra_bits will always |
| 3524 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3525 | round up iff the most significant of the extra bits is 1, and |
| 3526 | either: (a) the computation of x in step 2 had an inexact result, |
| 3527 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3528 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | 4. Conversion to a double is straightforward; all floating-point |
| 3531 | operations involved in the conversion are exact, so there's no |
| 3532 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3534 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3535 | The result will always be exactly representable as a double, except |
| 3536 | in the case that it overflows. To avoid dependence on the exact |
| 3537 | behaviour of ldexp on overflow, we check for overflow before |
| 3538 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3539 | returning. |
| 3540 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | /* Reduce to case where a and b are both positive. */ |
| 3543 | a_size = ABS(Py_SIZE(a)); |
| 3544 | b_size = ABS(Py_SIZE(b)); |
| 3545 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3546 | if (b_size == 0) { |
| 3547 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3548 | "division by zero"); |
| 3549 | goto error; |
| 3550 | } |
| 3551 | if (a_size == 0) |
| 3552 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | /* Fast path for a and b small (exactly representable in a double). |
| 3555 | Relies on floating-point division being correctly rounded; results |
| 3556 | may be subject to double rounding on x86 machines that operate with |
| 3557 | the x87 FPU set to 64-bit precision. */ |
| 3558 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3559 | (a_size == MANT_DIG_DIGITS+1 && |
| 3560 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3561 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3562 | (b_size == MANT_DIG_DIGITS+1 && |
| 3563 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3564 | if (a_is_small && b_is_small) { |
| 3565 | double da, db; |
| 3566 | da = a->ob_digit[--a_size]; |
| 3567 | while (a_size > 0) |
| 3568 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3569 | db = b->ob_digit[--b_size]; |
| 3570 | while (b_size > 0) |
| 3571 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3572 | result = da / db; |
| 3573 | goto success; |
| 3574 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | /* Catch obvious cases of underflow and overflow */ |
| 3577 | diff = a_size - b_size; |
| 3578 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3579 | /* Extreme overflow */ |
| 3580 | goto overflow; |
| 3581 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3582 | /* Extreme underflow */ |
| 3583 | goto underflow_or_zero; |
| 3584 | /* Next line is now safe from overflowing a Py_ssize_t */ |
| 3585 | diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) - |
| 3586 | bits_in_digit(b->ob_digit[b_size - 1]); |
| 3587 | /* Now diff = a_bits - b_bits. */ |
| 3588 | if (diff > DBL_MAX_EXP) |
| 3589 | goto overflow; |
| 3590 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3591 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3593 | /* Choose value for shift; see comments for step 1 above. */ |
| 3594 | shift = MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | inexact = 0; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | /* x = abs(a * 2**-shift) */ |
| 3599 | if (shift <= 0) { |
| 3600 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3601 | digit rem; |
| 3602 | /* x = a << -shift */ |
| 3603 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3604 | /* In practice, it's probably impossible to end up |
| 3605 | here. Both a and b would have to be enormous, |
| 3606 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3607 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3608 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | goto error; |
| 3610 | } |
| 3611 | x = _PyLong_New(a_size + shift_digits + 1); |
| 3612 | if (x == NULL) |
| 3613 | goto error; |
| 3614 | for (i = 0; i < shift_digits; i++) |
| 3615 | x->ob_digit[i] = 0; |
| 3616 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 3617 | a_size, -shift % PyLong_SHIFT); |
| 3618 | x->ob_digit[a_size + shift_digits] = rem; |
| 3619 | } |
| 3620 | else { |
| 3621 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 3622 | digit rem; |
| 3623 | /* x = a >> shift */ |
| 3624 | assert(a_size >= shift_digits); |
| 3625 | x = _PyLong_New(a_size - shift_digits); |
| 3626 | if (x == NULL) |
| 3627 | goto error; |
| 3628 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 3629 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 3630 | /* set inexact if any of the bits shifted out is nonzero */ |
| 3631 | if (rem) |
| 3632 | inexact = 1; |
| 3633 | while (!inexact && shift_digits > 0) |
| 3634 | if (a->ob_digit[--shift_digits]) |
| 3635 | inexact = 1; |
| 3636 | } |
| 3637 | long_normalize(x); |
| 3638 | x_size = Py_SIZE(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 3641 | reference to x, so it's safe to modify it in-place. */ |
| 3642 | if (b_size == 1) { |
| 3643 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 3644 | b->ob_digit[0]); |
| 3645 | long_normalize(x); |
| 3646 | if (rem) |
| 3647 | inexact = 1; |
| 3648 | } |
| 3649 | else { |
| 3650 | PyLongObject *div, *rem; |
| 3651 | div = x_divrem(x, b, &rem); |
| 3652 | Py_DECREF(x); |
| 3653 | x = div; |
| 3654 | if (x == NULL) |
| 3655 | goto error; |
| 3656 | if (Py_SIZE(rem)) |
| 3657 | inexact = 1; |
| 3658 | Py_DECREF(rem); |
| 3659 | } |
| 3660 | x_size = ABS(Py_SIZE(x)); |
| 3661 | assert(x_size > 0); /* result of division is never zero */ |
| 3662 | x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | /* The number of extra bits that have to be rounded away. */ |
| 3665 | extra_bits = MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG; |
| 3666 | assert(extra_bits == 2 || extra_bits == 3); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | /* Round by directly modifying the low digit of x. */ |
| 3669 | mask = (digit)1 << (extra_bits - 1); |
| 3670 | low = x->ob_digit[0] | inexact; |
| 3671 | if (low & mask && low & (3*mask-1)) |
| 3672 | low += mask; |
| 3673 | x->ob_digit[0] = low & ~(mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | /* Convert x to a double dx; the conversion is exact. */ |
| 3676 | dx = x->ob_digit[--x_size]; |
| 3677 | while (x_size > 0) |
| 3678 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 3679 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | /* Check whether ldexp result will overflow a double. */ |
| 3682 | if (shift + x_bits >= DBL_MAX_EXP && |
| 3683 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 3684 | goto overflow; |
| 3685 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3686 | |
| 3687 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3688 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3689 | |
| 3690 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3691 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3692 | |
| 3693 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3694 | PyErr_SetString(PyExc_OverflowError, |
| 3695 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3696 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3697 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3701 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3703 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3705 | CHECK_BINOP(a, b); |
| 3706 | |
| 3707 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 3708 | mod = NULL; |
| 3709 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3712 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3713 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3715 | PyLongObject *div, *mod; |
| 3716 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | z = PyTuple_New(2); |
| 3724 | if (z != NULL) { |
| 3725 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 3726 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
| 3727 | } |
| 3728 | else { |
| 3729 | Py_DECREF(div); |
| 3730 | Py_DECREF(mod); |
| 3731 | } |
| 3732 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3735 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3736 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3737 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 3740 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | PyLongObject *z = NULL; /* accumulated result */ |
| 3743 | Py_ssize_t i, j, k; /* counters */ |
| 3744 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3746 | /* 5-ary values. If the exponent is large enough, table is |
| 3747 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 3748 | */ |
| 3749 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 3750 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | /* a, b, c = v, w, x */ |
| 3753 | CHECK_BINOP(v, w); |
| 3754 | a = (PyLongObject*)v; Py_INCREF(a); |
| 3755 | b = (PyLongObject*)w; Py_INCREF(b); |
| 3756 | if (PyLong_Check(x)) { |
| 3757 | c = (PyLongObject *)x; |
| 3758 | Py_INCREF(x); |
| 3759 | } |
| 3760 | else if (x == Py_None) |
| 3761 | c = NULL; |
| 3762 | else { |
| 3763 | Py_DECREF(a); |
| 3764 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3765 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | if (Py_SIZE(b) < 0) { /* if exponent is negative */ |
| 3769 | if (c) { |
| 3770 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3771 | "cannot be negative when 3rd argument specified"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | goto Error; |
| 3773 | } |
| 3774 | else { |
| 3775 | /* else return a float. This works because we know |
| 3776 | that this calls float_pow() which converts its |
| 3777 | arguments to double. */ |
| 3778 | Py_DECREF(a); |
| 3779 | Py_DECREF(b); |
| 3780 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
| 3781 | } |
| 3782 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | if (c) { |
| 3785 | /* if modulus == 0: |
| 3786 | raise ValueError() */ |
| 3787 | if (Py_SIZE(c) == 0) { |
| 3788 | PyErr_SetString(PyExc_ValueError, |
| 3789 | "pow() 3rd argument cannot be 0"); |
| 3790 | goto Error; |
| 3791 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3793 | /* if modulus < 0: |
| 3794 | negativeOutput = True |
| 3795 | modulus = -modulus */ |
| 3796 | if (Py_SIZE(c) < 0) { |
| 3797 | negativeOutput = 1; |
| 3798 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 3799 | if (temp == NULL) |
| 3800 | goto Error; |
| 3801 | Py_DECREF(c); |
| 3802 | c = temp; |
| 3803 | temp = NULL; |
| 3804 | NEGATE(c); |
| 3805 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | /* if modulus == 1: |
| 3808 | return 0 */ |
| 3809 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 3810 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 3811 | goto Done; |
| 3812 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3814 | /* if base < 0: |
| 3815 | base = base % modulus |
| 3816 | Having the base positive just makes things easier. */ |
| 3817 | if (Py_SIZE(a) < 0) { |
| 3818 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 3819 | goto Error; |
| 3820 | Py_DECREF(a); |
| 3821 | a = temp; |
| 3822 | temp = NULL; |
| 3823 | } |
| 3824 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 3827 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 3830 | if (z == NULL) |
| 3831 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3833 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 3834 | * is NULL. |
| 3835 | */ |
| 3836 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3837 | do { \ |
| 3838 | if (c != NULL) { \ |
| 3839 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 3840 | goto Error; \ |
| 3841 | Py_XDECREF(X); \ |
| 3842 | X = temp; \ |
| 3843 | temp = NULL; \ |
| 3844 | } \ |
| 3845 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | /* Multiply two values, then reduce the result: |
| 3848 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3849 | #define MULT(X, Y, result) \ |
| 3850 | do { \ |
| 3851 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 3852 | if (temp == NULL) \ |
| 3853 | goto Error; \ |
| 3854 | Py_XDECREF(result); \ |
| 3855 | result = temp; \ |
| 3856 | temp = NULL; \ |
| 3857 | REDUCE(result); \ |
| 3858 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 3861 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 3862 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 3863 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 3864 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3867 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3869 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } |
| 3872 | } |
| 3873 | else { |
| 3874 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 3875 | Py_INCREF(z); /* still holds 1L */ |
| 3876 | table[0] = z; |
| 3877 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3878 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 3881 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 3884 | const int index = (bi >> j) & 0x1f; |
| 3885 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3886 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3888 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 3894 | temp = (PyLongObject *)long_sub(z, c); |
| 3895 | if (temp == NULL) |
| 3896 | goto Error; |
| 3897 | Py_DECREF(z); |
| 3898 | z = temp; |
| 3899 | temp = NULL; |
| 3900 | } |
| 3901 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3902 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3903 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3904 | if (z != NULL) { |
| 3905 | Py_DECREF(z); |
| 3906 | z = NULL; |
| 3907 | } |
| 3908 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3909 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 3911 | for (i = 0; i < 32; ++i) |
| 3912 | Py_XDECREF(table[i]); |
| 3913 | } |
| 3914 | Py_DECREF(a); |
| 3915 | Py_DECREF(b); |
| 3916 | Py_XDECREF(c); |
| 3917 | Py_XDECREF(temp); |
| 3918 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3921 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3922 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3924 | /* Implement ~x as -(x+1) */ |
| 3925 | PyLongObject *x; |
| 3926 | PyLongObject *w; |
| 3927 | if (ABS(Py_SIZE(v)) <=1) |
| 3928 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
| 3929 | w = (PyLongObject *)PyLong_FromLong(1L); |
| 3930 | if (w == NULL) |
| 3931 | return NULL; |
| 3932 | x = (PyLongObject *) long_add(v, w); |
| 3933 | Py_DECREF(w); |
| 3934 | if (x == NULL) |
| 3935 | return NULL; |
| 3936 | Py_SIZE(x) = -(Py_SIZE(x)); |
| 3937 | return (PyObject *)maybe_small_long(x); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3940 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3941 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 | PyLongObject *z; |
| 3944 | if (ABS(Py_SIZE(v)) <= 1) |
| 3945 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 3946 | z = (PyLongObject *)_PyLong_Copy(v); |
| 3947 | if (z != NULL) |
| 3948 | Py_SIZE(z) = -(Py_SIZE(v)); |
| 3949 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3952 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3953 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | if (Py_SIZE(v) < 0) |
| 3956 | return long_neg(v); |
| 3957 | else |
| 3958 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3961 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3962 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3967 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3968 | long_rshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3970 | PyLongObject *z = NULL; |
| 3971 | Py_ssize_t shiftby, newsize, wordshift, loshift, hishift, i, j; |
| 3972 | digit lomask, himask; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3974 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | if (Py_SIZE(a) < 0) { |
| 3977 | /* Right shifting negative numbers is harder */ |
| 3978 | PyLongObject *a1, *a2; |
| 3979 | a1 = (PyLongObject *) long_invert(a); |
| 3980 | if (a1 == NULL) |
| 3981 | goto rshift_error; |
| 3982 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 3983 | Py_DECREF(a1); |
| 3984 | if (a2 == NULL) |
| 3985 | goto rshift_error; |
| 3986 | z = (PyLongObject *) long_invert(a2); |
| 3987 | Py_DECREF(a2); |
| 3988 | } |
| 3989 | else { |
| 3990 | shiftby = PyLong_AsSsize_t((PyObject *)b); |
| 3991 | if (shiftby == -1L && PyErr_Occurred()) |
| 3992 | goto rshift_error; |
| 3993 | if (shiftby < 0) { |
| 3994 | PyErr_SetString(PyExc_ValueError, |
| 3995 | "negative shift count"); |
| 3996 | goto rshift_error; |
| 3997 | } |
| 3998 | wordshift = shiftby / PyLong_SHIFT; |
| 3999 | newsize = ABS(Py_SIZE(a)) - wordshift; |
| 4000 | if (newsize <= 0) |
| 4001 | return PyLong_FromLong(0); |
| 4002 | loshift = shiftby % PyLong_SHIFT; |
| 4003 | hishift = PyLong_SHIFT - loshift; |
| 4004 | lomask = ((digit)1 << hishift) - 1; |
| 4005 | himask = PyLong_MASK ^ lomask; |
| 4006 | z = _PyLong_New(newsize); |
| 4007 | if (z == NULL) |
| 4008 | goto rshift_error; |
| 4009 | if (Py_SIZE(a) < 0) |
| 4010 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4011 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 4012 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 4013 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4014 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4015 | } |
| 4016 | z = long_normalize(z); |
| 4017 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4018 | rshift_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | return (PyObject *) maybe_small_long(z); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4020 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4023 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4024 | long_lshift(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | /* This version due to Tim Peters */ |
| 4027 | PyLongObject *a = (PyLongObject*)v; |
| 4028 | PyLongObject *b = (PyLongObject*)w; |
| 4029 | PyLongObject *z = NULL; |
| 4030 | Py_ssize_t shiftby, oldsize, newsize, wordshift, remshift, i, j; |
| 4031 | twodigits accum; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4035 | shiftby = PyLong_AsSsize_t((PyObject *)b); |
| 4036 | if (shiftby == -1L && PyErr_Occurred()) |
| 4037 | goto lshift_error; |
| 4038 | if (shiftby < 0) { |
| 4039 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
| 4040 | goto lshift_error; |
| 4041 | } |
| 4042 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4043 | wordshift = shiftby / PyLong_SHIFT; |
| 4044 | remshift = shiftby - wordshift * PyLong_SHIFT; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4046 | oldsize = ABS(Py_SIZE(a)); |
| 4047 | newsize = oldsize + wordshift; |
| 4048 | if (remshift) |
| 4049 | ++newsize; |
| 4050 | z = _PyLong_New(newsize); |
| 4051 | if (z == NULL) |
| 4052 | goto lshift_error; |
| 4053 | if (Py_SIZE(a) < 0) |
| 4054 | NEGATE(z); |
| 4055 | for (i = 0; i < wordshift; i++) |
| 4056 | z->ob_digit[i] = 0; |
| 4057 | accum = 0; |
| 4058 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4059 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4060 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4061 | accum >>= PyLong_SHIFT; |
| 4062 | } |
| 4063 | if (remshift) |
| 4064 | z->ob_digit[newsize-1] = (digit)accum; |
| 4065 | else |
| 4066 | assert(!accum); |
| 4067 | z = long_normalize(z); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4068 | lshift_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4072 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4073 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4074 | be entirely zero. a and z may point to the same digit vector. */ |
| 4075 | |
| 4076 | static void |
| 4077 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | Py_ssize_t i; |
| 4080 | digit carry = 1; |
| 4081 | for (i = 0; i < m; ++i) { |
| 4082 | carry += a[i] ^ PyLong_MASK; |
| 4083 | z[i] = carry & PyLong_MASK; |
| 4084 | carry >>= PyLong_SHIFT; |
| 4085 | } |
| 4086 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4087 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4088 | |
| 4089 | /* Bitwise and/xor/or operations */ |
| 4090 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4091 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4092 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4093 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4094 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4096 | int nega, negb, negz; |
| 4097 | Py_ssize_t size_a, size_b, size_z, i; |
| 4098 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4100 | /* Bitwise operations for negative numbers operate as though |
| 4101 | on a two's complement representation. So convert arguments |
| 4102 | from sign-magnitude to two's complement, and convert the |
| 4103 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | /* If a is negative, replace it by its two's complement. */ |
| 4106 | size_a = ABS(Py_SIZE(a)); |
| 4107 | nega = Py_SIZE(a) < 0; |
| 4108 | if (nega) { |
| 4109 | z = _PyLong_New(size_a); |
| 4110 | if (z == NULL) |
| 4111 | return NULL; |
| 4112 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4113 | a = z; |
| 4114 | } |
| 4115 | else |
| 4116 | /* Keep reference count consistent. */ |
| 4117 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | /* Same for b. */ |
| 4120 | size_b = ABS(Py_SIZE(b)); |
| 4121 | negb = Py_SIZE(b) < 0; |
| 4122 | if (negb) { |
| 4123 | z = _PyLong_New(size_b); |
| 4124 | if (z == NULL) { |
| 4125 | Py_DECREF(a); |
| 4126 | return NULL; |
| 4127 | } |
| 4128 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4129 | b = z; |
| 4130 | } |
| 4131 | else |
| 4132 | Py_INCREF(b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4134 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4135 | if (size_a < size_b) { |
| 4136 | z = a; a = b; b = z; |
| 4137 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4138 | negz = nega; nega = negb; negb = negz; |
| 4139 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | /* JRH: The original logic here was to allocate the result value (z) |
| 4142 | as the longer of the two operands. However, there are some cases |
| 4143 | where the result is guaranteed to be shorter than that: AND of two |
| 4144 | positives, OR of two negatives: use the shorter number. AND with |
| 4145 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4146 | negative number. |
| 4147 | */ |
| 4148 | switch (op) { |
| 4149 | case '^': |
| 4150 | negz = nega ^ negb; |
| 4151 | size_z = size_a; |
| 4152 | break; |
| 4153 | case '&': |
| 4154 | negz = nega & negb; |
| 4155 | size_z = negb ? size_a : size_b; |
| 4156 | break; |
| 4157 | case '|': |
| 4158 | negz = nega | negb; |
| 4159 | size_z = negb ? size_b : size_a; |
| 4160 | break; |
| 4161 | default: |
| 4162 | PyErr_BadArgument(); |
| 4163 | return NULL; |
| 4164 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | /* We allow an extra digit if z is negative, to make sure that |
| 4167 | the final two's complement of z doesn't overflow. */ |
| 4168 | z = _PyLong_New(size_z + negz); |
| 4169 | if (z == NULL) { |
| 4170 | Py_DECREF(a); |
| 4171 | Py_DECREF(b); |
| 4172 | return NULL; |
| 4173 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | /* Compute digits for overlap of a and b. */ |
| 4176 | switch(op) { |
| 4177 | case '&': |
| 4178 | for (i = 0; i < size_b; ++i) |
| 4179 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4180 | break; |
| 4181 | case '|': |
| 4182 | for (i = 0; i < size_b; ++i) |
| 4183 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4184 | break; |
| 4185 | case '^': |
| 4186 | for (i = 0; i < size_b; ++i) |
| 4187 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4188 | break; |
| 4189 | default: |
| 4190 | PyErr_BadArgument(); |
| 4191 | return NULL; |
| 4192 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4194 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4195 | if (op == '^' && negb) |
| 4196 | for (; i < size_z; ++i) |
| 4197 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4198 | else if (i < size_z) |
| 4199 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4200 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | /* Complement result if negative. */ |
| 4203 | if (negz) { |
| 4204 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4205 | z->ob_digit[size_z] = PyLong_MASK; |
| 4206 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4207 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | Py_DECREF(a); |
| 4210 | Py_DECREF(b); |
| 4211 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4214 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4215 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | PyObject *c; |
| 4218 | CHECK_BINOP(a, b); |
| 4219 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4220 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4223 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4224 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | PyObject *c; |
| 4227 | CHECK_BINOP(a, b); |
| 4228 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4229 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4232 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4233 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | PyObject *c; |
| 4236 | CHECK_BINOP(a, b); |
| 4237 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4238 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4241 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4242 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4244 | if (PyLong_CheckExact(v)) |
| 4245 | Py_INCREF(v); |
| 4246 | else |
| 4247 | v = _PyLong_Copy((PyLongObject *)v); |
| 4248 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4251 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4252 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4254 | double result; |
| 4255 | result = PyLong_AsDouble(v); |
| 4256 | if (result == -1.0 && PyErr_Occurred()) |
| 4257 | return NULL; |
| 4258 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4261 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4262 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4263 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4264 | static PyObject * |
| 4265 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4266 | { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4267 | PyObject *obase = NULL, *x = NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4268 | Py_ssize_t base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | if (type != &PyLong_Type) |
| 4272 | return long_subtype_new(type, args, kwds); /* Wimp out */ |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4273 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist, |
| 4274 | &x, &obase)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | return NULL; |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4276 | if (x == NULL) { |
| 4277 | if (obase != NULL) { |
| 4278 | PyErr_SetString(PyExc_TypeError, |
| 4279 | "int() missing string argument"); |
| 4280 | return NULL; |
| 4281 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4283 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4284 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4286 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4287 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4288 | if (base == -1 && PyErr_Occurred()) |
| 4289 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4290 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4291 | PyErr_SetString(PyExc_ValueError, |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4292 | "int() base must be >= 2 and <= 36"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4293 | return NULL; |
| 4294 | } |
| 4295 | |
| 4296 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4297 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
| 4299 | /* Since PyLong_FromString doesn't have a length parameter, |
| 4300 | * check here for possible NULs in the string. */ |
| 4301 | char *string; |
| 4302 | Py_ssize_t size = Py_SIZE(x); |
| 4303 | if (PyByteArray_Check(x)) |
| 4304 | string = PyByteArray_AS_STRING(x); |
| 4305 | else |
| 4306 | string = PyBytes_AS_STRING(x); |
Christian Heimes | 79b97ee | 2012-09-12 15:31:43 +0200 | [diff] [blame] | 4307 | if (strlen(string) != (size_t)size || !size) { |
| 4308 | /* We only see this if there's a null byte in x or x is empty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4309 | x is a bytes or buffer, *and* a base is given. */ |
| 4310 | PyErr_Format(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4311 | "invalid literal for int() with base %d: %R", |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4312 | (int)base, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | return NULL; |
| 4314 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4315 | return PyLong_FromString(string, NULL, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | } |
| 4317 | else { |
| 4318 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4319 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | return NULL; |
| 4321 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4324 | /* Wimpy, slow approach to tp_new calls for subtypes of long: |
| 4325 | first create a regular long from whatever arguments we got, |
| 4326 | then allocate a subtype instance and initialize it from |
| 4327 | the regular long. The regular long is then thrown away. |
| 4328 | */ |
| 4329 | static PyObject * |
| 4330 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | PyLongObject *tmp, *newobj; |
| 4333 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
| 4336 | tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); |
| 4337 | if (tmp == NULL) |
| 4338 | return NULL; |
| 4339 | assert(PyLong_CheckExact(tmp)); |
| 4340 | n = Py_SIZE(tmp); |
| 4341 | if (n < 0) |
| 4342 | n = -n; |
| 4343 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 4344 | if (newobj == NULL) { |
| 4345 | Py_DECREF(tmp); |
| 4346 | return NULL; |
| 4347 | } |
| 4348 | assert(PyLong_Check(newobj)); |
| 4349 | Py_SIZE(newobj) = Py_SIZE(tmp); |
| 4350 | for (i = 0; i < n; i++) |
| 4351 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
| 4352 | Py_DECREF(tmp); |
| 4353 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4356 | static PyObject * |
| 4357 | long_getnewargs(PyLongObject *v) |
| 4358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | return Py_BuildValue("(N)", _PyLong_Copy(v)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4362 | static PyObject * |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4363 | long_get0(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | static PyObject * |
| 4368 | long_get1(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4372 | static PyObject * |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4373 | long__format__(PyObject *self, PyObject *args) |
| 4374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | PyObject *format_spec; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4376 | _PyUnicodeWriter writer; |
| 4377 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 4378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 4380 | return NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4381 | |
| 4382 | _PyUnicodeWriter_Init(&writer, 0); |
| 4383 | ret = _PyLong_FormatAdvancedWriter( |
| 4384 | &writer, |
| 4385 | self, |
| 4386 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 4387 | if (ret == -1) { |
| 4388 | _PyUnicodeWriter_Dealloc(&writer); |
| 4389 | return NULL; |
| 4390 | } |
| 4391 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4394 | /* Return a pair (q, r) such that a = b * q + r, and |
| 4395 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 4396 | In other words, q == a / b, rounded to the nearest integer using |
| 4397 | round-half-to-even. */ |
| 4398 | |
| 4399 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 4400 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4401 | { |
| 4402 | PyLongObject *quo = NULL, *rem = NULL; |
| 4403 | PyObject *one = NULL, *twice_rem, *result, *temp; |
| 4404 | int cmp, quo_is_odd, quo_is_neg; |
| 4405 | |
| 4406 | /* Equivalent Python code: |
| 4407 | |
| 4408 | def divmod_near(a, b): |
| 4409 | q, r = divmod(a, b) |
| 4410 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 4411 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 4412 | # positive, 2 * r < b if b negative. |
| 4413 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 4414 | exactly_half = 2*r == b |
| 4415 | if greater_than_half or exactly_half and q % 2 == 1: |
| 4416 | q += 1 |
| 4417 | r -= b |
| 4418 | return q, r |
| 4419 | |
| 4420 | */ |
| 4421 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 4422 | PyErr_SetString(PyExc_TypeError, |
| 4423 | "non-integer arguments in division"); |
| 4424 | return NULL; |
| 4425 | } |
| 4426 | |
| 4427 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 4428 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 4429 | |
| 4430 | one = PyLong_FromLong(1L); |
| 4431 | if (one == NULL) |
| 4432 | return NULL; |
| 4433 | |
| 4434 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 4435 | goto error; |
| 4436 | |
| 4437 | /* compare twice the remainder with the divisor, to see |
| 4438 | if we need to adjust the quotient and remainder */ |
| 4439 | twice_rem = long_lshift((PyObject *)rem, one); |
| 4440 | if (twice_rem == NULL) |
| 4441 | goto error; |
| 4442 | if (quo_is_neg) { |
| 4443 | temp = long_neg((PyLongObject*)twice_rem); |
| 4444 | Py_DECREF(twice_rem); |
| 4445 | twice_rem = temp; |
| 4446 | if (twice_rem == NULL) |
| 4447 | goto error; |
| 4448 | } |
| 4449 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 4450 | Py_DECREF(twice_rem); |
| 4451 | |
| 4452 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 4453 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 4454 | /* fix up quotient */ |
| 4455 | if (quo_is_neg) |
| 4456 | temp = long_sub(quo, (PyLongObject *)one); |
| 4457 | else |
| 4458 | temp = long_add(quo, (PyLongObject *)one); |
| 4459 | Py_DECREF(quo); |
| 4460 | quo = (PyLongObject *)temp; |
| 4461 | if (quo == NULL) |
| 4462 | goto error; |
| 4463 | /* and remainder */ |
| 4464 | if (quo_is_neg) |
| 4465 | temp = long_add(rem, (PyLongObject *)b); |
| 4466 | else |
| 4467 | temp = long_sub(rem, (PyLongObject *)b); |
| 4468 | Py_DECREF(rem); |
| 4469 | rem = (PyLongObject *)temp; |
| 4470 | if (rem == NULL) |
| 4471 | goto error; |
| 4472 | } |
| 4473 | |
| 4474 | result = PyTuple_New(2); |
| 4475 | if (result == NULL) |
| 4476 | goto error; |
| 4477 | |
| 4478 | /* PyTuple_SET_ITEM steals references */ |
| 4479 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 4480 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
| 4481 | Py_DECREF(one); |
| 4482 | return result; |
| 4483 | |
| 4484 | error: |
| 4485 | Py_XDECREF(quo); |
| 4486 | Py_XDECREF(rem); |
| 4487 | Py_XDECREF(one); |
| 4488 | return NULL; |
| 4489 | } |
| 4490 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4491 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4492 | long_round(PyObject *self, PyObject *args) |
| 4493 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4494 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4495 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4496 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 4497 | * the divmod_near operation, defined by: |
| 4498 | * |
| 4499 | * divmod_near(a, b) = (q, r) |
| 4500 | * |
| 4501 | * where q is the nearest integer to the quotient a / b (the |
| 4502 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 4503 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 4504 | * preferring even multiples in the case of a tie. |
| 4505 | * |
| 4506 | * So the nearest multiple of 10**n to m is: |
| 4507 | * |
| 4508 | * m - divmod_near(m, 10**n)[1]. |
| 4509 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 4511 | return NULL; |
| 4512 | if (o_ndigits == NULL) |
| 4513 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4514 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4515 | ndigits = PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | if (ndigits == NULL) |
| 4517 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4518 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4519 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | if (Py_SIZE(ndigits) >= 0) { |
| 4521 | Py_DECREF(ndigits); |
| 4522 | return long_long(self); |
| 4523 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4524 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4525 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 4526 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4527 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4528 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4529 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4530 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4531 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4532 | result = PyLong_FromLong(10L); |
| 4533 | if (result == NULL) { |
| 4534 | Py_DECREF(ndigits); |
| 4535 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4537 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4538 | temp = long_pow(result, ndigits, Py_None); |
| 4539 | Py_DECREF(ndigits); |
| 4540 | Py_DECREF(result); |
| 4541 | result = temp; |
| 4542 | if (result == NULL) |
| 4543 | return NULL; |
| 4544 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 4545 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4546 | Py_DECREF(result); |
| 4547 | result = temp; |
| 4548 | if (result == NULL) |
| 4549 | return NULL; |
| 4550 | |
| 4551 | temp = long_sub((PyLongObject *)self, |
| 4552 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 4553 | Py_DECREF(result); |
| 4554 | result = temp; |
| 4555 | |
| 4556 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4559 | static PyObject * |
| 4560 | long_sizeof(PyLongObject *v) |
| 4561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit); |
| 4565 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4568 | static PyObject * |
| 4569 | long_bit_length(PyLongObject *v) |
| 4570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | PyLongObject *result, *x, *y; |
| 4572 | Py_ssize_t ndigits, msd_bits = 0; |
| 4573 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | assert(v != NULL); |
| 4576 | assert(PyLong_Check(v)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | ndigits = ABS(Py_SIZE(v)); |
| 4579 | if (ndigits == 0) |
| 4580 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | msd = v->ob_digit[ndigits-1]; |
| 4583 | while (msd >= 32) { |
| 4584 | msd_bits += 6; |
| 4585 | msd >>= 6; |
| 4586 | } |
| 4587 | msd_bits += (long)(BitLengthTable[msd]); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 4590 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | /* expression above may overflow; use Python integers instead */ |
| 4593 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 4594 | if (result == NULL) |
| 4595 | return NULL; |
| 4596 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 4597 | if (x == NULL) |
| 4598 | goto error; |
| 4599 | y = (PyLongObject *)long_mul(result, x); |
| 4600 | Py_DECREF(x); |
| 4601 | if (y == NULL) |
| 4602 | goto error; |
| 4603 | Py_DECREF(result); |
| 4604 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 4607 | if (x == NULL) |
| 4608 | goto error; |
| 4609 | y = (PyLongObject *)long_add(result, x); |
| 4610 | Py_DECREF(x); |
| 4611 | if (y == NULL) |
| 4612 | goto error; |
| 4613 | Py_DECREF(result); |
| 4614 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4617 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4618 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | Py_DECREF(result); |
| 4620 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | PyDoc_STRVAR(long_bit_length_doc, |
| 4624 | "int.bit_length() -> int\n\ |
| 4625 | \n\ |
| 4626 | Number of bits necessary to represent self in binary.\n\ |
| 4627 | >>> bin(37)\n\ |
| 4628 | '0b100101'\n\ |
| 4629 | >>> (37).bit_length()\n\ |
| 4630 | 6"); |
| 4631 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 4632 | #if 0 |
| 4633 | static PyObject * |
| 4634 | long_is_finite(PyObject *v) |
| 4635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | Py_RETURN_TRUE; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 4637 | } |
| 4638 | #endif |
| 4639 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4640 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4641 | static PyObject * |
| 4642 | long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) |
| 4643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | PyObject *byteorder_str; |
| 4645 | PyObject *is_signed_obj = NULL; |
| 4646 | Py_ssize_t length; |
| 4647 | int little_endian; |
| 4648 | int is_signed; |
| 4649 | PyObject *bytes; |
| 4650 | static char *kwlist[] = {"length", "byteorder", "signed", NULL}; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4652 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist, |
| 4653 | &length, &byteorder_str, |
| 4654 | &is_signed_obj)) |
| 4655 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4657 | if (args != NULL && Py_SIZE(args) > 2) { |
| 4658 | PyErr_SetString(PyExc_TypeError, |
| 4659 | "'signed' is a keyword-only argument"); |
| 4660 | return NULL; |
| 4661 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
| 4664 | little_endian = 1; |
| 4665 | else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
| 4666 | little_endian = 0; |
| 4667 | else { |
| 4668 | PyErr_SetString(PyExc_ValueError, |
| 4669 | "byteorder must be either 'little' or 'big'"); |
| 4670 | return NULL; |
| 4671 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | if (is_signed_obj != NULL) { |
| 4674 | int cmp = PyObject_IsTrue(is_signed_obj); |
| 4675 | if (cmp < 0) |
| 4676 | return NULL; |
| 4677 | is_signed = cmp ? 1 : 0; |
| 4678 | } |
| 4679 | else { |
| 4680 | /* If the signed argument was omitted, use False as the |
| 4681 | default. */ |
| 4682 | is_signed = 0; |
| 4683 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | if (length < 0) { |
| 4686 | PyErr_SetString(PyExc_ValueError, |
| 4687 | "length argument must be non-negative"); |
| 4688 | return NULL; |
| 4689 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 4692 | if (bytes == NULL) |
| 4693 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4695 | if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes), |
| 4696 | length, little_endian, is_signed) < 0) { |
| 4697 | Py_DECREF(bytes); |
| 4698 | return NULL; |
| 4699 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4704 | PyDoc_STRVAR(long_to_bytes_doc, |
| 4705 | "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4706 | \n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4707 | Return an array of bytes representing an integer.\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4708 | \n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | The integer is represented using length bytes. An OverflowError is\n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4710 | raised if the integer is not representable with the given number of\n\ |
| 4711 | bytes.\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4712 | \n\ |
| 4713 | The byteorder argument determines the byte order used to represent the\n\ |
| 4714 | integer. If byteorder is 'big', the most significant byte is at the\n\ |
| 4715 | beginning of the byte array. If byteorder is 'little', the most\n\ |
| 4716 | significant byte is at the end of the byte array. To request the native\n\ |
| 4717 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
| 4718 | \n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4719 | The signed keyword-only argument determines whether two's complement is\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | used to represent the integer. If signed is False and a negative integer\n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4721 | is given, an OverflowError is raised."); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4722 | |
| 4723 | static PyObject * |
| 4724 | long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | PyObject *byteorder_str; |
| 4727 | PyObject *is_signed_obj = NULL; |
| 4728 | int little_endian; |
| 4729 | int is_signed; |
| 4730 | PyObject *obj; |
| 4731 | PyObject *bytes; |
| 4732 | PyObject *long_obj; |
| 4733 | static char *kwlist[] = {"bytes", "byteorder", "signed", NULL}; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist, |
| 4736 | &obj, &byteorder_str, |
| 4737 | &is_signed_obj)) |
| 4738 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | if (args != NULL && Py_SIZE(args) > 2) { |
| 4741 | PyErr_SetString(PyExc_TypeError, |
| 4742 | "'signed' is a keyword-only argument"); |
| 4743 | return NULL; |
| 4744 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
| 4747 | little_endian = 1; |
| 4748 | else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
| 4749 | little_endian = 0; |
| 4750 | else { |
| 4751 | PyErr_SetString(PyExc_ValueError, |
| 4752 | "byteorder must be either 'little' or 'big'"); |
| 4753 | return NULL; |
| 4754 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4756 | if (is_signed_obj != NULL) { |
| 4757 | int cmp = PyObject_IsTrue(is_signed_obj); |
| 4758 | if (cmp < 0) |
| 4759 | return NULL; |
| 4760 | is_signed = cmp ? 1 : 0; |
| 4761 | } |
| 4762 | else { |
| 4763 | /* If the signed argument was omitted, use False as the |
| 4764 | default. */ |
| 4765 | is_signed = 0; |
| 4766 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | bytes = PyObject_Bytes(obj); |
| 4769 | if (bytes == NULL) |
| 4770 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | long_obj = _PyLong_FromByteArray( |
| 4773 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 4774 | little_endian, is_signed); |
| 4775 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4777 | /* If from_bytes() was used on subclass, allocate new subclass |
| 4778 | * instance, initialize it with decoded long value and return it. |
| 4779 | */ |
| 4780 | if (type != &PyLong_Type && PyType_IsSubtype(type, &PyLong_Type)) { |
| 4781 | PyLongObject *newobj; |
| 4782 | int i; |
| 4783 | Py_ssize_t n = ABS(Py_SIZE(long_obj)); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 4786 | if (newobj == NULL) { |
| 4787 | Py_DECREF(long_obj); |
| 4788 | return NULL; |
| 4789 | } |
| 4790 | assert(PyLong_Check(newobj)); |
| 4791 | Py_SIZE(newobj) = Py_SIZE(long_obj); |
| 4792 | for (i = 0; i < n; i++) { |
| 4793 | newobj->ob_digit[i] = |
| 4794 | ((PyLongObject *)long_obj)->ob_digit[i]; |
| 4795 | } |
| 4796 | Py_DECREF(long_obj); |
| 4797 | return (PyObject *)newobj; |
| 4798 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 4803 | PyDoc_STRVAR(long_from_bytes_doc, |
| 4804 | "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\ |
| 4805 | \n\ |
| 4806 | Return the integer represented by the given array of bytes.\n\ |
| 4807 | \n\ |
| 4808 | The bytes argument must either support the buffer protocol or be an\n\ |
| 4809 | iterable object producing bytes. Bytes and bytearray are examples of\n\ |
| 4810 | built-in objects that support the buffer protocol.\n\ |
| 4811 | \n\ |
| 4812 | The byteorder argument determines the byte order used to represent the\n\ |
| 4813 | integer. If byteorder is 'big', the most significant byte is at the\n\ |
| 4814 | beginning of the byte array. If byteorder is 'little', the most\n\ |
| 4815 | significant byte is at the end of the byte array. To request the native\n\ |
| 4816 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
| 4817 | \n\ |
| 4818 | The signed keyword-only argument indicates whether two's complement is\n\ |
| 4819 | used to represent the integer."); |
| 4820 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4821 | static PyMethodDef long_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4822 | {"conjugate", (PyCFunction)long_long, METH_NOARGS, |
| 4823 | "Returns self, the complex conjugate of any int."}, |
| 4824 | {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS, |
| 4825 | long_bit_length_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 4826 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
| 4828 | "Returns always True."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 4829 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4830 | {"to_bytes", (PyCFunction)long_to_bytes, |
| 4831 | METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, |
| 4832 | {"from_bytes", (PyCFunction)long_from_bytes, |
| 4833 | METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes_doc}, |
| 4834 | {"__trunc__", (PyCFunction)long_long, METH_NOARGS, |
| 4835 | "Truncating an Integral returns itself."}, |
| 4836 | {"__floor__", (PyCFunction)long_long, METH_NOARGS, |
| 4837 | "Flooring an Integral returns itself."}, |
| 4838 | {"__ceil__", (PyCFunction)long_long, METH_NOARGS, |
| 4839 | "Ceiling of an Integral returns itself."}, |
| 4840 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 4841 | "Rounding an Integral returns itself.\n" |
| 4842 | "Rounding with an ndigits argument also returns an integer."}, |
| 4843 | {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, |
| 4844 | {"__format__", (PyCFunction)long__format__, METH_VARARGS}, |
| 4845 | {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, |
| 4846 | "Returns size in memory, in bytes"}, |
| 4847 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4848 | }; |
| 4849 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4850 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4851 | {"real", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4852 | (getter)long_long, (setter)NULL, |
| 4853 | "the real part of a complex number", |
| 4854 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4855 | {"imag", |
| 4856 | (getter)long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4857 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4858 | NULL}, |
| 4859 | {"numerator", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4860 | (getter)long_long, (setter)NULL, |
| 4861 | "the numerator of a rational number in lowest terms", |
| 4862 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4863 | {"denominator", |
| 4864 | (getter)long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4865 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4866 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4867 | {NULL} /* Sentinel */ |
| 4868 | }; |
| 4869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4870 | PyDoc_STRVAR(long_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 4871 | "int(x=0) -> integer\n\ |
| 4872 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4873 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 4874 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 4875 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 4876 | numbers, this truncates towards zero.\n\ |
| 4877 | \n\ |
| 4878 | If x is not a number or if base is given, then x must be a string,\n\ |
| 4879 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 4880 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 4881 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 4882 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 4883 | >>> int('0b100', base=0)\n\ |
| 4884 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4885 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4886 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4887 | (binaryfunc)long_add, /*nb_add*/ |
| 4888 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 4889 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 4890 | long_mod, /*nb_remainder*/ |
| 4891 | long_divmod, /*nb_divmod*/ |
| 4892 | long_pow, /*nb_power*/ |
| 4893 | (unaryfunc)long_neg, /*nb_negative*/ |
| 4894 | (unaryfunc)long_long, /*tp_positive*/ |
| 4895 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 4896 | (inquiry)long_bool, /*tp_bool*/ |
| 4897 | (unaryfunc)long_invert, /*nb_invert*/ |
| 4898 | long_lshift, /*nb_lshift*/ |
| 4899 | (binaryfunc)long_rshift, /*nb_rshift*/ |
| 4900 | long_and, /*nb_and*/ |
| 4901 | long_xor, /*nb_xor*/ |
| 4902 | long_or, /*nb_or*/ |
| 4903 | long_long, /*nb_int*/ |
| 4904 | 0, /*nb_reserved*/ |
| 4905 | long_float, /*nb_float*/ |
| 4906 | 0, /* nb_inplace_add */ |
| 4907 | 0, /* nb_inplace_subtract */ |
| 4908 | 0, /* nb_inplace_multiply */ |
| 4909 | 0, /* nb_inplace_remainder */ |
| 4910 | 0, /* nb_inplace_power */ |
| 4911 | 0, /* nb_inplace_lshift */ |
| 4912 | 0, /* nb_inplace_rshift */ |
| 4913 | 0, /* nb_inplace_and */ |
| 4914 | 0, /* nb_inplace_xor */ |
| 4915 | 0, /* nb_inplace_or */ |
| 4916 | long_div, /* nb_floor_divide */ |
| 4917 | long_true_divide, /* nb_true_divide */ |
| 4918 | 0, /* nb_inplace_floor_divide */ |
| 4919 | 0, /* nb_inplace_true_divide */ |
| 4920 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4921 | }; |
| 4922 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4923 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4925 | "int", /* tp_name */ |
| 4926 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 4927 | sizeof(digit), /* tp_itemsize */ |
| 4928 | long_dealloc, /* tp_dealloc */ |
| 4929 | 0, /* tp_print */ |
| 4930 | 0, /* tp_getattr */ |
| 4931 | 0, /* tp_setattr */ |
| 4932 | 0, /* tp_reserved */ |
| 4933 | long_to_decimal_string, /* tp_repr */ |
| 4934 | &long_as_number, /* tp_as_number */ |
| 4935 | 0, /* tp_as_sequence */ |
| 4936 | 0, /* tp_as_mapping */ |
| 4937 | (hashfunc)long_hash, /* tp_hash */ |
| 4938 | 0, /* tp_call */ |
| 4939 | long_to_decimal_string, /* tp_str */ |
| 4940 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4941 | 0, /* tp_setattro */ |
| 4942 | 0, /* tp_as_buffer */ |
| 4943 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 4944 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 4945 | long_doc, /* tp_doc */ |
| 4946 | 0, /* tp_traverse */ |
| 4947 | 0, /* tp_clear */ |
| 4948 | long_richcompare, /* tp_richcompare */ |
| 4949 | 0, /* tp_weaklistoffset */ |
| 4950 | 0, /* tp_iter */ |
| 4951 | 0, /* tp_iternext */ |
| 4952 | long_methods, /* tp_methods */ |
| 4953 | 0, /* tp_members */ |
| 4954 | long_getset, /* tp_getset */ |
| 4955 | 0, /* tp_base */ |
| 4956 | 0, /* tp_dict */ |
| 4957 | 0, /* tp_descr_get */ |
| 4958 | 0, /* tp_descr_set */ |
| 4959 | 0, /* tp_dictoffset */ |
| 4960 | 0, /* tp_init */ |
| 4961 | 0, /* tp_alloc */ |
| 4962 | long_new, /* tp_new */ |
| 4963 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4964 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4965 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 4966 | static PyTypeObject Int_InfoType; |
| 4967 | |
| 4968 | PyDoc_STRVAR(int_info__doc__, |
| 4969 | "sys.int_info\n\ |
| 4970 | \n\ |
| 4971 | A struct sequence that holds information about Python's\n\ |
| 4972 | internal representation of integers. The attributes are read only."); |
| 4973 | |
| 4974 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4976 | {"sizeof_digit", "size in bytes of the C type used to represent a digit"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 4978 | }; |
| 4979 | |
| 4980 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | "sys.int_info", /* name */ |
| 4982 | int_info__doc__, /* doc */ |
| 4983 | int_info_fields, /* fields */ |
| 4984 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 4985 | }; |
| 4986 | |
| 4987 | PyObject * |
| 4988 | PyLong_GetInfo(void) |
| 4989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4990 | PyObject* int_info; |
| 4991 | int field = 0; |
| 4992 | int_info = PyStructSequence_New(&Int_InfoType); |
| 4993 | if (int_info == NULL) |
| 4994 | return NULL; |
| 4995 | PyStructSequence_SET_ITEM(int_info, field++, |
| 4996 | PyLong_FromLong(PyLong_SHIFT)); |
| 4997 | PyStructSequence_SET_ITEM(int_info, field++, |
| 4998 | PyLong_FromLong(sizeof(digit))); |
| 4999 | if (PyErr_Occurred()) { |
| 5000 | Py_CLEAR(int_info); |
| 5001 | return NULL; |
| 5002 | } |
| 5003 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5006 | int |
| 5007 | _PyLong_Init(void) |
| 5008 | { |
| 5009 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | int ival, size; |
| 5011 | PyLongObject *v = small_ints; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { |
| 5014 | size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
| 5015 | if (Py_TYPE(v) == &PyLong_Type) { |
| 5016 | /* The element is already initialized, most likely |
| 5017 | * the Python interpreter was initialized before. |
| 5018 | */ |
| 5019 | Py_ssize_t refcnt; |
| 5020 | PyObject* op = (PyObject*)v; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5022 | refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); |
| 5023 | _Py_NewReference(op); |
| 5024 | /* _Py_NewReference sets the ref count to 1 but |
| 5025 | * the ref count might be larger. Set the refcnt |
| 5026 | * to the original refcnt + 1 */ |
| 5027 | Py_REFCNT(op) = refcnt + 1; |
| 5028 | assert(Py_SIZE(op) == size); |
| 5029 | assert(v->ob_digit[0] == abs(ival)); |
| 5030 | } |
| 5031 | else { |
| 5032 | PyObject_INIT(v, &PyLong_Type); |
| 5033 | } |
| 5034 | Py_SIZE(v) = size; |
| 5035 | v->ob_digit[0] = abs(ival); |
| 5036 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5037 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | /* initialize int_info */ |
| 5039 | if (Int_InfoType.tp_name == 0) |
| 5040 | PyStructSequence_InitType(&Int_InfoType, &int_info_desc); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | void |
| 5046 | PyLong_Fini(void) |
| 5047 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | /* Integers are currently statically allocated. Py_DECREF is not |
| 5049 | needed, but Python must forget about the reference or multiple |
| 5050 | reinitializations will fail. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5051 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5052 | int i; |
| 5053 | PyLongObject *v = small_ints; |
| 5054 | for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { |
| 5055 | _Py_DEC_REFTOTAL; |
| 5056 | _Py_ForgetReference((PyObject*)v); |
| 5057 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5058 | #endif |
| 5059 | } |