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 */ |
Victor Stinner | 08a80b1 | 2013-07-17 22:33:42 +0200 | [diff] [blame] | 20 | #define MEDIUM_VALUE(x) (assert(-1 <= Py_SIZE(x) && Py_SIZE(x) <= 1), \ |
| 21 | Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
| 23 | (sdigit)(x)->ob_digit[0])) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 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 | { |
Benjamin Peterson | 45c9dce | 2014-03-14 21:53:51 -0500 | [diff] [blame] | 39 | PyObject *v; |
Benjamin Peterson | 041c38a | 2014-03-14 21:47:23 -0500 | [diff] [blame] | 40 | assert(-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS); |
Benjamin Peterson | 45c9dce | 2014-03-14 21:53:51 -0500 | [diff] [blame] | 41 | v = (PyObject *)&small_ints[ival + NSMALLNEGINTS]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | Py_INCREF(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 43 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | if (ival >= 0) |
| 45 | quick_int_allocs++; |
| 46 | else |
| 47 | quick_neg_int_allocs++; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 48 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | return v; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 50 | } |
| 51 | #define CHECK_SMALL_INT(ival) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ |
| 53 | return get_small_int((sdigit)ival); \ |
| 54 | } while(0) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | static PyLongObject * |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 57 | maybe_small_long(PyLongObject *v) |
| 58 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 59 | if (v && Py_ABS(Py_SIZE(v)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | sdigit ival = MEDIUM_VALUE(v); |
| 61 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 62 | Py_DECREF(v); |
| 63 | return (PyLongObject *)get_small_int(ival); |
| 64 | } |
| 65 | } |
| 66 | return v; |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 67 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 68 | #else |
| 69 | #define CHECK_SMALL_INT(ival) |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 70 | #define maybe_small_long(val) (val) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 73 | /* If a freshly-allocated int is already shared, it must |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 74 | be a small integer, so negating it must go to PyLong_FromLong */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 75 | Py_LOCAL_INLINE(void) |
| 76 | _PyLong_Negate(PyLongObject **x_p) |
| 77 | { |
| 78 | PyLongObject *x; |
| 79 | |
| 80 | x = (PyLongObject *)*x_p; |
| 81 | if (Py_REFCNT(x) == 1) { |
| 82 | Py_SIZE(x) = -Py_SIZE(x); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | *x_p = (PyLongObject *)PyLong_FromLong(-MEDIUM_VALUE(x)); |
| 87 | Py_DECREF(x); |
| 88 | } |
| 89 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 90 | /* For int multiplication, use the O(N**2) school algorithm unless |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 91 | * both operands contain more than KARATSUBA_CUTOFF digits (this |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 92 | * being an internal Python int digit, in base BASE). |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 93 | */ |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 94 | #define KARATSUBA_CUTOFF 70 |
| 95 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 96 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 97 | /* For exponentiation, use the binary left-to-right algorithm |
| 98 | * unless the exponent contains more than FIVEARY_CUTOFF digits. |
| 99 | * In that case, do 5 bits at a time. The potential drawback is that |
| 100 | * a table of 2**5 intermediate results is computed. |
| 101 | */ |
| 102 | #define FIVEARY_CUTOFF 8 |
| 103 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 104 | #define SIGCHECK(PyTryBlock) \ |
| 105 | do { \ |
| 106 | if (PyErr_CheckSignals()) PyTryBlock \ |
| 107 | } while(0) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 108 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 109 | /* Normalize (remove leading zeros from) an int object. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 110 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 111 | of the algorithms used, this could save at most be one word anyway. */ |
| 112 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | static PyLongObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 114 | long_normalize(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 115 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 116 | Py_ssize_t j = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | Py_ssize_t i = j; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 120 | --i; |
| 121 | if (i != j) |
| 122 | Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
| 123 | return v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 126 | /* _PyLong_FromNbInt: Convert the given object to a PyLongObject |
| 127 | using the nb_int slot, if available. Raise TypeError if either the |
| 128 | nb_int slot is not available or the result of the call to nb_int |
| 129 | returns something not of type int. |
| 130 | */ |
| 131 | PyLongObject * |
| 132 | _PyLong_FromNbInt(PyObject *integral) |
| 133 | { |
| 134 | PyNumberMethods *nb; |
| 135 | PyObject *result; |
| 136 | |
| 137 | /* Fast path for the case that we already have an int. */ |
| 138 | if (PyLong_CheckExact(integral)) { |
| 139 | Py_INCREF(integral); |
| 140 | return (PyLongObject *)integral; |
| 141 | } |
| 142 | |
| 143 | nb = Py_TYPE(integral)->tp_as_number; |
| 144 | if (nb == NULL || nb->nb_int == NULL) { |
| 145 | PyErr_Format(PyExc_TypeError, |
| 146 | "an integer is required (got type %.200s)", |
| 147 | Py_TYPE(integral)->tp_name); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /* Convert using the nb_int slot, which should return something |
| 152 | of exact type int. */ |
| 153 | result = nb->nb_int(integral); |
| 154 | if (!result || PyLong_CheckExact(result)) |
| 155 | return (PyLongObject *)result; |
| 156 | if (!PyLong_Check(result)) { |
| 157 | PyErr_Format(PyExc_TypeError, |
| 158 | "__int__ returned non-int (type %.200s)", |
| 159 | result->ob_type->tp_name); |
| 160 | Py_DECREF(result); |
| 161 | return NULL; |
| 162 | } |
| 163 | /* Issue #17576: warn if 'result' not of exact type int. */ |
| 164 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 165 | "__int__ returned non-int (type %.200s). " |
| 166 | "The ability to return an instance of a strict subclass of int " |
| 167 | "is deprecated, and may be removed in a future version of Python.", |
| 168 | result->ob_type->tp_name)) { |
| 169 | Py_DECREF(result); |
| 170 | return NULL; |
| 171 | } |
| 172 | return (PyLongObject *)result; |
| 173 | } |
| 174 | |
| 175 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 176 | /* Allocate a new int object with size digits. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 177 | Return NULL and set exception if we run out of memory. */ |
| 178 | |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 179 | #define MAX_LONG_DIGITS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
Mark Dickinson | 5a74bf6 | 2009-02-15 11:04:38 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | PyLongObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 183 | _PyLong_New(Py_ssize_t size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | PyLongObject *result; |
| 186 | /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
| 187 | sizeof(digit)*size. Previous incarnations of this code used |
| 188 | sizeof(PyVarObject) instead of the offsetof, but this risks being |
| 189 | incorrect in the presence of padding between the PyVarObject header |
| 190 | and the digits. */ |
| 191 | if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
| 192 | PyErr_SetString(PyExc_OverflowError, |
| 193 | "too many digits in integer"); |
| 194 | return NULL; |
| 195 | } |
| 196 | result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
| 197 | size*sizeof(digit)); |
| 198 | if (!result) { |
| 199 | PyErr_NoMemory(); |
| 200 | return NULL; |
| 201 | } |
| 202 | return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 205 | PyObject * |
| 206 | _PyLong_Copy(PyLongObject *src) |
| 207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | PyLongObject *result; |
| 209 | Py_ssize_t i; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | assert(src != NULL); |
| 212 | i = Py_SIZE(src); |
| 213 | if (i < 0) |
| 214 | i = -(i); |
| 215 | if (i < 2) { |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 216 | sdigit ival = MEDIUM_VALUE(src); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | CHECK_SMALL_INT(ival); |
| 218 | } |
| 219 | result = _PyLong_New(i); |
| 220 | if (result != NULL) { |
| 221 | Py_SIZE(result) = Py_SIZE(src); |
| 222 | while (--i >= 0) |
| 223 | result->ob_digit[i] = src->ob_digit[i]; |
| 224 | } |
| 225 | return (PyObject *)result; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 228 | /* Create a new int object from a C long int */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 231 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | PyLongObject *v; |
| 234 | unsigned long abs_ival; |
| 235 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 236 | int ndigits = 0; |
| 237 | int sign = 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | CHECK_SMALL_INT(ival); |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | if (ival < 0) { |
| 242 | /* negate: can't write this as abs_ival = -ival since that |
| 243 | invokes undefined behaviour when ival is LONG_MIN */ |
| 244 | abs_ival = 0U-(unsigned long)ival; |
| 245 | sign = -1; |
| 246 | } |
| 247 | else { |
| 248 | abs_ival = (unsigned long)ival; |
| 249 | } |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | /* Fast path for single-digit ints */ |
| 252 | if (!(abs_ival >> PyLong_SHIFT)) { |
| 253 | v = _PyLong_New(1); |
| 254 | if (v) { |
| 255 | Py_SIZE(v) = sign; |
| 256 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 257 | abs_ival, unsigned long, digit); |
| 258 | } |
| 259 | return (PyObject*)v; |
| 260 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 261 | |
Mark Dickinson | 249b898 | 2009-04-27 19:41:00 +0000 | [diff] [blame] | 262 | #if PyLong_SHIFT==15 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | /* 2 digits */ |
| 264 | if (!(abs_ival >> 2*PyLong_SHIFT)) { |
| 265 | v = _PyLong_New(2); |
| 266 | if (v) { |
| 267 | Py_SIZE(v) = 2*sign; |
| 268 | v->ob_digit[0] = Py_SAFE_DOWNCAST( |
| 269 | abs_ival & PyLong_MASK, unsigned long, digit); |
| 270 | v->ob_digit[1] = Py_SAFE_DOWNCAST( |
| 271 | abs_ival >> PyLong_SHIFT, unsigned long, digit); |
| 272 | } |
| 273 | return (PyObject*)v; |
| 274 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 275 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | /* Larger numbers: loop to determine number of digits */ |
| 278 | t = abs_ival; |
| 279 | while (t) { |
| 280 | ++ndigits; |
| 281 | t >>= PyLong_SHIFT; |
| 282 | } |
| 283 | v = _PyLong_New(ndigits); |
| 284 | if (v != NULL) { |
| 285 | digit *p = v->ob_digit; |
| 286 | Py_SIZE(v) = ndigits*sign; |
| 287 | t = abs_ival; |
| 288 | while (t) { |
| 289 | *p++ = Py_SAFE_DOWNCAST( |
| 290 | t & PyLong_MASK, unsigned long, digit); |
| 291 | t >>= PyLong_SHIFT; |
| 292 | } |
| 293 | } |
| 294 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 297 | /* Create a new int object from a C unsigned long int */ |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 300 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | PyLongObject *v; |
| 303 | unsigned long t; |
| 304 | int ndigits = 0; |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (ival < PyLong_BASE) |
| 307 | return PyLong_FromLong(ival); |
| 308 | /* Count the number of Python digits. */ |
| 309 | t = (unsigned long)ival; |
| 310 | while (t) { |
| 311 | ++ndigits; |
| 312 | t >>= PyLong_SHIFT; |
| 313 | } |
| 314 | v = _PyLong_New(ndigits); |
| 315 | if (v != NULL) { |
| 316 | digit *p = v->ob_digit; |
| 317 | Py_SIZE(v) = ndigits; |
| 318 | while (ival) { |
| 319 | *p++ = (digit)(ival & PyLong_MASK); |
| 320 | ival >>= PyLong_SHIFT; |
| 321 | } |
| 322 | } |
| 323 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 326 | /* Create a new int object from a C double */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 328 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 329 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | PyLongObject *v; |
| 332 | double frac; |
| 333 | int i, ndig, expo, neg; |
| 334 | neg = 0; |
| 335 | if (Py_IS_INFINITY(dval)) { |
| 336 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 337 | "cannot convert float infinity to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | if (Py_IS_NAN(dval)) { |
| 341 | PyErr_SetString(PyExc_ValueError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 342 | "cannot convert float NaN to integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | if (dval < 0.0) { |
| 346 | neg = 1; |
| 347 | dval = -dval; |
| 348 | } |
| 349 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 350 | if (expo <= 0) |
| 351 | return PyLong_FromLong(0L); |
| 352 | ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
| 353 | v = _PyLong_New(ndig); |
| 354 | if (v == NULL) |
| 355 | return NULL; |
| 356 | frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
| 357 | for (i = ndig; --i >= 0; ) { |
| 358 | digit bits = (digit)frac; |
| 359 | v->ob_digit[i] = bits; |
| 360 | frac = frac - (double)bits; |
| 361 | frac = ldexp(frac, PyLong_SHIFT); |
| 362 | } |
| 363 | if (neg) |
| 364 | Py_SIZE(v) = -(Py_SIZE(v)); |
| 365 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 368 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 369 | * anything about what happens when a signed integer operation overflows, |
| 370 | * and some compilers think they're doing you a favor by being "clever" |
| 371 | * then. The bit pattern for the largest postive signed long is |
| 372 | * (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 373 | * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 374 | * However, some other compilers warn about applying unary minus to an |
| 375 | * unsigned operand. Hence the weird "0-". |
| 376 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
| 378 | #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] | 379 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 380 | /* Get a C long int from an int object or any object that has an __int__ |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 381 | method. |
| 382 | |
| 383 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 384 | the result. Otherwise *overflow is 0. |
| 385 | |
| 386 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 387 | In this case *overflow will be 0. |
| 388 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 389 | |
| 390 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 391 | PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 394 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | unsigned long x, prev; |
| 396 | long res; |
| 397 | Py_ssize_t i; |
| 398 | int sign; |
| 399 | int do_decref = 0; /* if nb_int was called */ |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | *overflow = 0; |
| 402 | if (vv == NULL) { |
| 403 | PyErr_BadInternalCall(); |
| 404 | return -1; |
| 405 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 406 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 407 | if (PyLong_Check(vv)) { |
| 408 | v = (PyLongObject *)vv; |
| 409 | } |
| 410 | else { |
| 411 | v = _PyLong_FromNbInt(vv); |
| 412 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | return -1; |
| 414 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | i = Py_SIZE(v); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | switch (i) { |
| 421 | case -1: |
| 422 | res = -(sdigit)v->ob_digit[0]; |
| 423 | break; |
| 424 | case 0: |
| 425 | res = 0; |
| 426 | break; |
| 427 | case 1: |
| 428 | res = v->ob_digit[0]; |
| 429 | break; |
| 430 | default: |
| 431 | sign = 1; |
| 432 | x = 0; |
| 433 | if (i < 0) { |
| 434 | sign = -1; |
| 435 | i = -(i); |
| 436 | } |
| 437 | while (--i >= 0) { |
| 438 | prev = x; |
| 439 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 440 | if ((x >> PyLong_SHIFT) != prev) { |
| 441 | *overflow = sign; |
| 442 | goto exit; |
| 443 | } |
| 444 | } |
| 445 | /* Haven't lost any bits, but casting to long requires extra |
| 446 | * care (see comment above). |
| 447 | */ |
| 448 | if (x <= (unsigned long)LONG_MAX) { |
| 449 | res = (long)x * sign; |
| 450 | } |
| 451 | else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
| 452 | res = LONG_MIN; |
| 453 | } |
| 454 | else { |
| 455 | *overflow = sign; |
| 456 | /* res is already set to -1 */ |
| 457 | } |
| 458 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 459 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 461 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | } |
| 463 | return res; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 466 | /* Get a C long int from an int object or any object that has an __int__ |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 467 | method. Return -1 and set an error if overflow occurs. */ |
| 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | long |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 470 | PyLong_AsLong(PyObject *obj) |
| 471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | int overflow; |
| 473 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 474 | if (overflow) { |
| 475 | /* XXX: could be cute and give a different |
| 476 | message for overflow == -1 */ |
| 477 | PyErr_SetString(PyExc_OverflowError, |
| 478 | "Python int too large to convert to C long"); |
| 479 | } |
| 480 | return result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 483 | /* Get a C int from an int object or any object that has an __int__ |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 484 | method. Return -1 and set an error if overflow occurs. */ |
| 485 | |
| 486 | int |
| 487 | _PyLong_AsInt(PyObject *obj) |
| 488 | { |
| 489 | int overflow; |
| 490 | long result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 491 | if (overflow || result > INT_MAX || result < INT_MIN) { |
| 492 | /* XXX: could be cute and give a different |
| 493 | message for overflow == -1 */ |
| 494 | PyErr_SetString(PyExc_OverflowError, |
| 495 | "Python int too large to convert to C int"); |
| 496 | return -1; |
| 497 | } |
| 498 | return (int)result; |
| 499 | } |
| 500 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 501 | /* Get a Py_ssize_t from an int object. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 502 | Returns -1 and sets an error condition if overflow occurs. */ |
| 503 | |
| 504 | Py_ssize_t |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 505 | PyLong_AsSsize_t(PyObject *vv) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 506 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | size_t x, prev; |
| 508 | Py_ssize_t i; |
| 509 | int sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | if (vv == NULL) { |
| 512 | PyErr_BadInternalCall(); |
| 513 | return -1; |
| 514 | } |
| 515 | if (!PyLong_Check(vv)) { |
| 516 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 517 | return -1; |
| 518 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | v = (PyLongObject *)vv; |
| 521 | i = Py_SIZE(v); |
| 522 | switch (i) { |
| 523 | case -1: return -(sdigit)v->ob_digit[0]; |
| 524 | case 0: return 0; |
| 525 | case 1: return v->ob_digit[0]; |
| 526 | } |
| 527 | sign = 1; |
| 528 | x = 0; |
| 529 | if (i < 0) { |
| 530 | sign = -1; |
| 531 | i = -(i); |
| 532 | } |
| 533 | while (--i >= 0) { |
| 534 | prev = x; |
| 535 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 536 | if ((x >> PyLong_SHIFT) != prev) |
| 537 | goto overflow; |
| 538 | } |
| 539 | /* Haven't lost any bits, but casting to a signed type requires |
| 540 | * extra care (see comment above). |
| 541 | */ |
| 542 | if (x <= (size_t)PY_SSIZE_T_MAX) { |
| 543 | return (Py_ssize_t)x * sign; |
| 544 | } |
| 545 | else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
| 546 | return PY_SSIZE_T_MIN; |
| 547 | } |
| 548 | /* else overflow */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 549 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 550 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | PyErr_SetString(PyExc_OverflowError, |
| 552 | "Python int too large to convert to C ssize_t"); |
| 553 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 556 | /* Get a C unsigned long int from an int object. |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 557 | Returns -1 and sets an error condition if overflow occurs. */ |
| 558 | |
| 559 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 560 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 562 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | unsigned long x, prev; |
| 564 | Py_ssize_t i; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | if (vv == NULL) { |
| 567 | PyErr_BadInternalCall(); |
| 568 | return (unsigned long)-1; |
| 569 | } |
| 570 | if (!PyLong_Check(vv)) { |
| 571 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 572 | return (unsigned long)-1; |
| 573 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | v = (PyLongObject *)vv; |
| 576 | i = Py_SIZE(v); |
| 577 | x = 0; |
| 578 | if (i < 0) { |
| 579 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 580 | "can't convert negative value to unsigned int"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | return (unsigned long) -1; |
| 582 | } |
| 583 | switch (i) { |
| 584 | case 0: return 0; |
| 585 | case 1: return v->ob_digit[0]; |
| 586 | } |
| 587 | while (--i >= 0) { |
| 588 | prev = x; |
| 589 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 590 | if ((x >> PyLong_SHIFT) != prev) { |
| 591 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 592 | "Python int too large to convert " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 593 | "to C unsigned long"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | return (unsigned long) -1; |
| 595 | } |
| 596 | } |
| 597 | return x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 600 | /* Get a C size_t from an int object. Returns (size_t)-1 and sets |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 601 | an error condition if overflow occurs. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 602 | |
| 603 | size_t |
| 604 | PyLong_AsSize_t(PyObject *vv) |
| 605 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 606 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | size_t x, prev; |
| 608 | Py_ssize_t i; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | if (vv == NULL) { |
| 611 | PyErr_BadInternalCall(); |
| 612 | return (size_t) -1; |
| 613 | } |
| 614 | if (!PyLong_Check(vv)) { |
| 615 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 616 | return (size_t)-1; |
| 617 | } |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | v = (PyLongObject *)vv; |
| 620 | i = Py_SIZE(v); |
| 621 | x = 0; |
| 622 | if (i < 0) { |
| 623 | PyErr_SetString(PyExc_OverflowError, |
| 624 | "can't convert negative value to size_t"); |
| 625 | return (size_t) -1; |
| 626 | } |
| 627 | switch (i) { |
| 628 | case 0: return 0; |
| 629 | case 1: return v->ob_digit[0]; |
| 630 | } |
| 631 | while (--i >= 0) { |
| 632 | prev = x; |
| 633 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 634 | if ((x >> PyLong_SHIFT) != prev) { |
| 635 | PyErr_SetString(PyExc_OverflowError, |
| 636 | "Python int too large to convert to C size_t"); |
Stefan Krah | b77c6c6 | 2011-09-12 16:22:47 +0200 | [diff] [blame] | 637 | return (size_t) -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | return x; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 643 | /* Get a C unsigned long int from an int object, ignoring the high bits. |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 644 | Returns -1 and sets an error condition if an error occurs. */ |
| 645 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 646 | static unsigned long |
| 647 | _PyLong_AsUnsignedLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 648 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 649 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | unsigned long x; |
| 651 | Py_ssize_t i; |
| 652 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | if (vv == NULL || !PyLong_Check(vv)) { |
| 655 | PyErr_BadInternalCall(); |
| 656 | return (unsigned long) -1; |
| 657 | } |
| 658 | v = (PyLongObject *)vv; |
| 659 | i = Py_SIZE(v); |
| 660 | switch (i) { |
| 661 | case 0: return 0; |
| 662 | case 1: return v->ob_digit[0]; |
| 663 | } |
| 664 | sign = 1; |
| 665 | x = 0; |
| 666 | if (i < 0) { |
| 667 | sign = -1; |
| 668 | i = -i; |
| 669 | } |
| 670 | while (--i >= 0) { |
| 671 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 672 | } |
| 673 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 676 | unsigned long |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 677 | PyLong_AsUnsignedLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyLongObject *lo; |
| 680 | unsigned long val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 681 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 682 | if (op == NULL) { |
| 683 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | return (unsigned long)-1; |
| 685 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 686 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 687 | if (PyLong_Check(op)) { |
| 688 | return _PyLong_AsUnsignedLongMask(op); |
| 689 | } |
| 690 | |
| 691 | lo = _PyLong_FromNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | if (lo == NULL) |
| 693 | return (unsigned long)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 694 | |
| 695 | val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
| 696 | Py_DECREF(lo); |
| 697 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 700 | int |
| 701 | _PyLong_Sign(PyObject *vv) |
| 702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | PyLongObject *v = (PyLongObject *)vv; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | assert(v != NULL); |
| 706 | assert(PyLong_Check(v)); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 711 | size_t |
| 712 | _PyLong_NumBits(PyObject *vv) |
| 713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | PyLongObject *v = (PyLongObject *)vv; |
| 715 | size_t result = 0; |
| 716 | Py_ssize_t ndigits; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | assert(v != NULL); |
| 719 | assert(PyLong_Check(v)); |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 720 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 722 | if (ndigits > 0) { |
| 723 | digit msd = v->ob_digit[ndigits - 1]; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 724 | if ((size_t)(ndigits - 1) > PY_SIZE_MAX / (size_t)PyLong_SHIFT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | goto Overflow; |
Mark Dickinson | fc9adb6 | 2012-10-06 18:50:02 +0100 | [diff] [blame] | 726 | result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | do { |
| 728 | ++result; |
| 729 | if (result == 0) |
| 730 | goto Overflow; |
| 731 | msd >>= 1; |
| 732 | } while (msd); |
| 733 | } |
| 734 | return result; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 735 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 736 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
| 738 | "to express in a platform size_t"); |
| 739 | return (size_t)-1; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 742 | PyObject * |
| 743 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | int little_endian, int is_signed) |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 745 | { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 746 | const unsigned char* pstartbyte; /* LSB of bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | int incr; /* direction to move pstartbyte */ |
| 748 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 749 | size_t numsignificantbytes; /* number of bytes that matter */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 750 | Py_ssize_t ndigits; /* number of Python int digits */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | PyLongObject* v; /* result */ |
| 752 | Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | if (n == 0) |
| 755 | return PyLong_FromLong(0L); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | if (little_endian) { |
| 758 | pstartbyte = bytes; |
| 759 | pendbyte = bytes + n - 1; |
| 760 | incr = 1; |
| 761 | } |
| 762 | else { |
| 763 | pstartbyte = bytes + n - 1; |
| 764 | pendbyte = bytes; |
| 765 | incr = -1; |
| 766 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | if (is_signed) |
| 769 | is_signed = *pendbyte >= 0x80; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | /* Compute numsignificantbytes. This consists of finding the most |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 772 | significant byte. Leading 0 bytes are insignificant if the number |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | is positive, and leading 0xff bytes if negative. */ |
| 774 | { |
| 775 | size_t i; |
| 776 | const unsigned char* p = pendbyte; |
| 777 | const int pincr = -incr; /* search MSB to LSB */ |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 778 | const unsigned char insignificant = is_signed ? 0xff : 0x00; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | for (i = 0; i < n; ++i, p += pincr) { |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 781 | if (*p != insignificant) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | break; |
| 783 | } |
| 784 | numsignificantbytes = n - i; |
| 785 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 786 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 787 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 788 | do for 0xffff = -0x0001. To be safe without bothering to |
| 789 | check every case, bump it regardless. */ |
| 790 | if (is_signed && numsignificantbytes < n) |
| 791 | ++numsignificantbytes; |
| 792 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 793 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 794 | /* How many Python int digits do we need? We have |
| 795 | 8*numsignificantbytes bits, and each Python int digit has |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
| 797 | /* catch overflow before it happens */ |
| 798 | if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
| 799 | PyErr_SetString(PyExc_OverflowError, |
| 800 | "byte array too long to convert to int"); |
| 801 | return NULL; |
| 802 | } |
| 803 | ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
| 804 | v = _PyLong_New(ndigits); |
| 805 | if (v == NULL) |
| 806 | return NULL; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 809 | the fly for signed numbers, and dealing with the mismatch between |
| 810 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 811 | { |
| 812 | size_t i; |
| 813 | twodigits carry = 1; /* for 2's-comp calculation */ |
| 814 | twodigits accum = 0; /* sliding register */ |
| 815 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 816 | const unsigned char* p = pstartbyte; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
| 819 | twodigits thisbyte = *p; |
| 820 | /* Compute correction for 2's comp, if needed. */ |
| 821 | if (is_signed) { |
| 822 | thisbyte = (0xff ^ thisbyte) + carry; |
| 823 | carry = thisbyte >> 8; |
| 824 | thisbyte &= 0xff; |
| 825 | } |
| 826 | /* Because we're going LSB to MSB, thisbyte is |
| 827 | more significant than what's already in accum, |
| 828 | so needs to be prepended to accum. */ |
| 829 | accum |= (twodigits)thisbyte << accumbits; |
| 830 | accumbits += 8; |
| 831 | if (accumbits >= PyLong_SHIFT) { |
| 832 | /* There's enough to fill a Python digit. */ |
| 833 | assert(idigit < ndigits); |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 834 | v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | ++idigit; |
| 836 | accum >>= PyLong_SHIFT; |
| 837 | accumbits -= PyLong_SHIFT; |
| 838 | assert(accumbits < PyLong_SHIFT); |
| 839 | } |
| 840 | } |
| 841 | assert(accumbits < PyLong_SHIFT); |
| 842 | if (accumbits) { |
| 843 | assert(idigit < ndigits); |
| 844 | v->ob_digit[idigit] = (digit)accum; |
| 845 | ++idigit; |
| 846 | } |
| 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 | Py_SIZE(v) = is_signed ? -idigit : idigit; |
| 850 | return (PyObject *)long_normalize(v); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | int |
| 854 | _PyLong_AsByteArray(PyLongObject* v, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | unsigned char* bytes, size_t n, |
| 856 | int little_endian, int is_signed) |
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 | Py_ssize_t i; /* index into v->ob_digit */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 859 | Py_ssize_t ndigits; /* |v->ob_size| */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | twodigits accum; /* sliding register */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 861 | unsigned int accumbits; /* # bits in accum */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 863 | digit carry; /* for computing 2's-comp */ |
| 864 | size_t j; /* # bytes filled */ |
| 865 | unsigned char* p; /* pointer to next byte in bytes */ |
| 866 | int pincr; /* direction to move p */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | assert(v != NULL && PyLong_Check(v)); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | if (Py_SIZE(v) < 0) { |
| 871 | ndigits = -(Py_SIZE(v)); |
| 872 | if (!is_signed) { |
| 873 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 874 | "can't convert negative int to unsigned"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | return -1; |
| 876 | } |
| 877 | do_twos_comp = 1; |
| 878 | } |
| 879 | else { |
| 880 | ndigits = Py_SIZE(v); |
| 881 | do_twos_comp = 0; |
| 882 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | if (little_endian) { |
| 885 | p = bytes; |
| 886 | pincr = 1; |
| 887 | } |
| 888 | else { |
| 889 | p = bytes + n - 1; |
| 890 | pincr = -1; |
| 891 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | /* Copy over all the Python digits. |
| 894 | It's crucial that every Python digit except for the MSD contribute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 895 | exactly PyLong_SHIFT bits to the total, so first assert that the int is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | normalized. */ |
| 897 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
| 898 | j = 0; |
| 899 | accum = 0; |
| 900 | accumbits = 0; |
| 901 | carry = do_twos_comp ? 1 : 0; |
| 902 | for (i = 0; i < ndigits; ++i) { |
| 903 | digit thisdigit = v->ob_digit[i]; |
| 904 | if (do_twos_comp) { |
| 905 | thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
| 906 | carry = thisdigit >> PyLong_SHIFT; |
| 907 | thisdigit &= PyLong_MASK; |
| 908 | } |
| 909 | /* Because we're going LSB to MSB, thisdigit is more |
| 910 | significant than what's already in accum, so needs to be |
| 911 | prepended to accum. */ |
| 912 | accum |= (twodigits)thisdigit << accumbits; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | /* The most-significant digit may be (probably is) at least |
| 915 | partly empty. */ |
| 916 | if (i == ndigits - 1) { |
| 917 | /* Count # of sign bits -- they needn't be stored, |
| 918 | * although for signed conversion we need later to |
| 919 | * make sure at least one sign bit gets stored. */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 920 | digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | while (s != 0) { |
| 922 | s >>= 1; |
| 923 | accumbits++; |
| 924 | } |
| 925 | } |
| 926 | else |
| 927 | accumbits += PyLong_SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | /* Store as many bytes as possible. */ |
| 930 | while (accumbits >= 8) { |
| 931 | if (j >= n) |
| 932 | goto Overflow; |
| 933 | ++j; |
| 934 | *p = (unsigned char)(accum & 0xff); |
| 935 | p += pincr; |
| 936 | accumbits -= 8; |
| 937 | accum >>= 8; |
| 938 | } |
| 939 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | /* Store the straggler (if any). */ |
| 942 | assert(accumbits < 8); |
| 943 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
| 944 | if (accumbits > 0) { |
| 945 | if (j >= n) |
| 946 | goto Overflow; |
| 947 | ++j; |
| 948 | if (do_twos_comp) { |
| 949 | /* Fill leading bits of the byte with sign bits |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 950 | (appropriately pretending that the int had an |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | infinite supply of sign bits). */ |
| 952 | accum |= (~(twodigits)0) << accumbits; |
| 953 | } |
| 954 | *p = (unsigned char)(accum & 0xff); |
| 955 | p += pincr; |
| 956 | } |
| 957 | else if (j == n && n > 0 && is_signed) { |
| 958 | /* The main loop filled the byte array exactly, so the code |
| 959 | just above didn't get to ensure there's a sign bit, and the |
| 960 | loop below wouldn't add one either. Make sure a sign bit |
| 961 | exists. */ |
| 962 | unsigned char msb = *(p - pincr); |
| 963 | int sign_bit_set = msb >= 0x80; |
| 964 | assert(accumbits == 0); |
| 965 | if (sign_bit_set == do_twos_comp) |
| 966 | return 0; |
| 967 | else |
| 968 | goto Overflow; |
| 969 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | /* Fill remaining bytes with copies of the sign bit. */ |
| 972 | { |
| 973 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 974 | for ( ; j < n; ++j, p += pincr) |
| 975 | *p = signbyte; |
| 976 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | return 0; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 979 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 980 | Overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
| 982 | return -1; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 983 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 986 | /* Create a new int object from a C pointer */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 987 | |
| 988 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 989 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 990 | { |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 991 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 992 | return PyLong_FromUnsignedLong((unsigned long)(Py_uintptr_t)p); |
| 993 | #else |
| 994 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 995 | #ifndef HAVE_LONG_LONG |
| 996 | # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 997 | #endif |
| 998 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 999 | # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1000 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); |
Mark Dickinson | 9104479 | 2012-10-18 19:21:43 +0100 | [diff] [blame] | 1002 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1003 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1006 | /* Get a C pointer from an int object. */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1007 | |
| 1008 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1009 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1010 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1011 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | long x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1015 | x = PyLong_AsLong(vv); |
| 1016 | else |
| 1017 | x = PyLong_AsUnsignedLong(vv); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1018 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1019 | |
| 1020 | #ifndef HAVE_LONG_LONG |
| 1021 | # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 1022 | #endif |
| 1023 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1024 | # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1025 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | PY_LONG_LONG x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
| 1029 | x = PyLong_AsLongLong(vv); |
| 1030 | else |
| 1031 | x = PyLong_AsUnsignedLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1032 | |
| 1033 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | if (x == -1 && PyErr_Occurred()) |
| 1036 | return NULL; |
| 1037 | return (void *)x; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1040 | #ifdef HAVE_LONG_LONG |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1041 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1042 | /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1043 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1044 | */ |
| 1045 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1046 | #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] | 1047 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1048 | /* Create a new int object from a C PY_LONG_LONG int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1049 | |
| 1050 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1051 | PyLong_FromLongLong(PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | PyLongObject *v; |
| 1054 | unsigned PY_LONG_LONG abs_ival; |
| 1055 | unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */ |
| 1056 | int ndigits = 0; |
| 1057 | int negative = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | CHECK_SMALL_INT(ival); |
| 1060 | if (ival < 0) { |
| 1061 | /* avoid signed overflow on negation; see comments |
| 1062 | in PyLong_FromLong above. */ |
| 1063 | abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1; |
| 1064 | negative = 1; |
| 1065 | } |
| 1066 | else { |
| 1067 | abs_ival = (unsigned PY_LONG_LONG)ival; |
| 1068 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | /* Count the number of Python digits. |
| 1071 | We used to pick 5 ("big enough for anything"), but that's a |
| 1072 | waste of time and space given that 5*15 = 75 bits are rarely |
| 1073 | needed. */ |
| 1074 | t = abs_ival; |
| 1075 | while (t) { |
| 1076 | ++ndigits; |
| 1077 | t >>= PyLong_SHIFT; |
| 1078 | } |
| 1079 | v = _PyLong_New(ndigits); |
| 1080 | if (v != NULL) { |
| 1081 | digit *p = v->ob_digit; |
| 1082 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1083 | t = abs_ival; |
| 1084 | while (t) { |
| 1085 | *p++ = (digit)(t & PyLong_MASK); |
| 1086 | t >>= PyLong_SHIFT; |
| 1087 | } |
| 1088 | } |
| 1089 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1092 | /* Create a new int object from a C unsigned PY_LONG_LONG int. */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1094 | PyObject * |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1095 | PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | PyLongObject *v; |
| 1098 | unsigned PY_LONG_LONG t; |
| 1099 | int ndigits = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | if (ival < PyLong_BASE) |
| 1102 | return PyLong_FromLong((long)ival); |
| 1103 | /* Count the number of Python digits. */ |
| 1104 | t = (unsigned PY_LONG_LONG)ival; |
| 1105 | while (t) { |
| 1106 | ++ndigits; |
| 1107 | t >>= PyLong_SHIFT; |
| 1108 | } |
| 1109 | v = _PyLong_New(ndigits); |
| 1110 | if (v != NULL) { |
| 1111 | digit *p = v->ob_digit; |
| 1112 | Py_SIZE(v) = ndigits; |
| 1113 | while (ival) { |
| 1114 | *p++ = (digit)(ival & PyLong_MASK); |
| 1115 | ival >>= PyLong_SHIFT; |
| 1116 | } |
| 1117 | } |
| 1118 | return (PyObject *)v; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1121 | /* Create a new int object from a C Py_ssize_t. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1122 | |
| 1123 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1124 | PyLong_FromSsize_t(Py_ssize_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | PyLongObject *v; |
| 1127 | size_t abs_ival; |
| 1128 | size_t t; /* unsigned so >> doesn't propagate sign bit */ |
| 1129 | int ndigits = 0; |
| 1130 | int negative = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | CHECK_SMALL_INT(ival); |
| 1133 | if (ival < 0) { |
| 1134 | /* avoid signed overflow when ival = SIZE_T_MIN */ |
| 1135 | abs_ival = (size_t)(-1-ival)+1; |
| 1136 | negative = 1; |
| 1137 | } |
| 1138 | else { |
| 1139 | abs_ival = (size_t)ival; |
| 1140 | } |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | /* Count the number of Python digits. */ |
| 1143 | t = abs_ival; |
| 1144 | while (t) { |
| 1145 | ++ndigits; |
| 1146 | t >>= PyLong_SHIFT; |
| 1147 | } |
| 1148 | v = _PyLong_New(ndigits); |
| 1149 | if (v != NULL) { |
| 1150 | digit *p = v->ob_digit; |
| 1151 | Py_SIZE(v) = negative ? -ndigits : ndigits; |
| 1152 | t = abs_ival; |
| 1153 | while (t) { |
| 1154 | *p++ = (digit)(t & PyLong_MASK); |
| 1155 | t >>= PyLong_SHIFT; |
| 1156 | } |
| 1157 | } |
| 1158 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1161 | /* Create a new int object from a C size_t. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1162 | |
| 1163 | PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1164 | PyLong_FromSize_t(size_t ival) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | PyLongObject *v; |
| 1167 | size_t t; |
| 1168 | int ndigits = 0; |
Mark Dickinson | 7ab6be2 | 2008-04-15 21:42:42 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | if (ival < PyLong_BASE) |
| 1171 | return PyLong_FromLong((long)ival); |
| 1172 | /* Count the number of Python digits. */ |
| 1173 | t = ival; |
| 1174 | while (t) { |
| 1175 | ++ndigits; |
| 1176 | t >>= PyLong_SHIFT; |
| 1177 | } |
| 1178 | v = _PyLong_New(ndigits); |
| 1179 | if (v != NULL) { |
| 1180 | digit *p = v->ob_digit; |
| 1181 | Py_SIZE(v) = ndigits; |
| 1182 | while (ival) { |
| 1183 | *p++ = (digit)(ival & PyLong_MASK); |
| 1184 | ival >>= PyLong_SHIFT; |
| 1185 | } |
| 1186 | } |
| 1187 | return (PyObject *)v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1190 | /* Get a C long long int from an int object or any object that has an |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1191 | __int__ method. Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1192 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1193 | PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1194 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | PyLongObject *v; |
| 1197 | PY_LONG_LONG bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | int res; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1199 | int do_decref = 0; /* if nb_int was called */ |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | if (vv == NULL) { |
| 1202 | PyErr_BadInternalCall(); |
| 1203 | return -1; |
| 1204 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1205 | |
| 1206 | if (PyLong_Check(vv)) { |
| 1207 | v = (PyLongObject *)vv; |
| 1208 | } |
| 1209 | else { |
| 1210 | v = _PyLong_FromNbInt(vv); |
| 1211 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | return -1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1213 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1215 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1216 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | switch(Py_SIZE(v)) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1218 | case -1: |
| 1219 | bytes = -(sdigit)v->ob_digit[0]; |
| 1220 | break; |
| 1221 | case 0: |
| 1222 | bytes = 0; |
| 1223 | break; |
| 1224 | case 1: |
| 1225 | bytes = v->ob_digit[0]; |
| 1226 | break; |
| 1227 | default: |
| 1228 | res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes, |
Serhiy Storchaka | c4f3212 | 2013-12-11 21:26:36 +0200 | [diff] [blame] | 1229 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | } |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1231 | if (do_decref) { |
| 1232 | Py_DECREF(v); |
| 1233 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
| 1236 | if (res < 0) |
| 1237 | return (PY_LONG_LONG)-1; |
| 1238 | else |
| 1239 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1242 | /* Get a C unsigned PY_LONG_LONG int from an int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1243 | Return -1 and set an error if overflow occurs. */ |
| 1244 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1245 | unsigned PY_LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1246 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | PyLongObject *v; |
| 1249 | unsigned PY_LONG_LONG bytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | int res; |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1251 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1252 | if (vv == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | PyErr_BadInternalCall(); |
| 1254 | return (unsigned PY_LONG_LONG)-1; |
| 1255 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 1256 | if (!PyLong_Check(vv)) { |
| 1257 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 1258 | return (unsigned PY_LONG_LONG)-1; |
| 1259 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | v = (PyLongObject*)vv; |
| 1262 | switch(Py_SIZE(v)) { |
| 1263 | case 0: return 0; |
| 1264 | case 1: return v->ob_digit[0]; |
| 1265 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1266 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1267 | res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1268 | SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
| 1271 | if (res < 0) |
| 1272 | return (unsigned PY_LONG_LONG)res; |
| 1273 | else |
| 1274 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1275 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1276 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1277 | /* Get a C unsigned long int from an int object, ignoring the high bits. |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1278 | Returns -1 and sets an error condition if an error occurs. */ |
| 1279 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1280 | static unsigned PY_LONG_LONG |
| 1281 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1282 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1283 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | unsigned PY_LONG_LONG x; |
| 1285 | Py_ssize_t i; |
| 1286 | int sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | if (vv == NULL || !PyLong_Check(vv)) { |
| 1289 | PyErr_BadInternalCall(); |
| 1290 | return (unsigned long) -1; |
| 1291 | } |
| 1292 | v = (PyLongObject *)vv; |
| 1293 | switch(Py_SIZE(v)) { |
| 1294 | case 0: return 0; |
| 1295 | case 1: return v->ob_digit[0]; |
| 1296 | } |
| 1297 | i = Py_SIZE(v); |
| 1298 | sign = 1; |
| 1299 | x = 0; |
| 1300 | if (i < 0) { |
| 1301 | sign = -1; |
| 1302 | i = -i; |
| 1303 | } |
| 1304 | while (--i >= 0) { |
| 1305 | x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
| 1306 | } |
| 1307 | return x * sign; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1308 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1309 | |
| 1310 | unsigned PY_LONG_LONG |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1311 | PyLong_AsUnsignedLongLongMask(PyObject *op) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | PyLongObject *lo; |
| 1314 | unsigned PY_LONG_LONG val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1315 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1316 | if (op == NULL) { |
| 1317 | PyErr_BadInternalCall(); |
| 1318 | return (unsigned long)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1320 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1321 | if (PyLong_Check(op)) { |
| 1322 | return _PyLong_AsUnsignedLongLongMask(op); |
| 1323 | } |
| 1324 | |
| 1325 | lo = _PyLong_FromNbInt(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | if (lo == NULL) |
| 1327 | return (unsigned PY_LONG_LONG)-1; |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1328 | |
| 1329 | val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
| 1330 | Py_DECREF(lo); |
| 1331 | return val; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1332 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1333 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1334 | /* Get a C long long int from an int object or any object that has an |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1335 | __int__ method. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1336 | |
Mark Dickinson | 8d48b43 | 2011-10-23 20:47:14 +0100 | [diff] [blame] | 1337 | On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 1338 | the result. Otherwise *overflow is 0. |
| 1339 | |
| 1340 | For other errors (e.g., TypeError), return -1 and set an error condition. |
| 1341 | In this case *overflow will be 0. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1342 | */ |
| 1343 | |
| 1344 | PY_LONG_LONG |
| 1345 | PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
| 1346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | /* This version by Tim Peters */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1348 | PyLongObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | unsigned PY_LONG_LONG x, prev; |
| 1350 | PY_LONG_LONG res; |
| 1351 | Py_ssize_t i; |
| 1352 | int sign; |
| 1353 | int do_decref = 0; /* if nb_int was called */ |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | *overflow = 0; |
| 1356 | if (vv == NULL) { |
| 1357 | PyErr_BadInternalCall(); |
| 1358 | return -1; |
| 1359 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1360 | |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1361 | if (PyLong_Check(vv)) { |
| 1362 | v = (PyLongObject *)vv; |
| 1363 | } |
| 1364 | else { |
| 1365 | v = _PyLong_FromNbInt(vv); |
| 1366 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | return -1; |
| 1368 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | } |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | i = Py_SIZE(v); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | switch (i) { |
| 1375 | case -1: |
| 1376 | res = -(sdigit)v->ob_digit[0]; |
| 1377 | break; |
| 1378 | case 0: |
| 1379 | res = 0; |
| 1380 | break; |
| 1381 | case 1: |
| 1382 | res = v->ob_digit[0]; |
| 1383 | break; |
| 1384 | default: |
| 1385 | sign = 1; |
| 1386 | x = 0; |
| 1387 | if (i < 0) { |
| 1388 | sign = -1; |
| 1389 | i = -(i); |
| 1390 | } |
| 1391 | while (--i >= 0) { |
| 1392 | prev = x; |
| 1393 | x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
| 1394 | if ((x >> PyLong_SHIFT) != prev) { |
| 1395 | *overflow = sign; |
| 1396 | goto exit; |
| 1397 | } |
| 1398 | } |
| 1399 | /* Haven't lost any bits, but casting to long requires extra |
| 1400 | * care (see comment above). |
| 1401 | */ |
| 1402 | if (x <= (unsigned PY_LONG_LONG)PY_LLONG_MAX) { |
| 1403 | res = (PY_LONG_LONG)x * sign; |
| 1404 | } |
| 1405 | else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
| 1406 | res = PY_LLONG_MIN; |
| 1407 | } |
| 1408 | else { |
| 1409 | *overflow = sign; |
| 1410 | /* res is already set to -1 */ |
| 1411 | } |
| 1412 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1413 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | if (do_decref) { |
Serhiy Storchaka | 31a6554 | 2013-12-11 21:07:54 +0200 | [diff] [blame] | 1415 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | } |
| 1417 | return res; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1420 | #endif /* HAVE_LONG_LONG */ |
| 1421 | |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1422 | #define CHECK_BINOP(v,w) \ |
| 1423 | do { \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1424 | if (!PyLong_Check(v) || !PyLong_Check(w)) \ |
| 1425 | Py_RETURN_NOTIMPLEMENTED; \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1426 | } while(0) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1427 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1428 | /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < |
| 1429 | 2**k if d is nonzero, else 0. */ |
| 1430 | |
| 1431 | static const unsigned char BitLengthTable[32] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 1433 | 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] | 1434 | }; |
| 1435 | |
| 1436 | static int |
| 1437 | bits_in_digit(digit d) |
| 1438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | int d_bits = 0; |
| 1440 | while (d >= 32) { |
| 1441 | d_bits += 6; |
| 1442 | d >>= 6; |
| 1443 | } |
| 1444 | d_bits += (int)BitLengthTable[d]; |
| 1445 | return d_bits; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1448 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1449 | * is modified in place, by adding y to it. Carries are propagated as far as |
| 1450 | * x[m-1], and the remaining carry (0 or 1) is returned. |
| 1451 | */ |
| 1452 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1453 | 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] | 1454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | Py_ssize_t i; |
| 1456 | digit carry = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | assert(m >= n); |
| 1459 | for (i = 0; i < n; ++i) { |
| 1460 | carry += x[i] + y[i]; |
| 1461 | x[i] = carry & PyLong_MASK; |
| 1462 | carry >>= PyLong_SHIFT; |
| 1463 | assert((carry & 1) == carry); |
| 1464 | } |
| 1465 | for (; carry && i < m; ++i) { |
| 1466 | carry += x[i]; |
| 1467 | x[i] = carry & PyLong_MASK; |
| 1468 | carry >>= PyLong_SHIFT; |
| 1469 | assert((carry & 1) == carry); |
| 1470 | } |
| 1471 | return carry; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
| 1475 | * is modified in place, by subtracting y from it. Borrows are propagated as |
| 1476 | * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
| 1477 | */ |
| 1478 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1479 | 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] | 1480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | Py_ssize_t i; |
| 1482 | digit borrow = 0; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | assert(m >= n); |
| 1485 | for (i = 0; i < n; ++i) { |
| 1486 | borrow = x[i] - y[i] - borrow; |
| 1487 | x[i] = borrow & PyLong_MASK; |
| 1488 | borrow >>= PyLong_SHIFT; |
| 1489 | borrow &= 1; /* keep only 1 sign bit */ |
| 1490 | } |
| 1491 | for (; borrow && i < m; ++i) { |
| 1492 | borrow = x[i] - borrow; |
| 1493 | x[i] = borrow & PyLong_MASK; |
| 1494 | borrow >>= PyLong_SHIFT; |
| 1495 | borrow &= 1; |
| 1496 | } |
| 1497 | return borrow; |
Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1498 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1499 | |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1500 | /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
| 1501 | * result in z[0:m], and return the d bits shifted out of the top. |
| 1502 | */ |
| 1503 | static digit |
| 1504 | 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] | 1505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | Py_ssize_t i; |
| 1507 | digit carry = 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | assert(0 <= d && d < PyLong_SHIFT); |
| 1510 | for (i=0; i < m; i++) { |
| 1511 | twodigits acc = (twodigits)a[i] << d | carry; |
| 1512 | z[i] = (digit)acc & PyLong_MASK; |
| 1513 | carry = (digit)(acc >> PyLong_SHIFT); |
| 1514 | } |
| 1515 | return carry; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
| 1519 | * result in z[0:m], and return the d bits shifted out of the bottom. |
| 1520 | */ |
| 1521 | static digit |
| 1522 | v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
| 1523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | Py_ssize_t i; |
| 1525 | digit carry = 0; |
| 1526 | digit mask = ((digit)1 << d) - 1U; |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 1527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | assert(0 <= d && d < PyLong_SHIFT); |
| 1529 | for (i=m; i-- > 0;) { |
| 1530 | twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
| 1531 | carry = (digit)acc & mask; |
| 1532 | z[i] = (digit)(acc >> d); |
| 1533 | } |
| 1534 | return carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1537 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 1538 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 1539 | It's OK for pin == pout on entry, which saves oodles of mallocs/frees in |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1540 | _PyLong_Format, but that should be done with great care since ints are |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1541 | immutable. */ |
| 1542 | |
| 1543 | static digit |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1544 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | twodigits rem = 0; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | assert(n > 0 && n <= PyLong_MASK); |
| 1549 | pin += size; |
| 1550 | pout += size; |
| 1551 | while (--size >= 0) { |
| 1552 | digit hi; |
| 1553 | rem = (rem << PyLong_SHIFT) | *--pin; |
| 1554 | *--pout = hi = (digit)(rem / n); |
| 1555 | rem -= (twodigits)hi * n; |
| 1556 | } |
| 1557 | return (digit)rem; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1560 | /* Divide an integer by a digit, returning both the quotient |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1561 | (as function result) and the remainder (through *prem). |
| 1562 | The sign of a is ignored; n should not be zero. */ |
| 1563 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1564 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1565 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1566 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1567 | const Py_ssize_t size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | assert(n > 0 && n <= PyLong_MASK); |
| 1571 | z = _PyLong_New(size); |
| 1572 | if (z == NULL) |
| 1573 | return NULL; |
| 1574 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 1575 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1578 | /* Convert an integer to a base 10 string. Returns a new non-shared |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1579 | string. (Return value is non-shared so that callers can modify the |
| 1580 | returned value if necessary.) */ |
| 1581 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1582 | static int |
| 1583 | long_to_decimal_string_internal(PyObject *aa, |
| 1584 | PyObject **p_output, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1585 | _PyUnicodeWriter *writer, |
| 1586 | _PyBytesWriter *bytes_writer, |
| 1587 | char **bytes_str) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | PyLongObject *scratch, *a; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1590 | PyObject *str = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | Py_ssize_t size, strlen, size_a, i, j; |
| 1592 | digit *pout, *pin, rem, tenpow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | int negative; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1594 | enum PyUnicode_Kind kind; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | a = (PyLongObject *)aa; |
| 1597 | if (a == NULL || !PyLong_Check(a)) { |
| 1598 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1599 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1601 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | negative = Py_SIZE(a) < 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | /* quick and dirty upper bound for the number of digits |
| 1605 | required to express a in base _PyLong_DECIMAL_BASE: |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | But log2(a) < size_a * PyLong_SHIFT, and |
| 1610 | log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
| 1611 | > 3 * _PyLong_DECIMAL_SHIFT |
| 1612 | */ |
| 1613 | if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { |
| 1614 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1615 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1616 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | } |
| 1618 | /* the expression size_a * PyLong_SHIFT is now safe from overflow */ |
| 1619 | size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); |
| 1620 | scratch = _PyLong_New(size); |
| 1621 | if (scratch == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1622 | return -1; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | /* convert array of base _PyLong_BASE digits in pin to an array of |
| 1625 | base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
| 1626 | Volume 2 (3rd edn), section 4.4, Method 1b). */ |
| 1627 | pin = a->ob_digit; |
| 1628 | pout = scratch->ob_digit; |
| 1629 | size = 0; |
| 1630 | for (i = size_a; --i >= 0; ) { |
| 1631 | digit hi = pin[i]; |
| 1632 | for (j = 0; j < size; j++) { |
| 1633 | twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
| 1634 | hi = (digit)(z / _PyLong_DECIMAL_BASE); |
| 1635 | pout[j] = (digit)(z - (twodigits)hi * |
| 1636 | _PyLong_DECIMAL_BASE); |
| 1637 | } |
| 1638 | while (hi) { |
| 1639 | pout[size++] = hi % _PyLong_DECIMAL_BASE; |
| 1640 | hi /= _PyLong_DECIMAL_BASE; |
| 1641 | } |
| 1642 | /* check for keyboard interrupt */ |
| 1643 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 1644 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1645 | return -1; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 1646 | }); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | } |
| 1648 | /* pout should have at least one digit, so that the case when a = 0 |
| 1649 | works correctly */ |
| 1650 | if (size == 0) |
| 1651 | pout[size++] = 0; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | /* calculate exact length of output string, and allocate */ |
| 1654 | strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
| 1655 | tenpow = 10; |
| 1656 | rem = pout[size-1]; |
| 1657 | while (rem >= tenpow) { |
| 1658 | tenpow *= 10; |
| 1659 | strlen++; |
| 1660 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1661 | if (writer) { |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1662 | if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) { |
| 1663 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1664 | return -1; |
Christian Heimes | 110ac16 | 2012-09-10 02:51:27 +0200 | [diff] [blame] | 1665 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1666 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1668 | else if (bytes_writer) { |
| 1669 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen); |
| 1670 | if (*bytes_str == NULL) { |
| 1671 | Py_DECREF(scratch); |
| 1672 | return -1; |
| 1673 | } |
| 1674 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1675 | else { |
| 1676 | str = PyUnicode_New(strlen, '9'); |
| 1677 | if (str == NULL) { |
| 1678 | Py_DECREF(scratch); |
| 1679 | return -1; |
| 1680 | } |
| 1681 | kind = PyUnicode_KIND(str); |
| 1682 | } |
| 1683 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1684 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1685 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1686 | /* pout[0] through pout[size-2] contribute exactly \ |
| 1687 | _PyLong_DECIMAL_SHIFT digits each */ \ |
| 1688 | for (i=0; i < size - 1; i++) { \ |
| 1689 | rem = pout[i]; \ |
| 1690 | for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ |
| 1691 | *--p = '0' + rem % 10; \ |
| 1692 | rem /= 10; \ |
| 1693 | } \ |
| 1694 | } \ |
| 1695 | /* pout[size-1]: always produce at least one decimal digit */ \ |
| 1696 | rem = pout[i]; \ |
| 1697 | do { \ |
| 1698 | *--p = '0' + rem % 10; \ |
| 1699 | rem /= 10; \ |
| 1700 | } while (rem != 0); \ |
| 1701 | \ |
| 1702 | /* and sign */ \ |
| 1703 | if (negative) \ |
| 1704 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1705 | } while (0) |
| 1706 | |
| 1707 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1708 | do { \ |
| 1709 | if (writer) \ |
| 1710 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ |
| 1711 | else \ |
| 1712 | p = (TYPE*)PyUnicode_DATA(str) + strlen; \ |
| 1713 | \ |
| 1714 | WRITE_DIGITS(p); \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1715 | \ |
| 1716 | /* check we've counted correctly */ \ |
| 1717 | if (writer) \ |
| 1718 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1719 | else \ |
| 1720 | assert(p == (TYPE*)PyUnicode_DATA(str)); \ |
| 1721 | } while (0) |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | /* fill the string right-to-left */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1724 | if (bytes_writer) { |
| 1725 | char *p = *bytes_str + strlen; |
| 1726 | WRITE_DIGITS(p); |
| 1727 | assert(p == *bytes_str); |
| 1728 | } |
| 1729 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1730 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1731 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1733 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1734 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1735 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1736 | } |
| 1737 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1738 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1739 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1740 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1741 | } |
| 1742 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1743 | #undef WRITE_UNICODE_DIGITS |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | Py_DECREF(scratch); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1746 | if (writer) { |
| 1747 | writer->pos += strlen; |
| 1748 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1749 | else if (bytes_writer) { |
| 1750 | (*bytes_str) += strlen; |
| 1751 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1752 | else { |
| 1753 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 1754 | *p_output = (PyObject *)str; |
| 1755 | } |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | static PyObject * |
| 1760 | long_to_decimal_string(PyObject *aa) |
| 1761 | { |
| 1762 | PyObject *v; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1763 | if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1764 | return NULL; |
| 1765 | return v; |
Mark Dickinson | 0a1efd0 | 2009-09-16 21:23:34 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1768 | /* Convert an int object to a string, using a given conversion base, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1769 | which should be one of 2, 8 or 16. Return a string object. |
| 1770 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' |
| 1771 | if alternate is nonzero. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1772 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1773 | static int |
| 1774 | long_format_binary(PyObject *aa, int base, int alternate, |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1775 | PyObject **p_output, _PyUnicodeWriter *writer, |
| 1776 | _PyBytesWriter *bytes_writer, char **bytes_str) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1777 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1778 | PyLongObject *a = (PyLongObject *)aa; |
Victor Stinner | 1285e5c | 2015-10-14 12:10:20 +0200 | [diff] [blame] | 1779 | PyObject *v = NULL; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1780 | Py_ssize_t sz; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | Py_ssize_t size_a; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1782 | enum PyUnicode_Kind kind; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1783 | int negative; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | int bits; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1785 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1786 | assert(base == 2 || base == 8 || base == 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | if (a == NULL || !PyLong_Check(a)) { |
| 1788 | PyErr_BadInternalCall(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1789 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 1791 | size_a = Py_ABS(Py_SIZE(a)); |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1792 | negative = Py_SIZE(a) < 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | /* Compute a rough upper bound for the length of the string */ |
| 1795 | switch (base) { |
| 1796 | case 16: |
| 1797 | bits = 4; |
| 1798 | break; |
| 1799 | case 8: |
| 1800 | bits = 3; |
| 1801 | break; |
| 1802 | case 2: |
| 1803 | bits = 1; |
| 1804 | break; |
| 1805 | default: |
| 1806 | assert(0); /* shouldn't ever get here */ |
| 1807 | bits = 0; /* to silence gcc warning */ |
| 1808 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1809 | |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1810 | /* Compute exact length 'sz' of output string. */ |
| 1811 | if (size_a == 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1812 | sz = 1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1813 | } |
| 1814 | else { |
| 1815 | Py_ssize_t size_a_in_bits; |
| 1816 | /* Ensure overflow doesn't occur during computation of sz. */ |
| 1817 | if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
| 1818 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 1819 | "int too large to format"); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1820 | return -1; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1821 | } |
| 1822 | size_a_in_bits = (size_a - 1) * PyLong_SHIFT + |
| 1823 | bits_in_digit(a->ob_digit[size_a - 1]); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1824 | /* Allow 1 character for a '-' sign. */ |
| 1825 | sz = negative + (size_a_in_bits + (bits - 1)) / bits; |
| 1826 | } |
| 1827 | if (alternate) { |
| 1828 | /* 2 characters for prefix */ |
| 1829 | sz += 2; |
Mark Dickinson | e284654 | 2012-04-20 21:21:24 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1832 | if (writer) { |
| 1833 | if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) |
| 1834 | return -1; |
| 1835 | kind = writer->kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | } |
Victor Stinner | 199c9a6 | 2015-10-14 09:47:23 +0200 | [diff] [blame] | 1837 | else if (bytes_writer) { |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1838 | *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz); |
| 1839 | if (*bytes_str == NULL) |
| 1840 | return -1; |
| 1841 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1843 | v = PyUnicode_New(sz, 'x'); |
| 1844 | if (v == NULL) |
| 1845 | return -1; |
| 1846 | kind = PyUnicode_KIND(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | } |
Mark Dickinson | 8accd6b | 2009-09-17 19:39:12 +0000 | [diff] [blame] | 1848 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1849 | #define WRITE_DIGITS(p) \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1850 | do { \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1851 | if (size_a == 0) { \ |
| 1852 | *--p = '0'; \ |
| 1853 | } \ |
| 1854 | else { \ |
| 1855 | /* JRH: special case for power-of-2 bases */ \ |
| 1856 | twodigits accum = 0; \ |
| 1857 | int accumbits = 0; /* # of bits in accum */ \ |
| 1858 | Py_ssize_t i; \ |
| 1859 | for (i = 0; i < size_a; ++i) { \ |
| 1860 | accum |= (twodigits)a->ob_digit[i] << accumbits; \ |
| 1861 | accumbits += PyLong_SHIFT; \ |
| 1862 | assert(accumbits >= bits); \ |
| 1863 | do { \ |
| 1864 | char cdigit; \ |
| 1865 | cdigit = (char)(accum & (base - 1)); \ |
| 1866 | cdigit += (cdigit < 10) ? '0' : 'a'-10; \ |
| 1867 | *--p = cdigit; \ |
| 1868 | accumbits -= bits; \ |
| 1869 | accum >>= bits; \ |
| 1870 | } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ |
| 1871 | } \ |
| 1872 | } \ |
| 1873 | \ |
| 1874 | if (alternate) { \ |
| 1875 | if (base == 16) \ |
| 1876 | *--p = 'x'; \ |
| 1877 | else if (base == 8) \ |
| 1878 | *--p = 'o'; \ |
| 1879 | else /* (base == 2) */ \ |
| 1880 | *--p = 'b'; \ |
| 1881 | *--p = '0'; \ |
| 1882 | } \ |
| 1883 | if (negative) \ |
| 1884 | *--p = '-'; \ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1885 | } while (0) |
| 1886 | |
| 1887 | #define WRITE_UNICODE_DIGITS(TYPE) \ |
| 1888 | do { \ |
| 1889 | if (writer) \ |
| 1890 | p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ |
| 1891 | else \ |
| 1892 | p = (TYPE*)PyUnicode_DATA(v) + sz; \ |
| 1893 | \ |
| 1894 | WRITE_DIGITS(p); \ |
| 1895 | \ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1896 | if (writer) \ |
| 1897 | assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ |
| 1898 | else \ |
| 1899 | assert(p == (TYPE*)PyUnicode_DATA(v)); \ |
| 1900 | } while (0) |
| 1901 | |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1902 | if (bytes_writer) { |
| 1903 | char *p = *bytes_str + sz; |
| 1904 | WRITE_DIGITS(p); |
| 1905 | assert(p == *bytes_str); |
| 1906 | } |
| 1907 | else if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1908 | Py_UCS1 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1909 | WRITE_UNICODE_DIGITS(Py_UCS1); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1910 | } |
| 1911 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1912 | Py_UCS2 *p; |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1913 | WRITE_UNICODE_DIGITS(Py_UCS2); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1914 | } |
| 1915 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1916 | Py_UCS4 *p; |
Victor Stinner | e577ab3 | 2012-05-29 18:51:10 +0200 | [diff] [blame] | 1917 | assert (kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1918 | WRITE_UNICODE_DIGITS(Py_UCS4); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1919 | } |
| 1920 | #undef WRITE_DIGITS |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1921 | #undef WRITE_UNICODE_DIGITS |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1922 | |
| 1923 | if (writer) { |
| 1924 | writer->pos += sz; |
| 1925 | } |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1926 | else if (bytes_writer) { |
| 1927 | (*bytes_str) += sz; |
| 1928 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1929 | else { |
| 1930 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 1931 | *p_output = v; |
| 1932 | } |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | PyObject * |
| 1937 | _PyLong_Format(PyObject *obj, int base) |
| 1938 | { |
| 1939 | PyObject *str; |
| 1940 | int err; |
| 1941 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1942 | err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1943 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1944 | err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1945 | if (err == -1) |
| 1946 | return NULL; |
| 1947 | return str; |
| 1948 | } |
| 1949 | |
| 1950 | int |
| 1951 | _PyLong_FormatWriter(_PyUnicodeWriter *writer, |
| 1952 | PyObject *obj, |
| 1953 | int base, int alternate) |
| 1954 | { |
| 1955 | if (base == 10) |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1956 | return long_to_decimal_string_internal(obj, NULL, writer, |
| 1957 | NULL, NULL); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1958 | else |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 1959 | return long_format_binary(obj, base, alternate, NULL, writer, |
| 1960 | NULL, NULL); |
| 1961 | } |
| 1962 | |
| 1963 | char* |
| 1964 | _PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str, |
| 1965 | PyObject *obj, |
| 1966 | int base, int alternate) |
| 1967 | { |
| 1968 | char *str2; |
| 1969 | int res; |
| 1970 | str2 = str; |
| 1971 | if (base == 10) |
| 1972 | res = long_to_decimal_string_internal(obj, NULL, NULL, |
| 1973 | writer, &str2); |
| 1974 | else |
| 1975 | res = long_format_binary(obj, base, alternate, NULL, NULL, |
| 1976 | writer, &str2); |
| 1977 | if (res < 0) |
| 1978 | return NULL; |
| 1979 | assert(str2 != NULL); |
| 1980 | return str2; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1983 | /* Table of digit values for 8-bit string -> integer conversion. |
| 1984 | * '0' maps to 0, ..., '9' maps to 9. |
| 1985 | * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
| 1986 | * All other indices map to 37. |
| 1987 | * 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] | 1988 | * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1989 | */ |
Raymond Hettinger | 3563153 | 2009-01-09 03:58:09 +0000 | [diff] [blame] | 1990 | unsigned char _PyLong_DigitValue[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1992 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1993 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 1994 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
| 1995 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1996 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1997 | 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 1998 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
| 1999 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2000 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2001 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2002 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2003 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2004 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2005 | 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
| 2006 | 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] | 2007 | }; |
| 2008 | |
| 2009 | /* *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] | 2010 | * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2011 | * non-digit (which may be *str!). A normalized int is returned. |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2012 | * The point to this routine is that it takes time linear in the number of |
| 2013 | * string characters. |
| 2014 | */ |
| 2015 | static PyLongObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2016 | long_from_binary_base(const char **str, int base) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2017 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2018 | const char *p = *str; |
| 2019 | const char *start = p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | int bits_per_char; |
| 2021 | Py_ssize_t n; |
| 2022 | PyLongObject *z; |
| 2023 | twodigits accum; |
| 2024 | int bits_in_accum; |
| 2025 | digit *pdigit; |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
| 2028 | n = base; |
| 2029 | for (bits_per_char = -1; n; ++bits_per_char) |
| 2030 | n >>= 1; |
| 2031 | /* n <- total # of bits needed, while setting p to end-of-string */ |
| 2032 | while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) |
| 2033 | ++p; |
| 2034 | *str = p; |
| 2035 | /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ |
| 2036 | n = (p - start) * bits_per_char + PyLong_SHIFT - 1; |
| 2037 | if (n / bits_per_char < p - start) { |
| 2038 | PyErr_SetString(PyExc_ValueError, |
| 2039 | "int string too large to convert"); |
| 2040 | return NULL; |
| 2041 | } |
| 2042 | n = n / PyLong_SHIFT; |
| 2043 | z = _PyLong_New(n); |
| 2044 | if (z == NULL) |
| 2045 | return NULL; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2046 | /* Read string from right, and fill in int from left; i.e., |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | * from least to most significant in both. |
| 2048 | */ |
| 2049 | accum = 0; |
| 2050 | bits_in_accum = 0; |
| 2051 | pdigit = z->ob_digit; |
| 2052 | while (--p >= start) { |
| 2053 | int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
| 2054 | assert(k >= 0 && k < base); |
| 2055 | accum |= (twodigits)k << bits_in_accum; |
| 2056 | bits_in_accum += bits_per_char; |
| 2057 | if (bits_in_accum >= PyLong_SHIFT) { |
| 2058 | *pdigit++ = (digit)(accum & PyLong_MASK); |
| 2059 | assert(pdigit - z->ob_digit <= n); |
| 2060 | accum >>= PyLong_SHIFT; |
| 2061 | bits_in_accum -= PyLong_SHIFT; |
| 2062 | assert(bits_in_accum < PyLong_SHIFT); |
| 2063 | } |
| 2064 | } |
| 2065 | if (bits_in_accum) { |
| 2066 | assert(bits_in_accum <= PyLong_SHIFT); |
| 2067 | *pdigit++ = (digit)accum; |
| 2068 | assert(pdigit - z->ob_digit <= n); |
| 2069 | } |
| 2070 | while (pdigit - z->ob_digit < n) |
| 2071 | *pdigit++ = 0; |
| 2072 | return long_normalize(z); |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2075 | /* Parses an int from a bytestring. Leading and trailing whitespace will be |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2076 | * ignored. |
| 2077 | * |
| 2078 | * If successful, a PyLong object will be returned and 'pend' will be pointing |
| 2079 | * to the first unused byte unless it's NULL. |
| 2080 | * |
| 2081 | * If unsuccessful, NULL will be returned. |
| 2082 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2083 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2084 | PyLong_FromString(const char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | int sign = 1, error_if_nonzero = 0; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2087 | const char *start, *orig_str = str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | PyLongObject *z = NULL; |
| 2089 | PyObject *strobj; |
| 2090 | Py_ssize_t slen; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | if ((base != 0 && base < 2) || base > 36) { |
| 2093 | PyErr_SetString(PyExc_ValueError, |
| 2094 | "int() arg 2 must be >= 2 and <= 36"); |
| 2095 | return NULL; |
| 2096 | } |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 2097 | while (*str != '\0' && Py_ISSPACE(Py_CHARMASK(*str))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | str++; |
| 2099 | if (*str == '+') |
| 2100 | ++str; |
| 2101 | else if (*str == '-') { |
| 2102 | ++str; |
| 2103 | sign = -1; |
| 2104 | } |
| 2105 | if (base == 0) { |
| 2106 | if (str[0] != '0') |
| 2107 | base = 10; |
| 2108 | else if (str[1] == 'x' || str[1] == 'X') |
| 2109 | base = 16; |
| 2110 | else if (str[1] == 'o' || str[1] == 'O') |
| 2111 | base = 8; |
| 2112 | else if (str[1] == 'b' || str[1] == 'B') |
| 2113 | base = 2; |
| 2114 | else { |
| 2115 | /* "old" (C-style) octal literal, now invalid. |
| 2116 | it might still be zero though */ |
| 2117 | error_if_nonzero = 1; |
| 2118 | base = 10; |
| 2119 | } |
| 2120 | } |
| 2121 | if (str[0] == '0' && |
| 2122 | ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
| 2123 | (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
| 2124 | (base == 2 && (str[1] == 'b' || str[1] == 'B')))) |
| 2125 | str += 2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | start = str; |
| 2128 | if ((base & (base - 1)) == 0) |
| 2129 | z = long_from_binary_base(&str, base); |
| 2130 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2131 | /*** |
| 2132 | Binary bases can be converted in time linear in the number of digits, because |
| 2133 | Python's representation base is binary. Other bases (including decimal!) use |
| 2134 | the simple quadratic-time algorithm below, complicated by some speed tricks. |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2135 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2136 | First some math: the largest integer that can be expressed in N base-B digits |
| 2137 | is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
| 2138 | case number of Python digits needed to hold it is the smallest integer n s.t. |
| 2139 | |
| 2140 | BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
| 2141 | BASE**n >= B**N [taking logs to base BASE] |
| 2142 | n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) |
| 2143 | |
| 2144 | The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2145 | this quickly. A Python int with that much space is reserved near the start, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2146 | and the result is computed into it. |
| 2147 | |
| 2148 | The input string is actually treated as being in base base**i (i.e., i digits |
| 2149 | are processed at a time), where two more static arrays hold: |
| 2150 | |
| 2151 | convwidth_base[base] = the largest integer i such that base**i <= BASE |
| 2152 | convmultmax_base[base] = base ** convwidth_base[base] |
| 2153 | |
| 2154 | The first of these is the largest i such that i consecutive input digits |
| 2155 | must fit in a single Python digit. The second is effectively the input |
| 2156 | base we're really using. |
| 2157 | |
| 2158 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base |
| 2159 | convmultmax_base[base], the result is "simply" |
| 2160 | |
| 2161 | (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 |
| 2162 | |
| 2163 | where B = convmultmax_base[base]. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2164 | |
| 2165 | Error analysis: as above, the number of Python digits `n` needed is worst- |
| 2166 | case |
| 2167 | |
| 2168 | n >= N * log(B)/log(BASE) |
| 2169 | |
| 2170 | where `N` is the number of input digits in base `B`. This is computed via |
| 2171 | |
| 2172 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2173 | |
| 2174 | below. Two numeric concerns are how much space this can waste, and whether |
| 2175 | the computed result can be too small. To be concrete, assume BASE = 2**15, |
| 2176 | which is the default (and it's unlikely anyone changes that). |
| 2177 | |
| 2178 | Waste isn't a problem: provided the first input digit isn't 0, the difference |
| 2179 | between the worst-case input with N digits and the smallest input with N |
| 2180 | digits is about a factor of B, but B is small compared to BASE so at most |
| 2181 | one allocated Python digit can remain unused on that count. If |
| 2182 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that |
| 2183 | and adding 1 returns a result 1 larger than necessary. However, that can't |
| 2184 | happen: whenever B is a power of 2, long_from_binary_base() is called |
| 2185 | instead, and it's impossible for B**i to be an integer power of 2**15 when |
| 2186 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be |
| 2187 | an exact integer when B is not a power of 2, since B**i has a prime factor |
| 2188 | other than 2 in that case, but (2**15)**j's only prime factor is 2). |
| 2189 | |
| 2190 | The computed result can be too small if the true value of N*log(B)/log(BASE) |
| 2191 | is a little bit larger than an exact integer, but due to roundoff errors (in |
| 2192 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) |
| 2193 | yields a numeric result a little less than that integer. Unfortunately, "how |
| 2194 | close can a transcendental function get to an integer over some range?" |
| 2195 | questions are generally theoretically intractable. Computer analysis via |
| 2196 | continued fractions is practical: expand log(B)/log(BASE) via continued |
| 2197 | fractions, giving a sequence i/j of "the best" rational approximations. Then |
| 2198 | j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that |
| 2199 | we can get very close to being in trouble, but very rarely. For example, |
| 2200 | 76573 is a denominator in one of the continued-fraction approximations to |
| 2201 | log(10)/log(2**15), and indeed: |
| 2202 | |
| 2203 | >>> log(10)/log(2**15)*76573 |
| 2204 | 16958.000000654003 |
| 2205 | |
| 2206 | is very close to an integer. If we were working with IEEE single-precision, |
| 2207 | rounding errors could kill us. Finding worst cases in IEEE double-precision |
| 2208 | requires better-than-double-precision log() functions, and Tim didn't bother. |
| 2209 | Instead the code checks to see whether the allocated space is enough as each |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2210 | new Python digit is added, and copies the whole thing to a larger int if not. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2211 | This should happen extremely rarely, and in fact I don't have a test case |
| 2212 | that triggers it(!). Instead the code was tested by artificially allocating |
| 2213 | just 1 digit at the start, so that the copying code was exercised for every |
| 2214 | digit beyond the first. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2215 | ***/ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2216 | twodigits c; /* current input character */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | Py_ssize_t size_z; |
| 2218 | int i; |
| 2219 | int convwidth; |
| 2220 | twodigits convmultmax, convmult; |
| 2221 | digit *pz, *pzstop; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2222 | const char* scan; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | static double log_base_BASE[37] = {0.0e0,}; |
| 2225 | static int convwidth_base[37] = {0,}; |
| 2226 | static twodigits convmultmax_base[37] = {0,}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | if (log_base_BASE[base] == 0.0) { |
| 2229 | twodigits convmax = base; |
| 2230 | int i = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2231 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2232 | log_base_BASE[base] = (log((double)base) / |
| 2233 | log((double)PyLong_BASE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | for (;;) { |
| 2235 | twodigits next = convmax * base; |
| 2236 | if (next > PyLong_BASE) |
| 2237 | break; |
| 2238 | convmax = next; |
| 2239 | ++i; |
| 2240 | } |
| 2241 | convmultmax_base[base] = convmax; |
| 2242 | assert(i > 0); |
| 2243 | convwidth_base[base] = i; |
| 2244 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | /* Find length of the string of numeric characters. */ |
| 2247 | scan = str; |
| 2248 | while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) |
| 2249 | ++scan; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2250 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2251 | /* Create an int object that can contain the largest possible |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | * integer with this base and length. Note that there's no |
| 2253 | * need to initialize z->ob_digit -- no slot is read up before |
| 2254 | * being stored into. |
| 2255 | */ |
| 2256 | size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
| 2257 | /* Uncomment next line to test exceedingly rare copy code */ |
| 2258 | /* size_z = 1; */ |
| 2259 | assert(size_z > 0); |
| 2260 | z = _PyLong_New(size_z); |
| 2261 | if (z == NULL) |
| 2262 | return NULL; |
| 2263 | Py_SIZE(z) = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | /* `convwidth` consecutive input digits are treated as a single |
| 2266 | * digit in base `convmultmax`. |
| 2267 | */ |
| 2268 | convwidth = convwidth_base[base]; |
| 2269 | convmultmax = convmultmax_base[base]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | /* Work ;-) */ |
| 2272 | while (str < scan) { |
| 2273 | /* grab up to convwidth digits from the input string */ |
| 2274 | c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
| 2275 | for (i = 1; i < convwidth && str != scan; ++i, ++str) { |
| 2276 | c = (twodigits)(c * base + |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2277 | (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | assert(c < PyLong_BASE); |
| 2279 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | convmult = convmultmax; |
| 2282 | /* Calculate the shift only if we couldn't get |
| 2283 | * convwidth digits. |
| 2284 | */ |
| 2285 | if (i != convwidth) { |
| 2286 | convmult = base; |
| 2287 | for ( ; i > 1; --i) |
| 2288 | convmult *= base; |
| 2289 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | /* Multiply z by convmult, and add c. */ |
| 2292 | pz = z->ob_digit; |
| 2293 | pzstop = pz + Py_SIZE(z); |
| 2294 | for (; pz < pzstop; ++pz) { |
| 2295 | c += (twodigits)*pz * convmult; |
| 2296 | *pz = (digit)(c & PyLong_MASK); |
| 2297 | c >>= PyLong_SHIFT; |
| 2298 | } |
| 2299 | /* carry off the current end? */ |
| 2300 | if (c) { |
| 2301 | assert(c < PyLong_BASE); |
| 2302 | if (Py_SIZE(z) < size_z) { |
| 2303 | *pz = (digit)c; |
| 2304 | ++Py_SIZE(z); |
| 2305 | } |
| 2306 | else { |
| 2307 | PyLongObject *tmp; |
| 2308 | /* Extremely rare. Get more space. */ |
| 2309 | assert(Py_SIZE(z) == size_z); |
| 2310 | tmp = _PyLong_New(size_z + 1); |
| 2311 | if (tmp == NULL) { |
| 2312 | Py_DECREF(z); |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | memcpy(tmp->ob_digit, |
| 2316 | z->ob_digit, |
| 2317 | sizeof(digit) * size_z); |
| 2318 | Py_DECREF(z); |
| 2319 | z = tmp; |
| 2320 | z->ob_digit[size_z] = (digit)c; |
| 2321 | ++size_z; |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | if (z == NULL) |
| 2327 | return NULL; |
| 2328 | if (error_if_nonzero) { |
| 2329 | /* reset the base to 0, else the exception message |
| 2330 | doesn't make too much sense */ |
| 2331 | base = 0; |
| 2332 | if (Py_SIZE(z) != 0) |
| 2333 | goto onError; |
| 2334 | /* there might still be other problems, therefore base |
| 2335 | remains zero here for the same reason */ |
| 2336 | } |
| 2337 | if (str == start) |
| 2338 | goto onError; |
| 2339 | if (sign < 0) |
| 2340 | Py_SIZE(z) = -(Py_SIZE(z)); |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 2341 | while (*str && Py_ISSPACE(Py_CHARMASK(*str))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | str++; |
| 2343 | if (*str != '\0') |
| 2344 | goto onError; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | long_normalize(z); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2346 | z = maybe_small_long(z); |
| 2347 | if (z == NULL) |
| 2348 | return NULL; |
| 2349 | if (pend != NULL) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2350 | *pend = (char *)str; |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2351 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2352 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2353 | onError: |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2354 | if (pend != NULL) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 2355 | *pend = (char *)str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | Py_XDECREF(z); |
| 2357 | slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
| 2358 | strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
| 2359 | if (strobj == NULL) |
| 2360 | return NULL; |
| 2361 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2362 | "invalid literal for int() with base %d: %.200R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | base, strobj); |
| 2364 | Py_DECREF(strobj); |
| 2365 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2368 | /* Since PyLong_FromString doesn't have a length parameter, |
| 2369 | * check here for possible NULs in the string. |
| 2370 | * |
| 2371 | * Reports an invalid literal as a bytes object. |
| 2372 | */ |
| 2373 | PyObject * |
| 2374 | _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) |
| 2375 | { |
| 2376 | PyObject *result, *strobj; |
| 2377 | char *end = NULL; |
| 2378 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 2379 | result = PyLong_FromString(s, &end, base); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2380 | if (end == NULL || (result != NULL && end == s + len)) |
| 2381 | return result; |
| 2382 | Py_XDECREF(result); |
| 2383 | strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); |
| 2384 | if (strobj != NULL) { |
| 2385 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2386 | "invalid literal for int() with base %d: %.200R", |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2387 | base, strobj); |
| 2388 | Py_DECREF(strobj); |
| 2389 | } |
| 2390 | return NULL; |
| 2391 | } |
| 2392 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2393 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2394 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2395 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2396 | PyObject *v, *unicode = PyUnicode_FromUnicode(u, length); |
| 2397 | if (unicode == NULL) |
| 2398 | return NULL; |
| 2399 | v = PyLong_FromUnicodeObject(unicode, base); |
| 2400 | Py_DECREF(unicode); |
| 2401 | return v; |
| 2402 | } |
| 2403 | |
| 2404 | PyObject * |
| 2405 | PyLong_FromUnicodeObject(PyObject *u, int base) |
| 2406 | { |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2407 | PyObject *result, *asciidig; |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2408 | char *buffer, *end = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | Py_ssize_t buflen; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 2410 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2411 | asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2412 | if (asciidig == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2414 | buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2415 | if (buffer == NULL) { |
| 2416 | Py_DECREF(asciidig); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2417 | if (!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
| 2418 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | } |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2420 | else { |
| 2421 | result = PyLong_FromString(buffer, &end, base); |
| 2422 | if (end == NULL || (result != NULL && end == buffer + buflen)) { |
| 2423 | Py_DECREF(asciidig); |
| 2424 | return result; |
| 2425 | } |
| 2426 | Py_DECREF(asciidig); |
| 2427 | Py_XDECREF(result); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 2428 | } |
Serhiy Storchaka | 579ddc2 | 2013-08-03 21:14:05 +0300 | [diff] [blame] | 2429 | PyErr_Format(PyExc_ValueError, |
| 2430 | "invalid literal for int() with base %d: %.200R", |
| 2431 | base, u); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 2432 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2435 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2436 | static PyLongObject *x_divrem |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | (PyLongObject *, PyLongObject *, PyLongObject **); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 2438 | static PyObject *long_long(PyObject *v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2439 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2440 | /* Int division with remainder, top-level routine */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2441 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2442 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2443 | long_divrem(PyLongObject *a, PyLongObject *b, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2445 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2446 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | if (size_b == 0) { |
| 2450 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 2451 | "integer division or modulo by zero"); |
| 2452 | return -1; |
| 2453 | } |
| 2454 | if (size_a < size_b || |
| 2455 | (size_a == size_b && |
| 2456 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 2457 | /* |a| < |b|. */ |
| 2458 | *pdiv = (PyLongObject*)PyLong_FromLong(0); |
| 2459 | if (*pdiv == NULL) |
| 2460 | return -1; |
| 2461 | Py_INCREF(a); |
| 2462 | *prem = (PyLongObject *) a; |
| 2463 | return 0; |
| 2464 | } |
| 2465 | if (size_b == 1) { |
| 2466 | digit rem = 0; |
| 2467 | z = divrem1(a, b->ob_digit[0], &rem); |
| 2468 | if (z == NULL) |
| 2469 | return -1; |
| 2470 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 2471 | if (*prem == NULL) { |
| 2472 | Py_DECREF(z); |
| 2473 | return -1; |
| 2474 | } |
| 2475 | } |
| 2476 | else { |
| 2477 | z = x_divrem(a, b, prem); |
| 2478 | if (z == NULL) |
| 2479 | return -1; |
| 2480 | } |
| 2481 | /* Set the signs. |
| 2482 | The quotient z has the sign of a*b; |
| 2483 | the remainder r has the sign of a, |
| 2484 | so a = b*z + r. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 2485 | if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) { |
| 2486 | _PyLong_Negate(&z); |
| 2487 | if (z == NULL) { |
| 2488 | Py_CLEAR(*prem); |
| 2489 | return -1; |
| 2490 | } |
| 2491 | } |
| 2492 | if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) { |
| 2493 | _PyLong_Negate(prem); |
| 2494 | if (*prem == NULL) { |
| 2495 | Py_DECREF(z); |
| 2496 | Py_CLEAR(*prem); |
| 2497 | return -1; |
| 2498 | } |
| 2499 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | *pdiv = maybe_small_long(z); |
| 2501 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2504 | /* Unsigned int division with remainder -- the algorithm. The arguments v1 |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2505 | and w1 should satisfy 2 <= Py_ABS(Py_SIZE(w1)) <= Py_ABS(Py_SIZE(v1)). */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2506 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2507 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2508 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | PyLongObject *v, *w, *a; |
| 2511 | Py_ssize_t i, k, size_v, size_w; |
| 2512 | int d; |
| 2513 | digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
| 2514 | twodigits vv; |
| 2515 | sdigit zhi; |
| 2516 | stwodigits z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 | /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
| 2519 | edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
| 2520 | handle the special case when the initial estimate q for a quotient |
| 2521 | digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
| 2522 | that won't overflow a digit. */ |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | /* allocate space; w will also be used to hold the final remainder */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2525 | size_v = Py_ABS(Py_SIZE(v1)); |
| 2526 | size_w = Py_ABS(Py_SIZE(w1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
| 2528 | v = _PyLong_New(size_v+1); |
| 2529 | if (v == NULL) { |
| 2530 | *prem = NULL; |
| 2531 | return NULL; |
| 2532 | } |
| 2533 | w = _PyLong_New(size_w); |
| 2534 | if (w == NULL) { |
| 2535 | Py_DECREF(v); |
| 2536 | *prem = NULL; |
| 2537 | return NULL; |
| 2538 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
| 2541 | shift v1 left by the same amount. Results go into w and v. */ |
| 2542 | d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]); |
| 2543 | carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
| 2544 | assert(carry == 0); |
| 2545 | carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
| 2546 | if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
| 2547 | v->ob_digit[size_v] = carry; |
| 2548 | size_v++; |
| 2549 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
| 2552 | at most (and usually exactly) k = size_v - size_w digits. */ |
| 2553 | k = size_v - size_w; |
| 2554 | assert(k >= 0); |
| 2555 | a = _PyLong_New(k); |
| 2556 | if (a == NULL) { |
| 2557 | Py_DECREF(w); |
| 2558 | Py_DECREF(v); |
| 2559 | *prem = NULL; |
| 2560 | return NULL; |
| 2561 | } |
| 2562 | v0 = v->ob_digit; |
| 2563 | w0 = w->ob_digit; |
| 2564 | wm1 = w0[size_w-1]; |
| 2565 | wm2 = w0[size_w-2]; |
| 2566 | for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
| 2567 | /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
| 2568 | single-digit quotient q, remainder in vk[0:size_w]. */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2571 | Py_DECREF(a); |
| 2572 | Py_DECREF(w); |
| 2573 | Py_DECREF(v); |
| 2574 | *prem = NULL; |
| 2575 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 2576 | }); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | /* estimate quotient digit q; may overestimate by 1 (rare) */ |
| 2579 | vtop = vk[size_w]; |
| 2580 | assert(vtop <= wm1); |
| 2581 | vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
| 2582 | q = (digit)(vv / wm1); |
| 2583 | r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
| 2584 | while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
| 2585 | | vk[size_w-2])) { |
| 2586 | --q; |
| 2587 | r += wm1; |
| 2588 | if (r >= PyLong_BASE) |
| 2589 | break; |
| 2590 | } |
| 2591 | assert(q <= PyLong_BASE); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
| 2594 | zhi = 0; |
| 2595 | for (i = 0; i < size_w; ++i) { |
| 2596 | /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
| 2597 | -PyLong_BASE * q <= z < PyLong_BASE */ |
| 2598 | z = (sdigit)vk[i] + zhi - |
| 2599 | (stwodigits)q * (stwodigits)w0[i]; |
| 2600 | vk[i] = (digit)z & PyLong_MASK; |
| 2601 | zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2602 | z, PyLong_SHIFT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | /* add w back if q was too large (this branch taken rarely) */ |
| 2606 | assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
| 2607 | if ((sdigit)vtop + zhi < 0) { |
| 2608 | carry = 0; |
| 2609 | for (i = 0; i < size_w; ++i) { |
| 2610 | carry += vk[i] + w0[i]; |
| 2611 | vk[i] = carry & PyLong_MASK; |
| 2612 | carry >>= PyLong_SHIFT; |
| 2613 | } |
| 2614 | --q; |
| 2615 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | /* store quotient digit */ |
| 2618 | assert(q < PyLong_BASE); |
| 2619 | *--ak = q; |
| 2620 | } |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | /* unshift remainder; we reuse w to store the result */ |
| 2623 | carry = v_rshift(w0, v0, size_w, d); |
| 2624 | assert(carry==0); |
| 2625 | Py_DECREF(v); |
Mark Dickinson | 17e4fdd | 2009-03-23 18:44:57 +0000 | [diff] [blame] | 2626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | *prem = long_normalize(w); |
| 2628 | return long_normalize(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2631 | /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
| 2632 | abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
| 2633 | rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
| 2634 | If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
| 2635 | e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
| 2636 | -1.0. */ |
| 2637 | |
| 2638 | /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
| 2639 | #if DBL_MANT_DIG == 53 |
| 2640 | #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
| 2641 | #else |
| 2642 | #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
| 2643 | #endif |
| 2644 | |
| 2645 | double |
| 2646 | _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
| 2647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
| 2649 | /* See below for why x_digits is always large enough. */ |
| 2650 | digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; |
| 2651 | double dx; |
| 2652 | /* Correction term for round-half-to-even rounding. For a digit x, |
| 2653 | "x + half_even_correction[x & 7]" gives x rounded to the nearest |
| 2654 | multiple of 4, rounding ties to a multiple of 8. */ |
| 2655 | 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] | 2656 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2657 | a_size = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | if (a_size == 0) { |
| 2659 | /* Special case for 0: significand 0.0, exponent 0. */ |
| 2660 | *e = 0; |
| 2661 | return 0.0; |
| 2662 | } |
| 2663 | a_bits = bits_in_digit(a->ob_digit[a_size-1]); |
| 2664 | /* The following is an overflow-free version of the check |
| 2665 | "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
| 2666 | if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
| 2667 | (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
| 2668 | a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2669 | goto overflow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
| 2673 | (shifting left if a_bits <= DBL_MANT_DIG + 2). |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | Number of digits needed for result: write // for floor division. |
| 2676 | Then if shifting left, we end up using |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | digits. If shifting right, we use |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
| 2685 | the inequalities |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
| 2688 | m // PyLong_SHIFT - n // PyLong_SHIFT <= |
| 2689 | 1 + (m - n - 1) // PyLong_SHIFT, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | 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] | 2692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | in both cases. |
| 2696 | */ |
| 2697 | if (a_bits <= DBL_MANT_DIG + 2) { |
| 2698 | shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
| 2699 | shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
| 2700 | x_size = 0; |
| 2701 | while (x_size < shift_digits) |
| 2702 | x_digits[x_size++] = 0; |
| 2703 | rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
| 2704 | (int)shift_bits); |
| 2705 | x_size += a_size; |
| 2706 | x_digits[x_size++] = rem; |
| 2707 | } |
| 2708 | else { |
| 2709 | shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
| 2710 | shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
| 2711 | rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
| 2712 | a_size - shift_digits, (int)shift_bits); |
| 2713 | x_size = a_size - shift_digits; |
| 2714 | /* For correct rounding below, we need the least significant |
| 2715 | bit of x to be 'sticky' for this shift: if any of the bits |
| 2716 | shifted out was nonzero, we set the least significant bit |
| 2717 | of x. */ |
| 2718 | if (rem) |
| 2719 | x_digits[0] |= 1; |
| 2720 | else |
| 2721 | while (shift_digits > 0) |
| 2722 | if (a->ob_digit[--shift_digits]) { |
| 2723 | x_digits[0] |= 1; |
| 2724 | break; |
| 2725 | } |
| 2726 | } |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2727 | 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] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | /* Round, and convert to double. */ |
| 2730 | x_digits[0] += half_even_correction[x_digits[0] & 7]; |
| 2731 | dx = x_digits[--x_size]; |
| 2732 | while (x_size > 0) |
| 2733 | dx = dx * PyLong_BASE + x_digits[--x_size]; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | /* Rescale; make correction if result is 1.0. */ |
| 2736 | dx /= 4.0 * EXP2_DBL_MANT_DIG; |
| 2737 | if (dx == 1.0) { |
| 2738 | if (a_bits == PY_SSIZE_T_MAX) |
| 2739 | goto overflow; |
| 2740 | dx = 0.5; |
| 2741 | a_bits += 1; |
| 2742 | } |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | *e = a_bits; |
| 2745 | return Py_SIZE(a) < 0 ? -dx : dx; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2746 | |
| 2747 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | /* exponent > PY_SSIZE_T_MAX */ |
| 2749 | PyErr_SetString(PyExc_OverflowError, |
| 2750 | "huge integer: number of bits overflows a Py_ssize_t"); |
| 2751 | *e = 0; |
| 2752 | return -1.0; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2755 | /* Get a C double from an int object. Rounds to the nearest double, |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2756 | using the round-half-to-even rule in the case of a tie. */ |
| 2757 | |
| 2758 | double |
| 2759 | PyLong_AsDouble(PyObject *v) |
| 2760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | Py_ssize_t exponent; |
| 2762 | double x; |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2763 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2764 | if (v == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | PyErr_BadInternalCall(); |
| 2766 | return -1.0; |
| 2767 | } |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2768 | if (!PyLong_Check(v)) { |
| 2769 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 2770 | return -1.0; |
| 2771 | } |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2772 | if (Py_ABS(Py_SIZE(v)) <= 1) { |
Yury Selivanov | a0fcaca | 2016-02-06 12:21:33 -0500 | [diff] [blame] | 2773 | /* Fast path; single digit long (31 bits) will cast safely |
| 2774 | to double. This improves performance of FP/long operations |
| 2775 | by 20%. |
Yury Selivanov | 186c30b | 2016-02-05 19:40:01 -0500 | [diff] [blame] | 2776 | */ |
| 2777 | return (double)MEDIUM_VALUE((PyLongObject *)v); |
| 2778 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
| 2780 | if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
| 2781 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 02515f7 | 2013-08-03 12:08:22 +0100 | [diff] [blame] | 2782 | "int too large to convert to float"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | return -1.0; |
| 2784 | } |
| 2785 | return ldexp(x, (int)exponent); |
Mark Dickinson | 6ecd9e5 | 2010-01-02 15:33:56 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2788 | /* Methods */ |
| 2789 | |
| 2790 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2791 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | Py_TYPE(v)->tp_free(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2796 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2797 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | Py_ssize_t sign; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | if (Py_SIZE(a) != Py_SIZE(b)) { |
| 2802 | sign = Py_SIZE(a) - Py_SIZE(b); |
| 2803 | } |
| 2804 | else { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2805 | Py_ssize_t i = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2807 | ; |
| 2808 | if (i < 0) |
| 2809 | sign = 0; |
| 2810 | else { |
| 2811 | sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i]; |
| 2812 | if (Py_SIZE(a) < 0) |
| 2813 | sign = -sign; |
| 2814 | } |
| 2815 | } |
| 2816 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2819 | #define TEST_COND(cond) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 2821 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2822 | static PyObject * |
| 2823 | long_richcompare(PyObject *self, PyObject *other, int op) |
| 2824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | int result; |
| 2826 | PyObject *v; |
| 2827 | CHECK_BINOP(self, other); |
| 2828 | if (self == other) |
| 2829 | result = 0; |
| 2830 | else |
| 2831 | result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
| 2832 | /* Convert the return value to a Boolean */ |
| 2833 | switch (op) { |
| 2834 | case Py_EQ: |
| 2835 | v = TEST_COND(result == 0); |
| 2836 | break; |
| 2837 | case Py_NE: |
| 2838 | v = TEST_COND(result != 0); |
| 2839 | break; |
| 2840 | case Py_LE: |
| 2841 | v = TEST_COND(result <= 0); |
| 2842 | break; |
| 2843 | case Py_GE: |
| 2844 | v = TEST_COND(result >= 0); |
| 2845 | break; |
| 2846 | case Py_LT: |
| 2847 | v = TEST_COND(result == -1); |
| 2848 | break; |
| 2849 | case Py_GT: |
| 2850 | v = TEST_COND(result == 1); |
| 2851 | break; |
| 2852 | default: |
| 2853 | PyErr_BadArgument(); |
| 2854 | return NULL; |
| 2855 | } |
| 2856 | Py_INCREF(v); |
| 2857 | return v; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2860 | static Py_hash_t |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2861 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2862 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2863 | Py_uhash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | Py_ssize_t i; |
| 2865 | int sign; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | i = Py_SIZE(v); |
| 2868 | switch(i) { |
| 2869 | case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
| 2870 | case 0: return 0; |
| 2871 | case 1: return v->ob_digit[0]; |
| 2872 | } |
| 2873 | sign = 1; |
| 2874 | x = 0; |
| 2875 | if (i < 0) { |
| 2876 | sign = -1; |
| 2877 | i = -(i); |
| 2878 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | while (--i >= 0) { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2880 | /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
| 2881 | want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
| 2882 | _PyHASH_MODULUS. |
| 2883 | |
| 2884 | The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
| 2885 | amounts to a rotation of the bits of x. To see this, write |
| 2886 | |
| 2887 | x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
| 2888 | |
| 2889 | where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
| 2890 | PyLong_SHIFT bits of x (those that are shifted out of the |
| 2891 | original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
| 2892 | _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
| 2893 | bits of x, shifted up. Then since 2**_PyHASH_BITS is |
| 2894 | congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
| 2895 | congruent to y modulo _PyHASH_MODULUS. So |
| 2896 | |
| 2897 | x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
| 2898 | |
| 2899 | The right-hand side is just the result of rotating the |
| 2900 | _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
| 2901 | not all _PyHASH_BITS bits of x are 1s, the same is true |
| 2902 | after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
| 2903 | the reduction of x*2**PyLong_SHIFT modulo |
| 2904 | _PyHASH_MODULUS. */ |
| 2905 | x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
| 2906 | (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | x += v->ob_digit[i]; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2908 | if (x >= _PyHASH_MODULUS) |
| 2909 | x -= _PyHASH_MODULUS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | } |
| 2911 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 2912 | if (x == (Py_uhash_t)-1) |
| 2913 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2914 | return (Py_hash_t)x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2918 | /* Add the absolute values of two integers. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2919 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2920 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2921 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2922 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2923 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | PyLongObject *z; |
| 2925 | Py_ssize_t i; |
| 2926 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | /* Ensure a is the larger of the two: */ |
| 2929 | if (size_a < size_b) { |
| 2930 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2931 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2932 | size_a = size_b; |
| 2933 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | } |
| 2935 | z = _PyLong_New(size_a+1); |
| 2936 | if (z == NULL) |
| 2937 | return NULL; |
| 2938 | for (i = 0; i < size_b; ++i) { |
| 2939 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 2940 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2941 | carry >>= PyLong_SHIFT; |
| 2942 | } |
| 2943 | for (; i < size_a; ++i) { |
| 2944 | carry += a->ob_digit[i]; |
| 2945 | z->ob_digit[i] = carry & PyLong_MASK; |
| 2946 | carry >>= PyLong_SHIFT; |
| 2947 | } |
| 2948 | z->ob_digit[i] = carry; |
| 2949 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | /* Subtract the absolute values of two integers. */ |
| 2953 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2954 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2955 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2956 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 2957 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | PyLongObject *z; |
| 2959 | Py_ssize_t i; |
| 2960 | int sign = 1; |
| 2961 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | /* Ensure a is the larger of the two: */ |
| 2964 | if (size_a < size_b) { |
| 2965 | sign = -1; |
| 2966 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2967 | { Py_ssize_t size_temp = size_a; |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 2968 | size_a = size_b; |
| 2969 | size_b = size_temp; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | } |
| 2971 | else if (size_a == size_b) { |
| 2972 | /* Find highest digit where a and b differ: */ |
| 2973 | i = size_a; |
| 2974 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 2975 | ; |
| 2976 | if (i < 0) |
| 2977 | return (PyLongObject *)PyLong_FromLong(0); |
| 2978 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 2979 | sign = -1; |
| 2980 | { PyLongObject *temp = a; a = b; b = temp; } |
| 2981 | } |
| 2982 | size_a = size_b = i+1; |
| 2983 | } |
| 2984 | z = _PyLong_New(size_a); |
| 2985 | if (z == NULL) |
| 2986 | return NULL; |
| 2987 | for (i = 0; i < size_b; ++i) { |
| 2988 | /* The following assumes unsigned arithmetic |
| 2989 | works module 2**N for some N>PyLong_SHIFT. */ |
| 2990 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 2991 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2992 | borrow >>= PyLong_SHIFT; |
| 2993 | borrow &= 1; /* Keep only one sign bit */ |
| 2994 | } |
| 2995 | for (; i < size_a; ++i) { |
| 2996 | borrow = a->ob_digit[i] - borrow; |
| 2997 | z->ob_digit[i] = borrow & PyLong_MASK; |
| 2998 | borrow >>= PyLong_SHIFT; |
| 2999 | borrow &= 1; /* Keep only one sign bit */ |
| 3000 | } |
| 3001 | assert(borrow == 0); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3002 | if (sign < 0) { |
Victor Stinner | 8bcf312 | 2016-08-17 19:48:33 +0200 | [diff] [blame^] | 3003 | Py_SIZE(z) = -Py_SIZE(z); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3004 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | return long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3008 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3009 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3014 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3015 | if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) + |
| 3017 | MEDIUM_VALUE(b)); |
| 3018 | return result; |
| 3019 | } |
| 3020 | if (Py_SIZE(a) < 0) { |
| 3021 | if (Py_SIZE(b) < 0) { |
| 3022 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3023 | if (z != NULL) { |
| 3024 | /* x_add received at least one multiple-digit int, |
| 3025 | and thus z must be a multiple-digit int. |
| 3026 | That also means z is not an element of |
| 3027 | small_ints, so negating it in-place is safe. */ |
| 3028 | assert(Py_REFCNT(z) == 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | Py_SIZE(z) = -(Py_SIZE(z)); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3030 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | } |
| 3032 | else |
| 3033 | z = x_sub(b, a); |
| 3034 | } |
| 3035 | else { |
| 3036 | if (Py_SIZE(b) < 0) |
| 3037 | z = x_sub(a, b); |
| 3038 | else |
| 3039 | z = x_add(a, b); |
| 3040 | } |
| 3041 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3044 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3045 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3050 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3051 | if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | PyObject* r; |
| 3053 | r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); |
| 3054 | return r; |
| 3055 | } |
| 3056 | if (Py_SIZE(a) < 0) { |
| 3057 | if (Py_SIZE(b) < 0) |
| 3058 | z = x_sub(a, b); |
| 3059 | else |
| 3060 | z = x_add(a, b); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3061 | if (z != NULL) { |
| 3062 | assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | Py_SIZE(z) = -(Py_SIZE(z)); |
Serhiy Storchaka | e63e5d6 | 2016-06-04 00:06:45 +0300 | [diff] [blame] | 3064 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | } |
| 3066 | else { |
| 3067 | if (Py_SIZE(b) < 0) |
| 3068 | z = x_add(a, b); |
| 3069 | else |
| 3070 | z = x_sub(a, b); |
| 3071 | } |
| 3072 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3075 | /* Grade school multiplication, ignoring the signs. |
| 3076 | * Returns the absolute value of the product, or NULL if error. |
| 3077 | */ |
| 3078 | static PyLongObject * |
| 3079 | x_mul(PyLongObject *a, PyLongObject *b) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3082 | Py_ssize_t size_a = Py_ABS(Py_SIZE(a)); |
| 3083 | Py_ssize_t size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | Py_ssize_t i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | z = _PyLong_New(size_a + size_b); |
| 3087 | if (z == NULL) |
| 3088 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
| 3091 | if (a == b) { |
| 3092 | /* Efficient squaring per HAC, Algorithm 14.16: |
| 3093 | * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
| 3094 | * Gives slightly less than a 2x speedup when a == b, |
| 3095 | * via exploiting that each entry in the multiplication |
| 3096 | * pyramid appears twice (except for the size_a squares). |
| 3097 | */ |
| 3098 | for (i = 0; i < size_a; ++i) { |
| 3099 | twodigits carry; |
| 3100 | twodigits f = a->ob_digit[i]; |
| 3101 | digit *pz = z->ob_digit + (i << 1); |
| 3102 | digit *pa = a->ob_digit + i + 1; |
| 3103 | digit *paend = a->ob_digit + size_a; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3106 | Py_DECREF(z); |
| 3107 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3108 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | carry = *pz + f * f; |
| 3111 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3112 | carry >>= PyLong_SHIFT; |
| 3113 | assert(carry <= PyLong_MASK); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | /* Now f is added in twice in each column of the |
| 3116 | * pyramid it appears. Same as adding f<<1 once. |
| 3117 | */ |
| 3118 | f <<= 1; |
| 3119 | while (pa < paend) { |
| 3120 | carry += *pz + *pa++ * f; |
| 3121 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3122 | carry >>= PyLong_SHIFT; |
| 3123 | assert(carry <= (PyLong_MASK << 1)); |
| 3124 | } |
| 3125 | if (carry) { |
| 3126 | carry += *pz; |
| 3127 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3128 | carry >>= PyLong_SHIFT; |
| 3129 | } |
| 3130 | if (carry) |
| 3131 | *pz += (digit)(carry & PyLong_MASK); |
| 3132 | assert((carry >> PyLong_SHIFT) == 0); |
| 3133 | } |
| 3134 | } |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3135 | else { /* a is not the same as b -- gradeschool int mult */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | for (i = 0; i < size_a; ++i) { |
| 3137 | twodigits carry = 0; |
| 3138 | twodigits f = a->ob_digit[i]; |
| 3139 | digit *pz = z->ob_digit + i; |
| 3140 | digit *pb = b->ob_digit; |
| 3141 | digit *pbend = b->ob_digit + size_b; |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | SIGCHECK({ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3144 | Py_DECREF(z); |
| 3145 | return NULL; |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 3146 | }); |
Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 3147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | while (pb < pbend) { |
| 3149 | carry += *pz + *pb++ * f; |
| 3150 | *pz++ = (digit)(carry & PyLong_MASK); |
| 3151 | carry >>= PyLong_SHIFT; |
| 3152 | assert(carry <= PyLong_MASK); |
| 3153 | } |
| 3154 | if (carry) |
| 3155 | *pz += (digit)(carry & PyLong_MASK); |
| 3156 | assert((carry >> PyLong_SHIFT) == 0); |
| 3157 | } |
| 3158 | } |
| 3159 | return long_normalize(z); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | /* A helper for Karatsuba multiplication (k_mul). |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 3163 | Takes an int "n" and an integer "size" representing the place to |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3164 | split, and sets low and high such that abs(n) == (high << size) + low, |
| 3165 | viewing the shift as being by digits. The sign bit is ignored, and |
| 3166 | the return values are >= 0. |
| 3167 | Returns 0 on success, -1 on failure. |
| 3168 | */ |
| 3169 | static int |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3170 | kmul_split(PyLongObject *n, |
| 3171 | Py_ssize_t size, |
| 3172 | PyLongObject **high, |
| 3173 | PyLongObject **low) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | PyLongObject *hi, *lo; |
| 3176 | Py_ssize_t size_lo, size_hi; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3177 | const Py_ssize_t size_n = Py_ABS(Py_SIZE(n)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3178 | |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3179 | size_lo = Py_MIN(size_n, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | size_hi = size_n - size_lo; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | if ((hi = _PyLong_New(size_hi)) == NULL) |
| 3183 | return -1; |
| 3184 | if ((lo = _PyLong_New(size_lo)) == NULL) { |
| 3185 | Py_DECREF(hi); |
| 3186 | return -1; |
| 3187 | } |
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 | memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
| 3190 | 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] | 3191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | *high = long_normalize(hi); |
| 3193 | *low = long_normalize(lo); |
| 3194 | return 0; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3197 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
| 3198 | |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3199 | /* Karatsuba multiplication. Ignores the input signs, and returns the |
| 3200 | * absolute value of the product (or NULL if error). |
| 3201 | * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
| 3202 | */ |
| 3203 | static PyLongObject * |
| 3204 | k_mul(PyLongObject *a, PyLongObject *b) |
| 3205 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3206 | Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3207 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | PyLongObject *ah = NULL; |
| 3209 | PyLongObject *al = NULL; |
| 3210 | PyLongObject *bh = NULL; |
| 3211 | PyLongObject *bl = NULL; |
| 3212 | PyLongObject *ret = NULL; |
| 3213 | PyLongObject *t1, *t2, *t3; |
| 3214 | Py_ssize_t shift; /* the number of digits we split off */ |
| 3215 | Py_ssize_t i; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
| 3218 | * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
| 3219 | * Then the original product is |
| 3220 | * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
| 3221 | * By picking X to be a power of 2, "*X" is just shifting, and it's |
| 3222 | * been reduced to 3 multiplies on numbers half the size. |
| 3223 | */ |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | /* We want to split based on the larger number; fiddle so that b |
| 3226 | * is largest. |
| 3227 | */ |
| 3228 | if (asize > bsize) { |
| 3229 | t1 = a; |
| 3230 | a = b; |
| 3231 | b = t1; |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | i = asize; |
| 3234 | asize = bsize; |
| 3235 | bsize = i; |
| 3236 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | /* Use gradeschool math when either number is too small. */ |
| 3239 | i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
| 3240 | if (asize <= i) { |
| 3241 | if (asize == 0) |
| 3242 | return (PyLongObject *)PyLong_FromLong(0); |
| 3243 | else |
| 3244 | return x_mul(a, b); |
| 3245 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3247 | /* If a is small compared to b, splitting on b gives a degenerate |
| 3248 | * case with ah==0, and Karatsuba may be (even much) less efficient |
| 3249 | * than "grade school" then. However, we can still win, by viewing |
| 3250 | * b as a string of "big digits", each of width a->ob_size. That |
| 3251 | * leads to a sequence of balanced calls to k_mul. |
| 3252 | */ |
| 3253 | if (2 * asize <= bsize) |
| 3254 | return k_lopsided_mul(a, b); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | /* Split a & b into hi & lo pieces. */ |
| 3257 | shift = bsize >> 1; |
| 3258 | if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
| 3259 | assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | if (a == b) { |
| 3262 | bh = ah; |
| 3263 | bl = al; |
| 3264 | Py_INCREF(bh); |
| 3265 | Py_INCREF(bl); |
| 3266 | } |
| 3267 | else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | /* The plan: |
| 3270 | * 1. Allocate result space (asize + bsize digits: that's always |
| 3271 | * enough). |
| 3272 | * 2. Compute ah*bh, and copy into result at 2*shift. |
| 3273 | * 3. Compute al*bl, and copy into result at 0. Note that this |
| 3274 | * can't overlap with #2. |
| 3275 | * 4. Subtract al*bl from the result, starting at shift. This may |
| 3276 | * underflow (borrow out of the high digit), but we don't care: |
| 3277 | * we're effectively doing unsigned arithmetic mod |
| 3278 | * BASE**(sizea + sizeb), and so long as the *final* result fits, |
| 3279 | * borrows and carries out of the high digit can be ignored. |
| 3280 | * 5. Subtract ah*bh from the result, starting at shift. |
| 3281 | * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
| 3282 | * at shift. |
| 3283 | */ |
Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | /* 1. Allocate result space. */ |
| 3286 | ret = _PyLong_New(asize + bsize); |
| 3287 | if (ret == NULL) goto fail; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3288 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | /* Fill with trash, to catch reference to uninitialized digits. */ |
| 3290 | memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3291 | #endif |
Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 3292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
| 3294 | if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
| 3295 | assert(Py_SIZE(t1) >= 0); |
| 3296 | assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
| 3297 | memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
| 3298 | Py_SIZE(t1) * sizeof(digit)); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | /* Zero-out the digits higher than the ah*bh copy. */ |
| 3301 | i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
| 3302 | if (i) |
| 3303 | memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
| 3304 | i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | /* 3. t2 <- al*bl, and copy into the low digits. */ |
| 3307 | if ((t2 = k_mul(al, bl)) == NULL) { |
| 3308 | Py_DECREF(t1); |
| 3309 | goto fail; |
| 3310 | } |
| 3311 | assert(Py_SIZE(t2) >= 0); |
| 3312 | assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
| 3313 | memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | /* Zero out remaining digits. */ |
| 3316 | i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
| 3317 | if (i) |
| 3318 | memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
| 3321 | * because it's fresher in cache. |
| 3322 | */ |
| 3323 | i = Py_SIZE(ret) - shift; /* # digits after shift */ |
| 3324 | (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
| 3325 | Py_DECREF(t2); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
| 3328 | Py_DECREF(t1); |
Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 3329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
| 3331 | if ((t1 = x_add(ah, al)) == NULL) goto fail; |
| 3332 | Py_DECREF(ah); |
| 3333 | Py_DECREF(al); |
| 3334 | ah = al = NULL; |
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 | if (a == b) { |
| 3337 | t2 = t1; |
| 3338 | Py_INCREF(t2); |
| 3339 | } |
| 3340 | else if ((t2 = x_add(bh, bl)) == NULL) { |
| 3341 | Py_DECREF(t1); |
| 3342 | goto fail; |
| 3343 | } |
| 3344 | Py_DECREF(bh); |
| 3345 | Py_DECREF(bl); |
| 3346 | bh = bl = NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | t3 = k_mul(t1, t2); |
| 3349 | Py_DECREF(t1); |
| 3350 | Py_DECREF(t2); |
| 3351 | if (t3 == NULL) goto fail; |
| 3352 | assert(Py_SIZE(t3) >= 0); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | /* Add t3. It's not obvious why we can't run out of room here. |
| 3355 | * See the (*) comment after this function. |
| 3356 | */ |
| 3357 | (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
| 3358 | Py_DECREF(t3); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | return long_normalize(ret); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3361 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3362 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | Py_XDECREF(ret); |
| 3364 | Py_XDECREF(ah); |
| 3365 | Py_XDECREF(al); |
| 3366 | Py_XDECREF(bh); |
| 3367 | Py_XDECREF(bl); |
| 3368 | return NULL; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3371 | /* (*) Why adding t3 can't "run out of room" above. |
| 3372 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3373 | Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
| 3374 | to start with: |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3375 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3376 | 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
| 3377 | bsize = c(bsize/2) + f(bsize/2). |
| 3378 | 2. shift = f(bsize/2) |
| 3379 | 3. asize <= bsize |
| 3380 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this |
| 3381 | routine, so asize > bsize/2 >= f(bsize/2) in this routine. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3382 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3383 | We allocated asize + bsize result digits, and add t3 into them at an offset |
| 3384 | of shift. This leaves asize+bsize-shift allocated digit positions for t3 |
| 3385 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = |
| 3386 | asize + c(bsize/2) available digit positions. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3387 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3388 | bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has |
| 3389 | at most c(bsize/2) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3390 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3391 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) |
| 3392 | digits, and al has at most f(bsize/2) digits in any case. So ah+al has at |
| 3393 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3394 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3395 | The product (ah+al)*(bh+bl) therefore has at most |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3396 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3397 | 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] | 3398 | |
Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 3399 | and we have asize + c(bsize/2) available digit positions. We need to show |
| 3400 | this is always enough. An instance of c(bsize/2) cancels out in both, so |
| 3401 | the question reduces to whether asize digits is enough to hold |
| 3402 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize, |
| 3403 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4, |
| 3404 | 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] | 3405 | 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] | 3406 | 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] | 3407 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits |
| 3408 | is enough to hold 2 bits. This is so if bsize >= 2, which holds because |
| 3409 | bsize >= KARATSUBA_CUTOFF >= 2. |
Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 3410 | |
Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 3411 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's |
| 3412 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract |
| 3413 | ah*bh and al*bl too. |
Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 3414 | */ |
| 3415 | |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3416 | /* b has at least twice the digits of a, and a is big enough that Karatsuba |
| 3417 | * would pay off *if* the inputs had balanced sizes. View b as a sequence |
| 3418 | * of slices, each with a->ob_size digits, and multiply the slices by a, |
| 3419 | * one at a time. This gives k_mul balanced inputs to work with, and is |
| 3420 | * also cache-friendly (we compute one double-width slice of the result |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 3421 | * at a time, then move on, never backtracking except for the helpful |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3422 | * single-width slice overlap between successive partial sums). |
| 3423 | */ |
| 3424 | static PyLongObject * |
| 3425 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
| 3426 | { |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3427 | const Py_ssize_t asize = Py_ABS(Py_SIZE(a)); |
| 3428 | Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | Py_ssize_t nbdone; /* # of b digits already multiplied */ |
| 3430 | PyLongObject *ret; |
| 3431 | PyLongObject *bslice = NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | assert(asize > KARATSUBA_CUTOFF); |
| 3434 | assert(2 * asize <= bsize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | /* Allocate result space, and zero it out. */ |
| 3437 | ret = _PyLong_New(asize + bsize); |
| 3438 | if (ret == NULL) |
| 3439 | return NULL; |
| 3440 | memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | /* Successive slices of b are copied into bslice. */ |
| 3443 | bslice = _PyLong_New(asize); |
| 3444 | if (bslice == NULL) |
| 3445 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3447 | nbdone = 0; |
| 3448 | while (bsize > 0) { |
| 3449 | PyLongObject *product; |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3450 | const Py_ssize_t nbtouse = Py_MIN(bsize, asize); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | /* Multiply the next slice of b by a. */ |
| 3453 | memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
| 3454 | nbtouse * sizeof(digit)); |
| 3455 | Py_SIZE(bslice) = nbtouse; |
| 3456 | product = k_mul(a, bslice); |
| 3457 | if (product == NULL) |
| 3458 | goto fail; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | /* Add into result. */ |
| 3461 | (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
| 3462 | product->ob_digit, Py_SIZE(product)); |
| 3463 | Py_DECREF(product); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | bsize -= nbtouse; |
| 3466 | nbdone += nbtouse; |
| 3467 | } |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | Py_DECREF(bslice); |
| 3470 | return long_normalize(ret); |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3471 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3472 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | Py_DECREF(ret); |
| 3474 | Py_XDECREF(bslice); |
| 3475 | return NULL; |
Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 3476 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3477 | |
| 3478 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3479 | long_mul(PyLongObject *a, PyLongObject *b) |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | CHECK_BINOP(a, b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | /* fast path for single-digit multiplication */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3486 | if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3488 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | return PyLong_FromLongLong((PY_LONG_LONG)v); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3490 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | /* if we don't have long long then we're almost certainly |
| 3492 | using 15-bit digits, so v will fit in a long. In the |
| 3493 | unlikely event that we're using 30-bit digits on a platform |
| 3494 | without long long, a large v will just cause us to fall |
| 3495 | through to the general multiplication code below. */ |
| 3496 | if (v >= LONG_MIN && v <= LONG_MAX) |
| 3497 | return PyLong_FromLong((long)v); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 3498 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | z = k_mul(a, b); |
| 3502 | /* Negate if exactly one of the inputs is negative. */ |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 3503 | if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { |
| 3504 | _PyLong_Negate(&z); |
| 3505 | if (z == NULL) |
| 3506 | return NULL; |
| 3507 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3511 | /* Fast modulo division for single-digit longs. */ |
| 3512 | static PyObject * |
| 3513 | fast_mod(PyLongObject *a, PyLongObject *b) |
| 3514 | { |
| 3515 | sdigit left = a->ob_digit[0]; |
| 3516 | sdigit right = b->ob_digit[0]; |
| 3517 | sdigit mod; |
| 3518 | |
| 3519 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3520 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3521 | |
| 3522 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3523 | /* 'a' and 'b' have the same sign. */ |
| 3524 | mod = left % right; |
| 3525 | } |
| 3526 | else { |
| 3527 | /* Either 'a' or 'b' is negative. */ |
| 3528 | mod = right - 1 - (left - 1) % right; |
| 3529 | } |
| 3530 | |
Victor Stinner | f963c13 | 2016-03-23 18:36:54 +0100 | [diff] [blame] | 3531 | return PyLong_FromLong(mod * (sdigit)Py_SIZE(b)); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | /* Fast floor division for single-digit longs. */ |
| 3535 | static PyObject * |
| 3536 | fast_floor_div(PyLongObject *a, PyLongObject *b) |
| 3537 | { |
| 3538 | sdigit left = a->ob_digit[0]; |
| 3539 | sdigit right = b->ob_digit[0]; |
| 3540 | sdigit div; |
| 3541 | |
| 3542 | assert(Py_ABS(Py_SIZE(a)) == 1); |
| 3543 | assert(Py_ABS(Py_SIZE(b)) == 1); |
| 3544 | |
| 3545 | if (Py_SIZE(a) == Py_SIZE(b)) { |
| 3546 | /* 'a' and 'b' have the same sign. */ |
| 3547 | div = left / right; |
| 3548 | } |
| 3549 | else { |
| 3550 | /* Either 'a' or 'b' is negative. */ |
| 3551 | div = -1 - (left - 1) / right; |
| 3552 | } |
| 3553 | |
| 3554 | return PyLong_FromLong(div); |
| 3555 | } |
| 3556 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3557 | /* The / and % operators are now defined in terms of divmod(). |
| 3558 | The expression a mod b has the value a - b*floor(a/b). |
| 3559 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3560 | |a| by |b|, with the sign of a. This is also expressed |
| 3561 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 3562 | Some examples: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | a b a rem b a mod b |
| 3564 | 13 10 3 3 |
| 3565 | -13 10 -3 7 |
| 3566 | 13 -10 3 -7 |
| 3567 | -13 -10 -3 -3 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3568 | 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] | 3569 | have different signs. We then subtract one from the 'div' |
| 3570 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3571 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3572 | /* Compute |
| 3573 | * *pdiv, *pmod = divmod(v, w) |
| 3574 | * NULL can be passed for pdiv or pmod, in which case that part of |
| 3575 | * the result is simply thrown away. The caller owns a reference to |
| 3576 | * each of these it requests (does not pass NULL for). |
| 3577 | */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3578 | static int |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3579 | l_divmod(PyLongObject *v, PyLongObject *w, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | PyLongObject *div, *mod; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3583 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3584 | if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) { |
| 3585 | /* Fast path for single-digit longs */ |
| 3586 | div = NULL; |
| 3587 | if (pdiv != NULL) { |
| 3588 | div = (PyLongObject *)fast_floor_div(v, w); |
| 3589 | if (div == NULL) { |
| 3590 | return -1; |
| 3591 | } |
| 3592 | } |
| 3593 | if (pmod != NULL) { |
| 3594 | mod = (PyLongObject *)fast_mod(v, w); |
| 3595 | if (mod == NULL) { |
| 3596 | Py_XDECREF(div); |
| 3597 | return -1; |
| 3598 | } |
| 3599 | *pmod = mod; |
| 3600 | } |
| 3601 | if (pdiv != NULL) { |
| 3602 | /* We only want to set `*pdiv` when `*pmod` is |
| 3603 | set successfully. */ |
| 3604 | *pdiv = div; |
| 3605 | } |
| 3606 | return 0; |
| 3607 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | if (long_divrem(v, w, &div, &mod) < 0) |
| 3609 | return -1; |
| 3610 | if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
| 3611 | (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
| 3612 | PyLongObject *temp; |
| 3613 | PyLongObject *one; |
| 3614 | temp = (PyLongObject *) long_add(mod, w); |
| 3615 | Py_DECREF(mod); |
| 3616 | mod = temp; |
| 3617 | if (mod == NULL) { |
| 3618 | Py_DECREF(div); |
| 3619 | return -1; |
| 3620 | } |
| 3621 | one = (PyLongObject *) PyLong_FromLong(1L); |
| 3622 | if (one == NULL || |
| 3623 | (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
| 3624 | Py_DECREF(mod); |
| 3625 | Py_DECREF(div); |
| 3626 | Py_XDECREF(one); |
| 3627 | return -1; |
| 3628 | } |
| 3629 | Py_DECREF(one); |
| 3630 | Py_DECREF(div); |
| 3631 | div = temp; |
| 3632 | } |
| 3633 | if (pdiv != NULL) |
| 3634 | *pdiv = div; |
| 3635 | else |
| 3636 | Py_DECREF(div); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | if (pmod != NULL) |
| 3639 | *pmod = mod; |
| 3640 | else |
| 3641 | Py_DECREF(mod); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | return 0; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3646 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3647 | long_div(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | PyLongObject *div; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | CHECK_BINOP(a, b); |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3652 | |
| 3653 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3654 | return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); |
| 3655 | } |
| 3656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
| 3658 | div = NULL; |
| 3659 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3662 | /* PyLong/PyLong -> float, with correctly rounded result. */ |
| 3663 | |
| 3664 | #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
| 3665 | #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
| 3666 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3667 | static PyObject * |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3668 | long_true_divide(PyObject *v, PyObject *w) |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3670 | PyLongObject *a, *b, *x; |
| 3671 | Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
| 3672 | digit mask, low; |
| 3673 | int inexact, negate, a_is_small, b_is_small; |
| 3674 | double dx, result; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | CHECK_BINOP(v, w); |
| 3677 | a = (PyLongObject *)v; |
| 3678 | b = (PyLongObject *)w; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | /* |
| 3681 | Method in a nutshell: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
| 3684 | 1. choose a suitable integer 'shift' |
| 3685 | 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
| 3686 | 3. adjust x for correct rounding |
| 3687 | 4. convert x to a double dx with the same value |
| 3688 | 5. return ldexp(dx, shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | In more detail: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3692 | 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
| 3693 | returns either 0.0 or -0.0, depending on the sign of b. For a and |
| 3694 | b both nonzero, ignore signs of a and b, and add the sign back in |
| 3695 | at the end. Now write a_bits and b_bits for the bit lengths of a |
| 3696 | and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
| 3697 | for b). Then |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | 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] | 3700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
| 3702 | so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
| 3703 | DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
| 3704 | the way, we can assume that |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | 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] | 3707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | 1. The integer 'shift' is chosen so that x has the right number of |
| 3709 | bits for a double, plus two or three extra bits that will be used |
| 3710 | in the rounding decisions. Writing a_bits and b_bits for the |
| 3711 | number of significant bits in a and b respectively, a |
| 3712 | straightforward formula for shift is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3716 | This is fine in the usual case, but if a/b is smaller than the |
| 3717 | smallest normal float then it can lead to double rounding on an |
| 3718 | IEEE 754 platform, giving incorrectly rounded results. So we |
| 3719 | adjust the formula slightly. The actual formula used is: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 | 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] | 3722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | 2. The quantity x is computed by first shifting a (left -shift bits |
| 3724 | if shift <= 0, right shift bits if shift > 0) and then dividing by |
| 3725 | b. For both the shift and the division, we keep track of whether |
| 3726 | the result is inexact, in a flag 'inexact'; this information is |
| 3727 | needed at the rounding stage. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | With the choice of shift above, together with our assumption that |
| 3730 | a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
| 3731 | that x >= 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3733 | 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
| 3734 | this with an exactly representable float of the form |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3736 | round(x/2**extra_bits) * 2**(extra_bits+shift). |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | For float representability, we need x/2**extra_bits < |
| 3739 | 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
| 3740 | DBL_MANT_DIG. This translates to the condition: |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | To round, we just modify the bottom digit of x in-place; this can |
| 3745 | end up giving a digit with value > PyLONG_MASK, but that's not a |
| 3746 | problem since digits can hold values up to 2*PyLONG_MASK+1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | With the original choices for shift above, extra_bits will always |
| 3749 | be 2 or 3. Then rounding under the round-half-to-even rule, we |
| 3750 | round up iff the most significant of the extra bits is 1, and |
| 3751 | either: (a) the computation of x in step 2 had an inexact result, |
| 3752 | or (b) at least one other of the extra bits is 1, or (c) the least |
| 3753 | significant bit of x (above those to be rounded) is 1. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | 4. Conversion to a double is straightforward; all floating-point |
| 3756 | operations involved in the conversion are exact, so there's no |
| 3757 | danger of rounding errors. |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
| 3760 | The result will always be exactly representable as a double, except |
| 3761 | in the case that it overflows. To avoid dependence on the exact |
| 3762 | behaviour of ldexp on overflow, we check for overflow before |
| 3763 | applying ldexp. The result of ldexp is adjusted for sign before |
| 3764 | returning. |
| 3765 | */ |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | /* Reduce to case where a and b are both positive. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3768 | a_size = Py_ABS(Py_SIZE(a)); |
| 3769 | b_size = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3770 | negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
| 3771 | if (b_size == 0) { |
| 3772 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 3773 | "division by zero"); |
| 3774 | goto error; |
| 3775 | } |
| 3776 | if (a_size == 0) |
| 3777 | goto underflow_or_zero; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 | /* Fast path for a and b small (exactly representable in a double). |
| 3780 | Relies on floating-point division being correctly rounded; results |
| 3781 | may be subject to double rounding on x86 machines that operate with |
| 3782 | the x87 FPU set to 64-bit precision. */ |
| 3783 | a_is_small = a_size <= MANT_DIG_DIGITS || |
| 3784 | (a_size == MANT_DIG_DIGITS+1 && |
| 3785 | a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3786 | b_is_small = b_size <= MANT_DIG_DIGITS || |
| 3787 | (b_size == MANT_DIG_DIGITS+1 && |
| 3788 | b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
| 3789 | if (a_is_small && b_is_small) { |
| 3790 | double da, db; |
| 3791 | da = a->ob_digit[--a_size]; |
| 3792 | while (a_size > 0) |
| 3793 | da = da * PyLong_BASE + a->ob_digit[--a_size]; |
| 3794 | db = b->ob_digit[--b_size]; |
| 3795 | while (b_size > 0) |
| 3796 | db = db * PyLong_BASE + b->ob_digit[--b_size]; |
| 3797 | result = da / db; |
| 3798 | goto success; |
| 3799 | } |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | /* Catch obvious cases of underflow and overflow */ |
| 3802 | diff = a_size - b_size; |
| 3803 | if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
| 3804 | /* Extreme overflow */ |
| 3805 | goto overflow; |
| 3806 | else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 3807 | /* Extreme underflow */ |
| 3808 | goto underflow_or_zero; |
| 3809 | /* Next line is now safe from overflowing a Py_ssize_t */ |
| 3810 | diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) - |
| 3811 | bits_in_digit(b->ob_digit[b_size - 1]); |
| 3812 | /* Now diff = a_bits - b_bits. */ |
| 3813 | if (diff > DBL_MAX_EXP) |
| 3814 | goto overflow; |
| 3815 | else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
| 3816 | goto underflow_or_zero; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 3817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | /* Choose value for shift; see comments for step 1 above. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3819 | shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3821 | inexact = 0; |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | /* x = abs(a * 2**-shift) */ |
| 3824 | if (shift <= 0) { |
| 3825 | Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
| 3826 | digit rem; |
| 3827 | /* x = a << -shift */ |
| 3828 | if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
| 3829 | /* In practice, it's probably impossible to end up |
| 3830 | here. Both a and b would have to be enormous, |
| 3831 | using close to SIZE_T_MAX bytes of memory each. */ |
| 3832 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 3833 | "intermediate overflow during division"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3834 | goto error; |
| 3835 | } |
| 3836 | x = _PyLong_New(a_size + shift_digits + 1); |
| 3837 | if (x == NULL) |
| 3838 | goto error; |
| 3839 | for (i = 0; i < shift_digits; i++) |
| 3840 | x->ob_digit[i] = 0; |
| 3841 | rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
| 3842 | a_size, -shift % PyLong_SHIFT); |
| 3843 | x->ob_digit[a_size + shift_digits] = rem; |
| 3844 | } |
| 3845 | else { |
| 3846 | Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
| 3847 | digit rem; |
| 3848 | /* x = a >> shift */ |
| 3849 | assert(a_size >= shift_digits); |
| 3850 | x = _PyLong_New(a_size - shift_digits); |
| 3851 | if (x == NULL) |
| 3852 | goto error; |
| 3853 | rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
| 3854 | a_size - shift_digits, shift % PyLong_SHIFT); |
| 3855 | /* set inexact if any of the bits shifted out is nonzero */ |
| 3856 | if (rem) |
| 3857 | inexact = 1; |
| 3858 | while (!inexact && shift_digits > 0) |
| 3859 | if (a->ob_digit[--shift_digits]) |
| 3860 | inexact = 1; |
| 3861 | } |
| 3862 | long_normalize(x); |
| 3863 | x_size = Py_SIZE(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | /* x //= b. If the remainder is nonzero, set inexact. We own the only |
| 3866 | reference to x, so it's safe to modify it in-place. */ |
| 3867 | if (b_size == 1) { |
| 3868 | digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
| 3869 | b->ob_digit[0]); |
| 3870 | long_normalize(x); |
| 3871 | if (rem) |
| 3872 | inexact = 1; |
| 3873 | } |
| 3874 | else { |
| 3875 | PyLongObject *div, *rem; |
| 3876 | div = x_divrem(x, b, &rem); |
| 3877 | Py_DECREF(x); |
| 3878 | x = div; |
| 3879 | if (x == NULL) |
| 3880 | goto error; |
| 3881 | if (Py_SIZE(rem)) |
| 3882 | inexact = 1; |
| 3883 | Py_DECREF(rem); |
| 3884 | } |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 3885 | x_size = Py_ABS(Py_SIZE(x)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | assert(x_size > 0); /* result of division is never zero */ |
| 3887 | 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] | 3888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | /* The number of extra bits that have to be rounded away. */ |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 3890 | extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | assert(extra_bits == 2 || extra_bits == 3); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | /* Round by directly modifying the low digit of x. */ |
| 3894 | mask = (digit)1 << (extra_bits - 1); |
| 3895 | low = x->ob_digit[0] | inexact; |
| 3896 | if (low & mask && low & (3*mask-1)) |
| 3897 | low += mask; |
| 3898 | x->ob_digit[0] = low & ~(mask-1U); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | /* Convert x to a double dx; the conversion is exact. */ |
| 3901 | dx = x->ob_digit[--x_size]; |
| 3902 | while (x_size > 0) |
| 3903 | dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
| 3904 | Py_DECREF(x); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3906 | /* Check whether ldexp result will overflow a double. */ |
| 3907 | if (shift + x_bits >= DBL_MAX_EXP && |
| 3908 | (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
| 3909 | goto overflow; |
| 3910 | result = ldexp(dx, (int)shift); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3911 | |
| 3912 | success: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3913 | return PyFloat_FromDouble(negate ? -result : result); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3914 | |
| 3915 | underflow_or_zero: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3917 | |
| 3918 | overflow: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3919 | PyErr_SetString(PyExc_OverflowError, |
| 3920 | "integer division result too large for a float"); |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 3921 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | return NULL; |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3926 | long_mod(PyObject *a, PyObject *b) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3927 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | PyLongObject *mod; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | CHECK_BINOP(a, b); |
| 3931 | |
Yury Selivanov | e0b2309 | 2016-02-11 10:26:27 -0500 | [diff] [blame] | 3932 | if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { |
| 3933 | return fast_mod((PyLongObject*)a, (PyLongObject*)b); |
| 3934 | } |
| 3935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3936 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
| 3937 | mod = NULL; |
| 3938 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3941 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 3942 | long_divmod(PyObject *a, PyObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | PyLongObject *div, *mod; |
| 3945 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3949 | if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
| 3950 | return NULL; |
| 3951 | } |
| 3952 | z = PyTuple_New(2); |
| 3953 | if (z != NULL) { |
| 3954 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 3955 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
| 3956 | } |
| 3957 | else { |
| 3958 | Py_DECREF(div); |
| 3959 | Py_DECREF(mod); |
| 3960 | } |
| 3961 | return z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3964 | /* pow(v, w, x) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3965 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3966 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3967 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
| 3969 | int negativeOutput = 0; /* if x<0 return negative output */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | PyLongObject *z = NULL; /* accumulated result */ |
| 3972 | Py_ssize_t i, j, k; /* counters */ |
| 3973 | PyLongObject *temp = NULL; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | /* 5-ary values. If the exponent is large enough, table is |
| 3976 | * precomputed so that table[i] == a**i % c for i in range(32). |
| 3977 | */ |
| 3978 | PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 3979 | 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] | 3980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | /* a, b, c = v, w, x */ |
| 3982 | CHECK_BINOP(v, w); |
| 3983 | a = (PyLongObject*)v; Py_INCREF(a); |
| 3984 | b = (PyLongObject*)w; Py_INCREF(b); |
| 3985 | if (PyLong_Check(x)) { |
| 3986 | c = (PyLongObject *)x; |
| 3987 | Py_INCREF(x); |
| 3988 | } |
| 3989 | else if (x == Py_None) |
| 3990 | c = NULL; |
| 3991 | else { |
| 3992 | Py_DECREF(a); |
| 3993 | Py_DECREF(b); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3994 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3997 | if (Py_SIZE(b) < 0) { /* if exponent is negative */ |
| 3998 | if (c) { |
Mark Dickinson | 0c346d8 | 2014-04-11 14:34:40 -0400 | [diff] [blame] | 3999 | PyErr_SetString(PyExc_ValueError, "pow() 2nd argument " |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4000 | "cannot be negative when 3rd argument specified"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4001 | goto Error; |
| 4002 | } |
| 4003 | else { |
| 4004 | /* else return a float. This works because we know |
| 4005 | that this calls float_pow() which converts its |
| 4006 | arguments to double. */ |
| 4007 | Py_DECREF(a); |
| 4008 | Py_DECREF(b); |
| 4009 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
| 4010 | } |
| 4011 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4013 | if (c) { |
| 4014 | /* if modulus == 0: |
| 4015 | raise ValueError() */ |
| 4016 | if (Py_SIZE(c) == 0) { |
| 4017 | PyErr_SetString(PyExc_ValueError, |
| 4018 | "pow() 3rd argument cannot be 0"); |
| 4019 | goto Error; |
| 4020 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | /* if modulus < 0: |
| 4023 | negativeOutput = True |
| 4024 | modulus = -modulus */ |
| 4025 | if (Py_SIZE(c) < 0) { |
| 4026 | negativeOutput = 1; |
| 4027 | temp = (PyLongObject *)_PyLong_Copy(c); |
| 4028 | if (temp == NULL) |
| 4029 | goto Error; |
| 4030 | Py_DECREF(c); |
| 4031 | c = temp; |
| 4032 | temp = NULL; |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4033 | _PyLong_Negate(&c); |
| 4034 | if (c == NULL) |
| 4035 | goto Error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4036 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | /* if modulus == 1: |
| 4039 | return 0 */ |
| 4040 | if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
| 4041 | z = (PyLongObject *)PyLong_FromLong(0L); |
| 4042 | goto Done; |
| 4043 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4044 | |
Tim Peters | 81a9315 | 2013-10-05 16:53:52 -0500 | [diff] [blame] | 4045 | /* Reduce base by modulus in some cases: |
| 4046 | 1. If base < 0. Forcing the base non-negative makes things easier. |
| 4047 | 2. If base is obviously larger than the modulus. The "small |
| 4048 | exponent" case later can multiply directly by base repeatedly, |
| 4049 | while the "large exponent" case multiplies directly by base 31 |
| 4050 | times. It can be unboundedly faster to multiply by |
| 4051 | base % modulus instead. |
| 4052 | We could _always_ do this reduction, but l_divmod() isn't cheap, |
| 4053 | so we only do it when it buys something. */ |
| 4054 | if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | if (l_divmod(a, c, NULL, &temp) < 0) |
| 4056 | goto Error; |
| 4057 | Py_DECREF(a); |
| 4058 | a = temp; |
| 4059 | temp = NULL; |
| 4060 | } |
| 4061 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | /* At this point a, b, and c are guaranteed non-negative UNLESS |
| 4064 | c is NULL, in which case a may be negative. */ |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 4067 | if (z == NULL) |
| 4068 | goto Error; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4070 | /* Perform a modular reduction, X = X % c, but leave X alone if c |
| 4071 | * is NULL. |
| 4072 | */ |
| 4073 | #define REDUCE(X) \ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4074 | do { \ |
| 4075 | if (c != NULL) { \ |
| 4076 | if (l_divmod(X, c, NULL, &temp) < 0) \ |
| 4077 | goto Error; \ |
| 4078 | Py_XDECREF(X); \ |
| 4079 | X = temp; \ |
| 4080 | temp = NULL; \ |
| 4081 | } \ |
| 4082 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | /* Multiply two values, then reduce the result: |
| 4085 | result = X*Y % c. If c is NULL, skip the mod. */ |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4086 | #define MULT(X, Y, result) \ |
| 4087 | do { \ |
| 4088 | temp = (PyLongObject *)long_mul(X, Y); \ |
| 4089 | if (temp == NULL) \ |
| 4090 | goto Error; \ |
| 4091 | Py_XDECREF(result); \ |
| 4092 | result = temp; \ |
| 4093 | temp = NULL; \ |
| 4094 | REDUCE(result); \ |
| 4095 | } while(0) |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4097 | if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
| 4098 | /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
| 4099 | /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
| 4100 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4101 | digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4103 | for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4104 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | if (bi & j) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4106 | MULT(z, a, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | } |
| 4110 | else { |
| 4111 | /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
| 4112 | Py_INCREF(z); /* still holds 1L */ |
| 4113 | table[0] = z; |
| 4114 | for (i = 1; i < 32; ++i) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4115 | MULT(table[i-1], a, table[i]); |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
| 4118 | const digit bi = b->ob_digit[i]; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
| 4121 | const int index = (bi >> j) & 0x1f; |
| 4122 | for (k = 0; k < 5; ++k) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4123 | MULT(z, z, z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4124 | if (index) |
Mark Dickinson | cdd01d2 | 2010-05-10 21:37:34 +0000 | [diff] [blame] | 4125 | MULT(z, table[index], z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | } |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | if (negativeOutput && (Py_SIZE(z) != 0)) { |
| 4131 | temp = (PyLongObject *)long_sub(z, c); |
| 4132 | if (temp == NULL) |
| 4133 | goto Error; |
| 4134 | Py_DECREF(z); |
| 4135 | z = temp; |
| 4136 | temp = NULL; |
| 4137 | } |
| 4138 | goto Done; |
Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 4139 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4140 | Error: |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4141 | Py_CLEAR(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | /* fall through */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4143 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
| 4145 | for (i = 0; i < 32; ++i) |
| 4146 | Py_XDECREF(table[i]); |
| 4147 | } |
| 4148 | Py_DECREF(a); |
| 4149 | Py_DECREF(b); |
| 4150 | Py_XDECREF(c); |
| 4151 | Py_XDECREF(temp); |
| 4152 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4155 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4156 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | /* Implement ~x as -(x+1) */ |
| 4159 | PyLongObject *x; |
| 4160 | PyLongObject *w; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4161 | if (Py_ABS(Py_SIZE(v)) <=1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4162 | return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
| 4163 | w = (PyLongObject *)PyLong_FromLong(1L); |
| 4164 | if (w == NULL) |
| 4165 | return NULL; |
| 4166 | x = (PyLongObject *) long_add(v, w); |
| 4167 | Py_DECREF(w); |
| 4168 | if (x == NULL) |
| 4169 | return NULL; |
| 4170 | Py_SIZE(x) = -(Py_SIZE(x)); |
| 4171 | return (PyObject *)maybe_small_long(x); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4174 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4175 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | PyLongObject *z; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4178 | if (Py_ABS(Py_SIZE(v)) <= 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | return PyLong_FromLong(-MEDIUM_VALUE(v)); |
| 4180 | z = (PyLongObject *)_PyLong_Copy(v); |
| 4181 | if (z != NULL) |
| 4182 | Py_SIZE(z) = -(Py_SIZE(v)); |
| 4183 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4186 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4187 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4189 | if (Py_SIZE(v) < 0) |
| 4190 | return long_neg(v); |
| 4191 | else |
| 4192 | return long_long((PyObject *)v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4195 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4196 | long_bool(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4198 | return Py_SIZE(v) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4201 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4202 | long_rshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | PyLongObject *z = NULL; |
| 4205 | Py_ssize_t shiftby, newsize, wordshift, loshift, hishift, i, j; |
| 4206 | digit lomask, himask; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | if (Py_SIZE(a) < 0) { |
| 4211 | /* Right shifting negative numbers is harder */ |
| 4212 | PyLongObject *a1, *a2; |
| 4213 | a1 = (PyLongObject *) long_invert(a); |
| 4214 | if (a1 == NULL) |
| 4215 | goto rshift_error; |
| 4216 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 4217 | Py_DECREF(a1); |
| 4218 | if (a2 == NULL) |
| 4219 | goto rshift_error; |
| 4220 | z = (PyLongObject *) long_invert(a2); |
| 4221 | Py_DECREF(a2); |
| 4222 | } |
| 4223 | else { |
| 4224 | shiftby = PyLong_AsSsize_t((PyObject *)b); |
| 4225 | if (shiftby == -1L && PyErr_Occurred()) |
| 4226 | goto rshift_error; |
| 4227 | if (shiftby < 0) { |
| 4228 | PyErr_SetString(PyExc_ValueError, |
| 4229 | "negative shift count"); |
| 4230 | goto rshift_error; |
| 4231 | } |
| 4232 | wordshift = shiftby / PyLong_SHIFT; |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4233 | newsize = Py_ABS(Py_SIZE(a)) - wordshift; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | if (newsize <= 0) |
| 4235 | return PyLong_FromLong(0); |
| 4236 | loshift = shiftby % PyLong_SHIFT; |
| 4237 | hishift = PyLong_SHIFT - loshift; |
| 4238 | lomask = ((digit)1 << hishift) - 1; |
| 4239 | himask = PyLong_MASK ^ lomask; |
| 4240 | z = _PyLong_New(newsize); |
| 4241 | if (z == NULL) |
| 4242 | goto rshift_error; |
| 4243 | if (Py_SIZE(a) < 0) |
| 4244 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4245 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 4246 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 4247 | if (i+1 < newsize) |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4248 | z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4249 | } |
| 4250 | z = long_normalize(z); |
| 4251 | } |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4252 | rshift_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4253 | return (PyObject *) maybe_small_long(z); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4254 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4257 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4258 | long_lshift(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4259 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | /* This version due to Tim Peters */ |
| 4261 | PyLongObject *a = (PyLongObject*)v; |
| 4262 | PyLongObject *b = (PyLongObject*)w; |
| 4263 | PyLongObject *z = NULL; |
| 4264 | Py_ssize_t shiftby, oldsize, newsize, wordshift, remshift, i, j; |
| 4265 | twodigits accum; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | CHECK_BINOP(a, b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 4268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | shiftby = PyLong_AsSsize_t((PyObject *)b); |
| 4270 | if (shiftby == -1L && PyErr_Occurred()) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4271 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | if (shiftby < 0) { |
| 4273 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4274 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | } |
| 4276 | /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
| 4277 | wordshift = shiftby / PyLong_SHIFT; |
| 4278 | remshift = shiftby - wordshift * PyLong_SHIFT; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 4279 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4280 | oldsize = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | newsize = oldsize + wordshift; |
| 4282 | if (remshift) |
| 4283 | ++newsize; |
| 4284 | z = _PyLong_New(newsize); |
| 4285 | if (z == NULL) |
Victor Stinner | 8aed6f1 | 2013-07-17 22:31:17 +0200 | [diff] [blame] | 4286 | return NULL; |
| 4287 | if (Py_SIZE(a) < 0) { |
| 4288 | assert(Py_REFCNT(z) == 1); |
| 4289 | Py_SIZE(z) = -Py_SIZE(z); |
| 4290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4291 | for (i = 0; i < wordshift; i++) |
| 4292 | z->ob_digit[i] = 0; |
| 4293 | accum = 0; |
| 4294 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 4295 | accum |= (twodigits)a->ob_digit[j] << remshift; |
| 4296 | z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
| 4297 | accum >>= PyLong_SHIFT; |
| 4298 | } |
| 4299 | if (remshift) |
| 4300 | z->ob_digit[newsize-1] = (digit)accum; |
| 4301 | else |
| 4302 | assert(!accum); |
| 4303 | z = long_normalize(z); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | return (PyObject *) maybe_small_long(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4307 | /* Compute two's complement of digit vector a[0:m], writing result to |
| 4308 | z[0:m]. The digit vector a need not be normalized, but should not |
| 4309 | be entirely zero. a and z may point to the same digit vector. */ |
| 4310 | |
| 4311 | static void |
| 4312 | v_complement(digit *z, digit *a, Py_ssize_t m) |
| 4313 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | Py_ssize_t i; |
| 4315 | digit carry = 1; |
| 4316 | for (i = 0; i < m; ++i) { |
| 4317 | carry += a[i] ^ PyLong_MASK; |
| 4318 | z[i] = carry & PyLong_MASK; |
| 4319 | carry >>= PyLong_SHIFT; |
| 4320 | } |
| 4321 | assert(carry == 0); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4322 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 4323 | |
| 4324 | /* Bitwise and/xor/or operations */ |
| 4325 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4326 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4327 | long_bitwise(PyLongObject *a, |
Benjamin Peterson | 513762f | 2012-12-26 16:43:33 -0600 | [diff] [blame] | 4328 | char op, /* '&', '|', '^' */ |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4329 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | int nega, negb, negz; |
| 4332 | Py_ssize_t size_a, size_b, size_z, i; |
| 4333 | PyLongObject *z; |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | /* Bitwise operations for negative numbers operate as though |
| 4336 | on a two's complement representation. So convert arguments |
| 4337 | from sign-magnitude to two's complement, and convert the |
| 4338 | result back to sign-magnitude at the end. */ |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | /* If a is negative, replace it by its two's complement. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4341 | size_a = Py_ABS(Py_SIZE(a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | nega = Py_SIZE(a) < 0; |
| 4343 | if (nega) { |
| 4344 | z = _PyLong_New(size_a); |
| 4345 | if (z == NULL) |
| 4346 | return NULL; |
| 4347 | v_complement(z->ob_digit, a->ob_digit, size_a); |
| 4348 | a = z; |
| 4349 | } |
| 4350 | else |
| 4351 | /* Keep reference count consistent. */ |
| 4352 | Py_INCREF(a); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | /* Same for b. */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4355 | size_b = Py_ABS(Py_SIZE(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | negb = Py_SIZE(b) < 0; |
| 4357 | if (negb) { |
| 4358 | z = _PyLong_New(size_b); |
| 4359 | if (z == NULL) { |
| 4360 | Py_DECREF(a); |
| 4361 | return NULL; |
| 4362 | } |
| 4363 | v_complement(z->ob_digit, b->ob_digit, size_b); |
| 4364 | b = z; |
| 4365 | } |
| 4366 | else |
| 4367 | Py_INCREF(b); |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | /* Swap a and b if necessary to ensure size_a >= size_b. */ |
| 4370 | if (size_a < size_b) { |
| 4371 | z = a; a = b; b = z; |
| 4372 | size_z = size_a; size_a = size_b; size_b = size_z; |
| 4373 | negz = nega; nega = negb; negb = negz; |
| 4374 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | /* JRH: The original logic here was to allocate the result value (z) |
| 4377 | as the longer of the two operands. However, there are some cases |
| 4378 | where the result is guaranteed to be shorter than that: AND of two |
| 4379 | positives, OR of two negatives: use the shorter number. AND with |
| 4380 | mixed signs: use the positive number. OR with mixed signs: use the |
| 4381 | negative number. |
| 4382 | */ |
| 4383 | switch (op) { |
| 4384 | case '^': |
| 4385 | negz = nega ^ negb; |
| 4386 | size_z = size_a; |
| 4387 | break; |
| 4388 | case '&': |
| 4389 | negz = nega & negb; |
| 4390 | size_z = negb ? size_a : size_b; |
| 4391 | break; |
| 4392 | case '|': |
| 4393 | negz = nega | negb; |
| 4394 | size_z = negb ? size_b : size_a; |
| 4395 | break; |
| 4396 | default: |
| 4397 | PyErr_BadArgument(); |
| 4398 | return NULL; |
| 4399 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 4400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | /* We allow an extra digit if z is negative, to make sure that |
| 4402 | the final two's complement of z doesn't overflow. */ |
| 4403 | z = _PyLong_New(size_z + negz); |
| 4404 | if (z == NULL) { |
| 4405 | Py_DECREF(a); |
| 4406 | Py_DECREF(b); |
| 4407 | return NULL; |
| 4408 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | /* Compute digits for overlap of a and b. */ |
| 4411 | switch(op) { |
| 4412 | case '&': |
| 4413 | for (i = 0; i < size_b; ++i) |
| 4414 | z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
| 4415 | break; |
| 4416 | case '|': |
| 4417 | for (i = 0; i < size_b; ++i) |
| 4418 | z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
| 4419 | break; |
| 4420 | case '^': |
| 4421 | for (i = 0; i < size_b; ++i) |
| 4422 | z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
| 4423 | break; |
| 4424 | default: |
| 4425 | PyErr_BadArgument(); |
| 4426 | return NULL; |
| 4427 | } |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | /* Copy any remaining digits of a, inverting if necessary. */ |
| 4430 | if (op == '^' && negb) |
| 4431 | for (; i < size_z; ++i) |
| 4432 | z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
| 4433 | else if (i < size_z) |
| 4434 | memcpy(&z->ob_digit[i], &a->ob_digit[i], |
| 4435 | (size_z-i)*sizeof(digit)); |
Mark Dickinson | 27a87a2 | 2009-10-25 20:43:34 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | /* Complement result if negative. */ |
| 4438 | if (negz) { |
| 4439 | Py_SIZE(z) = -(Py_SIZE(z)); |
| 4440 | z->ob_digit[size_z] = PyLong_MASK; |
| 4441 | v_complement(z->ob_digit, z->ob_digit, size_z+1); |
| 4442 | } |
Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 4443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | Py_DECREF(a); |
| 4445 | Py_DECREF(b); |
| 4446 | return (PyObject *)maybe_small_long(long_normalize(z)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4449 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4450 | long_and(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | PyObject *c; |
| 4453 | CHECK_BINOP(a, b); |
| 4454 | c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
| 4455 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4458 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4459 | long_xor(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | PyObject *c; |
| 4462 | CHECK_BINOP(a, b); |
| 4463 | c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
| 4464 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4467 | static PyObject * |
Martin v. Löwis | da86fcc | 2007-09-10 07:59:54 +0000 | [diff] [blame] | 4468 | long_or(PyObject *a, PyObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 4469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | PyObject *c; |
| 4471 | CHECK_BINOP(a, b); |
| 4472 | c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
| 4473 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4476 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4477 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | if (PyLong_CheckExact(v)) |
| 4480 | Py_INCREF(v); |
| 4481 | else |
| 4482 | v = _PyLong_Copy((PyLongObject *)v); |
| 4483 | return v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4486 | PyObject * |
| 4487 | _PyLong_GCD(PyObject *aarg, PyObject *barg) |
| 4488 | { |
| 4489 | PyLongObject *a, *b, *c = NULL, *d = NULL, *r; |
| 4490 | stwodigits x, y, q, s, t, c_carry, d_carry; |
| 4491 | stwodigits A, B, C, D, T; |
| 4492 | int nbits, k; |
| 4493 | Py_ssize_t size_a, size_b, alloc_a, alloc_b; |
| 4494 | digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end; |
| 4495 | |
| 4496 | a = (PyLongObject *)aarg; |
| 4497 | b = (PyLongObject *)barg; |
| 4498 | size_a = Py_SIZE(a); |
| 4499 | size_b = Py_SIZE(b); |
| 4500 | if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) { |
| 4501 | Py_INCREF(a); |
| 4502 | Py_INCREF(b); |
| 4503 | goto simple; |
| 4504 | } |
| 4505 | |
| 4506 | /* Initial reduction: make sure that 0 <= b <= a. */ |
| 4507 | a = (PyLongObject *)long_abs(a); |
| 4508 | if (a == NULL) |
| 4509 | return NULL; |
| 4510 | b = (PyLongObject *)long_abs(b); |
| 4511 | if (b == NULL) { |
| 4512 | Py_DECREF(a); |
| 4513 | return NULL; |
| 4514 | } |
| 4515 | if (long_compare(a, b) < 0) { |
| 4516 | r = a; |
| 4517 | a = b; |
| 4518 | b = r; |
| 4519 | } |
| 4520 | /* We now own references to a and b */ |
| 4521 | |
| 4522 | alloc_a = Py_SIZE(a); |
| 4523 | alloc_b = Py_SIZE(b); |
| 4524 | /* reduce until a fits into 2 digits */ |
| 4525 | while ((size_a = Py_SIZE(a)) > 2) { |
| 4526 | nbits = bits_in_digit(a->ob_digit[size_a-1]); |
| 4527 | /* extract top 2*PyLong_SHIFT bits of a into x, along with |
| 4528 | corresponding bits of b into y */ |
| 4529 | size_b = Py_SIZE(b); |
| 4530 | assert(size_b <= size_a); |
| 4531 | if (size_b == 0) { |
| 4532 | if (size_a < alloc_a) { |
| 4533 | r = (PyLongObject *)_PyLong_Copy(a); |
| 4534 | Py_DECREF(a); |
| 4535 | } |
| 4536 | else |
| 4537 | r = a; |
| 4538 | Py_DECREF(b); |
| 4539 | Py_XDECREF(c); |
| 4540 | Py_XDECREF(d); |
| 4541 | return (PyObject *)r; |
| 4542 | } |
| 4543 | x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) | |
| 4544 | ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) | |
| 4545 | (a->ob_digit[size_a-3] >> nbits)); |
| 4546 | |
| 4547 | y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) | |
| 4548 | (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) | |
| 4549 | (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0)); |
| 4550 | |
| 4551 | /* inner loop of Lehmer's algorithm; A, B, C, D never grow |
| 4552 | larger than PyLong_MASK during the algorithm. */ |
| 4553 | A = 1; B = 0; C = 0; D = 1; |
| 4554 | for (k=0;; k++) { |
| 4555 | if (y-C == 0) |
| 4556 | break; |
| 4557 | q = (x+(A-1))/(y-C); |
| 4558 | s = B+q*D; |
| 4559 | t = x-q*y; |
| 4560 | if (s > t) |
| 4561 | break; |
| 4562 | x = y; y = t; |
| 4563 | t = A+q*C; A = D; B = C; C = s; D = t; |
| 4564 | } |
| 4565 | |
| 4566 | if (k == 0) { |
| 4567 | /* no progress; do a Euclidean step */ |
| 4568 | if (l_divmod(a, b, NULL, &r) < 0) |
| 4569 | goto error; |
| 4570 | Py_DECREF(a); |
| 4571 | a = b; |
| 4572 | b = r; |
| 4573 | alloc_a = alloc_b; |
| 4574 | alloc_b = Py_SIZE(b); |
| 4575 | continue; |
| 4576 | } |
| 4577 | |
| 4578 | /* |
| 4579 | a, b = A*b-B*a, D*a-C*b if k is odd |
| 4580 | a, b = A*a-B*b, D*b-C*a if k is even |
| 4581 | */ |
| 4582 | if (k&1) { |
| 4583 | T = -A; A = -B; B = T; |
| 4584 | T = -C; C = -D; D = T; |
| 4585 | } |
| 4586 | if (c != NULL) |
| 4587 | Py_SIZE(c) = size_a; |
| 4588 | else if (Py_REFCNT(a) == 1) { |
| 4589 | Py_INCREF(a); |
| 4590 | c = a; |
| 4591 | } |
| 4592 | else { |
| 4593 | alloc_a = size_a; |
| 4594 | c = _PyLong_New(size_a); |
| 4595 | if (c == NULL) |
| 4596 | goto error; |
| 4597 | } |
| 4598 | |
| 4599 | if (d != NULL) |
| 4600 | Py_SIZE(d) = size_a; |
| 4601 | else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { |
| 4602 | Py_INCREF(b); |
| 4603 | d = b; |
| 4604 | Py_SIZE(d) = size_a; |
| 4605 | } |
| 4606 | else { |
| 4607 | alloc_b = size_a; |
| 4608 | d = _PyLong_New(size_a); |
| 4609 | if (d == NULL) |
| 4610 | goto error; |
| 4611 | } |
| 4612 | a_end = a->ob_digit + size_a; |
| 4613 | b_end = b->ob_digit + size_b; |
| 4614 | |
| 4615 | /* compute new a and new b in parallel */ |
| 4616 | a_digit = a->ob_digit; |
| 4617 | b_digit = b->ob_digit; |
| 4618 | c_digit = c->ob_digit; |
| 4619 | d_digit = d->ob_digit; |
| 4620 | c_carry = 0; |
| 4621 | d_carry = 0; |
| 4622 | while (b_digit < b_end) { |
| 4623 | c_carry += (A * *a_digit) - (B * *b_digit); |
| 4624 | d_carry += (D * *b_digit++) - (C * *a_digit++); |
| 4625 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4626 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4627 | c_carry >>= PyLong_SHIFT; |
| 4628 | d_carry >>= PyLong_SHIFT; |
| 4629 | } |
| 4630 | while (a_digit < a_end) { |
| 4631 | c_carry += A * *a_digit; |
| 4632 | d_carry -= C * *a_digit++; |
| 4633 | *c_digit++ = (digit)(c_carry & PyLong_MASK); |
| 4634 | *d_digit++ = (digit)(d_carry & PyLong_MASK); |
| 4635 | c_carry >>= PyLong_SHIFT; |
| 4636 | d_carry >>= PyLong_SHIFT; |
| 4637 | } |
| 4638 | assert(c_carry == 0); |
| 4639 | assert(d_carry == 0); |
| 4640 | |
| 4641 | Py_INCREF(c); |
| 4642 | Py_INCREF(d); |
| 4643 | Py_DECREF(a); |
| 4644 | Py_DECREF(b); |
| 4645 | a = long_normalize(c); |
| 4646 | b = long_normalize(d); |
| 4647 | } |
| 4648 | Py_XDECREF(c); |
| 4649 | Py_XDECREF(d); |
| 4650 | |
| 4651 | simple: |
| 4652 | assert(Py_REFCNT(a) > 0); |
| 4653 | assert(Py_REFCNT(b) > 0); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4654 | /* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid |
| 4655 | undefined behaviour when LONG_MAX type is smaller than 60 bits */ |
| 4656 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4657 | /* a fits into a long, so b must too */ |
| 4658 | x = PyLong_AsLong((PyObject *)a); |
| 4659 | y = PyLong_AsLong((PyObject *)b); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4660 | #elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4661 | x = PyLong_AsLongLong((PyObject *)a); |
| 4662 | y = PyLong_AsLongLong((PyObject *)b); |
| 4663 | #else |
| 4664 | # error "_PyLong_GCD" |
| 4665 | #endif |
| 4666 | x = Py_ABS(x); |
| 4667 | y = Py_ABS(y); |
| 4668 | Py_DECREF(a); |
| 4669 | Py_DECREF(b); |
| 4670 | |
| 4671 | /* usual Euclidean algorithm for longs */ |
| 4672 | while (y != 0) { |
| 4673 | t = y; |
| 4674 | y = x % y; |
| 4675 | x = t; |
| 4676 | } |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4677 | #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4678 | return PyLong_FromLong(x); |
Victor Stinner | 5783fd2 | 2015-09-19 13:39:03 +0200 | [diff] [blame] | 4679 | #elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 4680 | return PyLong_FromLongLong(x); |
| 4681 | #else |
| 4682 | # error "_PyLong_GCD" |
| 4683 | #endif |
| 4684 | |
| 4685 | error: |
| 4686 | Py_DECREF(a); |
| 4687 | Py_DECREF(b); |
| 4688 | Py_XDECREF(c); |
| 4689 | Py_XDECREF(d); |
| 4690 | return NULL; |
| 4691 | } |
| 4692 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4693 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 4694 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | double result; |
| 4697 | result = PyLong_AsDouble(v); |
| 4698 | if (result == -1.0 && PyErr_Occurred()) |
| 4699 | return NULL; |
| 4700 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4703 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4704 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 4705 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4706 | static PyObject * |
| 4707 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4708 | { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4709 | PyObject *obase = NULL, *x = NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4710 | Py_ssize_t base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | if (type != &PyLong_Type) |
| 4714 | return long_subtype_new(type, args, kwds); /* Wimp out */ |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4715 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist, |
| 4716 | &x, &obase)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | return NULL; |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4718 | if (x == NULL) { |
| 4719 | if (obase != NULL) { |
| 4720 | PyErr_SetString(PyExc_TypeError, |
| 4721 | "int() missing string argument"); |
| 4722 | return NULL; |
| 4723 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4724 | return PyLong_FromLong(0L); |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4725 | } |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4726 | if (obase == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4727 | return PyNumber_Long(x); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4728 | |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4729 | base = PyNumber_AsSsize_t(obase, NULL); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4730 | if (base == -1 && PyErr_Occurred()) |
| 4731 | return NULL; |
Gregory P. Smith | a689e52 | 2012-12-25 22:38:32 -0800 | [diff] [blame] | 4732 | if ((base != 0 && base < 2) || base > 36) { |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4733 | PyErr_SetString(PyExc_ValueError, |
Serhiy Storchaka | 0b386d5 | 2012-12-28 09:42:11 +0200 | [diff] [blame] | 4734 | "int() base must be >= 2 and <= 36"); |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 4735 | return NULL; |
| 4736 | } |
| 4737 | |
| 4738 | if (PyUnicode_Check(x)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4739 | return PyLong_FromUnicodeObject(x, (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | char *string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | if (PyByteArray_Check(x)) |
| 4743 | string = PyByteArray_AS_STRING(x); |
| 4744 | else |
| 4745 | string = PyBytes_AS_STRING(x); |
Serhiy Storchaka | f6d0aee | 2013-08-03 20:55:06 +0300 | [diff] [blame] | 4746 | return _PyLong_FromBytes(string, Py_SIZE(x), (int)base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | } |
| 4748 | else { |
| 4749 | PyErr_SetString(PyExc_TypeError, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 4750 | "int() can't convert non-string with explicit base"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | return NULL; |
| 4752 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4755 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 4756 | first create a regular int from whatever arguments we got, |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4757 | then allocate a subtype instance and initialize it from |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4758 | the regular int. The regular int is then thrown away. |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4759 | */ |
| 4760 | static PyObject * |
| 4761 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | PyLongObject *tmp, *newobj; |
| 4764 | Py_ssize_t i, n; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
| 4767 | tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); |
| 4768 | if (tmp == NULL) |
| 4769 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 4770 | assert(PyLong_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | n = Py_SIZE(tmp); |
| 4772 | if (n < 0) |
| 4773 | n = -n; |
| 4774 | newobj = (PyLongObject *)type->tp_alloc(type, n); |
| 4775 | if (newobj == NULL) { |
| 4776 | Py_DECREF(tmp); |
| 4777 | return NULL; |
| 4778 | } |
| 4779 | assert(PyLong_Check(newobj)); |
| 4780 | Py_SIZE(newobj) = Py_SIZE(tmp); |
| 4781 | for (i = 0; i < n; i++) |
| 4782 | newobj->ob_digit[i] = tmp->ob_digit[i]; |
| 4783 | Py_DECREF(tmp); |
| 4784 | return (PyObject *)newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4787 | static PyObject * |
| 4788 | long_getnewargs(PyLongObject *v) |
| 4789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4790 | return Py_BuildValue("(N)", _PyLong_Copy(v)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4793 | static PyObject * |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4794 | long_get0(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | return PyLong_FromLong(0L); |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | static PyObject * |
| 4799 | long_get1(PyLongObject *v, void *context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | return PyLong_FromLong(1L); |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4803 | static PyObject * |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4804 | long__format__(PyObject *self, PyObject *args) |
| 4805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4806 | PyObject *format_spec; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4807 | _PyUnicodeWriter writer; |
| 4808 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 4809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 4811 | return NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4812 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4813 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4814 | ret = _PyLong_FormatAdvancedWriter( |
| 4815 | &writer, |
| 4816 | self, |
| 4817 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 4818 | if (ret == -1) { |
| 4819 | _PyUnicodeWriter_Dealloc(&writer); |
| 4820 | return NULL; |
| 4821 | } |
| 4822 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4825 | /* Return a pair (q, r) such that a = b * q + r, and |
| 4826 | abs(r) <= abs(b)/2, with equality possible only if q is even. |
| 4827 | In other words, q == a / b, rounded to the nearest integer using |
| 4828 | round-half-to-even. */ |
| 4829 | |
| 4830 | PyObject * |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 4831 | _PyLong_DivmodNear(PyObject *a, PyObject *b) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4832 | { |
| 4833 | PyLongObject *quo = NULL, *rem = NULL; |
| 4834 | PyObject *one = NULL, *twice_rem, *result, *temp; |
| 4835 | int cmp, quo_is_odd, quo_is_neg; |
| 4836 | |
| 4837 | /* Equivalent Python code: |
| 4838 | |
| 4839 | def divmod_near(a, b): |
| 4840 | q, r = divmod(a, b) |
| 4841 | # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. |
| 4842 | # The expression r / b > 0.5 is equivalent to 2 * r > b if b is |
| 4843 | # positive, 2 * r < b if b negative. |
| 4844 | greater_than_half = 2*r > b if b > 0 else 2*r < b |
| 4845 | exactly_half = 2*r == b |
| 4846 | if greater_than_half or exactly_half and q % 2 == 1: |
| 4847 | q += 1 |
| 4848 | r -= b |
| 4849 | return q, r |
| 4850 | |
| 4851 | */ |
| 4852 | if (!PyLong_Check(a) || !PyLong_Check(b)) { |
| 4853 | PyErr_SetString(PyExc_TypeError, |
| 4854 | "non-integer arguments in division"); |
| 4855 | return NULL; |
| 4856 | } |
| 4857 | |
| 4858 | /* Do a and b have different signs? If so, quotient is negative. */ |
| 4859 | quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0); |
| 4860 | |
| 4861 | one = PyLong_FromLong(1L); |
| 4862 | if (one == NULL) |
| 4863 | return NULL; |
| 4864 | |
| 4865 | if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0) |
| 4866 | goto error; |
| 4867 | |
| 4868 | /* compare twice the remainder with the divisor, to see |
| 4869 | if we need to adjust the quotient and remainder */ |
| 4870 | twice_rem = long_lshift((PyObject *)rem, one); |
| 4871 | if (twice_rem == NULL) |
| 4872 | goto error; |
| 4873 | if (quo_is_neg) { |
| 4874 | temp = long_neg((PyLongObject*)twice_rem); |
| 4875 | Py_DECREF(twice_rem); |
| 4876 | twice_rem = temp; |
| 4877 | if (twice_rem == NULL) |
| 4878 | goto error; |
| 4879 | } |
| 4880 | cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b); |
| 4881 | Py_DECREF(twice_rem); |
| 4882 | |
| 4883 | quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0); |
| 4884 | if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) { |
| 4885 | /* fix up quotient */ |
| 4886 | if (quo_is_neg) |
| 4887 | temp = long_sub(quo, (PyLongObject *)one); |
| 4888 | else |
| 4889 | temp = long_add(quo, (PyLongObject *)one); |
| 4890 | Py_DECREF(quo); |
| 4891 | quo = (PyLongObject *)temp; |
| 4892 | if (quo == NULL) |
| 4893 | goto error; |
| 4894 | /* and remainder */ |
| 4895 | if (quo_is_neg) |
| 4896 | temp = long_add(rem, (PyLongObject *)b); |
| 4897 | else |
| 4898 | temp = long_sub(rem, (PyLongObject *)b); |
| 4899 | Py_DECREF(rem); |
| 4900 | rem = (PyLongObject *)temp; |
| 4901 | if (rem == NULL) |
| 4902 | goto error; |
| 4903 | } |
| 4904 | |
| 4905 | result = PyTuple_New(2); |
| 4906 | if (result == NULL) |
| 4907 | goto error; |
| 4908 | |
| 4909 | /* PyTuple_SET_ITEM steals references */ |
| 4910 | PyTuple_SET_ITEM(result, 0, (PyObject *)quo); |
| 4911 | PyTuple_SET_ITEM(result, 1, (PyObject *)rem); |
| 4912 | Py_DECREF(one); |
| 4913 | return result; |
| 4914 | |
| 4915 | error: |
| 4916 | Py_XDECREF(quo); |
| 4917 | Py_XDECREF(rem); |
| 4918 | Py_XDECREF(one); |
| 4919 | return NULL; |
| 4920 | } |
| 4921 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4922 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4923 | long_round(PyObject *self, PyObject *args) |
| 4924 | { |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4925 | PyObject *o_ndigits=NULL, *temp, *result, *ndigits; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4926 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4927 | /* To round an integer m to the nearest 10**n (n positive), we make use of |
| 4928 | * the divmod_near operation, defined by: |
| 4929 | * |
| 4930 | * divmod_near(a, b) = (q, r) |
| 4931 | * |
| 4932 | * where q is the nearest integer to the quotient a / b (the |
| 4933 | * nearest even integer in the case of a tie) and r == a - q * b. |
| 4934 | * Hence q * b = a - r is the nearest multiple of b to a, |
| 4935 | * preferring even multiples in the case of a tie. |
| 4936 | * |
| 4937 | * So the nearest multiple of 10**n to m is: |
| 4938 | * |
| 4939 | * m - divmod_near(m, 10**n)[1]. |
| 4940 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
| 4942 | return NULL; |
| 4943 | if (o_ndigits == NULL) |
| 4944 | return long_long(self); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4945 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4946 | ndigits = PyNumber_Index(o_ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | if (ndigits == NULL) |
| 4948 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4949 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4950 | /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 | if (Py_SIZE(ndigits) >= 0) { |
| 4952 | Py_DECREF(ndigits); |
| 4953 | return long_long(self); |
| 4954 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4955 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4956 | /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ |
| 4957 | temp = long_neg((PyLongObject*)ndigits); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | Py_DECREF(ndigits); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4959 | ndigits = temp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | if (ndigits == NULL) |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4961 | return NULL; |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4962 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4963 | result = PyLong_FromLong(10L); |
| 4964 | if (result == NULL) { |
| 4965 | Py_DECREF(ndigits); |
| 4966 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4967 | } |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 4968 | |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4969 | temp = long_pow(result, ndigits, Py_None); |
| 4970 | Py_DECREF(ndigits); |
| 4971 | Py_DECREF(result); |
| 4972 | result = temp; |
| 4973 | if (result == NULL) |
| 4974 | return NULL; |
| 4975 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 4976 | temp = _PyLong_DivmodNear(self, result); |
Mark Dickinson | 7f1bf80 | 2010-05-26 16:02:59 +0000 | [diff] [blame] | 4977 | Py_DECREF(result); |
| 4978 | result = temp; |
| 4979 | if (result == NULL) |
| 4980 | return NULL; |
| 4981 | |
| 4982 | temp = long_sub((PyLongObject *)self, |
| 4983 | (PyLongObject *)PyTuple_GET_ITEM(result, 1)); |
| 4984 | Py_DECREF(result); |
| 4985 | result = temp; |
| 4986 | |
| 4987 | return result; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4990 | static PyObject * |
| 4991 | long_sizeof(PyLongObject *v) |
| 4992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | Py_ssize_t res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4994 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 4995 | res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(v))*sizeof(digit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 4999 | static PyObject * |
| 5000 | long_bit_length(PyLongObject *v) |
| 5001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | PyLongObject *result, *x, *y; |
| 5003 | Py_ssize_t ndigits, msd_bits = 0; |
| 5004 | digit msd; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | assert(v != NULL); |
| 5007 | assert(PyLong_Check(v)); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5008 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 5009 | ndigits = Py_ABS(Py_SIZE(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | if (ndigits == 0) |
| 5011 | return PyLong_FromLong(0); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | msd = v->ob_digit[ndigits-1]; |
| 5014 | while (msd >= 32) { |
| 5015 | msd_bits += 6; |
| 5016 | msd >>= 6; |
| 5017 | } |
| 5018 | msd_bits += (long)(BitLengthTable[msd]); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
| 5021 | return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | /* expression above may overflow; use Python integers instead */ |
| 5024 | result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
| 5025 | if (result == NULL) |
| 5026 | return NULL; |
| 5027 | x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
| 5028 | if (x == NULL) |
| 5029 | goto error; |
| 5030 | y = (PyLongObject *)long_mul(result, x); |
| 5031 | Py_DECREF(x); |
| 5032 | if (y == NULL) |
| 5033 | goto error; |
| 5034 | Py_DECREF(result); |
| 5035 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5037 | x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
| 5038 | if (x == NULL) |
| 5039 | goto error; |
| 5040 | y = (PyLongObject *)long_add(result, x); |
| 5041 | Py_DECREF(x); |
| 5042 | if (y == NULL) |
| 5043 | goto error; |
| 5044 | Py_DECREF(result); |
| 5045 | result = y; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | return (PyObject *)result; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5048 | |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5049 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | Py_DECREF(result); |
| 5051 | return NULL; |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | PyDoc_STRVAR(long_bit_length_doc, |
| 5055 | "int.bit_length() -> int\n\ |
| 5056 | \n\ |
| 5057 | Number of bits necessary to represent self in binary.\n\ |
| 5058 | >>> bin(37)\n\ |
| 5059 | '0b100101'\n\ |
| 5060 | >>> (37).bit_length()\n\ |
| 5061 | 6"); |
| 5062 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5063 | #if 0 |
| 5064 | static PyObject * |
| 5065 | long_is_finite(PyObject *v) |
| 5066 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5067 | Py_RETURN_TRUE; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5068 | } |
| 5069 | #endif |
| 5070 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5071 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5072 | static PyObject * |
| 5073 | long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) |
| 5074 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5075 | PyObject *byteorder_str; |
| 5076 | PyObject *is_signed_obj = NULL; |
| 5077 | Py_ssize_t length; |
| 5078 | int little_endian; |
| 5079 | int is_signed; |
| 5080 | PyObject *bytes; |
| 5081 | static char *kwlist[] = {"length", "byteorder", "signed", NULL}; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist, |
| 5084 | &length, &byteorder_str, |
| 5085 | &is_signed_obj)) |
| 5086 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | if (args != NULL && Py_SIZE(args) > 2) { |
| 5089 | PyErr_SetString(PyExc_TypeError, |
| 5090 | "'signed' is a keyword-only argument"); |
| 5091 | return NULL; |
| 5092 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
| 5095 | little_endian = 1; |
| 5096 | else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
| 5097 | little_endian = 0; |
| 5098 | else { |
| 5099 | PyErr_SetString(PyExc_ValueError, |
| 5100 | "byteorder must be either 'little' or 'big'"); |
| 5101 | return NULL; |
| 5102 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | if (is_signed_obj != NULL) { |
| 5105 | int cmp = PyObject_IsTrue(is_signed_obj); |
| 5106 | if (cmp < 0) |
| 5107 | return NULL; |
| 5108 | is_signed = cmp ? 1 : 0; |
| 5109 | } |
| 5110 | else { |
| 5111 | /* If the signed argument was omitted, use False as the |
| 5112 | default. */ |
| 5113 | is_signed = 0; |
| 5114 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | if (length < 0) { |
| 5117 | PyErr_SetString(PyExc_ValueError, |
| 5118 | "length argument must be non-negative"); |
| 5119 | return NULL; |
| 5120 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | bytes = PyBytes_FromStringAndSize(NULL, length); |
| 5123 | if (bytes == NULL) |
| 5124 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes), |
| 5127 | length, little_endian, is_signed) < 0) { |
| 5128 | Py_DECREF(bytes); |
| 5129 | return NULL; |
| 5130 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5132 | return bytes; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5135 | PyDoc_STRVAR(long_to_bytes_doc, |
| 5136 | "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5137 | \n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5138 | Return an array of bytes representing an integer.\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5139 | \n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | The integer is represented using length bytes. An OverflowError is\n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5141 | raised if the integer is not representable with the given number of\n\ |
| 5142 | bytes.\n\ |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5143 | \n\ |
| 5144 | The byteorder argument determines the byte order used to represent the\n\ |
| 5145 | integer. If byteorder is 'big', the most significant byte is at the\n\ |
| 5146 | beginning of the byte array. If byteorder is 'little', the most\n\ |
| 5147 | significant byte is at the end of the byte array. To request the native\n\ |
| 5148 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
| 5149 | \n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5150 | The signed keyword-only argument determines whether two's complement is\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | 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] | 5152 | is given, an OverflowError is raised."); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5153 | |
| 5154 | static PyObject * |
| 5155 | long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5157 | PyObject *byteorder_str; |
| 5158 | PyObject *is_signed_obj = NULL; |
| 5159 | int little_endian; |
| 5160 | int is_signed; |
| 5161 | PyObject *obj; |
| 5162 | PyObject *bytes; |
| 5163 | PyObject *long_obj; |
| 5164 | static char *kwlist[] = {"bytes", "byteorder", "signed", NULL}; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5166 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist, |
| 5167 | &obj, &byteorder_str, |
| 5168 | &is_signed_obj)) |
| 5169 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | if (args != NULL && Py_SIZE(args) > 2) { |
| 5172 | PyErr_SetString(PyExc_TypeError, |
| 5173 | "'signed' is a keyword-only argument"); |
| 5174 | return NULL; |
| 5175 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5177 | if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
| 5178 | little_endian = 1; |
| 5179 | else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
| 5180 | little_endian = 0; |
| 5181 | else { |
| 5182 | PyErr_SetString(PyExc_ValueError, |
| 5183 | "byteorder must be either 'little' or 'big'"); |
| 5184 | return NULL; |
| 5185 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | if (is_signed_obj != NULL) { |
| 5188 | int cmp = PyObject_IsTrue(is_signed_obj); |
| 5189 | if (cmp < 0) |
| 5190 | return NULL; |
| 5191 | is_signed = cmp ? 1 : 0; |
| 5192 | } |
| 5193 | else { |
| 5194 | /* If the signed argument was omitted, use False as the |
| 5195 | default. */ |
| 5196 | is_signed = 0; |
| 5197 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5199 | bytes = PyObject_Bytes(obj); |
| 5200 | if (bytes == NULL) |
| 5201 | return NULL; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | long_obj = _PyLong_FromByteArray( |
| 5204 | (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
| 5205 | little_endian, is_signed); |
| 5206 | Py_DECREF(bytes); |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5207 | |
Serhiy Storchaka | ea36c94 | 2016-05-12 10:37:58 +0300 | [diff] [blame] | 5208 | if (type != &PyLong_Type) { |
| 5209 | Py_SETREF(long_obj, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 5210 | long_obj, NULL)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5211 | } |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | return long_obj; |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5216 | PyDoc_STRVAR(long_from_bytes_doc, |
| 5217 | "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\ |
| 5218 | \n\ |
| 5219 | Return the integer represented by the given array of bytes.\n\ |
| 5220 | \n\ |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 5221 | The bytes argument must be a bytes-like object (e.g. bytes or bytearray).\n\ |
Mark Dickinson | 078c253 | 2010-01-30 18:06:17 +0000 | [diff] [blame] | 5222 | \n\ |
| 5223 | The byteorder argument determines the byte order used to represent the\n\ |
| 5224 | integer. If byteorder is 'big', the most significant byte is at the\n\ |
| 5225 | beginning of the byte array. If byteorder is 'little', the most\n\ |
| 5226 | significant byte is at the end of the byte array. To request the native\n\ |
| 5227 | byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
| 5228 | \n\ |
| 5229 | The signed keyword-only argument indicates whether two's complement is\n\ |
| 5230 | used to represent the integer."); |
| 5231 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5232 | static PyMethodDef long_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5233 | {"conjugate", (PyCFunction)long_long, METH_NOARGS, |
| 5234 | "Returns self, the complex conjugate of any int."}, |
| 5235 | {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS, |
| 5236 | long_bit_length_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5237 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5238 | {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
| 5239 | "Returns always True."}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 5240 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | {"to_bytes", (PyCFunction)long_to_bytes, |
| 5242 | METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, |
| 5243 | {"from_bytes", (PyCFunction)long_from_bytes, |
| 5244 | METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes_doc}, |
| 5245 | {"__trunc__", (PyCFunction)long_long, METH_NOARGS, |
| 5246 | "Truncating an Integral returns itself."}, |
| 5247 | {"__floor__", (PyCFunction)long_long, METH_NOARGS, |
| 5248 | "Flooring an Integral returns itself."}, |
| 5249 | {"__ceil__", (PyCFunction)long_long, METH_NOARGS, |
| 5250 | "Ceiling of an Integral returns itself."}, |
| 5251 | {"__round__", (PyCFunction)long_round, METH_VARARGS, |
| 5252 | "Rounding an Integral returns itself.\n" |
| 5253 | "Rounding with an ndigits argument also returns an integer."}, |
| 5254 | {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, |
| 5255 | {"__format__", (PyCFunction)long__format__, METH_VARARGS}, |
| 5256 | {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, |
| 5257 | "Returns size in memory, in bytes"}, |
| 5258 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 5259 | }; |
| 5260 | |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5261 | static PyGetSetDef long_getset[] = { |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5262 | {"real", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5263 | (getter)long_long, (setter)NULL, |
| 5264 | "the real part of a complex number", |
| 5265 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5266 | {"imag", |
| 5267 | (getter)long_get0, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5268 | "the imaginary part of a complex number", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5269 | NULL}, |
| 5270 | {"numerator", |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5271 | (getter)long_long, (setter)NULL, |
| 5272 | "the numerator of a rational number in lowest terms", |
| 5273 | NULL}, |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5274 | {"denominator", |
| 5275 | (getter)long_get1, (setter)NULL, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5276 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 6bf1900 | 2009-05-02 17:57:52 +0000 | [diff] [blame] | 5277 | NULL}, |
Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 5278 | {NULL} /* Sentinel */ |
| 5279 | }; |
| 5280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5281 | PyDoc_STRVAR(long_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5282 | "int(x=0) -> integer\n\ |
| 5283 | int(x, base=10) -> integer\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5284 | \n\ |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 5285 | Convert a number or string to an integer, or return 0 if no arguments\n\ |
| 5286 | are given. If x is a number, return x.__int__(). For floating point\n\ |
| 5287 | numbers, this truncates towards zero.\n\ |
| 5288 | \n\ |
| 5289 | If x is not a number or if base is given, then x must be a string,\n\ |
| 5290 | bytes, or bytearray instance representing an integer literal in the\n\ |
| 5291 | given base. The literal can be preceded by '+' or '-' and be surrounded\n\ |
| 5292 | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\ |
| 5293 | Base 0 means to interpret the base from the string as an integer literal.\n\ |
| 5294 | >>> int('0b100', base=0)\n\ |
| 5295 | 4"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5296 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5297 | static PyNumberMethods long_as_number = { |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5298 | (binaryfunc)long_add, /*nb_add*/ |
| 5299 | (binaryfunc)long_sub, /*nb_subtract*/ |
| 5300 | (binaryfunc)long_mul, /*nb_multiply*/ |
| 5301 | long_mod, /*nb_remainder*/ |
| 5302 | long_divmod, /*nb_divmod*/ |
| 5303 | long_pow, /*nb_power*/ |
| 5304 | (unaryfunc)long_neg, /*nb_negative*/ |
| 5305 | (unaryfunc)long_long, /*tp_positive*/ |
| 5306 | (unaryfunc)long_abs, /*tp_absolute*/ |
| 5307 | (inquiry)long_bool, /*tp_bool*/ |
| 5308 | (unaryfunc)long_invert, /*nb_invert*/ |
| 5309 | long_lshift, /*nb_lshift*/ |
| 5310 | (binaryfunc)long_rshift, /*nb_rshift*/ |
| 5311 | long_and, /*nb_and*/ |
| 5312 | long_xor, /*nb_xor*/ |
| 5313 | long_or, /*nb_or*/ |
| 5314 | long_long, /*nb_int*/ |
| 5315 | 0, /*nb_reserved*/ |
| 5316 | long_float, /*nb_float*/ |
| 5317 | 0, /* nb_inplace_add */ |
| 5318 | 0, /* nb_inplace_subtract */ |
| 5319 | 0, /* nb_inplace_multiply */ |
| 5320 | 0, /* nb_inplace_remainder */ |
| 5321 | 0, /* nb_inplace_power */ |
| 5322 | 0, /* nb_inplace_lshift */ |
| 5323 | 0, /* nb_inplace_rshift */ |
| 5324 | 0, /* nb_inplace_and */ |
| 5325 | 0, /* nb_inplace_xor */ |
| 5326 | 0, /* nb_inplace_or */ |
| 5327 | long_div, /* nb_floor_divide */ |
| 5328 | long_true_divide, /* nb_true_divide */ |
| 5329 | 0, /* nb_inplace_floor_divide */ |
| 5330 | 0, /* nb_inplace_true_divide */ |
| 5331 | long_long, /* nb_index */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5332 | }; |
| 5333 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5334 | PyTypeObject PyLong_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5336 | "int", /* tp_name */ |
| 5337 | offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
| 5338 | sizeof(digit), /* tp_itemsize */ |
| 5339 | long_dealloc, /* tp_dealloc */ |
| 5340 | 0, /* tp_print */ |
| 5341 | 0, /* tp_getattr */ |
| 5342 | 0, /* tp_setattr */ |
| 5343 | 0, /* tp_reserved */ |
| 5344 | long_to_decimal_string, /* tp_repr */ |
| 5345 | &long_as_number, /* tp_as_number */ |
| 5346 | 0, /* tp_as_sequence */ |
| 5347 | 0, /* tp_as_mapping */ |
| 5348 | (hashfunc)long_hash, /* tp_hash */ |
| 5349 | 0, /* tp_call */ |
| 5350 | long_to_decimal_string, /* tp_str */ |
| 5351 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5352 | 0, /* tp_setattro */ |
| 5353 | 0, /* tp_as_buffer */ |
| 5354 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5355 | Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
| 5356 | long_doc, /* tp_doc */ |
| 5357 | 0, /* tp_traverse */ |
| 5358 | 0, /* tp_clear */ |
| 5359 | long_richcompare, /* tp_richcompare */ |
| 5360 | 0, /* tp_weaklistoffset */ |
| 5361 | 0, /* tp_iter */ |
| 5362 | 0, /* tp_iternext */ |
| 5363 | long_methods, /* tp_methods */ |
| 5364 | 0, /* tp_members */ |
| 5365 | long_getset, /* tp_getset */ |
| 5366 | 0, /* tp_base */ |
| 5367 | 0, /* tp_dict */ |
| 5368 | 0, /* tp_descr_get */ |
| 5369 | 0, /* tp_descr_set */ |
| 5370 | 0, /* tp_dictoffset */ |
| 5371 | 0, /* tp_init */ |
| 5372 | 0, /* tp_alloc */ |
| 5373 | long_new, /* tp_new */ |
| 5374 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 5375 | }; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5376 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5377 | static PyTypeObject Int_InfoType; |
| 5378 | |
| 5379 | PyDoc_STRVAR(int_info__doc__, |
| 5380 | "sys.int_info\n\ |
| 5381 | \n\ |
| 5382 | A struct sequence that holds information about Python's\n\ |
| 5383 | internal representation of integers. The attributes are read only."); |
| 5384 | |
| 5385 | static PyStructSequence_Field int_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | {"bits_per_digit", "size of a digit in bits"}, |
Mark Dickinson | 22b2018 | 2010-05-10 21:27:53 +0000 | [diff] [blame] | 5387 | {"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] | 5388 | {NULL, NULL} |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5389 | }; |
| 5390 | |
| 5391 | static PyStructSequence_Desc int_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | "sys.int_info", /* name */ |
| 5393 | int_info__doc__, /* doc */ |
| 5394 | int_info_fields, /* fields */ |
| 5395 | 2 /* number of fields */ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5396 | }; |
| 5397 | |
| 5398 | PyObject * |
| 5399 | PyLong_GetInfo(void) |
| 5400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5401 | PyObject* int_info; |
| 5402 | int field = 0; |
| 5403 | int_info = PyStructSequence_New(&Int_InfoType); |
| 5404 | if (int_info == NULL) |
| 5405 | return NULL; |
| 5406 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5407 | PyLong_FromLong(PyLong_SHIFT)); |
| 5408 | PyStructSequence_SET_ITEM(int_info, field++, |
| 5409 | PyLong_FromLong(sizeof(digit))); |
| 5410 | if (PyErr_Occurred()) { |
| 5411 | Py_CLEAR(int_info); |
| 5412 | return NULL; |
| 5413 | } |
| 5414 | return int_info; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5417 | int |
| 5418 | _PyLong_Init(void) |
| 5419 | { |
| 5420 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5421 | int ival, size; |
| 5422 | PyLongObject *v = small_ints; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5424 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { |
| 5425 | size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
| 5426 | if (Py_TYPE(v) == &PyLong_Type) { |
| 5427 | /* The element is already initialized, most likely |
| 5428 | * the Python interpreter was initialized before. |
| 5429 | */ |
| 5430 | Py_ssize_t refcnt; |
| 5431 | PyObject* op = (PyObject*)v; |
Christian Heimes | dfc12ed | 2008-01-31 15:16:38 +0000 | [diff] [blame] | 5432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5433 | refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); |
| 5434 | _Py_NewReference(op); |
| 5435 | /* _Py_NewReference sets the ref count to 1 but |
| 5436 | * the ref count might be larger. Set the refcnt |
| 5437 | * to the original refcnt + 1 */ |
| 5438 | Py_REFCNT(op) = refcnt + 1; |
| 5439 | assert(Py_SIZE(op) == size); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5440 | assert(v->ob_digit[0] == (digit)abs(ival)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | } |
| 5442 | else { |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 5443 | (void)PyObject_INIT(v, &PyLong_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | } |
| 5445 | Py_SIZE(v) = size; |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 5446 | v->ob_digit[0] = (digit)abs(ival); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5448 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 | /* initialize int_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 5450 | if (Int_InfoType.tp_name == NULL) { |
| 5451 | if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) |
| 5452 | return 0; |
| 5453 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 5454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | return 1; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | void |
| 5459 | PyLong_Fini(void) |
| 5460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | /* Integers are currently statically allocated. Py_DECREF is not |
| 5462 | needed, but Python must forget about the reference or multiple |
| 5463 | reinitializations will fail. */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5464 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | int i; |
| 5466 | PyLongObject *v = small_ints; |
| 5467 | for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { |
| 5468 | _Py_DEC_REFTOTAL; |
| 5469 | _Py_ForgetReference((PyObject*)v); |
| 5470 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5471 | #endif |
| 5472 | } |